Improved I mode conversion

This commit is contained in:
Andrew Murray 2019-05-08 21:58:33 +10:00
parent 0e36b288dd
commit 8a035b8c5d
9 changed files with 32 additions and 47 deletions

View File

@ -295,7 +295,7 @@ class TestFilePng(PillowTestCase):
for mode, num_transparent in { for mode, num_transparent in {
"1": 1994, "1": 1994,
"L": 559, "L": 559,
"I": 559, "I": 4096,
}.items(): }.items():
in_file = "Tests/images/"+mode.lower()+"_trns.png" in_file = "Tests/images/"+mode.lower()+"_trns.png"
im = Image.open(in_file) im = Image.open(in_file)

View File

@ -38,6 +38,8 @@ class TestImageConvert(PillowTestCase):
def _test_float_conversion(self, im): def _test_float_conversion(self, im):
orig = im.getpixel((5, 5)) orig = im.getpixel((5, 5))
if im.mode[0] == 'I':
orig //= 256
converted = im.convert('F').getpixel((5, 5)) converted = im.convert('F').getpixel((5, 5))
self.assertEqual(orig, converted) self.assertEqual(orig, converted)

View File

@ -17,13 +17,13 @@ class TestImageGetColors(PillowTestCase):
self.assertEqual(getcolors("1"), 2) self.assertEqual(getcolors("1"), 2)
self.assertEqual(getcolors("L"), 255) self.assertEqual(getcolors("L"), 255)
self.assertEqual(getcolors("I"), 255)
self.assertEqual(getcolors("F"), 255) self.assertEqual(getcolors("F"), 255)
self.assertEqual(getcolors("P"), 90) # fixed palette self.assertEqual(getcolors("P"), 90) # fixed palette
self.assertIsNone(getcolors("RGB")) self.assertIsNone(getcolors("RGB"))
self.assertIsNone(getcolors("RGBA")) self.assertIsNone(getcolors("RGBA"))
self.assertIsNone(getcolors("CMYK")) self.assertIsNone(getcolors("CMYK"))
self.assertIsNone(getcolors("YCbCr")) self.assertIsNone(getcolors("YCbCr"))
self.assertIsNone(getcolors("I"))
self.assertIsNone(getcolors("L", 128)) self.assertIsNone(getcolors("L", 128))
self.assertEqual(getcolors("L", 1024), 255) self.assertEqual(getcolors("L", 1024), 255)

View File

@ -21,7 +21,7 @@ class TestImageGetData(PillowTestCase):
self.assertEqual(getdata("1"), (0, 960, 960)) self.assertEqual(getdata("1"), (0, 960, 960))
self.assertEqual(getdata("L"), (16, 960, 960)) self.assertEqual(getdata("L"), (16, 960, 960))
self.assertEqual(getdata("I"), (16, 960, 960)) self.assertEqual(getdata("I"), (4313, 960, 960))
self.assertEqual(getdata("F"), (16.0, 960, 960)) self.assertEqual(getdata("F"), (16.0, 960, 960))
self.assertEqual(getdata("RGB"), ((11, 13, 52), 960, 960)) self.assertEqual(getdata("RGB"), ((11, 13, 52), 960, 960))
self.assertEqual(getdata("RGBA"), ((11, 13, 52, 255), 960, 960)) self.assertEqual(getdata("RGBA"), ((11, 13, 52, 255), 960, 960))

View File

@ -11,7 +11,7 @@ class TestImageGetExtrema(PillowTestCase):
self.assertEqual(extrema("1"), (0, 255)) self.assertEqual(extrema("1"), (0, 255))
self.assertEqual(extrema("L"), (0, 255)) self.assertEqual(extrema("L"), (0, 255))
self.assertEqual(extrema("I"), (0, 255)) self.assertEqual(extrema("I"), (150, 65280))
self.assertEqual(extrema("F"), (0, 255)) self.assertEqual(extrema("F"), (0, 255))
self.assertEqual(extrema("P"), (0, 225)) # fixed palette self.assertEqual(extrema("P"), (0, 225)) # fixed palette
self.assertEqual( self.assertEqual(
@ -20,7 +20,7 @@ class TestImageGetExtrema(PillowTestCase):
extrema("RGBA"), ((0, 255), (0, 255), (0, 255), (255, 255))) extrema("RGBA"), ((0, 255), (0, 255), (0, 255), (255, 255)))
self.assertEqual( self.assertEqual(
extrema("CMYK"), ((0, 255), (0, 255), (0, 255), (0, 0))) extrema("CMYK"), ((0, 255), (0, 255), (0, 255), (0, 0)))
self.assertEqual(extrema("I;16"), (0, 255)) self.assertEqual(extrema("I;16"), (150, 65280))
def test_true_16(self): def test_true_16(self):
im = Image.open("Tests/images/16_bit_noise.tif") im = Image.open("Tests/images/16_bit_noise.tif")

View File

@ -11,7 +11,7 @@ class TestImageHistogram(PillowTestCase):
self.assertEqual(histogram("1"), (256, 0, 10994)) self.assertEqual(histogram("1"), (256, 0, 10994))
self.assertEqual(histogram("L"), (256, 0, 638)) self.assertEqual(histogram("L"), (256, 0, 638))
self.assertEqual(histogram("I"), (256, 0, 638)) self.assertEqual(histogram("I"), (256, 1, 662))
self.assertEqual(histogram("F"), (256, 0, 638)) self.assertEqual(histogram("F"), (256, 0, 638))
self.assertEqual(histogram("P"), (256, 0, 1871)) self.assertEqual(histogram("P"), (256, 0, 1871))
self.assertEqual(histogram("RGB"), (768, 4, 675)) self.assertEqual(histogram("RGB"), (768, 4, 675))

View File

@ -17,6 +17,8 @@ class TestModeI16(PillowTestCase):
xy = x, y xy = x, y
p1 = pix1[xy] p1 = pix1[xy]
p2 = pix2[xy] p2 = pix2[xy]
if im1.mode[0] != "I" and im2.mode[0] == "I":
p2 //= 256
self.assertEqual( self.assertEqual(
p1, p2, p1, p2,
("got %r from mode %s at %s, expected %r" % ("got %r from mode %s at %s, expected %r" %

View File

@ -231,7 +231,7 @@ rgb2i(UINT8* out_, const UINT8* in, int xsize)
int x; int x;
INT32* out = (INT32*) out_; INT32* out = (INT32*) out_;
for (x = 0; x < xsize; x++, in += 4) for (x = 0; x < xsize; x++, in += 4)
*out++ = L24(in) >> 16; *out++ = L24(in) >> 8;
} }
static void static void
@ -575,8 +575,8 @@ l2i(UINT8* out_, const UINT8* in, int xsize)
{ {
int x; int x;
INT32* out = (INT32*) out_; INT32* out = (INT32*) out_;
for (x = 0; x < xsize; x++) for (x = 0; x < xsize; x++, in++)
*out++ = (INT32) *in++; *out++ = (INT32) (*in << 8);
} }
static void static void
@ -585,12 +585,7 @@ i2l(UINT8* out, const UINT8* in_, int xsize)
int x; int x;
INT32* in = (INT32*) in_; INT32* in = (INT32*) in_;
for (x = 0; x < xsize; x++, in++, out++) { for (x = 0; x < xsize; x++, in++, out++) {
if (*in <= 0) *out = (UINT8) (*in >> 8);
*out = 0;
else if (*in >= 255)
*out = 255;
else
*out = (UINT8) *in;
} }
} }
@ -601,7 +596,7 @@ i2f(UINT8* out_, const UINT8* in_, int xsize)
INT32* in = (INT32*) in_; INT32* in = (INT32*) in_;
FLOAT32* out = (FLOAT32*) out_; FLOAT32* out = (FLOAT32*) out_;
for (x = 0; x < xsize; x++) for (x = 0; x < xsize; x++)
*out++ = (FLOAT32) *in++; *out++ = (FLOAT32) (*in++ >> 8);
} }
static void static void
@ -610,12 +605,7 @@ i2rgb(UINT8* out, const UINT8* in_, int xsize)
int x; int x;
INT32* in = (INT32*) in_; INT32* in = (INT32*) in_;
for (x = 0; x < xsize; x++, in++, out+=4) { for (x = 0; x < xsize; x++, in++, out+=4) {
if (*in <= 0) out[0] = out[1] = out[2] = (UINT8) (*in >> 8);
out[0] = out[1] = out[2] = 0;
else if (*in >= 255)
out[0] = out[1] = out[2] = 255;
else
out[0] = out[1] = out[2] = (UINT8) *in;
out[3] = 255; out[3] = 255;
} }
} }
@ -664,7 +654,7 @@ f2i(UINT8* out_, const UINT8* in_, int xsize)
FLOAT32* in = (FLOAT32*) in_; FLOAT32* in = (FLOAT32*) in_;
INT32* out = (INT32*) out_; INT32* out = (INT32*) out_;
for (x = 0; x < xsize; x++) for (x = 0; x < xsize; x++)
*out++ = (INT32) *in++; *out++ = (INT32) *in++ << 8;
} }
/* ----------------- */ /* ----------------- */
@ -722,24 +712,22 @@ ycbcr2la(UINT8* out, const UINT8* in, int xsize)
static void static void
I_I16L(UINT8* out, const UINT8* in_, int xsize) I_I16L(UINT8* out, const UINT8* in_, int xsize)
{ {
int x, v; int x;
INT32* in = (INT32*) in_; INT32* in = (INT32*) in_;
for (x = 0; x < xsize; x++, in++) { for (x = 0; x < xsize; x++, in++) {
v = CLIP16(*in); *out++ = (UINT8) *in;
*out++ = (UINT8) v; *out++ = (UINT8) (*in >> 8);
*out++ = (UINT8) (v >> 8);
} }
} }
static void static void
I_I16B(UINT8* out, const UINT8* in_, int xsize) I_I16B(UINT8* out, const UINT8* in_, int xsize)
{ {
int x, v; int x;
INT32* in = (INT32*) in_; INT32* in = (INT32*) in_;
for (x = 0; x < xsize; x++, in++) { for (x = 0; x < xsize; x++, in++) {
v = CLIP16(*in); *out++ = (UINT8) (*in >> 8);
*out++ = (UINT8) (v >> 8); *out++ = (UINT8) *in;
*out++ = (UINT8) v;
} }
} }
@ -769,7 +757,7 @@ I16L_F(UINT8* out_, const UINT8* in, int xsize)
int x; int x;
FLOAT32* out = (FLOAT32*) out_; FLOAT32* out = (FLOAT32*) out_;
for (x = 0; x < xsize; x++, in += 2) for (x = 0; x < xsize; x++, in += 2)
*out++ = (FLOAT32) (in[0] + ((int) in[1] << 8)); *out++ = (FLOAT32) in[1];
} }
@ -779,7 +767,7 @@ I16B_F(UINT8* out_, const UINT8* in, int xsize)
int x; int x;
FLOAT32* out = (FLOAT32*) out_; FLOAT32* out = (FLOAT32*) out_;
for (x = 0; x < xsize; x++, in += 2) for (x = 0; x < xsize; x++, in += 2)
*out++ = (FLOAT32) (in[1] + ((int) in[0] << 8)); *out++ = (FLOAT32) in[0];
} }
static void static void
@ -787,8 +775,8 @@ L_I16L(UINT8* out, const UINT8* in, int xsize)
{ {
int x; int x;
for (x = 0; x < xsize; x++, in++) { for (x = 0; x < xsize; x++, in++) {
*out++ = *in << 8;
*out++ = *in; *out++ = *in;
*out++ = 0;
} }
} }
@ -797,8 +785,8 @@ L_I16B(UINT8* out, const UINT8* in, int xsize)
{ {
int x; int x;
for (x = 0; x < xsize; x++, in++) { for (x = 0; x < xsize; x++, in++) {
*out++ = 0;
*out++ = *in; *out++ = *in;
*out++ = *in << 8;
} }
} }
@ -807,10 +795,7 @@ I16L_L(UINT8* out, const UINT8* in, int xsize)
{ {
int x; int x;
for (x = 0; x < xsize; x++, in += 2) for (x = 0; x < xsize; x++, in += 2)
if (in[1] != 0) *out++ = in[1] + (in[0] << 8);
*out++ = 255;
else
*out++ = in[0];
} }
static void static void
@ -818,10 +803,7 @@ I16B_L(UINT8* out, const UINT8* in, int xsize)
{ {
int x; int x;
for (x = 0; x < xsize; x++, in += 2) for (x = 0; x < xsize; x++, in += 2)
if (in[0] != 0) *out++ = in[0] + (in[1] << 8);
*out++ = 255;
else
*out++ = in[1];
} }
static struct { static struct {
@ -1020,7 +1002,7 @@ p2i(UINT8* out_, const UINT8* in, int xsize, const UINT8* palette)
int x; int x;
INT32* out = (INT32*) out_; INT32* out = (INT32*) out_;
for (x = 0; x < xsize; x++) for (x = 0; x < xsize; x++)
*out++ = L(&palette[in[x]*4]) / 1000; *out++ = (L(&palette[in[x]*4]) / 1000) << 8;
} }
static void static void
@ -1029,7 +1011,7 @@ pa2i(UINT8* out_, const UINT8* in, int xsize, const UINT8* palette)
int x; int x;
INT32* out = (INT32*) out_; INT32* out = (INT32*) out_;
for (x = 0; x < xsize; x++, in += 4) for (x = 0; x < xsize; x++, in += 4)
*out++ = L(&palette[in[0]*4]) / 1000; *out++ = (L(&palette[in[0]*4]) / 1000) << 8;
} }
static void static void

View File

@ -135,11 +135,10 @@ ImagingGetHistogram(Imaging im, Imaging imMask, void* minmax)
if (imin >= imax) if (imin >= imax)
break; break;
ImagingSectionEnter(&cookie); ImagingSectionEnter(&cookie);
scale = 255.0F / (imax - imin);
for (y = 0; y < im->ysize; y++) { for (y = 0; y < im->ysize; y++) {
INT32* in = im->image32[y]; INT32* in = im->image32[y];
for (x = 0; x < im->xsize; x++) { for (x = 0; x < im->xsize; x++) {
i = (int) (((*in++)-imin)*scale); i = (int) (((*in++)-imin) * 255.0F / (imax - imin));
if (i >= 0 && i < 256) if (i >= 0 && i < 256)
h->histogram[i]++; h->histogram[i]++;
} }