Skip to content

Commit 64f2924

Browse files
authored
Merge pull request #6834 from radarhere/i16n
2 parents 079caf6 + 023d434 commit 64f2924

File tree

9 files changed

+41
-25
lines changed

9 files changed

+41
-25
lines changed

Tests/test_image_access.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -275,15 +275,10 @@ def test_get_vs_c(self):
275275
# self._test_get_access(hopper('PA')) # PA -- how do I make a PA image?
276276
self._test_get_access(hopper("F"))
277277

278-
im = Image.new("I;16", (10, 10), 40000)
279-
self._test_get_access(im)
280-
im = Image.new("I;16L", (10, 10), 40000)
281-
self._test_get_access(im)
282-
im = Image.new("I;16B", (10, 10), 40000)
283-
self._test_get_access(im)
284-
285-
im = Image.new("I", (10, 10), 40000)
286-
self._test_get_access(im)
278+
for mode in ("I;16", "I;16L", "I;16B", "I;16N", "I"):
279+
im = Image.new(mode, (10, 10), 40000)
280+
self._test_get_access(im)
281+
287282
# These don't actually appear to be modes that I can actually make,
288283
# as unpack sets them directly into the I mode.
289284
# im = Image.new('I;32L', (10, 10), -2**10)
@@ -322,15 +317,10 @@ def test_set_vs_c(self):
322317
# self._test_set_access(i, (128, 128)) #PA -- undone how to make
323318
self._test_set_access(hopper("F"), 1024.0)
324319

325-
im = Image.new("I;16", (10, 10), 40000)
326-
self._test_set_access(im, 45000)
327-
im = Image.new("I;16L", (10, 10), 40000)
328-
self._test_set_access(im, 45000)
329-
im = Image.new("I;16B", (10, 10), 40000)
330-
self._test_set_access(im, 45000)
320+
for mode in ("I;16", "I;16L", "I;16B", "I;16N", "I"):
321+
im = Image.new(mode, (10, 10), 40000)
322+
self._test_set_access(im, 45000)
331323

332-
im = Image.new("I", (10, 10), 40000)
333-
self._test_set_access(im, 45000)
334324
# im = Image.new('I;32L', (10, 10), -(2**10))
335325
# self._test_set_access(im, -(2**13)+1)
336326
# im = Image.new('I;32B', (10, 10), 2**10)

Tests/test_lib_pack.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ def test_I(self):
207207
0x01000083,
208208
)
209209

210+
def test_I16(self):
211+
self.assert_pack("I;16N", "I;16N", 2, 0x0201, 0x0403, 0x0605)
212+
210213
def test_F_float(self):
211214
self.assert_pack("F", "F;32F", 4, 1.539989614439558e-36, 4.063216068939723e-34)
212215

@@ -761,10 +764,12 @@ def test_I16(self):
761764
self.assert_unpack("I;16", "I;16N", 2, 0x0201, 0x0403, 0x0605)
762765
self.assert_unpack("I;16B", "I;16N", 2, 0x0201, 0x0403, 0x0605)
763766
self.assert_unpack("I;16L", "I;16N", 2, 0x0201, 0x0403, 0x0605)
767+
self.assert_unpack("I;16N", "I;16N", 2, 0x0201, 0x0403, 0x0605)
764768
else:
765769
self.assert_unpack("I;16", "I;16N", 2, 0x0102, 0x0304, 0x0506)
766770
self.assert_unpack("I;16B", "I;16N", 2, 0x0102, 0x0304, 0x0506)
767771
self.assert_unpack("I;16L", "I;16N", 2, 0x0102, 0x0304, 0x0506)
772+
self.assert_unpack("I;16N", "I;16N", 2, 0x0102, 0x0304, 0x0506)
768773

769774
def test_CMYK16(self):
770775
self.assert_unpack("CMYK", "CMYK;16L", 8, (2, 4, 6, 8), (10, 12, 14, 16))

Tests/test_mode_i16.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,7 @@ def tobytes(mode):
8888
def test_convert():
8989
im = original.copy()
9090

