Browse Source

Scale pixel value up to full allocation range

pull/1851/head
Ynse Hoornenborg 4 years ago
parent
commit
c4cf012ac4
  1. 30
      src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs
  2. 3
      tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs
  3. 2
      tests/ImageSharp.Tests/Formats/Pbm/PbmRoundTripTests.cs
  4. 3
      tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_L8_grayscale_plain.png
  5. 3
      tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_Rgb24_rgb_plain.png

30
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
/// <inheritdoc/>
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;
}
}
/// <inheritdoc/>
public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel<TPixel>
@ -63,6 +81,10 @@ namespace SixLabors.ImageSharp.Formats.Pbm
Buffer2D<TPixel> 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<TPixel>(Image<TPixel> image)
where TPixel : unmanaged, IPixel<TPixel>
{
int maxAllocationValue = (this.MaxPixelValue > 255) ? 65535 : 255;
float factor = maxAllocationValue / this.MaxPixelValue;
image.Mutate(x => x.Brightness(factor));
}
}
}

3
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<TPixel>(TestImageProvider<TPixel> provider, bool isGrayscale)

2
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)
{

3
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

3
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
Loading…
Cancel
Save