Skip to content

Enhance pixel formats and associated-alpha operations with optimizations#3154

Merged
JimBobSquarePants merged 9 commits into
mainfrom
js/premultiplied-pixel-formats
Jul 17, 2026
Merged

Enhance pixel formats and associated-alpha operations with optimizations#3154
JimBobSquarePants merged 9 commits into
mainfrom
js/premultiplied-pixel-formats

Conversation

@JimBobSquarePants

Copy link
Copy Markdown
Member

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 pull request introduces several improvements and new features to the ImageSharp library, focusing on enhanced AOT (Ahead-Of-Time) compilation support, expanded pixel format handling, and increased alpha representation awareness in the Color struct. Additionally, a new test project is added to the solution. The most important changes are summarized below.

AOT Compiler and Pixel Format Support

  • Added support for new pixel formats (e.g., Argb32P, Abgr32P, Bgra32P, HalfVector4P, NormalizedByte4P, Rgb96, Rgba128, Rgba32P, RgbaHalf, RgbaHalfP) in both pixel seeding and image cloning within AotCompilerTools.cs. This ensures these formats are available and optimized for AOT compilation scenarios. [1] [2] [3] [4]
  • Integrated EXR image format support into AOT compilation by including ExrEncoder, ExrDecoder, and their core methods in the compilation routines. [1] [2] [3] [4] [5]
  • Added new image processors to the AOT compilation pipeline, such as AutoLevelProcessor, ConvolutionProcessor, and MedianBlurProcessor, to broaden the range of supported image operations in AOT environments. [1] [2]

Color Struct and Alpha Representation Enhancements

  • Refactored the Color struct to explicitly track and expose the alpha representation (Associated vs. Unassociated) for both the color and its underlying data. This includes new constructor parameters, properties, and improved handling when converting from pixels or vectors. [1] [2]
  • Updated static methods like FromPixel, FromScaledVector, and their bulk span-based overloads to properly propagate alpha representation, ensuring accurate color conversions and preventing data loss, especially for associated alpha formats. [1] [2] [3]
  • Modified WithAlpha and ToHex methods to use the correct alpha representation and ensure consistent, precise color manipulation and serialization. [1] [2]

Solution and Project Structure

  • Added the ImageSharp.PublicApi.Tests project to the solution (ImageSharp.sln), including build configuration and solution folder mapping, to improve public API test coverage. [1] [2] [3]

These changes collectively enhance the robustness, flexibility, and future-proofing of ImageSharp for both runtime and compile-time scenarios.

Introduce Rgba32P, Bgra32P, Argb32P, Abgr32P, NormalizedByte4P, and HalfVector4P.
Add representation-aware scalar and bulk conversions, optimized pixel operations, and associated-alpha blending across all Porter-Duff modes. Include comprehensive conversion, layout, blending, and performance coverage.
Refactors `Color` to track both exposed and stored alpha representations, adds `ToScaledVector4(PixelAlphaRepresentation)`, and adjusts pixel conversion paths so associated formats preserve canonical values without unnecessary unpremultiply/reassociate loss. This also updates equality/hash behavior to compare canonical scaled values.

SIMD helpers are split into `MultiplyAddEstimate` vs `FusedMultiplyAdd`, with byte-to-float normalization updated to match scalar rounding exactly across vector widths. JPEG converters, resize kernels, and Porter-Duff/associated-alpha blending paths were updated to use the appropriate helper for either fast estimate or strict fused rounding semantics. Tests were expanded substantially (including exhaustive component/alpha cases and fused-order checks), incidental test cleanup was applied, benchmark comment snapshots were refreshed, and one black/white reference output image was updated.
Introduce explicit associated/unassociated pixel conversion APIs across `IPixel` and `PixelOperations`, and rework associated-alpha operations to use representation-aware bulk hooks. Update pixel implementations, converters, blenders, and generated code to consistently apply `Premultiply`/`UnPremultiply` semantics, plus SIMD-backed conversion paths for half and signed formats.

Fix codec and processing behavior where alpha handling was incorrect or lossy (notably TIFF/EXR decode/encode, convolution/resize/filter/normalization/dithering paths), and add targeted tests for these cases. Expand AOT compiler roots (including EXR and additional pixel/processor coverage), add a new public API test project for pixel-operations extensibility, and add broad alpha-representation and quantization regression coverage.
Introduces `RgbaHalf` and `RgbaHalfP` plus bulk pixel-operation support, updates AOT seeding, and routes EXR decoding through the new scaled conversions. Also fixes scaled/native range handling for half, SNORM, and signed integer pixel formats, and expands tests for the new layouts and edge cases.
Comment thread src/ImageSharp/PixelFormats/PixelImplementations/NormalizedByte4P.cs Dismissed
@JimBobSquarePants JimBobSquarePants added this to the v4.1.0 milestone Jul 17, 2026
JimBobSquarePants and others added 4 commits July 17, 2026 15:52
…int values'

Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
Slice the destination span to source length in `Vector4Converters.RgbaCompatible.FromVector4` so SIMD/scalar tails never process spare destination capacity. Expanded pixel-format tests to run across hardware intrinsic configurations, added dedicated RGB24/BGR24 bulk conversion coverage with sentinel checks for out-of-range writes, and relaxed associated-alpha half packing assertions to treat NaN payload differences as valid IEEE behavior.
@JimBobSquarePants
JimBobSquarePants merged commit 9a189d3 into main Jul 17, 2026
12 checks passed
@JimBobSquarePants
JimBobSquarePants deleted the js/premultiplied-pixel-formats branch July 17, 2026 07:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant