php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41820 Color noise in resized image when using IMAGECOPYRESAMPLED
Submitted: 2007-06-27 10:07 UTC Modified: 2009-05-07 01:00 UTC
Votes:19
Avg. Score:4.5 ± 0.7
Reproduced:16 of 16 (100.0%)
Same Version:2 (12.5%)
Same OS:1 (6.2%)
From: richard dot gavenda at rksting dot cz Assigned: pajoye (profile)
Status: No Feedback Package: GD related
PHP Version: 5.2.3 OS: Windows XP
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2007-06-27 10:07 UTC] richard dot gavenda at rksting dot cz
Description:
------------
When resizing an image filled with solid color using imagecopyresampled, a destination image is not filled with same solid color, but with noise of two colors. I was tried this with grayscale images. Second color in noise was exactly same as source color, but with RGB channels lowered by 1. When the source image was filled with color, which had RGB channels values equal to power of 2 (0,1,2,4,8,16,32,64,128) the noise disappears. 

Reproduce code:
---------------
$im = imagecreatetruecolor(1045,600);
$back = imagecolorallocate($im, 100, 100, 100);
imagefill($im, 0, 0, $back);
$im2 = imagecreatetruecolor(383,220);
imagecopyresampled($im2,$im,0,0,0,0,383,220,1045,600);
imagepng($im2);

Expected result:
----------------
PNG file filled with color 100,100,100(RGB)

Actual result:
--------------
PNG file filled with color noise of 100,100,100(RGB) and 99,99,99(RGB)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-06-28 07:29 UTC] pajoye@php.net
CopyResampled using floating points to calculate the destination color. It may result to rounding problem. However I cannot reproduce this problem on my development platforms (x86 32 and 64bit, amd 64).

What's your architecture?
 [2007-06-28 09:53 UTC] richard dot gavenda at rksting dot cz
AMD 32, Windows XP, PHP 5.2.3 - bug present
x86 64, Centos 4, PHP 4.3.9 - bug present
x86 32, Fedora Core 5, PHP 5.1.6 - bug present

Source image 800x600 filled with RBG(127,127,127):
target image 800x600 - OK
http://81.200.49.53:25459/resample.php?w=800&h=600&c=127

target image 701x600 - vertical stripes
http://81.200.49.53:25459/resample.php?w=701&h=600&c=127

target image 800x501 - horizontal stripes
http://81.200.49.53:25459/resample.php?w=800&h=501&c=127

target image 701x501 - noise
http://81.200.49.53:25459/resample.php?w=701&h=501&c=127

"When is source image filled with RGB values equal to power of 2 (0,1,2,4,8,16,32,64,128) the noise disappears", this rule i can reproduce only on AMD 32 architecture. On other platform is noise present regardless of fill color.
 [2007-06-28 09:58 UTC] pajoye@php.net
It is what I said, a rounding issue. I'm not sure yet how to make it more portable.
 [2007-06-28 10:00 UTC] richard dot gavenda at rksting dot cz
Position of horizontal and vertical stripes is different on different machines.
 [2007-06-28 10:08 UTC] pajoye@php.net
"Position of horizontal and vertical stripes is different on different
machines."

Yes, it is the problem with floating points value when we need a constant and precise result on different architecture. See; http://docs.sun.com/source/806-3568/ncg_goldberg.html for a good explanation about this problem.

For example, your images has variation between 126 and 127 for a given channel. It may be only something like 126.49999 and 126.50000.

I don't think I can fix it easily for this implementation (bicubic interpolation using floating points implementation).

Future versions will have more filters and using less volatile precisions.
 [2007-06-28 10:12 UTC] richard dot gavenda at rksting dot cz
So there must be some sort of bug in resampling algorithm. When i resizing bitmap filled with solid color in any bitmap editor, using any aproximation (linear, bicubic, etc.) I get an image filled with exactly same solid color.

With this issue I'm unable to reach one bit transparency of resized image.
 [2007-06-28 10:14 UTC] richard dot gavenda at rksting dot cz
Ok, so let's close whis thread. I will wait for future versions :-)
 [2008-07-20 13:44 UTC] pajoye@php.net
Can you try the patch available at #45030 please?
 [2008-07-28 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2009-04-29 21:52 UTC] neo at lozhki dot net
Is there a solution now after two years?
 [2009-04-29 22:28 UTC] pajoye@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.3-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/


 [2009-04-30 13:43 UTC] neo at lozhki dot net
I've installed the latest snapshot. The problem is still absolutely the same. I have horizontal bands of color #fefefe on the picture with clear white background after resampling. They are visible only on high quality monitors for designers, but they are. It's a bug.
 [2009-05-07 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2015-12-05 14:59 UTC] eneroth3 at gmail dot com
As a workaround I reversed all the colors with imagefilter() before and after calling imagecopyresampled. This seems to help for the white areas but would of course result in the same problem for black areas. However these are usually not as common.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun May 26 11:01:26 2019 UTC