From 764452a6aa334a87686ea0ed6e9faad33b3383e9 Mon Sep 17 00:00:00 2001 From: red Date: Sat, 1 Apr 2023 09:57:09 +0300 Subject: [PATCH 1/2] Fix incorrect comments --- src/ImageSharp/Image.FromFile.cs | 2 +- src/ImageSharp/Image.FromStream.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/Image.FromFile.cs b/src/ImageSharp/Image.FromFile.cs index 24a737493..884acf7a4 100644 --- a/src/ImageSharp/Image.FromFile.cs +++ b/src/ImageSharp/Image.FromFile.cs @@ -81,7 +81,7 @@ public abstract partial class Image /// A return value indicates whether the operation succeeded. /// /// The image file to open and to read the header from. - /// if the information can be read; otherwise, + /// The . /// The path is null. /// The file stream is not readable or the image format is not supported. /// The encoded image contains invalid content. diff --git a/src/ImageSharp/Image.FromStream.cs b/src/ImageSharp/Image.FromStream.cs index 1748ca00e..63f9e64f6 100644 --- a/src/ImageSharp/Image.FromStream.cs +++ b/src/ImageSharp/Image.FromStream.cs @@ -29,7 +29,7 @@ public abstract partial class Image /// /// The general decoder options. /// The image stream to read the header from. - /// if a match is found; otherwise, + /// The . /// The options are null. /// The stream is null. /// The stream is not readable or the image format is not supported. @@ -79,7 +79,7 @@ public abstract partial class Image /// Reads the raw image information from the specified stream without fully decoding it. /// /// The image stream to read the header from. - /// if the information can be read; otherwise, + /// The . /// The stream is null. /// The stream is not readable or the image format is not supported. /// The encoded image contains invalid content. From 32859b87fdee440f10e5978a78e731a9f142482d Mon Sep 17 00:00:00 2001 From: br3aker Date: Sun, 2 Apr 2023 19:20:09 +0300 Subject: [PATCH 2/2] Fixed invalid jpeg buffer width compliment for scalar color converters --- .../Decoder/SpectralConverter{TPixel}.cs | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index d6250127f..5add4c6f0 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -184,34 +184,39 @@ internal class SpectralConverter : SpectralConverter, IDisposable MemoryAllocator allocator = this.Configuration.MemoryAllocator; - // color converter from RGB to TPixel + // Color converter from RGB to TPixel JpegColorConverterBase converter = this.GetColorConverter(this.frame, this.jpegData); this.colorConverter = converter; - // resulting image size + // Resulting image size Size pixelSize = CalculateResultingImageSize(this.frame.PixelSize, this.targetSize, out int blockPixelSize); - // iteration data + // Iteration data int majorBlockWidth = this.frame.Components.Max((component) => component.SizeInBlocks.Width); int majorVerticalSamplingFactor = this.frame.Components.Max((component) => component.SamplingFactors.Height); this.pixelRowsPerStep = majorVerticalSamplingFactor * blockPixelSize; - // pixel buffer for resulting image + // Pixel buffer for resulting image this.pixelBuffer = allocator.Allocate2D( pixelSize.Width, pixelSize.Height, this.Configuration.PreferContiguousImageBuffers); this.paddedProxyPixelRow = allocator.Allocate(pixelSize.Width + 3); - // component processors from spectral to RGB + // Component processors from spectral to RGB int bufferWidth = majorBlockWidth * blockPixelSize; - int batchSize = converter.ElementsPerBatch; - int batchRemainder = bufferWidth & (batchSize - 1); - Size postProcessorBufferSize = new(bufferWidth + (batchSize - batchRemainder), this.pixelRowsPerStep); + + // Converters process pixels in batches and require target buffer size to be divisible by a batch size + // Corner case: image size including jpeg padding is already divisible by a batch size or remainder == 0 + int elementsPerBatch = converter.ElementsPerBatch; + int batchRemainder = bufferWidth & (elementsPerBatch - 1); + int widthComplementaryValue = batchRemainder == 0 ? 0 : elementsPerBatch - batchRemainder; + + Size postProcessorBufferSize = new(bufferWidth + widthComplementaryValue, this.pixelRowsPerStep); this.componentProcessors = this.CreateComponentProcessors(this.frame, this.jpegData, blockPixelSize, postProcessorBufferSize); - // single 'stride' rgba32 buffer for conversion between spectral and TPixel + // Single 'stride' rgba32 buffer for conversion between spectral and TPixel this.rgbBuffer = allocator.Allocate(pixelSize.Width * 3); }