91-
verify(im.convert("I;16"))
92-
verify(im.convert("I;16").convert("L"))
93-
verify(im.convert("I;16").convert("I"))
94-
95-
verify(im.convert("I;16B"))
96-
verify(im.convert("I;16B").convert("L"))
97-
verify(im.convert("I;16B").convert("I"))
91+
for mode in ("I;16", "I;16B", "I;16N"):
92+
verify(im.convert(mode))
93+
verify(im.convert(mode).convert("L"))
94+
verify(im.convert(mode).convert("I"))

docs/releasenotes/9.5.0.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,10 @@ Added support for saving PDFs in RGBA mode
6363
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6464

6565
Using the JPXDecode filter, PDFs can now be saved in RGBA mode.
66+
67+
68+
Improved I;16N support
69+
^^^^^^^^^^^^^^^^^^^^^^
70+
71+
Support has been added for I;16N access, packing and unpacking. Conversion to
72+
and from L mode has also been added.

src/PIL/PyAccess.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ def set_pixel(self, x, y, color):
320320
"1": _PyAccess8,
321321
"L": _PyAccess8,
322322
"P": _PyAccess8,
323+
"I;16N": _PyAccessI16_N,
323324
"LA": _PyAccess32_2,
324325
"La": _PyAccess32_2,
325326
"PA": _PyAccess32_2,

src/libImaging/Access.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
/* use make_hash.py from the pillow-scripts repository to calculate these values */
1515
#define ACCESS_TABLE_SIZE 27
16-
#define ACCESS_TABLE_HASH 3078
16+
#define ACCESS_TABLE_HASH 33051
1717

1818
static struct ImagingAccessInstance access_table[ACCESS_TABLE_SIZE];
1919

@@ -92,6 +92,12 @@ get_pixel_16B(Imaging im, int x, int y, void *color) {
9292
#endif
9393
}
9494

95+
static void
96+
get_pixel_16(Imaging im, int x, int y, void *color) {
97+
UINT8 *in = (UINT8 *)&im->image[y][x + x];
98+
memcpy(color, in, sizeof(UINT16));
99+
}
100+
95101
static void
96102
get_pixel_32(Imaging im, int x, int y, void *color) {
97103
memcpy(color, &im->image32[y][x], sizeof(INT32));
@@ -186,6 +192,7 @@ ImagingAccessInit() {
186192
ADD("I;16", get_pixel_16L, put_pixel_16L);
187193
ADD("I;16L", get_pixel_16L, put_pixel_16L);
188194
ADD("I;16B", get_pixel_16B, put_pixel_16B);
195+
ADD("I;16N", get_pixel_16, put_pixel_16L);
189196
ADD("I;32L", get_pixel_32L, put_pixel_32L);
190197
ADD("I;32B", get_pixel_32B, put_pixel_32B);
191198
ADD("F", get_pixel_32, put_pixel_32);

src/libImaging/Convert.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,13 @@ static struct {
990990
{"I;16L", "L", I16L_L},
991991
{"L", "I;16B", L_I16B},
992992
{"I;16B", "L", I16B_L},
993+
#ifdef WORDS_BIGENDIAN
994+
{"L", "I;16N", L_I16B},
995+
{"I;16N", "L", I16B_L},
996+
#else
997+
{"L", "I;16N", L_I16L},
998+
{"I;16N", "L", I16L_L},
999+
#endif
9931000

9941001
{"I;16", "F", I16L_F},
9951002
{"I;16L", "F", I16L_F},

src/libImaging/Pack.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ static struct {
664664
#endif
665665
{"I;16B", "I;16B", 16, copy2},
666666
{"I;16L", "I;16L", 16, copy2},
667+
{"I;16N", "I;16N", 16, copy2},
667668
{"I;16", "I;16N", 16, packI16N_I16}, // LibTiff native->image endian.
668669
{"I;16L", "I;16N", 16, packI16N_I16},
669670
{"I;16B", "I;16N", 16, packI16N_I16B},

src/libImaging/Unpack.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,6 +1762,7 @@ static struct {
17621762
{"I;16", "I;16", 16, copy2},
17631763
{"I;16B", "I;16B", 16, copy2},
17641764
{"I;16L", "I;16L", 16, copy2},
1765+
{"I;16N", "I;16N", 16, copy2},
17651766

17661767
{"I;16", "I;16N", 16, unpackI16N_I16}, // LibTiff native->image endian.
17671768
{"I;16L", "I;16N", 16, unpackI16N_I16}, // LibTiff native->image endian.

0 commit comments

Comments
 (0)