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

Losslessly converting color JPEG to grayscale JXL #3516

Open
chocolatkey opened this issue Apr 21, 2024 · 4 comments
Open

Losslessly converting color JPEG to grayscale JXL #3516

chocolatkey opened this issue Apr 21, 2024 · 4 comments
Labels
unrelated to 1.0 Things that need not be done before the 1.0 version milestone

Comments

@chocolatkey
Copy link

chocolatkey commented Apr 21, 2024

When it comes to manga (grayscale Japanese comic) files, many of the final production files that publishers end up encoding are JPEGs, due to their inclusion in EPUB files. When doing so, the majority of publishers do not encode these as grayscale (8BPP) JPEGs, even though this would result in no loss of data.
For a project I am working on, I would like to losslessly convert these JPEG files to JXL files to reduce their size, and have done so using cjxl input.jpg output.jxl. I would like to, however, also reduce the colorspace of these images at the same time, making them grayscale. I have confirmed that the images unsurprisingly remain in color after the conversion:

jxlinfo output.jxl

JPEG XL file format container (ISO/IEC 18181-2)
JPEG XL image, 900x1350, (possibly) lossless, 8-bit RGB
Color space: 3144-byte ICC profile, CMM type: "Lino", color space: "RGB ", rendering intent: 0
JPEG bitstream reconstruction data available
Brotli-compressed Exif metadata: 74 compressed bytes

Would it somehow be possible to losslessly reduce the colorspace during the JPEG --> JXL conversion?

@chocolatkey chocolatkey changed the title Losslessly converting JPEG to grayscale JXL Losslessly converting color JPEG to grayscale JXL Apr 21, 2024
@jendalinda
Copy link

jendalinda commented Apr 21, 2024

I'm not sure if this could be implemented in JXL's lossless JPEG transcoding.
You can use a program jpegtran to remove color information from JPEG images before transcoding to JXL. It will keep the grayscale part intact. It can also remove metadata if you don't need them.

@mo271 mo271 added the unrelated to 1.0 Things that need not be done before the 1.0 version milestone label Apr 22, 2024
@jonsneyers
Copy link
Member

Internally, JPEG XL stores grayscale and color images in the same way (grayscale images just happen to have all-zero chroma components). So compression-wise, there is not really anything to gain here.

For the bit-exact reconstruction of the JPEG images, obviously we cannot silently change RGB to Grayscale since that would result in a different reconstructed JPEG file. It also would lead to problems when there's an ICC profile in the JPEG, since you would get a grayscale image with an RGB ICC color profile, which is an invalid combination.

If you know the images are grayscale then as was mentioned above, you may want to use jpegtran before recompressing the jpeg files, e.g. using something like this:

jpegtran -copy none -optimize -grayscale input.jpg | cjxl - output.jxl

If you're not interested in reconstructing the jpeg file, which would not match the original one anyway, and want to trim of some more bytes, you can add --allow_jpeg_reconstruction=0 to the cjxl command line. The image data will still be the same, but you won't be able to generate a reconstructed jpeg file anymore if you do that.

@j7nj7n
Copy link

j7nj7n commented May 1, 2024

Note that you might get a slightly different gamma transfer function for greyscale files in the viewing application. The gamma for greyscale might be set to 2.2 instead of sRGB (sGray). This will result in a slight brightening of the blacks, which might go unnoticed. Some simple programs don't support ICC for greyscale to indicate a gamma. Greyscale uses 1/3-rd of the memory and opens sligthly faster. Even in normal JPEG color compresses down to nothing if absent.

@jendalinda
Copy link

cjxl will automatically set sRGB transfer fubnction in untagged images, so this should not be an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
unrelated to 1.0 Things that need not be done before the 1.0 version milestone
Projects
None yet
Development

No branches or pull requests

5 participants