Browse Source

Use cancellation token, when decoding exr images

pull/3096/head
Brian Popow 2 months ago
parent
commit
c0e3d283d8
  1. 14
      src/ImageSharp/Formats/Exr/ExrDecoderCore.cs
  2. 2
      src/ImageSharp/Formats/Exr/ExrEncoderCore.cs

14
src/ImageSharp/Formats/Exr/ExrDecoderCore.cs

@ -115,10 +115,10 @@ internal sealed class ExrDecoderCore : ImageDecoderCore
{ {
case ExrPixelType.Half: case ExrPixelType.Half:
case ExrPixelType.Float: case ExrPixelType.Float:
this.DecodeFloatingPointPixelData(stream, pixels); this.DecodeFloatingPointPixelData(stream, pixels, cancellationToken);
break; break;
case ExrPixelType.UnsignedInt: case ExrPixelType.UnsignedInt:
this.DecodeUnsignedIntPixelData(stream, pixels); this.DecodeUnsignedIntPixelData(stream, pixels, cancellationToken);
break; break;
default: default:
ExrThrowHelper.ThrowNotSupported("Pixel type is not supported"); ExrThrowHelper.ThrowNotSupported("Pixel type is not supported");
@ -128,7 +128,7 @@ internal sealed class ExrDecoderCore : ImageDecoderCore
return image; return image;
} }
private void DecodeFloatingPointPixelData<TPixel>(BufferedReadStream stream, Buffer2D<TPixel> pixels) private void DecodeFloatingPointPixelData<TPixel>(BufferedReadStream stream, Buffer2D<TPixel> pixels, CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
bool hasAlpha = this.HasAlpha(); bool hasAlpha = this.HasAlpha();
@ -168,7 +168,7 @@ internal sealed class ExrDecoderCore : ImageDecoderCore
for (int channelIdx = 0; channelIdx < this.Channels.Count; channelIdx++) for (int channelIdx = 0; channelIdx < this.Channels.Count; channelIdx++)
{ {
ExrChannelInfo channel = this.Channels[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++) for (int x = 0; x < width; x++)
@ -181,10 +181,12 @@ internal sealed class ExrDecoderCore : ImageDecoderCore
} }
stream.Position = nextRowOffsetPosition; stream.Position = nextRowOffsetPosition;
cancellationToken.ThrowIfCancellationRequested();
} }
} }
private void DecodeUnsignedIntPixelData<TPixel>(BufferedReadStream stream, Buffer2D<TPixel> pixels) private void DecodeUnsignedIntPixelData<TPixel>(BufferedReadStream stream, Buffer2D<TPixel> pixels, CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
bool hasAlpha = this.HasAlpha(); bool hasAlpha = this.HasAlpha();
@ -236,6 +238,8 @@ internal sealed class ExrDecoderCore : ImageDecoderCore
} }
stream.Position = nextRowOffsetPosition; stream.Position = nextRowOffsetPosition;
cancellationToken.ThrowIfCancellationRequested();
} }
} }

2
src/ImageSharp/Formats/Exr/ExrEncoderCore.cs

@ -241,7 +241,7 @@ internal sealed class ExrEncoderCore
using IMemoryOwner<uint> rgbBuffer = this.memoryAllocator.Allocate<uint>(width * 3, AllocationOptions.Clean); using IMemoryOwner<uint> rgbBuffer = this.memoryAllocator.Allocate<uint>(width * 3, AllocationOptions.Clean);
using IMemoryOwner<byte> rowBlockBuffer = this.memoryAllocator.Allocate<byte>((int)bytesPerBlock, AllocationOptions.Clean); using IMemoryOwner<byte> rowBlockBuffer = this.memoryAllocator.Allocate<byte>((int)bytesPerBlock, AllocationOptions.Clean);
Span<uint> redBuffer = rgbBuffer.GetSpan().Slice(0, width); Span<uint> redBuffer = rgbBuffer.GetSpan()[..width];
Span<uint> greenBuffer = rgbBuffer.GetSpan().Slice(width, width); Span<uint> greenBuffer = rgbBuffer.GetSpan().Slice(width, width);
Span<uint> blueBuffer = rgbBuffer.GetSpan().Slice(width * 2, width); Span<uint> blueBuffer = rgbBuffer.GetSpan().Slice(width * 2, width);

Loading…
Cancel
Save