skia2/modules/canvaskit/htmlcanvas/imagedata.js
Kevin Lubick 3f67f411d8 [canvaskit] Move from experimental to modules
Bug: skia:
Change-Id: I2ffd54cf81c974f3a80103e1726a06067cc90d82
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/200044
Reviewed-by: Mike Reed <reed@google.com>
Reviewed-by: Florin Malita <fmalita@chromium.org>
Commit-Queue: Kevin Lubick <kjlubick@google.com>
2019-03-11 21:09:52 +00:00

52 lines
1.4 KiB
JavaScript

function ImageData(arr, width, height) {
if (!width || height === 0) {
throw 'invalid dimensions, width and height must be non-zero';
}
if (arr.length % 4) {
throw 'arr must be a multiple of 4';
}
height = height || arr.length/(4*width);
Object.defineProperty(this, 'data', {
value: arr,
writable: false
});
Object.defineProperty(this, 'height', {
value: height,
writable: false
});
Object.defineProperty(this, 'width', {
value: width,
writable: false
});
}
CanvasKit.ImageData = function() {
if (arguments.length === 2) {
var width = arguments[0];
var height = arguments[1];
var byteLength = 4 * width * height;
return new ImageData(new Uint8ClampedArray(byteLength),
width, height);
} else if (arguments.length === 3) {
var arr = arguments[0];
if (arr.prototype.constructor !== Uint8ClampedArray ) {
throw 'bytes must be given as a Uint8ClampedArray';
}
var width = arguments[1];
var height = arguments[2];
if (arr % 4) {
throw 'bytes must be given in a multiple of 4';
}
if (arr % width) {
throw 'bytes must divide evenly by width';
}
if (height && (height !== (arr / (width * 4)))) {
throw 'invalid height given';
}
height = arr / (width * 4);
return new ImageData(arr, width, height);
} else {
throw 'invalid number of arguments - takes 2 or 3, saw ' + arguments.length;
}
}