Skip to content

Commit

Permalink
Fix pyjpegls handler for interleave mode 0 (#2007)
Browse files Browse the repository at this point in the history
  • Loading branch information
scaramallion committed Feb 11, 2024
1 parent 2877aa6 commit 0c4246e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/pydicom/data/hashes.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,9 @@
"mlut_18.dcm": "9c65b39df55dc46a4670f76e0ec1093d097206ed46c2d7e23b8051c87ef0228b",
"vlut_04.dcm": "64f54c0f490ce3fa2faac0a90a7ca0166caa025f8fdcfbe181906387a7867c27",
"HTJ2KLossless_08_RGB.dcm": "38f8e8adf46b928a12f1905df1405bc8a32c10286733c47562c75be84ceae00e",
"HTJ2K_08_RGB.dcm": "9a7ae1960f18315c4d58876c2a8333a704e89ca3697edd5b69f600773220eb90"
"HTJ2K_08_RGB.dcm": "9a7ae1960f18315c4d58876c2a8333a704e89ca3697edd5b69f600773220eb90",
"JLSL_RGB_ILV0.dcm": "f8836a650728f4f1b014a52905e321490f3eefcc0f71ac27fd2c1bd7fc5bbcc4",
"JLSL_RGB_ILV1.dcm": "281610d528d8e22bd52e79d17261ef0c238ef8cfc50696a2d9875a933108864e",
"JLSL_RGB_ILV2.dcm": "f8d670e9988cbca207d3367d916aff3cb508c076495211a0d132692266e9546d",
"JLSN_RGB_ILV0.dcm": "a377750d24bd3413d21faa343662dfff997db9acf65c0b095c5d8a95beb866fa"
}
6 changes: 5 additions & 1 deletion src/pydicom/data/urls.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,9 @@
"US1_UNCR.dcm": "https://github.com/pydicom/pydicom-data/raw/39a2eb31815eec435dc26c322c27aec5cfcbddb6/data/US1_UNCR.dcm",
"vlut_04.dcm": "https://github.com/pydicom/pydicom-data/raw/39a2eb31815eec435dc26c322c27aec5cfcbddb6/data/vlut_04.dcm",
"HTJ2KLossless_08_RGB.dcm": "https://github.com/pydicom/pydicom-data/raw/9bde4d24b2d4a666fa8c4e93c8bccb2a775ea536/data_store/data/HTJ2KLossless_08_RGB.dcm",
"HTJ2K_08_RGB.dcm": "https://github.com/pydicom/pydicom-data/raw/9bde4d24b2d4a666fa8c4e93c8bccb2a775ea536/data_store/data/HTJ2K_08_RGB.dcm"
"HTJ2K_08_RGB.dcm": "https://github.com/pydicom/pydicom-data/raw/9bde4d24b2d4a666fa8c4e93c8bccb2a775ea536/data_store/data/HTJ2K_08_RGB.dcm",
"JLSL_RGB_ILV0.dcm": "https://github.com/pydicom/pydicom-data/raw/7358d21f75fa8cdc8ae4e0bb02f7612c52a140ec/data_store/data/JLSL_RGB_ILV0.dcm",
"JLSL_RGB_ILV1.dcm": "https://github.com/pydicom/pydicom-data/raw/7358d21f75fa8cdc8ae4e0bb02f7612c52a140ec/data_store/data/JLSL_RGB_ILV1.dcm",
"JLSL_RGB_ILV2.dcm": "https://github.com/pydicom/pydicom-data/raw/7358d21f75fa8cdc8ae4e0bb02f7612c52a140ec/data_store/data/JLSL_RGB_ILV2.dcm",
"JLSN_RGB_ILV0.dcm": "https://github.com/pydicom/pydicom-data/raw/7358d21f75fa8cdc8ae4e0bb02f7612c52a140ec/data_store/data/JLSN_RGB_ILV0.dcm"
}
15 changes: 11 additions & 4 deletions src/pydicom/pixels/decoders/pyjpegls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,23 @@


DECODER_DEPENDENCIES = {
uid.JPEGLSLossless: ("jpeg_ls>=1.1"),
uid.JPEGLSNearLossless: ("jpeg_ls>=1.1"),
uid.JPEGLSLossless: ("numpy", "pyjpegls>=1.2"),
uid.JPEGLSNearLossless: ("numpy", "pyjpegls>=1.2"),
}


def is_available(uid: str) -> bool:
"""Return ``True`` if the decoder has its dependencies met, ``False`` otherwise"""
return _passes_version_check("jpeg_ls", (1, 1))
return _passes_version_check("jpeg_ls", (1, 2))


