Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce colors of raw ImageData without having to encode then decode a PNG #75

Closed
Merlin04 opened this issue Apr 28, 2022 · 3 comments
Closed

Comments

@Merlin04
Copy link

In my application I have to use UPNG to compress a large quantity of images - specifically, given an ImageData, I need to reduce the colors to the provided value then output another ImageData. The way I'm currently doing this involves encoding the data as a PNG then decoding it again:

function transform(quality: number, image: ImageData): ImageData {
    const png = UPNG.encode([image.data.buffer], image.width, image.height, quality);
    const buf = UPNG.toRGBA8(UPNG.decode(png))[0];
    return new ImageData(new Uint8ClampedArray(buf), image.width, image.height);
}

However, from some testing it appears the decoding of the PNG on the second line of the function creates a significant performance bottleneck. Is it possible to avoid creating a PNG in the first place so I don't have to decode it, and just reduce the number of colors of the raw pixel data and output that?

@photopea
Copy link
Owner

Sure, check out our quantizer: UPNG.quantize(), it is described in the README.md

@photopea
Copy link
Owner

@Merlin04
Copy link
Author

That works great, thanks so much for the help (and for making such a great library!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants