From: Christoph M. Becker Date: Sun, 25 Sep 2016 07:47:23 +0000 (+0200) Subject: Refactor imagegammacorrect() X-Git-Tag: php-7.2.0alpha1~1232^2~30 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=65ee87f20dfebb4cde5f908cd6ba57384810e740;p=php 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. --- 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 0000000000..517010b42f Binary files /dev/null and b/ext/gd/tests/imagegammacorrect_variation2_1_1.png differ diff --git a/ext/gd/tests/imagegammacorrect_variation2_1_2.png b/ext/gd/tests/imagegammacorrect_variation2_1_2.png new file mode 100644 index 0000000000..9b9d7f04a6 Binary files /dev/null and b/ext/gd/tests/imagegammacorrect_variation2_1_2.png differ 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 0000000000..f7384be913 Binary files /dev/null and b/ext/gd/tests/imagegammacorrect_variation2_2_1.png differ