Add packing from RGBA to BGRa

This commit is contained in:
Arjen Nienhuis 2016-08-07 15:34:45 +02:00
parent 124e42ade3
commit 916ea94052
2 changed files with 43 additions and 0 deletions

23
Tests/test_mode_bgra.py Normal file
View File

@ -0,0 +1,23 @@
from helper import unittest, PillowTestCase
from PIL import Image
class TestBGRa(PillowTestCase):
def test_bgra(self):
RGBA_RED_50 = b'\xff\x00\x00\x80' # 50% red RGBA
BGRa_RED_50 = b'\x00\x00\x80\x80' # 50% red BGRa
RGBa_RED_50 = b'\x80\x00\x00\x80' # 50% red RGBa
im = Image.frombuffer("RGBA", (1, 1), BGRa_RED_50, "raw", "BGRa", 4, 1)
self.assertEqual(im.tobytes(), RGBA_RED_50)
self.assertEqual(im.tobytes('raw', 'BGRa'), BGRa_RED_50)
im = Image.frombuffer("RGBa", (1, 1), BGRa_RED_50, "raw", "BGRa", 4, 1)
self.assertEqual(im.tobytes(), RGBa_RED_50)
self.assertEqual(im.tobytes('raw', 'BGRa'), BGRa_RED_50)
if __name__ == '__main__':
unittest.main()

View File

@ -72,6 +72,10 @@
#define C64L C64N #define C64L C64N
#endif #endif
/* like (a * b + 127) / 255), but much faster on most platforms */
#define MULDIV255(a, b, tmp)\
(tmp = (a) * (b) + 128, ((((tmp) >> 8) + (tmp)) >> 8))
static void static void
pack1(UINT8* out, const UINT8* in, int pixels) pack1(UINT8* out, const UINT8* in, int pixels)
{ {
@ -315,6 +319,21 @@ ImagingPackABGR(UINT8* out, const UINT8* in, int pixels)
} }
} }
void
ImagingPackBGRa(UINT8* out, const UINT8* in, int pixels)
{
int i;
/* BGRa, reversed bytes with premultiplied alplha */
for (i = 0; i < pixels; i++) {
int alpha = out[3] = in[A];
int tmp;
out[0] = MULDIV255(in[B], alpha, tmp);
out[1] = MULDIV255(in[G], alpha, tmp);
out[2] = MULDIV255(in[R], alpha, tmp);
out += 4; in += 4;
}
}
static void static void
packRGBL(UINT8* out, const UINT8* in, int pixels) packRGBL(UINT8* out, const UINT8* in, int pixels)
{ {
@ -525,6 +544,7 @@ static struct {
{"RGBA", "BGR", 24, ImagingPackBGR}, {"RGBA", "BGR", 24, ImagingPackBGR},
{"RGBA", "BGRA", 32, ImagingPackBGRA}, {"RGBA", "BGRA", 32, ImagingPackBGRA},
{"RGBA", "ABGR", 32, ImagingPackABGR}, {"RGBA", "ABGR", 32, ImagingPackABGR},
{"RGBA", "BGRa", 32, ImagingPackBGRa},
{"RGBA", "R", 8, band0}, {"RGBA", "R", 8, band0},
{"RGBA", "G", 8, band1}, {"RGBA", "G", 8, band1},
{"RGBA", "B", 8, band2}, {"RGBA", "B", 8, band2},