[canvaskit] Fix colorType bug
Bug: skia:9788
Change-Id: I1c7eeefb0e2883edc9095599ff2f10e8419f54c9
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/265956
Reviewed-by: Brian Osman <brianosman@google.com>
diff --git a/modules/canvaskit/CHANGELOG.md b/modules/canvaskit/CHANGELOG.md
index 7ba10bb..12a3d0c 100644
--- a/modules/canvaskit/CHANGELOG.md
+++ b/modules/canvaskit/CHANGELOG.md
@@ -17,6 +17,8 @@
### Fixed
- Bug that sometimes resulted in 'Cannot perform Construct on a neutered ArrayBuffer'
+ - Bug with SkImage.readPixels (skbug.com/9788)
+
## [0.11.0] - 2020-01-10
diff --git a/modules/canvaskit/interface.js b/modules/canvaskit/interface.js
index 77fd8cf..070b6e5 100644
--- a/modules/canvaskit/interface.js
+++ b/modules/canvaskit/interface.js
@@ -535,7 +535,9 @@
CanvasKit.SkImage.prototype.readPixels = function(imageInfo, srcX, srcY) {
var rowBytes;
- switch (imageInfo.colorType){
+ // Important to use ["string"] notation here, otherwise the closure compiler will
+ // minify away the colorType.
+ switch (imageInfo["colorType"]) {
case CanvasKit.ColorType.RGBA_8888:
rowBytes = imageInfo.width * 4; // 1 byte per channel == 4 bytes per pixel in 8888
break;
@@ -557,7 +559,7 @@
// Put those pixels into a typed array of the right format and then
// make a copy with slice() that we can return.
var retVal = null;
- switch (imageInfo.colorType){
+ switch (imageInfo["colorType"]) {
case CanvasKit.ColorType.RGBA_8888:
retVal = new Uint8Array(CanvasKit.HEAPU8.buffer, pPtr, pBytes).slice();
break;
diff --git a/modules/canvaskit/tests/core.spec.js b/modules/canvaskit/tests/core.spec.js
index 12d3cfb..5307868 100644
--- a/modules/canvaskit/tests/core.spec.js
+++ b/modules/canvaskit/tests/core.spec.js
@@ -110,6 +110,33 @@
decodeAndDrawSingleFrameImage('/assets/color_wheel.webp', 'drawImage_webp', done);
});
+ it('can readPixels from an SkImage', function(done) {
+ const imgPromise = fetch('/assets/mandrill_512.png')
+ .then((response) => response.arrayBuffer());
+ Promise.all([imgPromise, LoadCanvasKit]).then((values) => {
+ const imgData = values[0];
+ expect(imgData).toBeTruthy();
+ catchException(done, () => {
+ let img = CanvasKit.MakeImageFromEncoded(imgData);
+ expect(img).toBeTruthy();
+ const imageInfo = {
+ alphaType: CanvasKit.AlphaType.Unpremul,
+ colorType: CanvasKit.ColorType.RGBA_8888,
+ width: img.width(),
+ height: img.height(),
+ };
+
+ const pixels = img.readPixels(imageInfo, 0, 0);
+ // We know the image is 512 by 512 pixels in size, each pixel
+ // requires 4 bytes (R, G, B, A).
+ expect(pixels.length).toEqual(512 * 512 * 4);
+
+ img.delete();
+ done();
+ })();
+ });
+ });
+
it('can decode and draw an animated gif', function(done) {
const imgPromise = fetch('/assets/flightAnim.gif')
.then((response) => response.arrayBuffer());