From c0e3d283d81ab4066b93ef74bcf3ce2bc3e25dea Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Thu, 26 Mar 2026 19:18:21 +0100 Subject: [PATCH] Use cancellation token, when decoding exr images --- src/ImageSharp/Formats/Exr/ExrDecoderCore.cs | 14 +++++++++----- src/ImageSharp/Formats/Exr/ExrEncoderCore.cs | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/ImageSharp/Formats/Exr/ExrDecoderCore.cs b/src/ImageSharp/Formats/Exr/ExrDecoderCore.cs index 52a08e495d..555b07bacf 100644 --- a/src/ImageSharp/Formats/Exr/ExrDecoderCore.cs +++ b/src/ImageSharp/Formats/Exr/ExrDecoderCore.cs @@ -115,10 +115,10 @@ internal sealed class ExrDecoderCore : ImageDecoderCore { case ExrPixelType.Half: case ExrPixelType.Float: - this.DecodeFloatingPointPixelData(stream, pixels); + this.DecodeFloatingPointPixelData(stream, pixels, cancellationToken); break; case ExrPixelType.UnsignedInt: - this.DecodeUnsignedIntPixelData(stream, pixels); + this.DecodeUnsignedIntPixelData(stream, pixels, cancellationToken); break; default: ExrThrowHelper.ThrowNotSupported("Pixel type is not supported"); @@ -128,7 +128,7 @@ internal sealed class ExrDecoderCore : ImageDecoderCore return image; } - private void DecodeFloatingPointPixelData(BufferedReadStream stream, Buffer2D pixels) + private void DecodeFloatingPointPixelData(BufferedReadStream stream, Buffer2D pixels, CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { bool hasAlpha = this.HasAlpha(); @@ -168,7 +168,7 @@ internal sealed class ExrDecoderCore : ImageDecoderCore for (int channelIdx = 0; channelIdx < this.Channels.Count; channelIdx++) { ExrChannelInfo channel = this.Channels[channelIdx]; - offset += ReadFloatChannelData(stream, channel, decompressedPixelData.Slice(offset), redPixelData, greenPixelData, bluePixelData, alphaPixelData, width); + offset += ReadFloatChannelData(stream, channel, decompressedPixelData[offset..], redPixelData, greenPixelData, bluePixelData, alphaPixelData, width); } for (int x = 0; x < width; x++) @@ -181,10 +181,12 @@ internal sealed class ExrDecoderCore : ImageDecoderCore } stream.Position = nextRowOffsetPosition; + + cancellationToken.ThrowIfCancellationRequested(); } } - private void DecodeUnsignedIntPixelData(BufferedReadStream stream, Buffer2D pixels) + private void DecodeUnsignedIntPixelData(BufferedReadStream stream, Buffer2D pixels, CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { bool hasAlpha = this.HasAlpha(); @@ -236,6 +238,8 @@ internal sealed class ExrDecoderCore : ImageDecoderCore } stream.Position = nextRowOffsetPosition; + + cancellationToken.ThrowIfCancellationRequested(); } } diff --git a/src/ImageSharp/Formats/Exr/ExrEncoderCore.cs b/src/ImageSharp/Formats/Exr/ExrEncoderCore.cs index ff606b677a..d6736597a9 100644 --- a/src/ImageSharp/Formats/Exr/ExrEncoderCore.cs +++ b/src/ImageSharp/Formats/Exr/ExrEncoderCore.cs @@ -241,7 +241,7 @@ internal sealed class ExrEncoderCore using IMemoryOwner rgbBuffer = this.memoryAllocator.Allocate(width * 3, AllocationOptions.Clean); using IMemoryOwner rowBlockBuffer = this.memoryAllocator.Allocate((int)bytesPerBlock, AllocationOptions.Clean); - Span redBuffer = rgbBuffer.GetSpan().Slice(0, width); + Span redBuffer = rgbBuffer.GetSpan()[..width]; Span greenBuffer = rgbBuffer.GetSpan().Slice(width, width); Span blueBuffer = rgbBuffer.GetSpan().Slice(width * 2, width);