diff --git a/src/UglyToad.PdfPig/Images/ColorSpaceDetailsByteConverter.cs b/src/UglyToad.PdfPig/Images/ColorSpaceDetailsByteConverter.cs index 93645845..00d91e30 100644 --- a/src/UglyToad.PdfPig/Images/ColorSpaceDetailsByteConverter.cs +++ b/src/UglyToad.PdfPig/Images/ColorSpaceDetailsByteConverter.cs @@ -34,7 +34,7 @@ public static ReadOnlySpan Convert(ColorSpaceDetails details, ReadOnlySpan if (bitsPerComponent != 8) { // Unpack components such that they occupy one byte each - data = UnpackComponents(data, bitsPerComponent); + data = UnpackComponents(data, bitsPerComponent, details.Type); } // Remove padding bytes when the stride width differs from the image width @@ -48,7 +48,7 @@ public static ReadOnlySpan Convert(ColorSpaceDetails details, ReadOnlySpan return details.Transform(data); } - private static Span UnpackComponents(Span input, int bitsPerComponent) + private static Span UnpackComponents(Span input, int bitsPerComponent, ColorSpace colorSpace) { if (bitsPerComponent == 16) // Example with MOZILLA-3136-0.pdf (page 3) { @@ -72,6 +72,24 @@ private static Span UnpackComponents(Span input, int bitsPerComponen int right = (int)Math.Pow(2, bitsPerComponent) - 1; int u = 0; + + // TODO - 1bpc + DeviceGray is required for JBIG2 but needs to be investigated + // Why is this required? This does not belong here imo + if (bitsPerComponent == 1 && colorSpace != ColorSpace.Indexed) + { + foreach (byte b in input) + { + // Enumerate bits in bitsPerComponent-sized chunks from MSB to LSB, masking on the appropriate bits + for (int i = end; i >= 0; --i) + { + unpacked[u++] = (byte)((b >> i) & right) == 1 ? byte.MinValue : byte.MaxValue; + } + } + + return unpacked; + } + + // Default method foreach (byte b in input) { // Enumerate bits in bitsPerComponent-sized chunks from MSB to LSB, masking on the appropriate bits