Enhance pixel formats and associated-alpha operations with optimizations#3154
Merged
Conversation
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.
…int values' Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
…ating point values'" This reverts commit ba4d1df.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Prerequisites
Description
This pull request introduces several improvements and new features to the
ImageSharplibrary, focusing on enhanced AOT (Ahead-Of-Time) compilation support, expanded pixel format handling, and increased alpha representation awareness in theColorstruct. Additionally, a new test project is added to the solution. The most important changes are summarized below.AOT Compiler and Pixel Format Support
Argb32P,Abgr32P,Bgra32P,HalfVector4P,NormalizedByte4P,Rgb96,Rgba128,Rgba32P,RgbaHalf,RgbaHalfP) in both pixel seeding and image cloning withinAotCompilerTools.cs. This ensures these formats are available and optimized for AOT compilation scenarios. [1] [2] [3] [4]ExrEncoder,ExrDecoder, and their core methods in the compilation routines. [1] [2] [3] [4] [5]AutoLevelProcessor,ConvolutionProcessor, andMedianBlurProcessor, to broaden the range of supported image operations in AOT environments. [1] [2]ColorStruct and Alpha Representation EnhancementsColorstruct to explicitly track and expose the alpha representation (Associatedvs.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]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]WithAlphaandToHexmethods to use the correct alpha representation and ensure consistent, precise color manipulation and serialization. [1] [2]Solution and Project Structure
ImageSharp.PublicApi.Testsproject 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
ImageSharpfor both runtime and compile-time scenarios.