From f8990a79b8bf93a974c2bfe33c2ad8b0119ef178 Mon Sep 17 00:00:00 2001 From: Kasper Frank Date: Fri, 7 May 2021 11:51:41 +0200 Subject: [PATCH] Add indexed color space for stencils --- .../Images/PngFromPdfImageFactoryTests.cs | 2 +- .../Graphics/Colors/ColorSpaceDetails.cs | 20 +++++++++++++++---- .../Util/ColorSpaceDetailsParser.cs | 7 +++++-- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/UglyToad.PdfPig.Tests/Images/PngFromPdfImageFactoryTests.cs b/src/UglyToad.PdfPig.Tests/Images/PngFromPdfImageFactoryTests.cs index fc16c9b9..76f5dccd 100644 --- a/src/UglyToad.PdfPig.Tests/Images/PngFromPdfImageFactoryTests.cs +++ b/src/UglyToad.PdfPig.Tests/Images/PngFromPdfImageFactoryTests.cs @@ -150,7 +150,7 @@ var decodedBytes = ImageHelpers.LoadFileBytes("ccittfax-decoded.bin"); var image = new TestPdfImage { - ColorSpaceDetails = IndexedColorSpaceDetails.CCITTFaxColorSpaceDetails, + ColorSpaceDetails = IndexedColorSpaceDetails.StencilBlackIs1, DecodedBytes = decodedBytes, WidthInSamples = 1800, HeightInSamples = 3113, diff --git a/src/UglyToad.PdfPig/Graphics/Colors/ColorSpaceDetails.cs b/src/UglyToad.PdfPig/Graphics/Colors/ColorSpaceDetails.cs index 7b613114..8a81cdee 100644 --- a/src/UglyToad.PdfPig/Graphics/Colors/ColorSpaceDetails.cs +++ b/src/UglyToad.PdfPig/Graphics/Colors/ColorSpaceDetails.cs @@ -85,13 +85,25 @@ public class IndexedColorSpaceDetails : ColorSpaceDetails { /// - /// The lossless CCITT compression schemes are used for bitonal black and white images. - /// This is equivalent to an IndexedColorSpaceDetails with a black and white palette. + /// A color space useful for extracting stencil masks as black-and-white images. + /// Index 0 is black and index 1 is white. + /// + internal static readonly IndexedColorSpaceDetails StencilBlackIs0 + = new IndexedColorSpaceDetails(DeviceGrayColorSpaceDetails.Instance, 1, new byte[] { 0, 255 }); + + /// + /// A color space useful for extracting stencil masks as black-and-white images. /// Index 0 is white and index 1 is black. /// - internal static readonly IndexedColorSpaceDetails CCITTFaxColorSpaceDetails + internal static readonly IndexedColorSpaceDetails StencilBlackIs1 = new IndexedColorSpaceDetails(DeviceGrayColorSpaceDetails.Instance, 1, new byte[] { 255, 0 }); + internal static ColorSpaceDetails Stencil(decimal[] decode) + { + return decode.Length >= 2 && decode[0] == 1 && decode[1] == 0 ? + StencilBlackIs1 : StencilBlackIs0 /* default */; + } + /// /// The base color space in which the values in the color table are to be interpreted. /// It can be any device or CIE-based color space or(in PDF 1.3) a Separation or DeviceN space, @@ -119,7 +131,7 @@ HiVal = hiVal; ColorTable = colorTable; BaseType = baseColorSpaceDetails.BaseType; - } + } } /// diff --git a/src/UglyToad.PdfPig/Util/ColorSpaceDetailsParser.cs b/src/UglyToad.PdfPig/Util/ColorSpaceDetailsParser.cs index 0e53c038..17e4130d 100644 --- a/src/UglyToad.PdfPig/Util/ColorSpaceDetailsParser.cs +++ b/src/UglyToad.PdfPig/Util/ColorSpaceDetailsParser.cs @@ -75,9 +75,12 @@ ILookupFilterProvider filterProvider, bool cannotRecurse = false) { - if (filterProvider.GetFilters(imageDictionary).OfType().Any()) + if (imageDictionary.GetDictionaryObject(NameToken.ImageMask, NameToken.Im) != null) { - return IndexedColorSpaceDetails.CCITTFaxColorSpaceDetails; + var decodeRaw = imageDictionary.GetDictionaryObject(NameToken.Decode, NameToken.D) as ArrayToken + ?? new ArrayToken(EmptyArray.Instance); + var decode = decodeRaw.Data.OfType().Select(x => x.Data).ToArray(); + return IndexedColorSpaceDetails.Stencil(decode); } if (!colorSpace.HasValue)