Skip to content

Add JXL support - #2917

Merged
davisking merged 29 commits into
davisking:masterfrom
arrufat:add-jxl-support
Mar 4, 2024
Merged

Add JXL support#2917
davisking merged 29 commits into
davisking:masterfrom
arrufat:add-jxl-support

Conversation

@arrufat

@arrufat arrufat commented Feb 25, 2024

Copy link
Copy Markdown
Contributor

This PR adds basic support for the JPEG XL format: loading and saving JXL images from and to all supported dlib pixel formats with appropriate conversions.

The utility of this PR is limited, since it can already be done with dlib::ffmpeg::load_frame and dlib::ffmpeg::save_frame. However, as far as I can tell, it's not possible to store true grayscale or lossless images with that (even with -distance 0 -modular 1).

Moreover, I intend to use JXL for all my datasets from now on, since I will save about 20-40% of disk space with reversible pixel perfect conversion from JPEG and PNG. All that while decoding images faster than libjpeg-turbo.

So, I might add more features specific to JPEG XL in the future (animations, grayscale + alpha, lossless JPEG transcoding).

@arrufat

arrufat commented Feb 27, 2024

Copy link
Copy Markdown
Contributor Author

I think it's done. However, maybe it comes at a wrong time, with all the CMake stuff being refreshed…

@arrufat

arrufat commented Feb 28, 2024

Copy link
Copy Markdown
Contributor Author

I just found out that the JXL decoder handles the EXIF orientation automatically, so we don't need to do anything to support this here.

The Exif orientation should be ignored by applications; the JPEG XL codestream orientation takes precedence and libjxl will by default apply the correct orientation automatically (see JxlDecoderSetKeepOrientation).

Just tried on a JPEG that had EXIF orientation:

cjxl image.jpg image.jxl

By the default, it will be in lossless JPEG mode and store necessary information to reconstruct the JPEG.

Then, if I call load_image on image.jpg and image.jxl, only JXL is displayed with the correct orientation.

Note that we can reconstruct the exact JPEG image with:

djxl image.jxl image-rec.jpg

And the MD5SUM of image.jpg and image-rec.jpg match.

@arrufat

arrufat commented Feb 28, 2024

Copy link
Copy Markdown
Contributor Author

OK, now it's done for real: added Python bindings and updated imglab, too.

Comment thread dlib/image_loader/jxl_loader.cpp Outdated
Co-authored-by: Davis E. King <davis685@gmail.com>
Comment thread dlib/image_loader/jxl_loader.h Outdated
Comment thread dlib/image_loader/jxl_loader.h Outdated
Comment thread dlib/image_loader/jxl_loader.h Outdated
Co-authored-by: Davis E. King <davis685@gmail.com>
Comment thread dlib/image_loader/jxl_loader_abstract.h
Comment thread dlib/image_saver/save_jxl_abstract.h Outdated
Comment thread dlib/image_saver/save_jxl.h Outdated
@arrufat

arrufat commented Feb 29, 2024

Copy link
Copy Markdown
Contributor Author

Done. If I (or someone) have the need for more JPEG XL features at some point, I will add them:

  • grayscale_alpha_pixel: it would require a new pixel type
  • storing the image in floating point: it would require rgb_float, rgb_alpha_float
  • saving and loading animations: overload the load and save functions with std::vector
  • transcoding from and to JPEG: this might be useful to convert imglab datasets.

Comment thread dlib/image_loader/jxl_loader.h Outdated
Comment thread dlib/image_loader/jxl_loader.h Outdated
@davisking

Copy link
Copy Markdown
Owner

Nice, this is sweet.

@davisking
davisking merged commit 9a30c6d into davisking:master Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants