Skip to content

Support alternative color quantization algorithms#17270

Closed
cmb69 wants to merge 5 commits into
php:masterfrom
cmb69:cmb/color-quantization
Closed

Support alternative color quantization algorithms#17270
cmb69 wants to merge 5 commits into
php:masterfrom
cmb69:cmb/color-quantization

Conversation

@cmb69

@cmb69 cmb69 commented Dec 25, 2024

Copy link
Copy Markdown
Member

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.
Comment thread ext/gd/gd.c Outdated
Comment thread ext/gd/gd.c
if (gdImageTrueColorToPaletteSetMethod(im, method, speed)) {
RETURN_TRUE;
} else {
php_error_docref(NULL, E_WARNING, "Couldn't set quantization method");

Copy link
Copy Markdown
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
Copy Markdown
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

cmb69 commented Dec 26, 2024

Copy link
Copy Markdown
Member Author

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.

@cmb69

cmb69 commented Feb 14, 2025

Copy link
Copy Markdown
Member Author

Also would need to cater to libgd/libgd#953.

@pierrejoye

Copy link
Copy Markdown
Contributor

My plan was to expose them once #22532 is merged. That's the libgd/ sync only. Following PR adds new APIs etc. See https://wiki.php.net/rfc/ext-gd-2.4 (draft, writing it)

@pierrejoye pierrejoye closed this Jul 8, 2026
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.

3 participants