[Image-SIG] Inconsistency with BGR packing

Alexey Borzenkov snaury at gmail.com
Sat Feb 3 15:20:20 CET 2007


Reposting in plain text:

---------- Forwarded message ----------
From: Alexey Borzenkov <snaury at gmail.com>
Date: Feb 3, 2007 2:08 PM
Subject: Inconsistency with BGR packing
To: image-sig at python.org


Hi everyone,

Yesterday I've tried using PIL to load and save .bmp files that
contain alpha channel. Well, loading was easy to do without even
modifying the library:

from PIL import BmpImagePlugin
BmpImagePlugin.BIT2MODE [32] = ("RGBA", "BGRA")
BmpImagePlugin.SAVE["RGBA"] = ("BGRA", 32, 0)

However when I tried to save bitmap after loading it I suddenly found
that packing into BGRA is not supported. I wonder if this was decided
some time ago as "the right thing", or if this was just accidentally
left out? (as it seems inconsistent to me that it can unpack
BGRA/ABGR, but can't pack it back) Fix for this is pretty trivial, but
as it contains tabulation characters in addition to seeing it below
you can download it here:
http://snaury.googlepages.com/Imaging-1.1.6-df-bgr.patch

I hope here is the right place to submit a patch, as PIL site told to
do so. And if what's done below is in any way wrong, I'd like to hear
your reasons.

diff -druN Imaging-1.1.6-orig/libImaging/Pack.c Imaging-1.1.6/libImaging/Pack.c
--- Imaging-1.1.6-orig/libImaging/Pack.c    Sun Dec  3 14:37:25 2006
+++ Imaging-1.1.6/libImaging/Pack.c    Sat Feb  3 09:56:20 2007
@@ -287,6 +287,34 @@
     }
 }

+void
+ImagingPackBGRA(UINT8* out, const UINT8* in, int pixels)
+{
+    int i;
+    /* BGRX, reversed bytes with right padding */
+    for (i = 0; i < pixels; i++) {
+    out[0] = in[B];
+    out[1] = in[G];
+    out[2] = in[R];
+    out[3] = in[A];
+    out += 4; in += 4;
+    }
+}
+
+void
+ImagingPackABGR(UINT8* out, const UINT8* in, int pixels)
+{
 +    int i;
+    /* XBGR, reversed bytes with left padding */
+    for (i = 0; i < pixels; i++) {
+    out[0] = in[A];
+    out[1] = in[B];
+    out[2] = in[G];
+    out[3] = in[R];
+    out += 4; in += 4;
+    }
+}
+
 static void
 packRGBL(UINT8* out, const UINT8* in, int pixels)
 {
@@ -460,6 +488,9 @@
     {"RGBA",    "RGBA",        32,    copy4},
     {"RGBA",    "RGBA;L",    32,    packRGBXL},
     {"RGBA",    "RGB",        24,    ImagingPackRGB},
+    {"RGBA",    "BGR",        24,    ImagingPackBGR},
+    {"RGBA",    "BGRA",        32,    ImagingPackBGRA},
+    {"RGBA",    "ABGR",        32,    ImagingPackABGR},
     {"RGBA",       "R",            8,      band0},
     {"RGBA",       "G",            8,      band1},
     {"RGBA",       "B",            8,      band2},
@@ -469,6 +500,9 @@
     {"RGBX",    "RGBX",        32,    copy4},
     {"RGBX",    "RGBX;L",    32,    packRGBXL},
     {"RGBX",    "RGB",        32,    ImagingPackRGB},
+    {"RGBX",    "BGR",        32,    ImagingPackBGR},
+    {"RGBX",    "BGRX",        32,    ImagingPackBGRX},
+    {"RGBX",    "XBGR",        32,    ImagingPackXBGR},
     {"RGBX",       "R",            8,      band0},
     {"RGBX",       "G",            8,      band1},
     {"RGBX",       "B",            8,      band2},


More information about the Image-SIG mailing list