Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added bilinear filtering for drawimage #72

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/bitmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,40 @@ class Bitmap {
this.data[i+3]);
}

/**
* Get the bilinear interpolated RGBA value at a given pixel position and returns it as a hexadecimal number(See {@link NAMED_COLORS} for examples)
*
* @param {number} x fractional X axis potiion
* @param {number} y fractional Y axis position
*
* @returns {number}
*
* @memberof Bitmap
*/
getPixelRGBA_bilinear(x,y) {
let floorx = Math.floor(x),
floory = Math.floor(y),
fractx = x-floorx,
fracty = y-floory,

i00 = this.calculateIndex(floorx, floory),
i01 = this.calculateIndex(floorx, Math.min(this.height-1,floory+1)),
i10 = this.calculateIndex(Math.min(this.width-1,floorx+1), floory),
i11 = this.calculateIndex(Math.min(this.width-1,floorx+1), Math.min(this.height-1,floory+1)),

r00 = this.data[i00], r01 = this.data[i01], r10 = this.data[i10], r11 = this.data[i11],
g00 = this.data[i00+1], g01 = this.data[i01+1], g10 = this.data[i10+1], g11 = this.data[i11+1],
b00 = this.data[i00+2], b01 = this.data[i01+2], b10 = this.data[i10+2], b11 = this.data[i11+2],
a00 = this.data[i00+3], a01 = this.data[i01+3], a10 = this.data[i10+3], a11 = this.data[i11+3];

r00 += (r01-r00)*fracty; r10 += (r11-r10)*fracty; r00 += (r10-r00)*fractx;
g00 += (g01-g00)*fracty; g10 += (g11-g10)*fracty; g00 += (g10-g00)*fractx;
b00 += (b01-b00)*fracty; b10 += (b11-b10)*fracty; b00 += (b10-b00)*fractx;
a00 += (a01-a00)*fracty; a10 += (a11-a10)*fracty; a00 += (a10-a00)*fractx;

return uint32.fromBytesBigEndian(r00,g00,b00,a00);
}

/**
* Get Pixel RGBA Seperate
*
Expand Down
5 changes: 4 additions & 1 deletion src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,10 @@ class Context {
for(var j=0; j<dh; j++) {
var ty = j/dh;
var ssy = sy+Math.floor(ty * sh);
var rgba = bitmap.getPixelRGBA(ssx,ssy);
//var rgba = bitmap.getPixelRGBA(ssx,ssy);
var fgRGBA = bitmap.getPixelRGBA_bilinear(sx+tx*sw,sy+ty*sh);
var bgRGBA = this.bitmap.getPixelRGBA_bilinear(dx+i, dy+j);
var rgba = this.composite(0, 0, bgRGBA, fgRGBA);
this.bitmap.setPixelRGBA(dx+i, dy+j, rgba);
}
}
Expand Down