Skip to content

Commit bfafb07

Browse files
authored
Update fximg.utils.ts
1 parent c403ce6 commit bfafb07

1 file changed

Lines changed: 20 additions & 18 deletions

File tree

fximg.utils.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ namespace helper {
8282
const srcW = fximgWidthOf(src);
8383
const srcH = fximgHeightOf(src);
8484

85+
// คำนวณ scale factor (จริง ๆ คือ ratio)
86+
const scaleX = wSrc / wDst;
87+
const scaleY = hSrc / hdst;
88+
8589
// Clip rectangle ทั้ง src และ dst (เหมือนมาตรฐาน)
8690
let clipWDst = wDst;
8791
let clipHDst = hDst;
@@ -108,31 +112,29 @@ namespace helper {
108112
let anyChange = false;
109113

110114
for (let dx = 0; dx < clipWDst; dx++) {
111-
const sx = xSrc + dx;
112-
const tx = xDst + dx;
113-
114-
// ดึง column ส่วนที่ต้องการจาก src (offset ySrc)
115-
fximgGetRows(src, sx, rowBuf, clipHSrc);
115+
// วนทุกพิกเซลปลายทาง (nearest neighbor)
116+
for (let y = 0; y < dh; y++) {
117+
const srcY_float = sy + y * scaleY;
118+
const srcY = Math.floor(srcY_float + 0.5); // หรือ Math.round() ก็ได้
116119

117-
fximgGetRows(dst, tx, dstRow, dstH);
120+
if (srcY < 0 || srcY >= srcH) continue;
118121

119-
let colChanged = false;
122+
for (let x = 0; x < dw; x++) {
123+
const srcX_float = sx + x * scaleX;
124+
const srcX = Math.floor(srcX_float + 0.5);
120125

121-
for (let dy = 0; dy < clipHDst; dy++) {
122-
const syPixel = rowBuf[dy]; // pixel จาก src (หลัง shift ySrc แล้ว)
126+
if (srcX < 0 || srcX >= srcW) continue;
123127

124-
if (transparent && syPixel < 1) continue;
128+
const pixel = fximgGetPixel(src, srcX, srcY); // ต้องมี helper นี้ไหม? ถ้ายังไม่มีก็ implement ง่าย
125129

126-
const oldPixel = dstRow[yDst + dy];
127-
if (oldPixel === syPixel) continue;
128-
dstRow[yDst + dy] = syPixel;
129-
colChanged = true, anyChange = true;
130-
}
130+
if (transparent && pixel < 1) continue;
131131

132-
if (!(colChanged || !check)) continue;
133-
fximgSetRows(dst, tx, dstRow, dstH);
132+
const old = fximgGetPixel(dst, dx + x, dy + y);
133+
if (old === pixel) continue;
134134

135-
// ถ้า check=true และยังไม่มี change เลย → สามารถ break ได้เร็ว แต่เวอร์ชันนี้ scan หมดก่อน
135+
fximgSetPixel(dst, dx + x, dy + y, pixel);
136+
anyChange = true;
137+
}
136138
}
137139

138140
return check ? anyChange : true;

0 commit comments

Comments
 (0)