Skip to content

Commit d582241

Browse files
committed
Fix #67325: imagetruecolortopalette: white is duplicated in palette
gdImageTrueColorToPalette() is sometimes wasteful by putting multiple white color entries into the palette. This is caused by an obvious typo, where to avoid a division by zero when `total` is zero, `count` is checked instead of `total`. We fix this issue to improve the quality of the color quantization. Cf. <libgd/libgd@24b4550f>
1 parent 9513187 commit d582241

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? 2016, PHP 5.6.27
44

5+
- GD:
6+
. Fixed bug #67325 (imagetruecolortopalette: white is duplicated in palette).
7+
(cmb)
8+
59
15 Sep 2016, PHP 5.6.26
610

711
- Core:

ext/gd/libgd/gd_topal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ LOCAL (void)
760760
cinfo->colormap[2][icolor] = (JSAMPLE) ((c2total + (total >> 1)) / total);
761761
#else
762762
/* 2.0.16: Paul den Dulk found an occasion where total can be 0 */
763-
if (count)
763+
if (total)
764764
{
765765
nim->red[icolor] = (int) ((c0total + (total >> 1)) / total);
766766
nim->green[icolor] = (int) ((c1total + (total >> 1)) / total);

ext/gd/tests/bug67325.jpeg

13.3 KB
Loading

ext/gd/tests/bug67325.phpt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
Bug #67325 (imagetruecolortopalette: white is duplicated in palette)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('gd')) die('skip gd extension not available');
6+
if (!(imagetypes() & IMG_JPG)) die('skip JPEG support not available');
7+
?>
8+
--FILE--
9+
<?php
10+
$filename = __DIR__ . DIRECTORY_SEPARATOR . 'bug67325.jpeg';
11+
12+
$im = imagecreatefromjpeg($filename);
13+
imagetruecolortopalette($im, 0, 256);
14+
15+
$white = 0;
16+
for ($i = 0; $i < 256; $i++) {
17+
$components = imagecolorsforindex($im, $i);
18+
if ($components['red'] === 255 && $components['green'] === 255 && $components['blue'] === 255) {
19+
$white++;
20+
}
21+
}
22+
var_dump($white);
23+
24+
imagedestroy($im);
25+
?>
26+
===DONE===
27+
--EXPECT--
28+
int(0)
29+
===DONE===

0 commit comments

Comments
 (0)