From 2ecbcce4156c21b8a40a0c85c89c1b9f7cd33610 Mon Sep 17 00:00:00 2001 From: Arjen Nienhuis Date: Sat, 6 Aug 2016 10:54:58 +0200 Subject: [PATCH] add unpacking from BRGa --- Tests/test_lib_pack.py | 2 ++ libImaging/Unpack.c | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/Tests/test_lib_pack.py b/Tests/test_lib_pack.py index 461d0bf8e..5c2af8d04 100644 --- a/Tests/test_lib_pack.py +++ b/Tests/test_lib_pack.py @@ -122,7 +122,9 @@ class TestLibPack(PillowTestCase): self.assertEqual(unpack("RGB", "XBGR", 4), (4, 3, 2)) self.assertEqual(unpack("RGBA", "RGBA", 4), (1, 2, 3, 4)) + self.assertEqual(unpack("RGBA", "RGBa", 4), (63, 127, 191, 4)) self.assertEqual(unpack("RGBA", "BGRA", 4), (3, 2, 1, 4)) + self.assertEqual(unpack("RGBA", "BGRa", 4), (191, 127, 63, 4)) self.assertEqual(unpack("RGBA", "ARGB", 4), (2, 3, 4, 1)) self.assertEqual(unpack("RGBA", "ABGR", 4), (4, 3, 2, 1)) self.assertEqual(unpack("RGBA", "RGBA;15", 2), (8, 131, 0, 0)) diff --git a/libImaging/Unpack.c b/libImaging/Unpack.c index 702bc9f1f..8112afd8d 100644 --- a/libImaging/Unpack.c +++ b/libImaging/Unpack.c @@ -747,6 +747,30 @@ unpackRGBa(UINT8* out, const UINT8* in, int pixels) } } +static void +unpackBGRa(UINT8* out, const UINT8* in, int pixels) +{ + int i; + /* premultiplied BGRA */ + for (i = 0; i < pixels; i++) { + int a = in[3]; + if (!a) + out[R] = out[G] = out[B] = out[A] = 0; + else if (a == 255) { + out[R] = in[2]; + out[G] = in[1]; + out[B] = in[0]; + out[A] = a; + } else { + out[R] = CLIP(in[2] * 255 / a); + out[G] = CLIP(in[1] * 255 / a); + out[B] = CLIP(in[0] * 255 / a); + out[A] = a; + } + out += 4; in += 4; + } +} + static void unpackRGBAI(UINT8* out, const UINT8* in, int pixels) { @@ -1206,6 +1230,7 @@ static struct { {"RGBA", "LA;16B", 32, unpackRGBALA16B}, {"RGBA", "RGBA", 32, copy4}, {"RGBA", "RGBa", 32, unpackRGBa}, + {"RGBA", "BGRa", 32, unpackBGRa}, {"RGBA", "RGBA;I", 32, unpackRGBAI}, {"RGBA", "RGBA;L", 32, unpackRGBAL}, {"RGBA", "RGBA;15", 16, ImagingUnpackRGBA15},