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 @@ -15,6 +15,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
* `isPointInPath()` now ignores the current transform and no longer counts points that are only inside the stroke (#2634)
* Fix off-by-one RGB values from `getImageData` on semi-transparent pixels (#1933)
* Fix compilation on platforms that use ijg-libjpeg instead of libjpeg-turbo (#2563)
* `createImageData()`, `getImageData()` and `putImageData()` no longer count Cairo's row padding as pixel size, which gave wrong lengths and row offsets for narrow A8, A1 and RGB16_565 canvases (#2623)

3.2.3
==================
Expand Down
33 changes: 27 additions & 6 deletions src/CanvasRenderingContext2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,25 @@ Context2d::SetLanguage(const Napi::CallbackInfo& info, const Napi::Value& value)
state->lang = lang;
}

/*
* Bytes per pixel in ImageData. This can't be derived from the Cairo stride,
* which pads each row to 4 bytes (a 1px wide A8 surface has a 4 byte stride).
* A1 is bit-packed and isn't supported by get/putImageData.
*/

static int
imageDataBytesPerPixel(cairo_format_t format) {
switch (format) {
case CAIRO_FORMAT_RGB16_565:
return 2;
case CAIRO_FORMAT_A8:
case CAIRO_FORMAT_A1:
return 1;
default: // ARGB32, RGB24, RGB30
return 4;
}
}

/*
* Put image data.
*
Expand Down Expand Up @@ -781,7 +800,7 @@ Context2d::PutImageData(const Napi::CallbackInfo& info) {
}

int dstStride = canvas()->stride();
int Bpp = dstStride / canvas()->getWidth();
int Bpp = imageDataBytesPerPixel(canvas()->getFormat());
int srcStride = Bpp * imageData->width();

int64_t sx = 0
Expand Down Expand Up @@ -1024,7 +1043,7 @@ Context2d::GetImageData(const Napi::CallbackInfo& info) {
}

int srcStride = canvas->stride();
int bpp = srcStride / width;
int bpp = imageDataBytesPerPixel(canvas->getFormat());
// Note: barely fits: INT32_MAX * INT32_MAX * 4. Bpp can't be bigger than 4!
uint64_t size = (uint64_t)sw * sh * bpp;
int64_t dstStride = sw * bpp;
Expand Down Expand Up @@ -1177,17 +1196,19 @@ Context2d::CreateImageData(const Napi::CallbackInfo& info){
height = info[1].ToNumber().UnwrapOr(zero).Uint32Value();
}

int stride = canvas->stride();
double Bpp = static_cast<double>(stride) / canvas->getWidth();
int64_t nBytes = Bpp * width * height + .5;
double pixels = static_cast<double>(width) * height;
double size = canvas->getFormat() == CAIRO_FORMAT_A1
? std::ceil(pixels / 8) // bit-packed
: pixels * imageDataBytesPerPixel(canvas->getFormat());

if (nBytes > INT32_MAX) {
if (size > INT32_MAX) {
// INT32_MAX is what Firefox limits the buffer to
std::string msg = "buffer exceeds " + std::to_string(INT32_MAX) + " bytes";
Napi::Error::New(env, msg).ThrowAsJavaScriptException();
return env.Undefined();
}

int64_t nBytes = static_cast<int64_t>(size);
Napi::ArrayBuffer ab = Napi::ArrayBuffer::New(env, nBytes);
Napi::Value arr;

Expand Down
61 changes: 61 additions & 0 deletions test/canvas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,23 @@ describe('Canvas', function () {
assert.equal(data.width, 2)
assert.equal(data.height, 2)
})

it('does not count row padding as pixel size on narrow canvases (#2623)', function () {
// Cairo pads rows to 4 bytes, so a 1px wide canvas has a 4 byte stride
const a8 = createCanvas(1, 1).getContext('2d', { pixelFormat: 'A8' })
assert.equal(a8.createImageData(1, 2).data.length, 2)
assert.equal(a8.createImageData(3, 2).data.length, 6)

const rgb16 = createCanvas(1, 1).getContext('2d', { pixelFormat: 'RGB16_565' })
assert.equal(rgb16.createImageData(1, 2).data.length, 2)
assert.equal(rgb16.createImageData(3, 2).data.length, 6)

const a1 = createCanvas(1, 1).getContext('2d', { pixelFormat: 'A1' })
assert.equal(a1.createImageData(2, 6).data.length, Math.ceil(2 * 6 / 8))

const argb = createCanvas(1, 1).getContext('2d')
assert.equal(argb.createImageData(1, 2).data.length, 1 * 2 * 4)
})
})

describe('ImageData', function () {
Expand Down Expand Up @@ -1315,6 +1332,30 @@ describe('Canvas', function () {
assert.equal(191, imageData.data[5])
})

it('uses a tight row length on narrow canvases (#2623)', function () {
const canvas = createCanvas(1, 3)
const ctx = canvas.getContext('2d', { pixelFormat: 'A8' })
ctx.fillStyle = 'rgba(0,0,0,0.25)'
ctx.fillRect(0, 0, 1, 1)
ctx.fillStyle = 'rgba(0,0,0,0.5)'
ctx.fillRect(0, 1, 1, 1)
ctx.fillStyle = 'rgba(0,0,0,0.75)'
ctx.fillRect(0, 2, 1, 1)

const imageData = ctx.getImageData(0, 0, 1, 3)
assert.equal(imageData.data.length, 3)
assert.deepEqual(Array.from(imageData.data), [63, 127, 191])

const rgb16 = createCanvas(1, 2).getContext('2d', { pixelFormat: 'RGB16_565' })
rgb16.fillStyle = '#f00'
rgb16.fillRect(0, 0, 1, 1)
rgb16.fillStyle = '#00f'
rgb16.fillRect(0, 1, 1, 1)
const rgbData = rgb16.getImageData(0, 0, 1, 2)
assert.equal(rgbData.data.length, 2)
assert.deepEqual(Array.from(rgbData.data), [0b11111 << 11, 0b11111])
})

it('works, full width, A1')

it('works, full width, RGB30')
Expand Down Expand Up @@ -2229,6 +2270,26 @@ describe('Canvas', function () {
assert.equal(pixel.data[1], 65500)
})

it('reads tight ImageData rows on narrow canvases (#2623)', function () {
const canvas = createCanvas(1, 3)
const ctx = canvas.getContext('2d', { pixelFormat: 'A8' })

const imgData = ctx.createImageData(1, 3)
imgData.data.set([10, 20, 30])
ctx.putImageData(imgData, 0, 0)
assert.deepEqual(Array.from(ctx.getImageData(0, 0, 1, 3).data), [10, 20, 30])

const wide = createImageData(new Uint8ClampedArray([1, 2, 3, 4, 5, 6]), 2, 3)
ctx.putImageData(wide, 0, 0)
assert.deepEqual(Array.from(ctx.getImageData(0, 0, 1, 3).data), [1, 3, 5])

const rgb16 = createCanvas(1, 2).getContext('2d', { pixelFormat: 'RGB16_565' })
const rgbData = rgb16.createImageData(1, 2)
rgbData.data.set([65535, 65500])
rgb16.putImageData(rgbData, 0, 0)
assert.deepEqual(Array.from(rgb16.getImageData(0, 0, 1, 2).data), [65535, 65500])
})

it('safely handles integer overflow in sx + sw and sy + sh', function () {
const canvas = createCanvas(256, 256)
const ctx = canvas.getContext('2d', {pixelFormat: 'A8'})
Expand Down
Loading