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

Support alternative color quantization algorithms #17270

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

cmb69
Copy link
Member

@cmb69 cmb69 commented Dec 25, 2024

As is, ext/gd only supports the default color quantization algorithm, which is used for imagetruecolortopalette(). However, libgd supports alternative algorithms as of GD 2.1.0, namely NeuQuant (which is built in) and libimagequant (which is used by pngquant).

We add support for these alternative color quantization algorithms as small wrappers around the two respective libgd functions, introducing also a couple of constants. We also add the nnquant module to our bundled libgd.


TODO:

References:
https://libgd.github.io/manuals/2.3.3/files/gd_topal-c.html

As is, ext/gd only supports the default color quantization algorithm,
which is used for `imagetruecolortopalette()`.  However, libgd supports
alternative algorithms as of GD 2.1.0, namely NeuQuant (which is built
in) and libimagequant (which is used by pngquant).

We add support for these alternative color quantization algorithms as
small wrappers around the two respective libgd functions, introducing
also a couple of constants.  We also add the nnquant module to our
bundled libgd.
ext/gd/gd.c Outdated Show resolved Hide resolved
if (gdImageTrueColorToPaletteSetMethod(im, method, speed)) {
RETURN_TRUE;
} else {
php_error_docref(NULL, E_WARNING, "Couldn't set quantization method");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question you know better gd than I do, seems to me the only failure case left is when the method is not available correct ? so always will be unavailable then. dunno if we should raise an exception and this call only returns true.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I agree, but we should add info about the available quantization methods first.

We might need to check for vcomp140.dll, too.
* proper range check for $method
* error message must refer to `IMG_QUANT_*`
* gd_topal.c must include libimagequant.h (like upstream does)
* fix _aligned_malloc() arguments
  <libgd/libgd#914>
@cmb69
Copy link
Member Author

cmb69 commented Dec 26, 2024

While adding a new test case, I've noticed that two others would fail when libimagequant is available. I'm somewhat surprised that this apparently hasn't been reported already.

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

Successfully merging this pull request may close these issues.

2 participants