Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
### Added
### Fixed
* Load images from Node.js object URLs (#2525)
* Fix off-by-one RGB values from `getImageData` on semi-transparent pixels (#1933)

3.2.3
==================
Expand Down
7 changes: 3 additions & 4 deletions src/CanvasRenderingContext2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1079,10 +1079,9 @@ Context2d::GetImageData(const Napi::CallbackInfo& info) {
dst[bx + 2] = b;
} else {
// Undo alpha pre-multiplication
float alphaR = (float)255 / a;
dst[bx + 0] = (int)((float)r * alphaR);
dst[bx + 1] = (int)((float)g * alphaR);
dst[bx + 2] = (int)((float)b * alphaR);
dst[bx + 0] = (r * 255 + a / 2) / a;
dst[bx + 1] = (g * 255 + a / 2) / a;
dst[bx + 2] = (b * 255 + a / 2) / a;
}

}
Expand Down
9 changes: 9 additions & 0 deletions test/canvas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2224,6 +2224,15 @@ describe('Canvas', function () {
assert.equal(result.data[255], 255) // not black or junk data
assert.equal(result.data[255 * 256], 255) // not black or junk data
})

it('round-trips semi-transparent pixels through getImageData (#1933)', function () {
const canvas = createCanvas(1, 1)
const ctx = canvas.getContext('2d')
ctx.putImageData(createImageData(new Uint8ClampedArray([252, 252, 252, 252]), 1), 0, 0)
assert.deepEqual(Array.from(ctx.getImageData(0, 0, 1, 1).data), [252, 252, 252, 252])
ctx.putImageData(createImageData(new Uint8ClampedArray([255, 128, 1, 2]), 1), 0, 0)
assert.deepEqual(Array.from(ctx.getImageData(0, 0, 1, 1).data), [255, 128, 0, 2])
})
})

it('Canvas#createPNGStream()', function (done) {
Expand Down