jamsinclair / jSquash

Browser & Web Worker focussed wasm bundles derived from the Squoosh App.
Apache License 2.0
222 stars 14 forks source link

Improve @jquash/avif decode speed #43

Open talhaozdemir opened 8 months ago

talhaozdemir commented 8 months ago

Decode speed is about 1200ms - 1500ms, but when I use an AVIF supported browser, speed is about 200ms - 300ms. Is there any way to increase decoding speed?

jamsinclair commented 8 months ago

Thanks @talhaozdemir! I think I might need a little more information:

talhaozdemir commented 8 months ago

hi, thanks for reply, here is what I tried;

import * as avif from "@jsquash/avif";
.
.
.

const buffer = base64ToArrayBuffer(imageFile);
let imgData = await avif.decode(buffer);
var base64 = imageDataToBase64(imgData);
image.onload = function () {

callback();
};
image.src = base64;

imageFile is an base64 avif file

and some functions for above code;

function base64ToArrayBuffer(base64) {
  base64 = base64.replace(/^data:image\/(png|jpeg|jpg|avif);base64,/, "");
  var binaryString = atob(base64);
  var bytes = new Uint8Array(binaryString.length);
  for (var i = 0; i < binaryString.length; i++) {
    bytes[i] = binaryString.charCodeAt(i);
  }
  return bytes.buffer;
}

function imageDataToBase64(imageData) {
  const canvas = document.createElement("canvas");
  canvas.width = imageData.width;
  canvas.height = imageData.height;

  const ctx = canvas.getContext("2d");
  ctx.putImageData(imageData, 0, 0);

  return canvas.toDataURL();
}

and yes it is browser's native decoding speed

jamsinclair commented 8 months ago

Thanks for the extra context! That makes it very clear. Yeah... I am not sure if we can compete with the browser's own decoder.

There are some optimizations that could be made to help improve the speed of this project's AVIF decoder like

I don't have the expertise or time to explore that right now, but I would consider a pull request if someone is interested.

If you're after speed in the browser, I recommend sticking with the Canvas API or seeing if there's a more optimized AVIF wasm library out there.

talhaozdemir commented 8 months ago

Thank you for the suggestions; I don't have much technical knowledge in these areas either. These suggestions are sufficient, you can close this issue if you want.

jamsinclair commented 8 months ago

Looks like there is an existing PR upstream for upgrading to a newer avif version. When I find time I'll see if we can pull this in.

https://github.com/GoogleChromeLabs/squoosh/pull/1381

talhaozdemir commented 8 months ago

Awesome news! Thanks for your support.