Skip to content

Commit

Permalink
Fixed JPEG2000 I;16 images on big endian
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Dec 21, 2021
1 parent 591e79e commit 344aaf2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
3 changes: 0 additions & 3 deletions Tests/test_file_jpeg2k.py
Expand Up @@ -10,7 +10,6 @@
assert_image_equal,
assert_image_similar,
assert_image_similar_tofile,
is_big_endian,
skip_unless_feature,
)

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


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


@pytest.mark.xfail(is_big_endian(), reason="Fails on big-endian")
def test_16bit_monochrome_j2k_like_tiff():
with Image.open("Tests/images/16bit.cropped.tif") as tiff_16bit:
assert_image_similar_tofile(tiff_16bit, "Tests/images/16bit.cropped.j2k", 1e-3)
Expand Down
6 changes: 4 additions & 2 deletions src/libImaging/Jpeg2KDecode.c
Expand Up @@ -180,9 +180,11 @@ j2ku_gray_i(
case 2:
for (y = 0; y < h; ++y) {
const UINT16 *data = (const UINT16 *)&tiledata[2 * y * w];
UINT16 *row = (UINT16 *)im->image[y0 + y] + x0;
UINT8 *row = (UINT8 *)im->image[y0 + y] + x0;
for (x = 0; x < w; ++x) {
*row++ = j2ku_shift(offset + *data++, shift);
UINT16 pixel = j2ku_shift(offset + *data++, shift);
*row++ = pixel;
*row++ = pixel >> 8;
}
}
break;
Expand Down
19 changes: 10 additions & 9 deletions src/libImaging/Jpeg2KEncode.c
Expand Up @@ -110,8 +110,15 @@ j2k_pack_i16(Imaging im, UINT8 *buf, unsigned x0, unsigned y0, unsigned w, unsig
for (y = 0; y < h; ++y) {
UINT8 *data = (UINT8 *)(im->image[y + y0] + x0);
for (x = 0; x < w; ++x) {
*ptr++ = *data++;
*ptr++ = *data++;
#ifdef WORDS_BIGENDIAN
ptr[0] = data[1];
ptr[1] = data[0];
#else
ptr[0] = data[0];
ptr[1] = data[1];
#endif
ptr += 2;
data += 2;
}
}
}
Expand Down Expand Up @@ -301,13 +308,7 @@ j2k_encode_entry(Imaging im, ImagingCodecState state) {
components = 1;
color_space = OPJ_CLRSPC_GRAY;
pack = j2k_pack_l;
} else if (strcmp(im->mode, "I;16") == 0) {
components = 1;
color_space = OPJ_CLRSPC_GRAY;
pack = j2k_pack_i16;
prec = 16;
bpp = 12;
} else if (strcmp(im->mode, "I;16B") == 0) {
} else if (strcmp(im->mode, "I;16") == 0 || strcmp(im->mode, "I;16B") == 0) {
components = 1;
color_space = OPJ_CLRSPC_GRAY;
pack = j2k_pack_i16;
Expand Down

0 comments on commit 344aaf2

Please sign in to comment.