From 77b020b374631d66fd73a1f94f26b7d87b776d68 Mon Sep 17 00:00:00 2001 From: Benjamin Gilbert Date: Sat, 24 Jan 2015 01:13:58 -0500 Subject: [PATCH] Special-case opaque pixels in RGBa unpacker Avoid the expensive multiply and divide when the pixel is opaque. On my system, this change gives a 5.76x speedup loading an opaque image with this call: PIL.Image.frombuffer('RGBA', (1000, 1000), buf, 'raw', 'RGBa', 0, 1) --- libImaging/Unpack.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/libImaging/Unpack.c b/libImaging/Unpack.c index 7c453dbfd..bc8ed3213 100644 --- a/libImaging/Unpack.c +++ b/libImaging/Unpack.c @@ -638,7 +638,12 @@ unpackRGBa(UINT8* out, const UINT8* in, int pixels) int a = in[3]; if (!a) out[R] = out[G] = out[B] = out[A] = 0; - else { + else if (a == 255) { + out[R] = in[0]; + out[G] = in[1]; + out[B] = in[2]; + out[A] = a; + } else { out[R] = CLIP(in[0] * 255 / a); out[G] = CLIP(in[1] * 255 / a); out[B] = CLIP(in[2] * 255 / a);