Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding bug in OpenJPEG third party module #4517

Open
zivy opened this issue Mar 14, 2024 · 6 comments
Open

encoding bug in OpenJPEG third party module #4517

zivy opened this issue Mar 14, 2024 · 6 comments
Labels
type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances

Comments

@zivy
Copy link
Member

zivy commented Mar 14, 2024

The version of OpenJPEG in ITK which is used for JPEG2000 compression (default codec when writing DICOM via GDCM) has a bug encoding int32, it silently fails writing (incorrect write). When attempting to read the result the reader will throw an exception reporting the issue (correct behavior). This appears to be resolved in newer versions of OpenJPEG, so likely updating the module to a newer version will resolve the issue.

Note that the bug was still in OpenJPEG circa 2020 (see this discussion).

Python code illustrating the issue:

import itk
import numpy as np
from imagecodecs import jpeg2k_encode, jpeg2k_decode

file_name = "slice.dcm"
arr = np.arange(100, dtype="int32").reshape(10,10)

image = itk.GetImageFromArray(arr)
itk.imwrite(image, file_name, compression=True)
itk.imread(file_name)

#encoding/decoding works with the openjpeg version used by the imagecodecs package which relies
#on version 2.5.0 of openjpeg
encoded_arr = jpeg2k_encode(arr)
decoded_arr = jpeg2k_decode(encoded_arr)
@zivy zivy added the type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances label Mar 14, 2024
@issakomi
Copy link
Member

issakomi commented Mar 15, 2024

The version of OpenJPEG in ITK which is used for JPEG2000 compression (default codec when writing DICOM via GDCM)

GDCM uses own OpenJPEG library (or "system" optionally), it is libitkgdcmopenjp2-5.4.a in ITK build. Yes, it is rather old, 2.3.0 (from 2019, AFAIK). CC @malaterre

...
[ 37%] Building C object Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmopenjpeg/src/lib/openjp2/CMakeFiles/gdcmopenjp2.dir/j2k.c.o
[ 37%] Building C object Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmopenjpeg/src/lib/openjp2/CMakeFiles/gdcmopenjp2.dir/jp2.c.o
[ 37%] Building C object Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmopenjpeg/src/lib/openjp2/CMakeFiles/gdcmopenjp2.dir/mct.c.o
[ 37%] Building C object Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmopenjpeg/src/lib/openjp2/CMakeFiles/gdcmopenjp2.dir/mqc.c.o
[ 37%] Building C object Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmopenjpeg/src/lib/openjp2/CMakeFiles/gdcmopenjp2.dir/openjpeg.c.o
...

@blowekamp
Copy link
Member

Is this an issue that need to be reported and addressed in the GDCM repo?

@issakomi
Copy link
Member

issakomi commented Mar 22, 2024

Is this an issue that need to be reported and addressed in the GDCM repo?

GDCM uses own OpenJPEG library (or "system" optionally)

Yes, but also there may be other issues with BitsAllocated 32 sometimes and the line
prec=%u (should be between 1 and 38 according to the JPEG2000 norm. OpenJpeg only supports up to 31)
is still in OpenJpeg master, s. here and here. It is not simple, IMHO. Maybe build GDCM with system's OpenJpeg (on Linux it is easy and the version is 2.5.0+) and test, does it help or not.

Edit: also see this line in GDCM.

@zivy
Copy link
Member Author

zivy commented Apr 5, 2024

Just posted the issue on the GDCM bug-tracker (https://sourceforge.net/p/gdcm/bugs/559/).

@malaterre, really appreciate your help resolving this. We have some naive SimpleITK users who encountered the problem, using a system OpenJPEG is not an option for them - it is beyond what they can do. Thanks.

@malaterre
Copy link
Member

Just posted the issue on the GDCM bug-tracker (https://sourceforge.net/p/gdcm/bugs/559/).

@malaterre, really appreciate your help resolving this. We have some naive SimpleITK users who encountered the problem, using a system OpenJPEG is not an option for them - it is beyond what they can do. Thanks.

@zivy I was not involved in the original Modules/ThirdParty/OpenJPEG, please update and you should be done. Last I checked this is the j2k module used by ITK/GDCM.

@zivy
Copy link
Member Author

zivy commented Apr 5, 2024

Hello @malaterre,

Sorry, but I'm a bit confused.

Per @issakomi's observation, the OpenJPEG library used by GDCM appears to be fromModules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmopenjpeg which is separate from the Modules/ThirdParty/OpenJPEG (I originally thought that module was what needed to be upgraded). This is why I ended up posting the request for updating the library on the gdcm issue tracker. What am I missing here? Thanks for taking a look at this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances
Projects
None yet
Development

No branches or pull requests

4 participants