PeachImage
PeachImage.DecoderOptions
DecoderOptions.MaxExpansionRatio Property
The most decoded bytes the image may declare per byte of its file, so a few hundred bytes of PNG, GIF or WebP cannot decode into gigabytes. The ratio is the decoded pixel bytes (width x height x bytes per pixel of the decoded pixel format) divided by the compressed bytes (the length of the stream from the image’s start). Checked from the header, before any pixel memory is allocated; a larger ratio throws ImageTooLargeException. When null (the default) there is no ratio limit. Use MaxExpansionRatios to give formats their own limits.
public System.Nullable<double> MaxExpansionRatio { get; init; }
Property Value
System.Nullable<System.Double>
Remarks
Applies to every format. A non-seekable stream is buffered into memory first, since the ratio needs the length.
The right value depends on the format, which is why one number for everything is a poor fit. Photographic JPEG expands by roughly 3 to 30 times (4 for CMYK), so a ceiling of 100 to 200 is generous. Lossy WebP and AVIF are similar; a few hundred is safe. Lossless formats are different: flat graphics compress extremely well, and this library’s own encoder writes a flat 400x400 RGBA PNG at about 5,500 times its size and a flat 2000x2000 one at about 27,000 times, so a PNG, lossless WebP or GIF ceiling needs to be in the tens of thousands. Combine a tight default with loose entries for the lossless formats through MaxExpansionRatios.