def _decode_frame(src: bytes, runner: DecodeRunner) -> bytearray:
"""Return the decoded image data in `src` as a :class:`bytearray`."""
return cast(bytearray, jpeg_ls.decode_from_buffer(src))
buffer, info = jpeg_ls.decode_pixel_data(src)
# Interleave mode 0 is colour-by-plane, 1 and 2 are colour-by-pixel
if info["components"] > 1 and info["interleave_mode"] == 0:
runner.set_option("planar_configuration", 1)
else:
runner.set_option("planar_configuration", 0)

return cast(bytearray, buffer)
69 changes: 66 additions & 3 deletions tests/pixels/pixels_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -1731,6 +1731,48 @@ def test(ref, arr, **kwargs):
# tsyntax, (bits allocated, stored), (frames, rows, cols, planes), VR, PI, pixel repr.


# JLSL, (8, 8), (1, 256, 256, 3), OB, RGB, 0
# Plane interleaved (ILV 0)
def test(ref, arr, **kwargs):
assert arr[124:128, 40].tolist() == [
[115, 109, 91],
[109, 105, 100],
[100, 111, 94],
[192, 53, 172],
]


JLSL_08_08_3_0_1F_ILV0 = PixelReference("JLSL_RGB_ILV0.dcm", "u1", test)


# JLSL, (8, 8), (1, 256, 256, 3), OB, RGB, 0
# Line interleaved (ILV 1)
def test(ref, arr, **kwargs):
assert arr[124:128, 40].tolist() == [
[115, 109, 91],
[109, 105, 100],
[100, 111, 94],
[192, 53, 172],
]


JLSL_08_08_3_0_1F_ILV1 = PixelReference("JLSL_RGB_ILV1.dcm", "u1", test)


# JLSL, (8, 8), (1, 256, 256, 3), OB, RGB, 0
# Sample interleaved (ILV 2)
def test(ref, arr, **kwargs):
assert arr[124:128, 40].tolist() == [
[115, 109, 91],
[109, 105, 100],
[100, 111, 94],
[192, 53, 172],
]


JLSL_08_08_3_0_1F_ILV2 = PixelReference("JLSL_RGB_ILV2.dcm", "u1", test)


# JLSL, (16, 16), (1, 64, 64, 1), OW, MONOCHROME2, 1
def test(ref, arr, **kwargs):
# pylibjpeg, pyjpegls
Expand Down Expand Up @@ -1768,8 +1810,22 @@ def test(ref, arr, **kwargs):
JLSN_08_01_1_0_1F = PixelReference("JPEGLSNearLossless_08.dcm", "u1", test)


# JLSN, (8, 8), (1, 256, 256, 3), OB, RGB, 0
# Plane interleaved (ILV 0), lossy error 3
def test(ref, arr, **kwargs):
assert arr[124:128, 40].tolist() == [
[118, 110, 92],
[110, 103, 99],
[97, 113, 96],
[191, 55, 175],
]


JLSN_08_08_3_0_1F_ILV0 = PixelReference("JLSN_RGB_ILV0.dcm", "u1", test)


# JLSN, (8, 8), (1, 100, 100, 3), OB, RGB, 0
# Line interleaved
# Line interleaved (ILV 1)
def test(ref, arr, **kwargs):
assert arr[0, 0].tolist() == [255, 0, 0]
assert arr[10, 0].tolist() == [255, 130, 130]
Expand All @@ -1787,7 +1843,7 @@ def test(ref, arr, **kwargs):


# JLSN, (8, 8), (1, 100, 100, 3), OB, RGB, 0
# Sample interleaved
# Sample interleaved (ILV 2)
def test(ref, arr, **kwargs):
assert arr[0, 0].tolist() == [255, 0, 0]
assert arr[10, 0].tolist() == [255, 130, 130]
Expand Down Expand Up @@ -1820,9 +1876,16 @@ def test(ref, arr, **kwargs):
JLSN_16_16_1_0_1F = PixelReference("JPEGLSNearLossless_16.dcm", "u2", test)


PIXEL_REFERENCE[JPEGLSLossless] = [JLSL_16_16_1_1_1F, JLSL_16_12_1_1_10F]
PIXEL_REFERENCE[JPEGLSLossless] = [
JLSL_08_08_3_0_1F_ILV0,
JLSL_08_08_3_0_1F_ILV1,
JLSL_08_08_3_0_1F_ILV2,
JLSL_16_16_1_1_1F,
JLSL_16_12_1_1_10F,
]
PIXEL_REFERENCE[JPEGLSNearLossless] = [
JLSN_08_01_1_0_1F,
JLSN_08_08_3_0_1F_ILV0,
JLSN_08_08_1_0_3F_LINE,
JLSN_08_08_1_0_3F_SAMPLE,
JLSN_16_16_1_0_1F,
Expand Down

0 comments on commit 0c4246e

Please sign in to comment.