1cb93a386Sopenharmony_ci(function(window) { 2cb93a386Sopenharmony_ci 3cb93a386Sopenharmony_cilet CanvasKit = null; 4cb93a386Sopenharmony_ci 5cb93a386Sopenharmony_ciwindow.loadPolyfill = () => { 6cb93a386Sopenharmony_ci // TODO(kjlubick): change ready().then() to just then() when using a newer version 7cb93a386Sopenharmony_ci // from npm (see 8cb93a386Sopenharmony_ci // https://skia.googlesource.com/skia/+/d1285b131bcf9c10fe1ad16fd2830c556715ed9e) 9cb93a386Sopenharmony_ci return CanvasKitInit({ 10cb93a386Sopenharmony_ci locateFile: (file) => 'https://unpkg.com/canvaskit-wasm@0.6.0/bin/'+file, 11cb93a386Sopenharmony_ci }).ready().then((CK) => { 12cb93a386Sopenharmony_ci CanvasKit = CK; 13cb93a386Sopenharmony_ci }); 14cb93a386Sopenharmony_ci} 15cb93a386Sopenharmony_ci 16cb93a386Sopenharmony_ciwindow.createImageData = (src, options) => { 17cb93a386Sopenharmony_ci const skImg = CanvasKit.MakeImageFromEncoded(src); 18cb93a386Sopenharmony_ci // we know width and height 19cb93a386Sopenharmony_ci const imageInfo = { 20cb93a386Sopenharmony_ci width: options.resizeWidth || skImg.width(), 21cb93a386Sopenharmony_ci height: options.resizeHeight || skImg.height(), 22cb93a386Sopenharmony_ci alphaType: options.premul ? CanvasKit.AlphaType.Premul : CanvasKit.AlphaType.Unpremul, 23cb93a386Sopenharmony_ci } 24cb93a386Sopenharmony_ci switch (options.colorType) { 25cb93a386Sopenharmony_ci case "float32": 26cb93a386Sopenharmony_ci imageInfo.colorType = CanvasKit.ColorType.RGBA_F32; 27cb93a386Sopenharmony_ci break; 28cb93a386Sopenharmony_ci 29cb93a386Sopenharmony_ci case "uint8": 30cb93a386Sopenharmony_ci default: 31cb93a386Sopenharmony_ci imageInfo.colorType = CanvasKit.ColorType.RGBA_8888; 32cb93a386Sopenharmony_ci break; 33cb93a386Sopenharmony_ci } 34cb93a386Sopenharmony_ci 35cb93a386Sopenharmony_ci const pixels = skImg.readPixels(imageInfo, 0, 0); 36cb93a386Sopenharmony_ci let output; 37cb93a386Sopenharmony_ci // ImageData at the moment only supports Uint8, so we have to convert our numbers to that 38cb93a386Sopenharmony_ci switch (options.colorType) { 39cb93a386Sopenharmony_ci case "float32": 40cb93a386Sopenharmony_ci // This will make an extra copy, which is a limitation of the native Browser's 41cb93a386Sopenharmony_ci // ImageData support. 42cb93a386Sopenharmony_ci output = new Uint8ClampedArray(pixels); 43cb93a386Sopenharmony_ci break; 44cb93a386Sopenharmony_ci 45cb93a386Sopenharmony_ci case "uint8": 46cb93a386Sopenharmony_ci default: 47cb93a386Sopenharmony_ci // We can cast w/o another copy 48cb93a386Sopenharmony_ci output = new Uint8ClampedArray(pixels.buffer); 49cb93a386Sopenharmony_ci break; 50cb93a386Sopenharmony_ci } 51cb93a386Sopenharmony_ci 52cb93a386Sopenharmony_ci 53cb93a386Sopenharmony_ci const ret = new ImageData(output, imageInfo.width, imageInfo.height); 54cb93a386Sopenharmony_ci skImg.delete(); 55cb93a386Sopenharmony_ci 56cb93a386Sopenharmony_ci return ret; 57cb93a386Sopenharmony_ci} 58cb93a386Sopenharmony_ci 59cb93a386Sopenharmony_ci 60cb93a386Sopenharmony_ci})(window); 61