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

Add support for JPEG-LS encoding/decoding #142

Merged
merged 3 commits into from
Jan 3, 2022
Merged

Conversation

hackermd
Copy link
Collaborator

@hackermd hackermd commented Dec 10, 2021

This PR will make it possible to encode and decode images using the JPEG-LS codec (which is much much fast than JPEG2000 for both encoding and decoding).

It would introduce additional dependencies:

  1. pillow-jpls: enables reading/writing JPEG-LS images using the Pillow API
  2. pylibjpeg: enables reading of JPEG, JPEG-LS, and JPEG-2000 compressed frames via pydicom

@hackermd hackermd added the enhancement New feature or request label Dec 10, 2021
@hackermd
Copy link
Collaborator Author

We could avoid the pylibjpeg dependency by implementing JPEG-LS decoding in pydicom using Pillow (with the extension).

Copy link
Collaborator

@CPBridge CPBridge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I am not too concerned about the new dependency.

However, currently while this ability to encode JPEG-LS frames will be available, it will not be used by any of the implemented SOP classes. Seg/pm/sc all have a list of supported transfer syntaxes in their implementations (and docstrings). Should we enable support for JPEG-LS in these classes in this PR too?

@hackermd
Copy link
Collaborator Author

Looks good! I am not too concerned about the new dependency.

However, currently while this ability to encode JPEG-LS frames will be available, it will not be used by any of the implemented SOP classes. Seg/pm/sc all have a list of supported transfer syntaxes in their implementations (and docstrings). Should we enable support for JPEG-LS in these classes in this PR too?

Addressed via 6f58aed and 99a9da5

Copy link
Collaborator

@CPBridge CPBridge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question to perhaps think about, but otherwise this is good to go

@@ -454,7 +489,7 @@ def __init__(
instance_number=instance_number,
manufacturer=ref_ds.Manufacturer,
modality=ref_ds.Modality,
transfer_syntax_uid=None, # FIXME: frame encoding
transfer_syntax_uid=transfer_syntax_uid,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a though, should the default behaviour be to copy the transfer syntax from the input images, and not re-encode the pixels?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally agree.

However, this may be a good opportunity to avoid some of the exotic lossless JEPG transfer syntaxes that are not widely supported and thereby facilitate downstream decoding. Thoughts?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you suggesting that if no transfer syntax is explicitly specified, we selectively re-encode some lossless codecs into a more commonly-supported format?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, or just not compress at all by default (which has been the behavior so far)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If any of the source images was lossy compressed, we also shouldn't re-compress the enhanced image with a lossy transfer syntax

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree completely - if the images have been lossy compressed a different compression algorithm can create artifacts; even the same lossy compression technique with different parameters can create a mess. If the original compressed bytes aren't passed through then a lossless algorithm is the only safe mechanism.

I think there is a DICOM field which designates whether the image has been lossy compressed or not. If the images are transmitted decompressed this field should be set to true so that downstream the consumer knows that the images are not the original image data.

I don't know if the FDA wants the lossy compression ratio put into the image or not - there was a big discussion of this 20 years ago but I haven't been following it since then.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, the main thing to avoid would be to decompress lossy data and then recompress losslessly (or store with no compression, as is currently the default) thus giving the impression of lossless compression when in fact information has been lost. That's why I'm suggesting simply leaving the transfer syntax and compression alone by default in those situations unless the user specifically requests otherwise. However I don't see a harm in silently translating between different lossless formats, although it is a bit magical

Copy link
Collaborator Author

@hackermd hackermd Jan 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user can currently not set a lossy transfer syntax. If the pixel data was lossy compressed in the first place, the attribute LossyImageCompression should already reflect that (and should stay unchanged even if pixel data is subsequently encoded using a lossless transfer syntax).

The behaviour so far has been to decode the pixel data and re-encode it uncompressed. We can (and probably should) change that behavior upon refactoring of the legacy conversion package as part of #34

@hackermd hackermd merged commit 6db292f into master Jan 3, 2022
@hackermd hackermd deleted the feature/jpegls-encoding branch January 3, 2022 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants