Skip to content

Instantly share code, notes, and snippets.

@yjbanov
Created December 8, 2021 22:42
Show Gist options
  • Save yjbanov/81dac10c913c6045a4524364e7dce60a to your computer and use it in GitHub Desktop.
Save yjbanov/81dac10c913c6045a4524364e7dce60a to your computer and use it in GitHub Desktop.
frame decode order skia vs webcodecs
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SkImage.readPixels</title>
</head>
<body>
<canvas id=foo width=300 height=300></canvas>
<script type="text/javascript"
src="https://unpkg.com/[email protected]/bin/canvaskit.js"></script>
<script type="text/javascript">
async function run() {
/// An animated GIF image with 3 1x1 pixel frames (a red, green, and blue
/// frames). The GIF animates forever, and each frame has a 100ms delay.
let kAnimatedGif = Uint8Array.from([
0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00, 0xa1, 0x03, 0x00,
0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0xff, 0xff, 0xff, 0x21,
0xff, 0x0b, 0x4e, 0x45, 0x54, 0x53, 0x43, 0x41, 0x50, 0x45, 0x32, 0x2e, 0x30,
0x03, 0x01, 0x00, 0x00, 0x00, 0x21, 0xf9, 0x04, 0x00, 0x0a, 0x00, 0xff, 0x00,
0x2c, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x4c,
0x01, 0x00, 0x21, 0xf9, 0x04, 0x00, 0x0a, 0x00, 0xff, 0x00, 0x2c, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x54, 0x01, 0x00, 0x21,
0xf9, 0x04, 0x00, 0x0a, 0x00, 0xff, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x01,
0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x44, 0x01, 0x00, 0x3b,
]);
let decoder = new ImageDecoder({
type: 'image/gif',
data: kAnimatedGif.buffer,
premultiplyAlpha: 'premultiply',
colorSpaceConversion: 'default',
preferAnimation: true
});
await decoder.tracks.ready;
await decoder.completed;
console.log('WebCodecs:');
for (let i = 0; i < decoder.tracks.selectedTrack.frameCount; i++) {
let decodeResult = await decoder.decode({frameIndex: i});
let videoFrame = decodeResult.image;
let destination = new Uint8Array(videoFrame.allocationSize());
await videoFrame.copyTo(destination);
console.log(`${videoFrame.format}: ${destination}`);
}
console.log('CanvasKit:');
const ckLoaded = CanvasKitInit({locateFile: (file) => 'https://unpkg.com/[email protected]/bin/'+file});
ckLoaded.then((CanvasKit) => {
let animatedImage = CanvasKit.MakeAnimatedImageFromEncoded(kAnimatedGif);
let frameCount = animatedImage.getFrameCount();
for (let i = 0; i < frameCount; i++) {
animatedImage.decodeNextFrame();
let image = animatedImage.makeImageAtCurrentFrame();
let imageInfo = {
alphaType: CanvasKit.AlphaType.Premul,
colorType: CanvasKit.ColorType.RGBA_8888,
colorSpace: CanvasKit.ColorSpace.SRGB,
width: image.width(),
height: image.height()
};
console.log(`RGBA: ${image.readPixels(0, 0, imageInfo)}`);
}
});
}
run();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment