diff --git a/src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs b/src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs index bd99a578aa..427ea15e8c 100644 --- a/src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs +++ b/src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs @@ -7,6 +7,7 @@ using SixLabors.ImageSharp.IO; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing; namespace SixLabors.ImageSharp.Formats.Pbm { @@ -52,6 +53,23 @@ namespace SixLabors.ImageSharp.Formats.Pbm /// Size IImageDecoderInternals.Dimensions => this.PixelSize; + private bool NeedsUpscaling + { + get + { + bool needsUpscaling = false; + if (this.ColorType != PbmColorType.BlackAndWhite) + { + if (this.MaxPixelValue is not 255 and not 65535) + { + needsUpscaling = true; + } + } + + return needsUpscaling; + } + } + /// public Image Decode(BufferedReadStream stream, CancellationToken cancellationToken) where TPixel : unmanaged, IPixel @@ -63,6 +81,10 @@ namespace SixLabors.ImageSharp.Formats.Pbm Buffer2D pixels = image.GetRootFramePixelBuffer(); this.ProcessPixels(stream, pixels); + if (this.NeedsUpscaling) + { + this.ProcessUpscaling(image); + } return image; } @@ -165,5 +187,13 @@ namespace SixLabors.ImageSharp.Formats.Pbm PlainDecoder.Process(this.Configuration, pixels, stream, this.ColorType, this.MaxPixelValue); } } + + private void ProcessUpscaling(Image image) + where TPixel : unmanaged, IPixel + { + int maxAllocationValue = (this.MaxPixelValue > 255) ? 65535 : 255; + float factor = maxAllocationValue / this.MaxPixelValue; + image.Mutate(x => x.Brightness(factor)); + } } } diff --git a/tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs index 479db2ca57..8b8e1a08ff 100644 --- a/tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs @@ -74,10 +74,13 @@ namespace SixLabors.ImageSharp.Tests.Formats.Pbm } [Theory] + [WithFile(BlackAndWhitePlain, PixelTypes.L8, true)] [WithFile(BlackAndWhiteBinary, PixelTypes.L8, true)] + [WithFile(GrayscalePlain, PixelTypes.L8, true)] [WithFile(GrayscalePlainNormalized, PixelTypes.L8, true)] [WithFile(GrayscaleBinary, PixelTypes.L8, true)] [WithFile(GrayscaleBinaryWide, PixelTypes.L16, true)] + [WithFile(RgbPlain, PixelTypes.Rgb24, false)] [WithFile(RgbPlainNormalized, PixelTypes.Rgb24, false)] [WithFile(RgbBinary, PixelTypes.Rgb24, false)] public void DecodeReferenceImage(TestImageProvider provider, bool isGrayscale) diff --git a/tests/ImageSharp.Tests/Formats/Pbm/PbmRoundTripTests.cs b/tests/ImageSharp.Tests/Formats/Pbm/PbmRoundTripTests.cs index cba75b2a01..9521ee7e94 100644 --- a/tests/ImageSharp.Tests/Formats/Pbm/PbmRoundTripTests.cs +++ b/tests/ImageSharp.Tests/Formats/Pbm/PbmRoundTripTests.cs @@ -17,6 +17,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Pbm [InlineData(BlackAndWhitePlain)] [InlineData(BlackAndWhiteBinary)] [InlineData(GrayscalePlain)] + [InlineData(GrayscalePlainNormalized)] [InlineData(GrayscaleBinary)] public void PbmGrayscaleImageCanRoundTrip(string imagePath) { @@ -36,6 +37,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Pbm [Theory] [InlineData(RgbPlain)] + [InlineData(RgbPlainNormalized)] [InlineData(RgbBinary)] public void PbmColorImageCanRoundTrip(string imagePath) { diff --git a/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_L8_grayscale_plain.png b/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_L8_grayscale_plain.png new file mode 100644 index 0000000000..9c86c2fc10 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_L8_grayscale_plain.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f8e8b8a1a05e76b1eeb577373c3a6f492e356f0dd58489afded248415cec4a07 +size 145 diff --git a/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_Rgb24_rgb_plain.png b/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_Rgb24_rgb_plain.png new file mode 100644 index 0000000000..421a598493 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_Rgb24_rgb_plain.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c44322c4bf461acea27053057f5241afb029d9a1e66e94dcf1be6f86f7f97727 +size 152