blob: cfc4da857cc2f1ba9df8b425da65766ca261cec3 [file] [log] [blame]
Kevin Lubick53eabf62018-12-10 12:41:26 -05001function ImageData(arr, width, height) {
2 if (!width || height === 0) {
3 throw 'invalid dimensions, width and height must be non-zero';
4 }
5 if (arr.length % 4) {
6 throw '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 'bytes must be given as a Uint8ClampedArray';
35 }
36 var width = arguments[1];
37 var height = arguments[2];
38 if (arr % 4) {
39 throw 'bytes must be given in a multiple of 4';
40 }
41 if (arr % width) {
42 throw 'bytes must divide evenly by width';
43 }
44 if (height && (height !== (arr / (width * 4)))) {
45 throw 'invalid height given';
46 }
47 height = arr / (width * 4);
48 return new ImageData(arr, width, height);
49 } else {
50 throw 'invalid number of arguments - takes 2 or 3, saw ' + arguments.length;
51 }
52}