-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #386: gdImageGrayScale() may produce colors
We have to make sure to avoid alpha-blending issues by explicitly switching to `gdEffectReplace` and to restore the old value afterwards. We also document the algorithm used by `gdImageGrayScale()` and note its limitations regarding palette images.
- Loading branch information
Showing
6 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/basic | ||
/bug00386 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
IF(PNG_FOUND) | ||
LIST(APPEND TESTS_FILES | ||
basic | ||
bug00386 | ||
) | ||
ENDIF(PNG_FOUND) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
if HAVE_LIBPNG | ||
libgd_test_programs += \ | ||
gdimagegrayscale/basic | ||
gdimagegrayscale/basic \ | ||
gdimagegrayscale/bug00386 | ||
endif | ||
|
||
EXTRA_DIST += \ | ||
gdimagegrayscale/CMakeLists.txt \ | ||
gdimagegrayscale/basic.png \ | ||
gdimagegrayscale/basic_exp.png | ||
gdimagegrayscale/basic_exp.png \ | ||
gdimagegrayscale/bug00386.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Test that gdImageGrayScale() produces pure gray-scale images | ||
* | ||
* We are reading a PNG image with transparency, apply gdImageGrayScale() and | ||
* test that each pixel has equal red, blue and green components. To avoid | ||
* flooding the test log, we bail out after the first non gray-scale pixel. | ||
* | ||
* We also make sure that the image is a truecolor image, since otherwise it is | ||
* not guaranteed that all pixels are actually gray-scale. | ||
* | ||
* See also <https://github.com/libgd/libgd/issues/386>. | ||
*/ | ||
|
||
|
||
#include "gd.h" | ||
#include "gdtest.h" | ||
|
||
|
||
int main() | ||
{ | ||
gdImagePtr im; | ||
FILE *fp; | ||
int res; | ||
int i, j; | ||
|
||
fp = gdTestFileOpen2("gdimagegrayscale", "bug00386.png"); | ||
gdTestAssert(fp != NULL); | ||
im = gdImageCreateFromPng(fp); | ||
gdTestAssert(im != NULL); | ||
fclose(fp); | ||
|
||
res = gdImageTrueColor(im); | ||
gdTestAssert(res != 0); | ||
|
||
res = gdImageGrayScale(im); | ||
gdTestAssert(res != 0); | ||
|
||
for (i = 0; i < gdImageSX(im); i++) { | ||
for (j = 0; j < gdImageSY(im); j++) { | ||
int color = gdImageGetTrueColorPixel(im, i, j); | ||
int red = gdImageRed(im, color); | ||
int green = gdImageGreen(im, color); | ||
int blue = gdImageBlue(im, color); | ||
if (!gdTestAssert(red == green && green == blue)) { | ||
return gdNumFailures(); | ||
} | ||
} | ||
} | ||
|
||
return gdNumFailures(); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.