From 6fa223c0dbc4530dfa160fcff232abe08084c93a Mon Sep 17 00:00:00 2001 From: Kasper Frank Date: Mon, 10 May 2021 17:27:08 +0200 Subject: [PATCH] Image data might contain trailing EOL marker --- .../Images/Png/PngFromPdfImageFactory.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/UglyToad.PdfPig/Images/Png/PngFromPdfImageFactory.cs b/src/UglyToad.PdfPig/Images/Png/PngFromPdfImageFactory.cs index 5fe9166c..3ee8b93e 100644 --- a/src/UglyToad.PdfPig/Images/Png/PngFromPdfImageFactory.cs +++ b/src/UglyToad.PdfPig/Images/Png/PngFromPdfImageFactory.cs @@ -2,6 +2,8 @@ { using Content; using Graphics.Colors; + using System.Collections.Generic; + using UglyToad.PdfPig.Core; internal static class PngFromPdfImageFactory { @@ -32,7 +34,18 @@ var builder = PngBuilder.Create(image.WidthInSamples, image.HeightInSamples, false); - var isCorrectlySized = bytesPure.Count == (image.WidthInSamples * image.HeightInSamples * numberOfComponents); + var requiredSize = (image.WidthInSamples * image.HeightInSamples * numberOfComponents); + + var actualSize = bytesPure.Count; + var isCorrectlySized = bytesPure.Count == requiredSize || + // Spec, p. 37: "...error if the stream contains too much data, with the exception that + // there may be an extra end-of-line marker..." + (actualSize == requiredSize + 1 && bytesPure[actualSize - 1] == ReadHelper.AsciiLineFeed) || + (actualSize == requiredSize + 1 && bytesPure[actualSize - 1] == ReadHelper.AsciiCarriageReturn) || + // The combination of a CARRIAGE RETURN followed immediately by a LINE FEED is treated as one EOL marker. + (actualSize == requiredSize + 2 && + bytesPure[actualSize - 2] == ReadHelper.AsciiCarriageReturn && + bytesPure[actualSize - 1] == ReadHelper.AsciiLineFeed); if (!isCorrectlySized) {