Skip to content

Support for JPEG XL (JXL) images - #3153

Draft
winscripter wants to merge 59 commits into
SixLabors:mainfrom
winscripter:jxl-support
Draft

Support for JPEG XL (JXL) images#3153
winscripter wants to merge 59 commits into
SixLabors:mainfrom
winscripter:jxl-support

Conversation

@winscripter

Copy link
Copy Markdown

Prerequisites

  • I have written a descriptive pull-request title
  • I have verified that there are no overlapping pull-requests open
  • I have verified that I am following the existing coding patterns and practice as demonstrated in the repository. These follow strict Stylecop rules 👮.
  • I have provided test coverage for my change (where applicable)

Description

This is a work-in-progress PR whose goal is to introduce decoding and encoding of JPEG XL (*.jxl) images.

Reference software
I use libjxl as reference. See https://github.com/libjxl/libjxl.

Performance
I will begin by applying light optimizations as I implement parts of the JPEG XL codec. Once the codec seems complete enough to handle decoding and encoding of JPEG XL images, I will apply heavier optimizations. Examples include but are not limited to stack allocation, array pooling, and SIMD.

Other components
The JPEG XL codec, additionally, uses the LZ77 and Brotli compression codec. I will also have to implement those eventually.

Implementations
The JPEG XL codec lives under src/ImageSharp/Formats/Jxl.

Brotli and LZ77 implementations will live under src/ImageSharp/Compression.

Testing
I will start adding tests whenever the codec is complete enough to handle decoding of JPEG XL images.

Additionally, JPEG XL reference software, libjxl, contains its own tests too, which I might also implement without modification.

@CLAassistant

CLAassistant commented Jul 15, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Implementation of ac_strategy.h and ac_strategy.c
For now JxlMemoryManager will be a wrapper around MemoryPool<T>.
Implementation of image.h and image.c; AC strategy implementation was slightly adjusted to reduce errors.
This is an implementation of field_encodings.h.

Note that I avoided implementing EnumValid() and Values() functions, as we have dedicated methods in .NET to do exactly that (Enum.IsDefined, Enum.GetValues)
Implementation of spline.h
Implemented ANS constants
@winscripter

Copy link
Copy Markdown
Author

While I'm working on this, I'd like to note something important.

Libjxl is licensed under the BSD 3-Clause license, and since I'm using libjxl code as reference, that means the license must be included.

I'm not really sure what would be the proper way to include the license. I might place the LICENSE.txt file in the Jxl folder or add a README linking to the libjxl repo.

Comment thread src/ImageSharp/Formats/Jxl/Metadata/JxlExifOrientation.cs Outdated
Comment thread src/ImageSharp/Formats/Jxl/Metadata/JxlExtraChannel.cs Outdated
Comment thread src/ImageSharp/Formats/Jxl/Splines/JxlSplineEntropyContext.cs Outdated
Comment thread src/ImageSharp/Formats/Jxl/JxlFrameDimensions.cs Outdated
It is too large for a struct.
Add JxlAnsEntry and JxlAnsSymbol.

See ans_common.h. These correspond to the Entry and Symbol structures within AliasTable.
Comment thread src/ImageSharp/Formats/Jxl/IO/JxlAnsHelper.cs Outdated
Comment thread src/ImageSharp/Formats/Jxl/IO/JxlAnsHelper.cs Outdated
Comment thread src/ImageSharp/Formats/Jxl/JxlThrowHelper.cs Outdated
Currently, there's a VarLenUint8/VarLenUint16 as well as histogram parsing implementation.

I will additionally have to implement parsing of ANS codes, uint config and LZ77 parameters.
Comment thread src/ImageSharp/Formats/Jxl/Fields/JxlF16Coder.cs Outdated
Comment thread src/ImageSharp/Formats/Jxl/IO/FrameHeader/JxlColorTransformHelpers.cs Outdated
Comment thread src/ImageSharp/Formats/Jxl/Processing/Butteraugli/ButteraugliParameters.cs Outdated
Comment thread src/ImageSharp/Formats/Jxl/Processing/Butteraugli/Butteraugli.cs Outdated
Comment thread src/ImageSharp/Formats/Jxl/Processing/Butteraugli/Butteraugli.cs
Comment thread src/ImageSharp/Formats/Jxl/Processing/JxlQuantizer.cs Outdated
Comment thread src/ImageSharp/Formats/Jxl/Processing/JxlQuantizer.cs Outdated
Comment on lines +15 to +21
// Prefer arrays so we can set values like this:
// JxlOpsinInverseMatrix m = ...;
// m.OpsinBiases[0] = 1f;
// An InlineArray can't do that.
public float[] OpsinBiases { get; set; } = new float[3];

public float[] QuantBiases { get; set; } = new float[4];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

An InlineArray can't do that.

It's good that you followed the suggestion about properties, but in this case that's the reason why "InlineArray can't do that".
When they're just a field, then it's possible, so:

Suggested change
// Prefer arrays so we can set values like this:
// JxlOpsinInverseMatrix m = ...;
// m.OpsinBiases[0] = 1f;
// An InlineArray can't do that.
public float[] OpsinBiases { get; set; } = new float[3];
public float[] QuantBiases { get; set; } = new float[4];
public InlineArray3<float> OpsinBiases;
public InlineArray4<float> QuantBiases;

and it avoids the allocation of the two float arrays.

/// <returns>Boolean indicating whether the coordinates are out of bounds</returns>
private static bool IsOutOfBounds(int a, int b, int size)
{
int position = a + b;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there a possiblity for overflow (at least theoretically)?
If so, could use long position = a + b to be safe.

Comment on lines +8 to +10
// Use arrays instead of InlineArrays because, with inline arrays we can't do:
// JxlOpsinParameters parameters = ...;
// parameters.OpsinBiasesCbrt[0] /* <-- error */ = 1.25f;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same as above. Use fields, not properties here.

The InverseOpsinMatrix with 36 elements is 144 bytes, so still OK when on stack space.


public static bool ReadPermutation(int skip, int size, Span<int> order, JxlBitReader bitReader, JxlAnsSymbolReader reader, Span<byte> contextMap)
{
Span<uint> lehmer = stackalloc uint[size];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is size limited to some maximum value?
If so, there should be an assert here to make it clear.

For the stackalloc it's better to use a constant value, then slice it if needed. Thus produces most of the time better code. E.g. stackalloc uint[128].Slice(0, size).

public static bool ReadPermutation(int skip, int size, Span<int> order, JxlBitReader bitReader, JxlAnsSymbolReader reader, Span<byte> contextMap)
{
Span<uint> lehmer = stackalloc uint[size];
lehmer.Clear();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍🏻 for being safe here, as there's no guarantee for zeroing by the runtime (it should do so, but there're edge case where it isn't).
And if once [SkipLocalsInit] is applied in this project (or is it already?), then this prevent a hard to find 🐛.

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.

4 participants