diff --git a/Tests/test_lib_pack.py b/Tests/test_lib_pack.py index b3355b65f..7675348b3 100644 --- a/Tests/test_lib_pack.py +++ b/Tests/test_lib_pack.py @@ -101,8 +101,11 @@ def test_unpack(): assert_equal(unpack("RGB", "RGB;R", 3), (128, 64, 192)) assert_equal(unpack("RGB", "RGB;16B", 6), (1, 3, 5)) # ? assert_equal(unpack("RGB", "BGR", 3), (3, 2, 1)) + assert_equal(unpack("RGB", "RGB;15", 2), (8, 131, 0)) assert_equal(unpack("RGB", "BGR;15", 2), (0, 131, 8)) + assert_equal(unpack("RGB", "RGB;16", 2), (8, 64, 0)) assert_equal(unpack("RGB", "BGR;16", 2), (0, 64, 8)) + assert_equal(unpack("RGB", "RGB;4B", 2), (17, 0, 34)) assert_equal(unpack("RGB", "RGBX", 4), (1, 2, 3)) assert_equal(unpack("RGB", "BGRX", 4), (3, 2, 1)) @@ -113,11 +116,17 @@ def test_unpack(): assert_equal(unpack("RGBA", "BGRA", 4), (3, 2, 1, 4)) assert_equal(unpack("RGBA", "ARGB", 4), (2, 3, 4, 1)) assert_equal(unpack("RGBA", "ABGR", 4), (4, 3, 2, 1)) + assert_equal(unpack("RGBA", "RGBA;15", 2), (8, 131, 0, 0)) + assert_equal(unpack("RGBA", "BGRA;15", 2), (0, 131, 8, 0)) + assert_equal(unpack("RGBA", "RGBA;4B", 2), (17, 0, 34, 0)) assert_equal(unpack("RGBX", "RGBX", 4), (1, 2, 3, 4)) # 4->255? assert_equal(unpack("RGBX", "BGRX", 4), (3, 2, 1, 255)) assert_equal(unpack("RGBX", "XRGB", 4), (2, 3, 4, 255)) assert_equal(unpack("RGBX", "XBGR", 4), (4, 3, 2, 255)) + assert_equal(unpack("RGBX", "RGB;15", 2), (8, 131, 0, 255)) + assert_equal(unpack("RGBX", "BGR;15", 2), (0, 131, 8, 255)) + assert_equal(unpack("RGBX", "RGB;4B", 2), (17, 0, 34, 255)) assert_equal(unpack("CMYK", "CMYK", 4), (1, 2, 3, 4)) assert_equal(unpack("CMYK", "CMYK;I", 4), (254, 253, 252, 251)) diff --git a/libImaging/Unpack.c b/libImaging/Unpack.c index d014fb815..5be744ac5 100644 --- a/libImaging/Unpack.c +++ b/libImaging/Unpack.c @@ -441,6 +441,36 @@ ImagingUnpackBGR(UINT8* out, const UINT8* in, int pixels) } } +void +ImagingUnpackRGB15(UINT8* out, const UINT8* in, int pixels) +{ + int i, pixel; + /* RGB, 5 bits per pixel */ + for (i = 0; i < pixels; i++) { + pixel = in[0] + (in[1] << 8); + out[R] = (pixel & 31) * 255 / 31; + out[G] = ((pixel>>5) & 31) * 255 / 31; + out[B] = ((pixel>>10) & 31) * 255 / 31; + out[A] = 255; + out += 4; in += 2; + } +} + +void +ImagingUnpackRGBA15(UINT8* out, const UINT8* in, int pixels) +{ + int i, pixel; + /* RGB, 5/5/5/1 bits per pixel */ + for (i = 0; i < pixels; i++) { + pixel = in[0] + (in[1] << 8); + out[R] = (pixel & 31) * 255 / 31; + out[G] = ((pixel>>5) & 31) * 255 / 31; + out[B] = ((pixel>>10) & 31) * 255 / 31; + out[A] = (pixel>>15) * 255; + out += 4; in += 2; + } +} + void ImagingUnpackBGR15(UINT8* out, const UINT8* in, int pixels) { @@ -456,6 +486,36 @@ ImagingUnpackBGR15(UINT8* out, const UINT8* in, int pixels) } } +void +ImagingUnpackBGRA15(UINT8* out, const UINT8* in, int pixels) +{ + int i, pixel; + /* RGB, reversed bytes, 5/5/5/1 bits per pixel */ + for (i = 0; i < pixels; i++) { + pixel = in[0] + (in[1] << 8); + out[B] = (pixel & 31) * 255 / 31; + out[G] = ((pixel>>5) & 31) * 255 / 31; + out[R] = ((pixel>>10) & 31) * 255 / 31; + out[A] = (pixel>>15) * 255; + out += 4; in += 2; + } +} + +void +ImagingUnpackRGB16(UINT8* out, const UINT8* in, int pixels) +{ + int i, pixel; + /* RGB, 5/6/5 bits per pixel */ + for (i = 0; i < pixels; i++) { + pixel = in[0] + (in[1] << 8); + out[R] = (pixel & 31) * 255 / 31; + out[G] = ((pixel>>5) & 63) * 255 / 63; + out[B] = ((pixel>>11) & 31) * 255 / 31; + out[A] = 255; + out += 4; in += 2; + } +} + void ImagingUnpackBGR16(UINT8* out, const UINT8* in, int pixels) { @@ -471,6 +531,36 @@ ImagingUnpackBGR16(UINT8* out, const UINT8* in, int pixels) } } +void +ImagingUnpackRGB4B(UINT8* out, const UINT8* in, int pixels) +{ + int i, pixel; + /* RGB, 4 bits per pixel */ + for (i = 0; i < pixels; i++) { + pixel = in[0] + (in[1] << 8); + out[R] = (pixel & 15) * 17; + out[G] = ((pixel>>4) & 15) * 17; + out[B] = ((pixel>>8) & 15) * 17; + out[A] = 255; + out += 4; in += 2; + } +} + +void +ImagingUnpackRGBA4B(UINT8* out, const UINT8* in, int pixels) +{ + int i, pixel; + /* RGBA, 4 bits per pixel */ + for (i = 0; i < pixels; i++) { + pixel = in[0] + (in[1] << 8); + out[R] = (pixel & 15) * 17; + out[G] = ((pixel>>4) & 15) * 17; + out[B] = ((pixel>>8) & 15) * 17; + out[A] = ((pixel>>12) & 15) * 17; + out += 4; in += 2; + } +} + static void ImagingUnpackBGRX(UINT8* out, const UINT8* in, int pixels) { @@ -889,8 +979,11 @@ static struct { {"RGB", "RGB;R", 24, unpackRGBR}, {"RGB", "RGB;16B", 48, unpackRGB16B}, {"RGB", "BGR", 24, ImagingUnpackBGR}, + {"RGB", "RGB;15", 16, ImagingUnpackRGB15}, {"RGB", "BGR;15", 16, ImagingUnpackBGR15}, + {"RGB", "RGB;16", 16, ImagingUnpackRGB16}, {"RGB", "BGR;16", 16, ImagingUnpackBGR16}, + {"RGB", "RGB;4B", 16, ImagingUnpackRGB4B}, {"RGB", "BGR;5", 16, ImagingUnpackBGR15}, /* compat */ {"RGB", "RGBX", 32, copy4}, {"RGB", "RGBX;L", 32, unpackRGBAL}, @@ -909,6 +1002,9 @@ static struct { {"RGBA", "RGBa", 32, unpackRGBa}, {"RGBA", "RGBA;I", 32, unpackRGBAI}, {"RGBA", "RGBA;L", 32, unpackRGBAL}, + {"RGBA", "RGBA;15", 16, ImagingUnpackRGBA15}, + {"RGBA", "BGRA;15", 16, ImagingUnpackBGRA15}, + {"RGBA", "RGBA;4B", 16, ImagingUnpackRGBA4B}, {"RGBA", "RGBA;16B", 64, unpackRGBA16B}, {"RGBA", "BGRA", 32, unpackBGRA}, {"RGBA", "ARGB", 32, unpackARGB}, @@ -924,8 +1020,9 @@ static struct { {"RGBX", "RGB;L", 24, unpackRGBL}, {"RGBX", "RGB;16B", 48, unpackRGB16B}, {"RGBX", "BGR", 24, ImagingUnpackBGR}, + {"RGBX", "RGB;15", 16, ImagingUnpackRGB15}, {"RGBX", "BGR;15", 16, ImagingUnpackBGR15}, - {"RGB", "BGR;16", 16, ImagingUnpackBGR16}, + {"RGBX", "RGB;4B", 16, ImagingUnpackRGB4B}, {"RGBX", "BGR;5", 16, ImagingUnpackBGR15}, /* compat */ {"RGBX", "RGBX", 32, copy4}, {"RGBX", "RGBX;L", 32, unpackRGBAL},