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