|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64736 Incorrect calculation in gdColorMatch
Submitted: 2013-04-29 23:05 UTC Modified: 2015-03-26 05:30 UTC
From: Assigned: pajoye (profile)
Status: Wont fix Package: GD related
PHP Version: 5.5Git-2013-04-29 (Git) OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
New email:
PHP Version: OS:


 [2013-04-29 23:05 UTC]
Compilations warnings in gd_crop.c flag a potential calculation issue in gdColorMatch:

/home/cjones/php-5.5/ext/gd/libgd/gd_crop.c:340: warning: suggest parentheses around arithmetic in operand of ^
/home/cjones/php-5.5/ext/gd/libgd/gd_crop.c:340: warning: suggest parentheses around arithmetic in operand of ^

The code at line 340 is:

	const double dist_perc = sqrt(dist / (255^2 + 255^2 + 255^2));

The denominator is currently evaluated as 253. Was it instead intended be (3 * (255 * 255)) == 195075 ?


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2013-04-30 05:21 UTC]
That's the classic distance calculation in the RGB cube, does ^ not have 
precedence with the compiler you use? It should (or has in all platforms I use) 
 [2013-04-30 05:21 UTC]
-Status: Open +Status: Feedback
 [2013-04-30 14:48 UTC]
In C, "^" is the bitwise exclusive OR operator (and has lower precedence than 
"+"). Pow() is the power function.
 [2013-04-30 14:48 UTC]
-Status: Feedback +Status: Open
 [2015-03-25 01:24 UTC]
-Assigned To: +Assigned To: pajoye
 [2015-03-25 01:24 UTC]
It also seems to me, that the calculation is wrong.

Assuming that dr, dg, db and da are in range [0, 255], dist
would be in the range [0, 510]. Assuming 255^2 is supposed to mean
squaring, the argument to sqrt() (in the assignment to dist_perc)
is in the range [0, 0.00196], so dist_perc is in the range [0,
0.04428] -- apparently not, what is supposed to represent a

Assuming 255^2 is actually supposed to mean XOR, the respective
argument to sqrt() would be in the range [0, 0.6719], so dist_perc
is in the range [0, 0.8197] -- looks rather fine, but seems
somewhat arbitrarily.

With the following, dist_perc would be in the range [0, 1].

  const double dist_perc = dist / sqrt(4 * 255 * 255);
However, I have some doubts, that the Pythagorean theorem applies
to four dimensions in this manner.

Anyhow, changing the calculation of dist_perc would constitute a BC
 [2015-03-26 05:30 UTC]
-Status: Assigned +Status: Wont fix
 [2015-03-26 05:30 UTC]
it is a good enough approximation. I may change it at some time in gd 2.2, using actual color comparison methods in sRGB space, if not too slow :)
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sat Sep 30 16:01:24 2023 UTC