xref: /aosp_15_r20/external/skia/modules/canvaskit/htmlcanvas/imagedata.js (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1function ImageData(arr, width, height) {
2  if (!width || height === 0) {
3    throw new TypeError('invalid dimensions, width and height must be non-zero');
4  }
5  if (arr.length % 4) {
6    throw new TypeError('arr must be a multiple of 4');
7  }
8  height = height || arr.length/(4*width);
9
10  Object.defineProperty(this, 'data', {
11    value: arr,
12    writable: false
13  });
14  Object.defineProperty(this, 'height', {
15    value: height,
16    writable: false
17  });
18  Object.defineProperty(this, 'width', {
19    value: width,
20    writable: false
21  });
22}
23
24CanvasKit.ImageData = function() {
25  if (arguments.length === 2) {
26    var width = arguments[0];
27    var height = arguments[1];
28    var byteLength = 4 * width * height;
29    return new ImageData(new Uint8ClampedArray(byteLength),
30                         width, height);
31  } else if (arguments.length === 3) {
32    var arr = arguments[0];
33    if (arr.prototype.constructor !== Uint8ClampedArray ) {
34      throw new TypeError('bytes must be given as a Uint8ClampedArray');
35    }
36    var width = arguments[1];
37    var height = arguments[2];
38    if (arr % 4) {
39      throw new TypeError('bytes must be given in a multiple of 4');
40    }
41    if (arr % width) {
42      throw new TypeError('bytes must divide evenly by width');
43    }
44    if (height && (height !== (arr / (width * 4)))) {
45      throw new TypeError('invalid height given');
46    }
47    height = arr / (width * 4);
48    return new ImageData(arr, width, height);
49  } else {
50    throw new TypeError('invalid number of arguments - takes 2 or 3, saw ' + arguments.length);
51  }
52}