Skip to content

Commit 3c39be3

Browse files
authored
Merge pull request #5901 from radarhere/i16
Improved I;16 operations on big endian
2 parents fccc261 + aeb549e commit 3c39be3

File tree

5 files changed

+18
-16
lines changed

5 files changed

+18
-16
lines changed

Tests/test_file_jpeg2k.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
assert_image_equal,
1111
assert_image_similar,
1212
assert_image_similar_tofile,
13-
is_big_endian,
1413
skip_unless_feature,
1514
)
1615

@@ -234,13 +233,11 @@ def test_16bit_monochrome_has_correct_mode():
234233
assert jp2.mode == "I;16"
235234

236235

237-
@pytest.mark.xfail(is_big_endian(), reason="Fails on big-endian")
238236
def test_16bit_monochrome_jp2_like_tiff():
239237
with Image.open("Tests/images/16bit.cropped.tif") as tiff_16bit:
240238
assert_image_similar_tofile(tiff_16bit, "Tests/images/16bit.cropped.jp2", 1e-3)
241239

242240

243-
@pytest.mark.xfail(is_big_endian(), reason="Fails on big-endian")
244241
def test_16bit_monochrome_j2k_like_tiff():
245242
with Image.open("Tests/images/16bit.cropped.tif") as tiff_16bit:
246243
assert_image_similar_tofile(tiff_16bit, "Tests/images/16bit.cropped.j2k", 1e-3)

Tests/test_file_png.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
assert_image_equal,
1414
assert_image_equal_tofile,
1515
hopper,
16-
is_big_endian,
1716
is_win32,
1817
mark_if_feature_version,
1918
skip_unless_feature,
@@ -77,7 +76,6 @@ def get_chunks(self, filename):
7776
png.crc(cid, s)
7877
return chunks
7978

80-
@pytest.mark.xfail(is_big_endian(), reason="Fails on big-endian")
8179
def test_sanity(self, tmp_path):
8280

8381
# internal version number

src/libImaging/Jpeg2KDecode.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,11 @@ j2ku_gray_i(
180180
case 2:
181181
for (y = 0; y < h; ++y) {
182182
const UINT16 *data = (const UINT16 *)&tiledata[2 * y * w];
183-
UINT16 *row = (UINT16 *)im->image[y0 + y] + x0;
183+
UINT8 *row = (UINT8 *)im->image[y0 + y] + x0;
184184
for (x = 0; x < w; ++x) {
185-
*row++ = j2ku_shift(offset + *data++, shift);
185+
UINT16 pixel = j2ku_shift(offset + *data++, shift);
186+
*row++ = pixel;
187+
*row++ = pixel >> 8;
186188
}
187189
}
188190
break;

src/libImaging/Jpeg2KEncode.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,15 @@ j2k_pack_i16(Imaging im, UINT8 *buf, unsigned x0, unsigned y0, unsigned w, unsig
110110
for (y = 0; y < h; ++y) {
111111
UINT8 *data = (UINT8 *)(im->image[y + y0] + x0);
112112
for (x = 0; x < w; ++x) {
113-
*ptr++ = *data++;
114-
*ptr++ = *data++;
113+
#ifdef WORDS_BIGENDIAN
114+
ptr[0] = data[1];
115+
ptr[1] = data[0];
116+
#else
117+
ptr[0] = data[0];
118+
ptr[1] = data[1];
119+
#endif
120+
ptr += 2;
121+
data += 2;
115122
}
116123
}
117124
}
@@ -301,13 +308,7 @@ j2k_encode_entry(Imaging im, ImagingCodecState state) {
301308
components = 1;
302309
color_space = OPJ_CLRSPC_GRAY;
303310
pack = j2k_pack_l;
304-
} else if (strcmp(im->mode, "I;16") == 0) {
305-
components = 1;
306-
color_space = OPJ_CLRSPC_GRAY;
307-
pack = j2k_pack_i16;
308-
prec = 16;
309-
bpp = 12;
310-
} else if (strcmp(im->mode, "I;16B") == 0) {
311+
} else if (strcmp(im->mode, "I;16") == 0 || strcmp(im->mode, "I;16B") == 0) {
311312
components = 1;
312313
color_space = OPJ_CLRSPC_GRAY;
313314
pack = j2k_pack_i16;

src/libImaging/Pack.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,11 @@ static struct {
656656

657657
/* storage modes */
658658
{"I;16", "I;16", 16, copy2},
659+
#ifdef WORDS_BIGENDIAN
660+
{"I;16", "I;16B", 16, packI16N_I16},
661+
#else
659662
{"I;16", "I;16B", 16, packI16N_I16B},
663+
#endif
660664
{"I;16B", "I;16B", 16, copy2},
661665
{"I;16L", "I;16L", 16, copy2},
662666
{"I;16", "I;16N", 16, packI16N_I16}, // LibTiff native->image endian.

0 commit comments

Comments
 (0)