Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/gd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,9 @@ BGD_DECLARE(void) gdImageSetPixel (gdImagePtr im, int x, int y, int color)
case gdEffectMultiply :
im->tpixels[y][x] = gdLayerMultiply(im->tpixels[y][x], color);
break;
case gdEffectDiff :
im->tpixels[y][x] = gdLayerDiff(im->tpixels[y][x], color);
break;
}
} else {
im->pixels[y][x] = color;
Expand Down Expand Up @@ -4070,6 +4073,7 @@ BGD_DECLARE(int) gdImageCompare (gdImagePtr im1, gdImagePtr im2)
* - <gdImageAlphaBlending>
* - <gdLayerOverlay>
* - <gdLayerMultiply>
* - <gdLayerDiff>
*/
BGD_DECLARE(int) gdAlphaBlend (int dst, int src)
{
Expand Down Expand Up @@ -4131,6 +4135,7 @@ static int gdAlphaOverlayColor (int src, int dst, int max );
* - <gdImageAlphaBlending>
* - <gdAlphaBlend>
* - <gdLayerMultiply>
* - <gdLayerDiff>
*/
BGD_DECLARE(int) gdLayerOverlay (int dst, int src)
{
Expand Down Expand Up @@ -4170,6 +4175,7 @@ static int gdAlphaOverlayColor (int src, int dst, int max )
* - <gdImageAlphaBlending>
* - <gdAlphaBlend>
* - <gdLayerOverlay>
* - <gdLayerDiff>
*/
BGD_DECLARE(int) gdLayerMultiply (int dst, int src)
{
Expand All @@ -4193,6 +4199,45 @@ BGD_DECLARE(int) gdLayerMultiply (int dst, int src)
);
}

/**
* Function: gdLayerDiff
*
* Difference of two colors; commutative operation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it note that it's absolute ?

*
* Parameters:
* dst - The first color.
* src - The second color.
*
* See also:
* - <gdImageAlphaBlending>
* - <gdAlphaBlend>
* - <gdLayerMultiply>
* - <gdLayerOverlay>
*/
BGD_DECLARE(int) gdLayerDiff (int dst, int src)
{
int r1,b1,g1,a1,r2,b2,g2,a2;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spaces after commas please

int diff_a,diff_r,diff_g,diff_b;

a1 = gdTrueColorGetAlpha(dst);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm on the fence whether all these variables are needed vs a single statement.

    return gdTrueColorAlpha(
        abs(gdTrueColorGetAlpha(dst) - gdTrueColorGetAlpha(src)),
        abs(gdTrueColorGetRed(dst) - gdTrueColorGetRed(src)),
        abs(gdTrueColorGetGreen(dst) - gdTrueColorGetGreen(src)),
        abs(gdTrueColorGetBlue(dst) - gdTrueColorGetBlue(src)));

a2 = gdTrueColorGetAlpha(src);
diff_a = abs(a1 - a2);

r1 = gdTrueColorGetRed(dst);
r2 = gdTrueColorGetRed(src);
diff_r = abs(r1 - r2);

g1 = gdTrueColorGetGreen(dst);
g2 = gdTrueColorGetGreen(src);
diff_g = abs(g1 - g2);

b1 = gdTrueColorGetBlue(dst);
b2 = gdTrueColorGetBlue(src);
diff_b = abs(b1 - b2);

return gdTrueColorAlpha(diff_r, diff_g, diff_b, diff_a);
}

/**
* Function: gdImageAlphaBlending
*
Expand Down
4 changes: 4 additions & 0 deletions src/gd.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ extern "C" {
* gdEffectOverlay - overlay pixels, see <gdLayerOverlay>
* gdEffectMultiply - overlay pixels with multiply effect, see
* <gdLayerMultiply>
* gdEffectDiff - replace pixels with their difference, see
* <gdLayerDiff>
*
* See also:
* - <gdImageAlphaBlending>
Expand All @@ -229,6 +231,7 @@ extern "C" {
#define gdEffectNormal 2
#define gdEffectOverlay 3
#define gdEffectMultiply 4
#define gdEffectDiff 5

#define GD_TRUE 1
#define GD_FALSE 0
Expand All @@ -246,6 +249,7 @@ extern "C" {
BGD_DECLARE(int) gdAlphaBlend (int dest, int src);
BGD_DECLARE(int) gdLayerOverlay (int dest, int src);
BGD_DECLARE(int) gdLayerMultiply (int dest, int src);
BGD_DECLARE(int) gdLayerDiff (int dest, int src);


/**
Expand Down
1 change: 1 addition & 0 deletions tests/gdimagesetpixel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ LIST(APPEND TESTS_FILES
bug00186
gdeffectoverlay
gdeffectmultiply
gdeffectdiff
)

IF(PNG_FOUND)
Expand Down
3 changes: 2 additions & 1 deletion tests/gdimagesetpixel/Makemodule.am
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
libgd_test_programs += \
gdimagesetpixel/bug00186 \
gdimagesetpixel/gdeffectmultiply \
gdimagesetpixel/gdeffectoverlay
gdimagesetpixel/gdeffectoverlay \
gdimagesetpixel/gdeffectdiff

if HAVE_LIBPNG
libgd_test_programs += \
Expand Down
33 changes: 33 additions & 0 deletions tests/gdimagesetpixel/gdeffectdiff.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "gd.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a proper file header: license, copyright, and text explaining the test goals.

#include "gdtest.h"

int main()
{
gdImagePtr im;
int x, y, c;
int r=0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spaces around binary operators like =



Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only one blank line

im = gdImageCreateTrueColor(256, 256);
gdImageAlphaBlending( im, gdEffectReplace );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no space after ( or before )

for (x=0; x<256; x++) {
for (y=0; y<256; y++) {
c = (y/2 << 24) + (x << 16) + (x << 8) + x;
gdImageSetPixel(im, x, y, c );
}
}
gdImageAlphaBlending( im, gdEffectDiff );
gdImageFilledRectangle(im, 0, 0, 255, 255, 0xff7f00);

if (gdTrueColorGetGreen(gdImageGetPixel(im, 0, 128)) != 0x7f) {
r = 1;
}
if (gdTrueColorGetGreen(gdImageGetPixel(im, 128, 128)) != 0x01) {
r = 1;
}
if (gdTrueColorGetGreen(gdImageGetPixel(im, 255, 128)) != 0x80) {
r = 1;
}
gdImageDestroy(im);
return r;
}
Loading