Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for B&W pbm images with width not dividable by 8 #2481

Merged
merged 6 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions src/ImageSharp/Formats/Pbm/BinaryDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ private static void ProcessBlackAndWhite<TPixel>(Configuration configuration, Bu
{
int width = pixels.Width;
int height = pixels.Height;
int startBit = 0;
MemoryAllocator allocator = configuration.MemoryAllocator;
using IMemoryOwner<L8> row = allocator.Allocate<L8>(width);
Span<L8> rowSpan = row.GetSpan();
Expand All @@ -162,23 +161,12 @@ private static void ProcessBlackAndWhite<TPixel>(Configuration configuration, Bu
for (int x = 0; x < width;)
{
int raw = stream.ReadByte();
int bit = startBit;
startBit = 0;
for (; bit < 8; bit++)
int stopBit = Math.Min(8, width - x);
ynse01 marked this conversation as resolved.
Show resolved Hide resolved
for (int bit = 0; bit < stopBit; bit++)
{
bool bitValue = (raw & (0x80 >> bit)) != 0;
rowSpan[x] = bitValue ? black : white;
x++;
if (x == width)
{
startBit = (bit + 1) & 7; // Round off to below 8.
if (startBit != 0)
{
stream.Seek(-1, System.IO.SeekOrigin.Current);
}

break;
}
}
}

Expand Down
40 changes: 22 additions & 18 deletions src/ImageSharp/Formats/Pbm/BinaryEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,40 @@ public static void WritePixels<TPixel>(Configuration configuration, Stream strea
{
WriteGrayscale(configuration, stream, image);
}
else
else if (componentType == PbmComponentType.Short)
{
WriteWideGrayscale(configuration, stream, image);
}
else
{
throw new ImageFormatException("Component type not supported for Grayscale PBM.");
}
}
else if (colorType == PbmColorType.Rgb)
{
if (componentType == PbmComponentType.Byte)
{
WriteRgb(configuration, stream, image);
}
else
else if (componentType == PbmComponentType.Short)
{
WriteWideRgb(configuration, stream, image);
}
else
{
throw new ImageFormatException("Component type not supported for Color PBM.");
}
}
else
{
WriteBlackAndWhite(configuration, stream, image);
if (componentType == PbmComponentType.Bit)
{
WriteBlackAndWhite(configuration, stream, image);
}
else
{
throw new ImageFormatException("Component type not supported for Black & White PBM.");
}
}
}

Expand Down Expand Up @@ -164,8 +179,6 @@ private static void WriteBlackAndWhite<TPixel>(Configuration configuration, Stre
using IMemoryOwner<L8> row = allocator.Allocate<L8>(width);
Span<L8> rowSpan = row.GetSpan();

int previousValue = 0;
int startBit = 0;
for (int y = 0; y < height; y++)
{
Span<TPixel> pixelSpan = pixelBuffer.DangerousGetRowSpan(y);
Expand All @@ -177,28 +190,19 @@ private static void WriteBlackAndWhite<TPixel>(Configuration configuration, Stre

for (int x = 0; x < width;)
{
int value = previousValue;
for (int i = startBit; i < 8; i++)
int value = 0;
int stopBit = Math.Min(8, width - x);
for (int i = 0; i < stopBit; i++)
{
if (rowSpan[x].PackedValue < 128)
{
value |= 0x80 >> i;
}

x++;
if (x == width)
{
previousValue = value;
startBit = (i + 1) & 7; // Round off to below 8.
break;
}
}

if (startBit == 0)
{
stream.WriteByte((byte)value);
previousValue = 0;
}
stream.WriteByte((byte)value);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void ImageLoadRgb24CanDecode(string imagePath)
[Theory]
[WithFile(BlackAndWhitePlain, PixelTypes.L8, "pbm")]
[WithFile(BlackAndWhiteBinary, PixelTypes.L8, "pbm")]
[WithFile(Issue2477, PixelTypes.L8, "pbm")]
[WithFile(GrayscalePlain, PixelTypes.L8, "pgm")]
[WithFile(GrayscalePlainNormalized, PixelTypes.L8, "pgm")]
[WithFile(GrayscaleBinary, PixelTypes.L8, "pgm")]
Expand Down
6 changes: 6 additions & 0 deletions tests/ImageSharp.Tests/Formats/Pbm/PbmEncoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class PbmEncoderTests
{
{ BlackAndWhiteBinary, PbmColorType.BlackAndWhite },
{ BlackAndWhitePlain, PbmColorType.BlackAndWhite },
{ Issue2477, PbmColorType.BlackAndWhite },
{ GrayscaleBinary, PbmColorType.Grayscale },
{ GrayscaleBinaryWide, PbmColorType.Grayscale },
{ GrayscalePlain, PbmColorType.Grayscale },
Expand Down Expand Up @@ -96,6 +97,11 @@ public void PbmEncoder_P1_Works<TPixel>(TestImageProvider<TPixel> provider)
public void PbmEncoder_P4_Works<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> => TestPbmEncoderCore(provider, PbmColorType.BlackAndWhite, PbmEncoding.Binary);

[Theory]
[WithFile(Issue2477, PixelTypes.Rgb24)]
public void PbmEncoder_P4_Irregular_Works<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> => TestPbmEncoderCore(provider, PbmColorType.BlackAndWhite, PbmEncoding.Binary);

[Theory]
[WithFile(GrayscalePlainMagick, PixelTypes.Rgb24)]
public void PbmEncoder_P2_Works<TPixel>(TestImageProvider<TPixel> provider)
Expand Down
1 change: 1 addition & 0 deletions tests/ImageSharp.Tests/TestImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,5 +1038,6 @@ public static class Pbm
public const string RgbPlain = "Pbm/rgb_plain.ppm";
public const string RgbPlainNormalized = "Pbm/rgb_plain_normalized.ppm";
public const string RgbPlainMagick = "Pbm/rgb_plain_magick.ppm";
public const string Issue2477 = "Pbm/issue2477.pbm";
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions tests/Images/Input/Pbm/issue2477.pbm
Git LFS file not shown