From 65ee87f20dfebb4cde5f908cd6ba57384810e740 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Sun, 25 Sep 2016 09:47:23 +0200 Subject: [PATCH] Refactor imagegammacorrect() We apply the law `(b**r)**s == b**(r*s)` which holds for all non-negative b and positive r,s, so a single pow() suffices. Furthermore, we precompute the gamma, so the refactored code is simpler and faster. --- ext/gd/gd.c | 16 ++-- .../tests/imagegammacorrect_variation2.phpt | 72 ++++++++++++++++++ .../imagegammacorrect_variation2_1_1.png | Bin 0 -> 2009 bytes .../imagegammacorrect_variation2_1_2.png | Bin 0 -> 2121 bytes .../imagegammacorrect_variation2_2_1.png | Bin 0 -> 2150 bytes 5 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 ext/gd/tests/imagegammacorrect_variation2.phpt create mode 100644 ext/gd/tests/imagegammacorrect_variation2_1_1.png create mode 100644 ext/gd/tests/imagegammacorrect_variation2_1_2.png create mode 100644 ext/gd/tests/imagegammacorrect_variation2_2_1.png diff --git a/ext/gd/gd.c b/ext/gd/gd.c index 692497d186..b726aab10c 100644 --- a/ext/gd/gd.c +++ b/ext/gd/gd.c @@ -3025,7 +3025,7 @@ PHP_FUNCTION(imagegammacorrect) zval *IM; gdImagePtr im; int i; - double input, output; + double input, output, gamma; if (zend_parse_parameters(ZEND_NUM_ARGS(), "rdd", &IM, &input, &output) == FAILURE) { return; @@ -3036,6 +3036,8 @@ PHP_FUNCTION(imagegammacorrect) RETURN_FALSE; } + gamma = input / output; + if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(IM), "Image", le_gd)) == NULL) { RETURN_FALSE; } @@ -3048,9 +3050,9 @@ PHP_FUNCTION(imagegammacorrect) c = gdImageGetPixel(im, x, y); gdImageSetPixel(im, x, y, gdTrueColorAlpha( - (int) ((pow((pow((gdTrueColorGetRed(c) / 255.0), input)), 1.0 / output) * 255) + .5), - (int) ((pow((pow((gdTrueColorGetGreen(c) / 255.0), input)), 1.0 / output) * 255) + .5), - (int) ((pow((pow((gdTrueColorGetBlue(c) / 255.0), input)), 1.0 / output) * 255) + .5), + (int) ((pow((gdTrueColorGetRed(c) / 255.0), gamma) * 255) + .5), + (int) ((pow((gdTrueColorGetGreen(c) / 255.0), gamma) * 255) + .5), + (int) ((pow((gdTrueColorGetBlue(c) / 255.0), gamma) * 255) + .5), gdTrueColorGetAlpha(c) ) ); @@ -3060,9 +3062,9 @@ PHP_FUNCTION(imagegammacorrect) } for (i = 0; i < gdImageColorsTotal(im); i++) { - im->red[i] = (int)((pow((pow((im->red[i] / 255.0), input)), 1.0 / output) * 255) + .5); - im->green[i] = (int)((pow((pow((im->green[i] / 255.0), input)), 1.0 / output) * 255) + .5); - im->blue[i] = (int)((pow((pow((im->blue[i] / 255.0), input)), 1.0 / output) * 255) + .5); + im->red[i] = (int)((pow((im->red[i] / 255.0), gamma) * 255) + .5); + im->green[i] = (int)((pow((im->green[i] / 255.0), gamma) * 255) + .5); + im->blue[i] = (int)((pow((im->blue[i] / 255.0), gamma) * 255) + .5); } RETURN_TRUE; diff --git a/ext/gd/tests/imagegammacorrect_variation2.phpt b/ext/gd/tests/imagegammacorrect_variation2.phpt new file mode 100644 index 0000000000..4a317b5d90 --- /dev/null +++ b/ext/gd/tests/imagegammacorrect_variation2.phpt @@ -0,0 +1,72 @@ +--TEST-- +Apply imagegammacorrect() to a step wedge +--SKIPIF-- + +--FILE-- + +===DONE=== +--EXPECT-- +palette gamma (1, 2): The images are equal. +truecolor gamma (1, 2): The images are equal. +palette gamma (1, 1): The images are equal. +truecolor gamma (1, 1): The images are equal. +palette gamma (2, 1): The images are equal. +truecolor gamma (2, 1): The images are equal. +===DONE=== diff --git a/ext/gd/tests/imagegammacorrect_variation2_1_1.png b/ext/gd/tests/imagegammacorrect_variation2_1_1.png new file mode 100644 index 0000000000000000000000000000000000000000..517010b42ffe99a80fc22fb519a34b54f707a268 GIT binary patch literal 2009 zcmeAS@N?(olHy`uVBq!ia0y~yU}|7sV0^&A1Qgk|*?TjP;wb| z85r2RJzX3_D(1YszL58bg8<9H&#wb| z85r1qd%8G=RLpsM-Eq~c01;QmzegmFI#j*1`X9J=;UCKzsc0_Vdp@SIXu+znm-IfBfaUTmRCm zeJ{U!7nl3{{Rbfi3!er?1%(3)0s;vP92^D=EG!ZXOiVlsjErmz4Ghc<4x`dTBv=|> ze*0ctW4np9f%`M#Xv7Vh^xXLSt@Va`?AvZ1D3iTW$F}W#<6G@pdd#<(d!eg42V+YPm@w^I{-@3~QC_wZZc uq21f|-}F=8Q@wUA|7ejte5$HH>_0d07{0Ro{2JIcVDNPHb6Mw<&;$UEEPSs3 literal 0 HcmV?d00001 diff --git a/ext/gd/tests/imagegammacorrect_variation2_2_1.png b/ext/gd/tests/imagegammacorrect_variation2_2_1.png new file mode 100644 index 0000000000000000000000000000000000000000..f7384be913a4581f91d67b275b91a7f528192cc9 GIT binary patch literal 2150 zcmeAS@N?(olHy`uVBq!ia0y~yU}|7sV0^&A1Qgk|*?TjP;wb| z85lSuJY5_^D(1Ys?wKcJD8lMEeOHy-`}$DF3GW@GJOuh~*f;Eca{r&tjcvI*-(J6d z{lm8c3!UwEzt>h&+sIA7J2m?|0q)_U-e}r&n0VDKjL@U|@D|Xkb)OIKUtvkifvfVZgw`BEi7K#KXYI z$kxy>Dm_$!<@c3e-?!g9_v5GJ{kr=9yUQ6q#1}J;M((gl)Q!5);kRe%A5T2^)^4M% z-VR&eob%jy`ogzgH{HH{