Skip to content

Fix #53580: During resize gdImageCopyResampled cause colors change #7402

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions ext/gd/libgd/gd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2612,7 +2612,6 @@ void gdImageCopyResampled (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, i
green /= spixels;
blue /= spixels;
alpha /= spixels;
alpha += 0.5;
}
if ( alpha_sum != 0.0f) {
if( contrib_sum != 0.0f) {
Expand All @@ -2622,20 +2621,12 @@ void gdImageCopyResampled (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, i
green /= alpha_sum;
blue /= alpha_sum;
}
/* Clamping to allow for rounding errors above */
if (red > 255.0f) {
red = 255.0f;
}
if (green > 255.0f) {
green = 255.0f;
}
if (blue > 255.0f) {
blue = 255.0f;
}
if (alpha > gdAlphaMax) {
alpha = gdAlphaMax;
}
gdImageSetPixel(dst, x, y, gdTrueColorAlpha ((int) red, (int) green, (int) blue, (int) alpha));
/* Round up closest next channel value and clamp to max channel value */
red = red >= 255.5 ? 255 : red+0.5;
blue = blue >= 255.5 ? 255 : blue+0.5;
green = green >= 255.5 ? 255 : green+0.5;
alpha = alpha >= gdAlphaMax+0.5 ? 255 : alpha+0.5;
gdImageSetPixel(dst, x, y, gdTrueColorAlpha ((int)red, (int)green, (int)blue, (int)alpha));
}
}
}
Expand Down
7 changes: 1 addition & 6 deletions ext/gd/libgd/gd_png.c
Original file line number Diff line number Diff line change
Expand Up @@ -728,12 +728,7 @@ void gdImagePngCtxEx (gdImagePtr im, gdIOCtx * outfile, int level, int basefilte
*/
a = gdTrueColorGetAlpha(thisPixel);
/* Andrew Hull: >> 6, not >> 7! (gd 2.0.5) */
if (a == 127) {
*pOutputRow++ = 0;
} else {
*pOutputRow++ = 255 - ((a << 1) + (a >> 6));
}

*pOutputRow++ = 255 - ((a << 1) + (a >> 6));
}
}
}
Expand Down
32 changes: 32 additions & 0 deletions ext/gd/tests/bug53580.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
Bug #53580 (During resize gdImageCopyResampled cause colors change)
--SKIPIF--
<?php
if (!extension_loaded('gd')) die("skip gd extension not available");
if (!GD_BUNDLED && version_compare(GD_VERSION, "2.3.2") <= 0) {
die("skip test requires GD > 2.3.2");
}
?>
--FILE--
<?php
$w0 = 100;
$h0 = 100;
$w1 = 150;
$h1 = 150;
$c0 = 0xffffff;

$im0 = imagecreatetruecolor($w0, $h0);
imagefilledrectangle($im0, 0, 0, $w0 - 1, $h0 - 1, $c0);

$im1 = imagecreatetruecolor($w1, $h1);
imagecopyresampled($im1, $im0, 0, 0, 0, 0, $w1, $h1, $w0, $h0);

for ($i = 0; $i < $w1; $i++) {
for ($j = 0; $j < $h1; $j++) {
if (($c1 = imagecolorat($im1, $i, $j)) !== $c0) {
printf("%d,%d = %d\n", $i, $j, $c1);
}
}
}
?>
--EXPECT--