Browse Source

Various review comments

pull/1851/head
Ynse Hoornenborg 4 years ago
parent
commit
87ce4e53e2
  1. 1
      src/ImageSharp/Formats/Pbm/PbmEncoderCore.cs
  2. 10
      src/ImageSharp/Formats/Pbm/PlainEncoder.cs
  3. 1
      tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs
  4. 24
      tests/ImageSharp.Tests/Formats/Pbm/PbmRoundTripTests.cs
  5. 65
      tests/ImageSharp.Tests/Formats/Pbm/PbmTestUtils.cs

1
src/ImageSharp/Formats/Pbm/PbmEncoderCore.cs

@ -88,6 +88,7 @@ namespace SixLabors.ImageSharp.Formats.Pbm
if (this.colorType != PbmColorType.BlackAndWhite)
{
this.maxPixelValue = this.options.MaxPixelValue ?? metadata.MaxPixelValue;
this.maxPixelValue = Math.Max(this.maxPixelValue, PbmConstants.MaxLength);
}
}

10
src/ImageSharp/Formats/Pbm/PlainEncoder.cs

@ -76,7 +76,7 @@ namespace SixLabors.ImageSharp.Formats.Pbm
MemoryAllocator allocator = configuration.MemoryAllocator;
using IMemoryOwner<L8> row = allocator.Allocate<L8>(width);
Span<L8> rowSpan = row.GetSpan();
IMemoryOwner<byte> plainMemory = allocator.Allocate<byte>(width * MaxCharsPerPixelGrayscale);
using IMemoryOwner<byte> plainMemory = allocator.Allocate<byte>(width * MaxCharsPerPixelGrayscale);
Span<byte> plainSpan = plainMemory.GetSpan();
for (int y = 0; y < height; y++)
@ -108,7 +108,7 @@ namespace SixLabors.ImageSharp.Formats.Pbm
MemoryAllocator allocator = configuration.MemoryAllocator;
using IMemoryOwner<L16> row = allocator.Allocate<L16>(width);
Span<L16> rowSpan = row.GetSpan();
IMemoryOwner<byte> plainMemory = allocator.Allocate<byte>(width * MaxCharsPerPixelGrayscaleWide);
using IMemoryOwner<byte> plainMemory = allocator.Allocate<byte>(width * MaxCharsPerPixelGrayscaleWide);
Span<byte> plainSpan = plainMemory.GetSpan();
for (int y = 0; y < height; y++)
@ -140,7 +140,7 @@ namespace SixLabors.ImageSharp.Formats.Pbm
MemoryAllocator allocator = configuration.MemoryAllocator;
using IMemoryOwner<Rgb24> row = allocator.Allocate<Rgb24>(width);
Span<Rgb24> rowSpan = row.GetSpan();
IMemoryOwner<byte> plainMemory = allocator.Allocate<byte>(width * MaxCharsPerPixelRgb);
using IMemoryOwner<byte> plainMemory = allocator.Allocate<byte>(width * MaxCharsPerPixelRgb);
Span<byte> plainSpan = plainMemory.GetSpan();
for (int y = 0; y < height; y++)
@ -178,7 +178,7 @@ namespace SixLabors.ImageSharp.Formats.Pbm
MemoryAllocator allocator = configuration.MemoryAllocator;
using IMemoryOwner<Rgb48> row = allocator.Allocate<Rgb48>(width);
Span<Rgb48> rowSpan = row.GetSpan();
IMemoryOwner<byte> plainMemory = allocator.Allocate<byte>(width * MaxCharsPerPixelRgbWide);
using IMemoryOwner<byte> plainMemory = allocator.Allocate<byte>(width * MaxCharsPerPixelRgbWide);
Span<byte> plainSpan = plainMemory.GetSpan();
for (int y = 0; y < height; y++)
@ -216,7 +216,7 @@ namespace SixLabors.ImageSharp.Formats.Pbm
MemoryAllocator allocator = configuration.MemoryAllocator;
using IMemoryOwner<L8> row = allocator.Allocate<L8>(width);
Span<L8> rowSpan = row.GetSpan();
IMemoryOwner<byte> plainMemory = allocator.Allocate<byte>(width * MaxCharsPerPixelBlackAndWhite);
using IMemoryOwner<byte> plainMemory = allocator.Allocate<byte>(width * MaxCharsPerPixelBlackAndWhite);
Span<byte> plainSpan = plainMemory.GetSpan();
for (int y = 0; y < height; y++)

1
tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs

@ -84,6 +84,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Pbm
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> image = provider.GetImage();
image.DebugSave(provider);
image.CompareToReferenceOutput(provider, grayscale: isGrayscale);
}

24
tests/ImageSharp.Tests/Formats/Pbm/RoundTripTests.cs → tests/ImageSharp.Tests/Formats/Pbm/PbmRoundTripTests.cs

@ -11,8 +11,28 @@ using static SixLabors.ImageSharp.Tests.TestImages.Pbm;
namespace SixLabors.ImageSharp.Tests.Formats.Pbm
{
[Trait("Format", "Pbm")]
public class RoundTripTests
public class PbmRoundTripTests
{
[Theory]
[InlineData(BlackAndWhiteBinary)]
[InlineData(GrayscalePlain)]
[InlineData(GrayscaleBinary)]
public void PbmGrayscaleImageCanRoundTrip(string imagePath)
{
// Arrange
var testFile = TestFile.Create(imagePath);
using var stream = new MemoryStream(testFile.Bytes, false);
// Act
using var originalImage = Image.Load(stream);
Image<Rgb24> colorImage = originalImage.CloneAs<Rgb24>();
using Image<Rgb24> encodedImage = this.RoundTrip(colorImage);
// Assert
Assert.NotNull(encodedImage);
ImageComparer.Exact.VerifySimilarity(colorImage, encodedImage);
}
[Theory]
[InlineData(RgbPlain)]
[InlineData(RgbBinary)]
@ -37,7 +57,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Pbm
using var decodedStream = new MemoryStream();
originalImage.SaveAsPbm(decodedStream);
decodedStream.Seek(0, SeekOrigin.Begin);
var encodedImage = (Image<TPixel>)Image.Load(decodedStream);
var encodedImage = Image.Load<TPixel>(decodedStream);
return encodedImage;
}
}

65
tests/ImageSharp.Tests/Formats/Pbm/PbmTestUtils.cs

@ -1,65 +0,0 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.IO;
using ImageMagick;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
using Xunit;
namespace SixLabors.ImageSharp.Tests.Formats.Pbm
{
public static class PbmTestUtils
{
public static void CompareWithReferenceDecoder<TPixel>(
TestImageProvider<TPixel> provider,
Image<TPixel> image,
bool useExactComparer = true,
float compareTolerance = 0.01f)
where TPixel : unmanaged, ImageSharp.PixelFormats.IPixel<TPixel>
{
string path = TestImageProvider<TPixel>.GetFilePathOrNull(provider);
if (path == null)
{
throw new InvalidOperationException("CompareToOriginal() works only with file providers!");
}
var testFile = TestFile.Create(path);
Image<Rgba32> magickImage = DecodeWithMagick<Rgba32>(Configuration.Default, new FileInfo(testFile.FullPath));
if (useExactComparer)
{
ImageComparer.Exact.VerifySimilarity(magickImage, image);
}
else
{
ImageComparer.Tolerant(compareTolerance).VerifySimilarity(magickImage, image);
}
}
public static Image<TPixel> DecodeWithMagick<TPixel>(Configuration configuration, FileInfo fileInfo)
where TPixel : unmanaged, ImageSharp.PixelFormats.IPixel<TPixel>
{
using (var magickImage = new MagickImage(fileInfo))
{
magickImage.AutoOrient();
var result = new Image<TPixel>(configuration, magickImage.Width, magickImage.Height);
Assert.True(result.TryGetSinglePixelSpan(out Span<TPixel> resultPixels));
using (IUnsafePixelCollection<ushort> pixels = magickImage.GetPixelsUnsafe())
{
byte[] data = pixels.ToByteArray(PixelMapping.RGBA);
PixelOperations<TPixel>.Instance.FromRgba32Bytes(
configuration,
data,
resultPixels,
resultPixels.Length);
}
return result;
}
}
}
}
Loading…
Cancel
Save