saucecontrol / PhotoSauce

MagicScaler high-performance, high-quality image processing pipeline for .NET
http://photosauce.net/
MIT License
587 stars 49 forks source link

Buffer insufficient error when decoding WEBP image. #167

Open shravan2x opened 1 day ago

shravan2x commented 1 day ago

The issue occurs only when the Width and Height settings are used. It succeeds otherwise. Libwebp works correctly, but Windows default codecs fail.

Repro:

byte[] fileBytes = File.ReadAllBytes("photo-1517022812141-23620dba5c23.webp");

using MemoryStream processedImageMemoryStream = new MemoryStream();
ProcessImageSettings processImageSettings = new ProcessImageSettings()
{
    Width = 1500,
    Height = 0,
    ResizeMode = CropScaleMode.Max,
    Interpolation = InterpolationSettings.Lanczos,
    EncoderOptions = new JpegEncoderOptions() { Quality = 95, SuppressApp0 = true }
};
MagicImageProcessor.ProcessImage(fileBytes, processedImageMemoryStream, processImageSettings);

Image that causes the issue (zipped as github didn't allow uploading .webp files directly) photo-1517022812141-23620dba5c23.zip

Stack trace:

System.Runtime.InteropServices.COMException (0x88982F8C): The buffer allocated is insufficient. (0x88982F8C)
   at PhotoSauce.MagicScaler.WicFramePixelSource.CopyPixelsInternal(PixelArea& prc, Int32 cbStride, Int32 cbBufferSize, Byte* pbBuffer)
   at PhotoSauce.MagicScaler.Transforms.ConversionTransform.copyPixelsDirect(PixelArea& prc, Int32 cbStride, Byte* pbBuffer)
   at PhotoSauce.MagicScaler.PixelSource.CopyPixels(PixelArea& prc, Int32 cbStride, Int32 cbBufferSize, Byte* pbBuffer)
   at PhotoSauce.MagicScaler.Transforms.ConvolutionTransform`3.CopyPixelsInternal(PixelArea& prc, Int32 cbStride, Int32 cbBufferSize, Byte* pbBuffer)
   at PhotoSauce.MagicScaler.Transforms.ConvolutionTransform`3.CopyPixelsInternal(PixelArea& prc, Int32 cbStride, Int32 cbBufferSize, Byte* pbBuffer)
   at PhotoSauce.MagicScaler.Transforms.ConversionTransform.copyPixelsBuffered(PixelArea& prc, Int32 cbStride, Byte* pbBuffer)
   at PhotoSauce.MagicScaler.PixelSource.CopyPixels(PixelArea& prc, Int32 cbStride, Int32 cbBufferSize, Byte* pbBuffer)
   at PhotoSauce.Interop.Wic.IWICBitmapSourceImpl.copyPixels(IWICBitmapSource* pinst, WICRect* prc, UInt32 cbStride, UInt32 cbBufferSize, Byte* pbBuffer)
   at PhotoSauce.MagicScaler.WicImageEncoder.writeSource(IWICBitmapFrameEncode* frame, PixelSource src, PixelArea area)
   at PhotoSauce.MagicScaler.WicImageEncoder.WriteFrame(IPixelSource source, IMetadataSource meta, Rectangle area)
   at PhotoSauce.MagicScaler.MagicImageProcessor.WriteOutput(PipelineContext ctx, Stream ostm)
   at PhotoSauce.MagicScaler.MagicImageProcessor.ProcessImage(ReadOnlySpan`1 imgBuffer, Stream outStream, ProcessImageSettings settings)
saucecontrol commented 13 hours ago

Thanks for the report. I see the same when using the WIC codecs. Worth noting it does not repro when using the libwebp plugin.

Edit: Oops, I missed your comment about libwebp working 😅. That'll be the workaround for now, but I'll see what's up with WIC.