Tk Source Code

View Ticket
Login
Ticket UUID: 454861
Title: incorrect X masks used on Windows
Type: Bug Version: obsolete: 8.3.3
Submitter: lloydlim Created on: 2001-08-24 05:29:29
Subsystem: 68. Win Window Operations Assigned To: chengyemao
Priority: 5 Medium Severity:
Status: Closed Last Modified: 2003-10-29 09:06:35
Resolution: Invalid Closed By: hobbs
    Closed on: 2003-10-29 02:06:35
Description:
This is a little complicated.  Let me know if I need to
clarify anything.

On Windows, Tk draws images onto the screen with
TkPutImage, which is an emulation of XPutImage. 
TkPutImage blits it as a DIB so the XImage data format
on Windows is DIB format.  In 32-bit mode, the first
four bytes (the first pixel) of a DIB looks like this:

BB GG RR 00

Now if you look at this from the point of view of an
XImage, this is an XImage in ZPixmap format in LSBFirst
byte order (as specified by Tk).  With X, you can read
the 32-bit pixel directly as a 32-bit integer.  Since
you're on a little-endian processor, you'd get:

0x00RRGGBB

Therefore, the values of the Z bit masks are supposed
to be:

red_mask = 0xFF0000
green_mask = 0xFF00
blue_mask = 0xFF

Tk 8.3.3 has these values:

red_mask = 0xff
green_mask = 0xff00
blue_mask = 0xff0000

So why does the color work ok?  As far as I can tell,
the only code that accesses the pixels directly is the
image code.  The ImageGetPixel and PutPixel routines
inadvertantly swap things around so it actually matches
the incorrect masks.  The code looks correct, but it's
not.  The Windows GetRValue, GetGValue, and GetBValue
macros are intended for COLORREF format, which is
0x00BBGGRR.

The same problem occurs with 24-bit images too.  The
only difference is there's no zero padding for 24 bit.

We discovered this bug because our software manipulates
XImages directly, in a platform independent manner.

Luckily, this bug seems pretty isolated from the rest
of Tk.  The patches below are fairly simple.  I ran the
fixed code through the Tk test suite and did not see
any new errors.  I tested the fixed code with images
and with colored buttons at 8, 16, 24, and 32 bit
depths.

Note that the new code is slightly faster in 32-bit
mode because it copies pixels as 32-bit ints (like
you're supposed to be able to do) instead of swapping
bytes around.


--- tkWinX.c.old        Mon Aug 20 14:35:22 2001
+++ tkWinX.c    Thu Aug 23 21:38:37 2001
@@ -388,9 +388,9 @@
        } else if (screen->root_depth >= 24) {
            screen->root_visual->class = TrueColor;
            screen->root_visual->map_entries = 256;
-           screen->root_visual->red_mask = 0xff;
+           screen->root_visual->red_mask = 0xff0000;
            screen->root_visual->green_mask = 0xff00;
-           screen->root_visual->blue_mask = 0xff0000;
+           screen->root_visual->blue_mask = 0xff;
        }
     }
     screen->root_visual->bits_per_rgb =
screen->root_depth;


--- tkWinImage.c.old    Mon Jul  9 12:46:10 2001
+++ tkWinImage.c        Thu Aug 23 21:38:37 2001
@@ -75,8 +75,11 @@
 
     switch (image->bits_per_pixel) {
        case 32:
+           pixel = (*(DWORD*)srcPtr);
+           break;
        case 24:
-           pixel = RGB(srcPtr[2], srcPtr[1],
srcPtr[0]);
+           pixel = (srcPtr[0] | ((WORD) srcPtr[1] <<
8)
+                   | ((DWORD) srcPtr[2] << 16));
            break;
        case 16:
            pixel = RGB(((((WORD*)srcPtr)[0]) >> 7) &
0xf8,
@@ -124,18 +127,19 @@
     switch  (image->bits_per_pixel) {
        case 32:
            /*
-            * Pixel is DWORD: 0x00BBGGRR
+            * Pixel is DWORD: 0x00RRGGBB
             */
 
-           destPtr[3] = 0;
+           (*(DWORD*)destPtr) = pixel;
+           break;
        case 24:
            /*
-            * Pixel is triplet: 0xBBGGRR.
+            * Pixel is triplet: 0xRRGGBB.
             */
 
-           destPtr[0] = (unsigned char)
GetBValue(pixel);
-           destPtr[1] = (unsigned char)
GetGValue(pixel);
-           destPtr[2] = (unsigned char)
GetRValue(pixel);
+           destPtr[0] = (unsigned char) pixel;
+           destPtr[1] = (unsigned char) (((WORD)
(pixel)) >> 8);
+           destPtr[2] = (unsigned char) ((pixel) >>
16);
            break;
        case 16:
            /*
@@ -245,9 +249,9 @@
                + (bitmap_pad - 1)) >> 3) &
~((bitmap_pad >> 3) - 1);
     }
 
-    imagePtr->red_mask = 0;
-    imagePtr->green_mask = 0;
-    imagePtr->blue_mask = 0;
+    imagePtr->red_mask = visual->red_mask;
+    imagePtr->green_mask = visual->green_mask;
+    imagePtr->blue_mask = visual->blue_mask;
 
     imagePtr->f.put_pixel = PutPixel;
     imagePtr->f.get_pixel = ImageGetPixel;
User Comments: hobbs added on 2003-10-29 09:06:35:
Logged In: YES 
user_id=72656

never mind ... applying these changes (I did miss the 24/32 
ImageGetPixel changes) swaps R & B now for me ... so I'm 
not sure that it's correct.  Or perhaps something else is 
funny.  I do think correct mask setting should be done, but 
his change clearly crashes because most calls to 
XCreateImage on Windows are with a NULL visual.

chengyemao added on 2003-10-28 13:11:20:
Logged In: YES 
user_id=191079

Go ahead to make the patch. I will do the verification and let 
you know the results.

hobbs added on 2003-10-28 07:14:04:

File Added - 65624: winrgb.454861.patch

Logged In: YES 
user_id=72656

attached is a patch against 8.4.almost5 of Lloyd's changes.  I 
don't know how one would verify that this is all correct 
though.

chengyemao added on 2001-12-13 13:12:53:
Logged In: YES 
user_id=191079

I don't think the current X Masks used in Windows are 
wrong.  These masks are provided as defaults when an XImage 
is created.  It is the user's responsibility to interprete 
the data using whatever color masks in XImage.  These masks 
should be used together with the bit order of the local 
processor to correctly interprete the color bits.  In 
current Windows implementation of Tk, functions such as 
PutPixel, TkPutImage all assumes the default masks, which 
may be wrong if a user choose to apply different color 
masks.

Attachments: