From 6adf699f287514c469d4827316e856204f4f84c6 Mon Sep 17 00:00:00 2001 From: culler Date: Mon, 3 Aug 2020 01:28:50 +0000 Subject: Fix [fca13549b6]: TkMacOSXRGBPixel() used incorrectly in ImageGetPixel() --- macosx/tkMacOSXImage.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/macosx/tkMacOSXImage.c b/macosx/tkMacOSXImage.c index 4691e41..8a7aac4 100644 --- a/macosx/tkMacOSXImage.c +++ b/macosx/tkMacOSXImage.c @@ -314,7 +314,12 @@ ImageGetPixel( int x, int y) { - unsigned char r = 0, g = 0, b = 0; + unsigned short r = 0, g = 0, b = 0; + + /* + * Compute 8 bit red green and blue values, which are multiplied by 256 and + * ed as inputs to TkMacOSXRGBPixel. + */ if (image && image->data) { unsigned char *srcPtr = ((unsigned char*) image->data) @@ -322,7 +327,7 @@ ImageGetPixel( + (((image->xoffset + x) * image->bits_per_pixel) / NBBY); switch (image->bits_per_pixel) { - case 32: + case 32: /* 8 bits per channel */ r = (*((unsigned int*) srcPtr) >> 16) & 0xff; g = (*((unsigned int*) srcPtr) >> 8) & 0xff; b = (*((unsigned int*) srcPtr) ) & 0xff; @@ -332,12 +337,12 @@ ImageGetPixel( r = srcPtr[1]; g = srcPtr[2]; b = srcPtr[3]; }*/ break; - case 16: + case 16: /* 5 bits per channel */ r = (*((unsigned short*) srcPtr) >> 7) & 0xf8; g = (*((unsigned short*) srcPtr) >> 2) & 0xf8; b = (*((unsigned short*) srcPtr) << 3) & 0xf8; break; - case 8: + case 8: /* 2 bits per channel */ r = (*srcPtr << 2) & 0xc0; g = (*srcPtr << 4) & 0xc0; b = (*srcPtr << 6) & 0xc0; @@ -345,7 +350,7 @@ ImageGetPixel( g |= g >> 2 | g >> 4 | g >> 6; b |= b >> 2 | b >> 4 | b >> 6; break; - case 4: { + case 4: { /* 1 bit per channel */ unsigned char c = (x % 2) ? *srcPtr : (*srcPtr >> 4); r = (c & 0x04) ? 0xff : 0; @@ -353,12 +358,13 @@ ImageGetPixel( b = (c & 0x01) ? 0xff : 0; break; } - case 1: + case 1: /* Black-white bitmap. */ r = g = b = ((*srcPtr) & (0x80 >> (x % 8))) ? 0xff : 0; break; } } - return TkMacOSXRGBPixel(r, g, b); + + return TkMacOSXRGBPixel(r<<8, g<<8, b<<8); } /* -- cgit v0.12 From d67e38a86d79f6df28051611978dcaac8b1be9cc Mon Sep 17 00:00:00 2001 From: culler Date: Mon, 3 Aug 2020 02:16:07 +0000 Subject: Be a little more efficient. --- macosx/tkMacOSXColor.c | 13 ++++++------- macosx/tkMacOSXImage.c | 4 ++-- macosx/tkMacOSXPort.h | 2 +- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/macosx/tkMacOSXColor.c b/macosx/tkMacOSXColor.c index b4347b8..ce91520 100644 --- a/macosx/tkMacOSXColor.c +++ b/macosx/tkMacOSXColor.c @@ -112,11 +112,10 @@ void initColorTable() * Return an unsigned long value suitable for use in the pixel * field of an XColor with the specified red, green and blue * intensities. The inputs are cast as unsigned longs but are - * expected to have values representable by an unsigned short - * as used in the XColor struct. These values are divided by - * 256 to generate a 24-bit RGB pixel value. + * expected to have values representable by an unsigned char. * - * This is called by the TkpGetPixel macro, used in xcolor.c. + * This is called in the TkpGetPixel macro, used in xcolor.c, + * and in ImageGetPixel. * * Results: * An unsigned long that can be used as the pixel field of an XColor. @@ -134,9 +133,9 @@ TkMacOSXRGBPixel( { MacPixel p; p.pixel.colortype = rgbColor; - p.pixel.value = (((red >> 8) & 0xff) << 16) | - (((green >> 8) & 0xff) << 8) | - ((blue >> 8) & 0xff); + p.pixel.value = ((red & 0xff) << 16) | + ((green & 0xff) << 8) | + (blue & 0xff); return p.ulong; } diff --git a/macosx/tkMacOSXImage.c b/macosx/tkMacOSXImage.c index 8a7aac4..2967bb0 100644 --- a/macosx/tkMacOSXImage.c +++ b/macosx/tkMacOSXImage.c @@ -314,7 +314,7 @@ ImageGetPixel( int x, int y) { - unsigned short r = 0, g = 0, b = 0; + unsigned char r = 0, g = 0, b = 0; /* * Compute 8 bit red green and blue values, which are multiplied by 256 and @@ -364,7 +364,7 @@ ImageGetPixel( } } - return TkMacOSXRGBPixel(r<<8, g<<8, b<<8); + return TkMacOSXRGBPixel(r, g, b); } /* diff --git a/macosx/tkMacOSXPort.h b/macosx/tkMacOSXPort.h index f5c3689..4347766 100644 --- a/macosx/tkMacOSXPort.h +++ b/macosx/tkMacOSXPort.h @@ -168,6 +168,6 @@ MODULE_SCOPE unsigned long TkMacOSXRGBPixel(unsigned long red, unsigned long green, unsigned long blue); -#define TkpGetPixel(p) (TkMacOSXRGBPixel(p->red, p->green, p->blue)) +#define TkpGetPixel(p) (TkMacOSXRGBPixel(p->red >> 8, p->green >> 8, p->blue >> 8)) #endif /* _TKMACPORT */ -- cgit v0.12 From 2c6e31f7fd99017ab613b39e77357ad91b0be8c7 Mon Sep 17 00:00:00 2001 From: culler Date: Mon, 3 Aug 2020 12:41:10 +0000 Subject: Edit comments. --- macosx/tkMacOSXImage.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/macosx/tkMacOSXImage.c b/macosx/tkMacOSXImage.c index 2967bb0..0e33b23 100644 --- a/macosx/tkMacOSXImage.c +++ b/macosx/tkMacOSXImage.c @@ -300,7 +300,11 @@ DestroyImage( * Get a single pixel from an image. * * Results: - * Returns the 32 bit pixel value. + * The XColor structure contains an unsigned long field named pixel which + * identifies the color. This function returns the unsigned long that + * would be used as the pixel value of an XColor that has the same red + * green and blue components as the XImage pixel at the specified + * location. * * Side effects: * None. @@ -317,8 +321,8 @@ ImageGetPixel( unsigned char r = 0, g = 0, b = 0; /* - * Compute 8 bit red green and blue values, which are multiplied by 256 and - * ed as inputs to TkMacOSXRGBPixel. + * Compute 8 bit red green and blue values, which are passed as inputs to + * TkMacOSXRGBPixel to produce the pixel value. */ if (image && image->data) { -- cgit v0.12