Skip to content

Commit

Permalink
Update UnpackComponents() to account for 1bpc + DeviceGray (hack for …
Browse files Browse the repository at this point in the history
…Jbig2)
  • Loading branch information
BobLd committed Jan 21, 2025
1 parent b7e2245 commit 5209850
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/UglyToad.PdfPig/Images/ColorSpaceDetailsByteConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static ReadOnlySpan<byte> 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
Expand All @@ -48,7 +48,7 @@ public static ReadOnlySpan<byte> Convert(ColorSpaceDetails details, ReadOnlySpan
return details.Transform(data);
}

private static Span<byte> UnpackComponents(Span<byte> input, int bitsPerComponent)
private static Span<byte> UnpackComponents(Span<byte> input, int bitsPerComponent, ColorSpace colorSpace)
{
if (bitsPerComponent == 16) // Example with MOZILLA-3136-0.pdf (page 3)
{
Expand All @@ -72,6 +72,24 @@ private static Span<byte> UnpackComponents(Span<byte> 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
Expand Down

0 comments on commit 5209850

Please sign in to comment.