Skip to content

Add L32F and La32F to both ColorType and DynamicImage #2935

Closed

Description

@mstoeckl

As advised in the PR implementing this feature, #2934, I am moving the discussion to a this new issue thread. I have not found any existing issue that quite covers it.

I would like to add ColorType::L32F and ColorType::La32F entries to ColorType, and matching DynamicImage::Luma32F and DynamicImage::LumaA32F variants. I do not have a particularly strong need for either (yet), but think having them makes the library easier to use and the costs are not too steep.

  1. The ImageDecoder trait only decodes to valid ColorTypes, although it also provides an ExtendedColorType that indicates roughly the original color type was. This can be somewhat inefficient (bitmaps get expanded to ColorType::L8 at minimum) but ensures users of ImageDecoder do not need to write much casework to handle the possible ColorTypes. (Although I suspect many either decode to DynamicImage, whose conversion functions solve the problem; or read a specific format for which the range of color types is limited.) The most general ColorType is Rgba32F, and almost all image format contents can be converted to it with negligible loss of information. (TIFF's 64F and multiband; and the many YCbCr color types are the main exception.)

    There are a few formats that can contain single channel floating point data, most notably EXR, TIFF, DDS, and PHM/PFM, which can hold either f16 or f32. The nearest containing ColorType at the moment is Rgb32F, which requires 3x (or more, for f16) the memory. I expect it may also be briefly confusing if someone loads a depth or bump map and gets RGB data out of it. Encoding such a DynamicImage as grayscale into an image format is possible through the high level API since b773c9d (using DynamicImage::to_luma_32f() followed by ImageBuffer::write_to), but is not easy to find.

  2. DynamicImage can contain image data whose specific color type is chosen at runtime. I consider its main uses to be to efficently convert images between color types (for example, given one process that naturally produces a range of color types, and one process that naturally accepts a different range, one can use DynamicImage as the interface between them.), and to perform operations (resizing, cropping) on images of uncertain color type. It provides some methods to convert between color types, like DynamicImage::grayscale, and the many "DynamicImage::to_" methods to pick a specific type.

    However, it is missing (or has incomplete) methods that modify just one aspect of the current color type, like the grayscale/rgb distinction, the absence/presence of alpha, and the channel type. Similarly to the PNG transform flags, I think such methods would be useful for those who can afford to process some but not all of the full variability of the color types. Adding L32F and La32F variants makes the set of color types within DynamicImage complete, so that e.g. DynamicImage::grayscale always produces a Luma or LumaA image, that when passed to an external process (image encoder, etc.) will automatically be recognized as a Luma/LumaA image.

The main costs of adding L32F and La32F are, I think, the increases in API complexity (more named functions), in internal complexity (more casework), and in code generation (a proportional increase in monomorphized implementations for DynamicImage, with some central conversion logic growing quadratically. The quadratic growth could be avoided using two-step conversions, but that adds a bit more internal complexity.)

History and related:

Side note: Why keep updating the color types?

I think the set of color types supported by an image processing or rendering library should periodically be revisited.

As the years go by, compute and display hardware will continue to improve, and both drive changes in which formats are needed. Over the last forty years we have mostly seen fixed range images and rendering (with RGB values encoded as integers, mapping to [0,1]) with a transfer function applied in the display, but now people have more reasons to use high dynamic range images and floating point types. Specifically:

  1. Computation is much less expensive, making it easier to apply physically based rendering methods, which tend to calculate directly with physical quantities (luminance) that have a high dynamic range. This mostly affects 3d rendering, but 3d rendering requires 2d textures for reflective color, emission, and other properties; and produces 2d intermediate images.

  2. Improvements in display accuracy make the benefits of accurate color representation visible, so there is stronger interest in HDR and wide color gamut images. While the recent popularization of CICP makes it practical to compress HDR images into u8/u10/u12 color channels, accurate color blending and other computation involves conversion to a (linear, floating point) working space. Increases in display resolution and frame rate have also outpaced single threaded performance, which pushes applications to GPU rendering, where f16 and f32 are well supported color types.

Because hardware has improved, we no longer have a strong need for paletted images, interlaced images, sub-8 bpc formats, or (even) chroma subsampling. But I expect we will want image libraries that support a full set of operations on floating point color types; the only question is when.

Activity

  1. RunDevelopment commented on Jun 2, 2026

    @RunDevelopment
    Member

    Resolved by #2934.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions