Basic unit tests of CCITT Fax filter decoding

This commit is contained in:
Kasper Frank
2021-05-06 21:35:17 +02:00
parent 9c2cd89de4
commit 8c6f705d2a
6 changed files with 904 additions and 9 deletions

View File

@@ -0,0 +1,40 @@
namespace UglyToad.PdfPig.Tests.Filters
{
using System.Collections.Generic;
using UglyToad.PdfPig.Filters;
using UglyToad.PdfPig.Tests.Images;
using UglyToad.PdfPig.Tokens;
using Xunit;
public class CcittFaxDecodeFilterTests
{
[Fact]
public void CanDecodeCCittFaxCompressedImageData()
{
var encodedBytes = ImageHelpers.LoadFileBytes("ccittfax-encoded.bin");
var filter = new CcittFaxDecodeFilter();
var dictionary = new Dictionary<NameToken, IToken>
{
{ NameToken.D, new ArrayToken(new []{ new NumericToken(1), new NumericToken(0) })},
{ NameToken.W, new NumericToken(1800) },
{ NameToken.H, new NumericToken(3113) },
{ NameToken.Bpc, new NumericToken(1) },
{ NameToken.F, NameToken.CcittfaxDecode },
{ NameToken.DecodeParms,
new DictionaryToken(new Dictionary<NameToken, IToken>
{
{ NameToken.K, new NumericToken(-1) },
{ NameToken.Columns, new NumericToken(1800) },
{ NameToken.Rows, new NumericToken(3113) },
{ NameToken.BlackIs1, BooleanToken.True }
})
}
};
var expectedBytes = ImageHelpers.LoadFileBytes("ccittfax-decoded.bin");
var decodedBytes = filter.Decode(encodedBytes, new DictionaryToken(dictionary), 0);
Assert.Equal(expectedBytes, decodedBytes);
}
}
}

File diff suppressed because one or more lines are too long

Binary file not shown.

After

Width:  |  Height:  |  Size: 522 KiB

View File

@@ -0,0 +1,39 @@
namespace UglyToad.PdfPig.Tests.Images
{
using System;
using System.IO;
using UglyToad.PdfPig.Images.Png;
public static class ImageHelpers
{
private static readonly string FilesFolder = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "..", "Images", "Files"));
public static byte[] LoadFileBytes(string filename)
{
return File.ReadAllBytes(Path.Combine(FilesFolder, filename));
}
public static bool ImagesAreEqual(byte[] first, byte[] second)
{
var png1 = Png.Open(first);
var png2 = Png.Open(second);
if (png1.Width != png2.Width || png1.Height != png2.Height || png1.HasAlphaChannel != png2.HasAlphaChannel)
{
return false;
}
for (var y = 0; y < png1.Height; y++)
{
for (var x = 0; x < png1.Width; x++)
{
if (!png1.GetPixel(x, y).Equals(png2.GetPixel(x, y)))
{
return false;
}
}
}
return true;
}
}
}

View File

@@ -1,7 +1,5 @@
namespace UglyToad.PdfPig.Tests.Images
{
using System;
using System.IO;
using System.Linq;
using UglyToad.PdfPig.Graphics.Colors;
using UglyToad.PdfPig.Images.Png;
@@ -38,7 +36,7 @@
};
Assert.True(PngFromPdfImageFactory.TryGenerate(image, out var bytes));
Assert.Equal(LoadImage("3x3.png"), bytes);
Assert.True(ImageHelpers.ImagesAreEqual(LoadImage("3x3.png"), bytes));
}
[Fact]
@@ -60,7 +58,7 @@
};
Assert.True(PngFromPdfImageFactory.TryGenerate(image, out var bytes));
Assert.Equal(LoadImage("3x3.png"), bytes);
Assert.True(ImageHelpers.ImagesAreEqual(LoadImage("3x3.png"), bytes));
}
[Fact]
@@ -82,7 +80,7 @@
};
Assert.True(PngFromPdfImageFactory.TryGenerate(image, out var bytes));
Assert.Equal(LoadImage("3x3.png"), bytes);
Assert.True(ImageHelpers.ImagesAreEqual(LoadImage("3x3.png"), bytes));
}
[Fact]
@@ -104,7 +102,7 @@
};
Assert.True(PngFromPdfImageFactory.TryGenerate(image, out var bytes));
Assert.Equal(LoadImage("3x3.png"), bytes);
Assert.True(ImageHelpers.ImagesAreEqual(LoadImage("3x3.png"), bytes));
}
[Fact]
@@ -143,13 +141,29 @@
};
Assert.True(PngFromPdfImageFactory.TryGenerate(image, out var bytes));
Assert.Equal(LoadImage("3x3.png"), bytes);
Assert.True(ImageHelpers.ImagesAreEqual(LoadImage("3x3.png"), bytes));
}
[Fact]
public void CanGeneratePngFromCcittFaxDecodedImageData()
{
var decodedBytes = ImageHelpers.LoadFileBytes("ccittfax-decoded.bin");
var image = new TestPdfImage
{
ColorSpaceDetails = IndexedColorSpaceDetails.CCITTFaxColorSpaceDetails,
DecodedBytes = decodedBytes,
WidthInSamples = 1800,
HeightInSamples = 3113,
BitsPerComponent = 1
};
Assert.True(PngFromPdfImageFactory.TryGenerate(image, out var bytes));
Assert.True(ImageHelpers.ImagesAreEqual(LoadImage("ccittfax.png"), bytes));
}
private static byte[] LoadImage(string name)
{
var folder = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "..", "Images", "Files"));
return File.ReadAllBytes(Path.Combine(folder, name));
return ImageHelpers.LoadFileBytes(name);
}
}
}