mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-10-08 00:14:35 +08:00
Basic unit tests of CCITT Fax filter decoding
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
BIN
src/UglyToad.PdfPig.Tests/Images/Files/ccittfax-decoded.bin
Normal file
BIN
src/UglyToad.PdfPig.Tests/Images/Files/ccittfax-decoded.bin
Normal file
Binary file not shown.
802
src/UglyToad.PdfPig.Tests/Images/Files/ccittfax-encoded.bin
Normal file
802
src/UglyToad.PdfPig.Tests/Images/Files/ccittfax-encoded.bin
Normal file
File diff suppressed because one or more lines are too long
BIN
src/UglyToad.PdfPig.Tests/Images/Files/ccittfax.png
Normal file
BIN
src/UglyToad.PdfPig.Tests/Images/Files/ccittfax.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 522 KiB |
39
src/UglyToad.PdfPig.Tests/Images/ImageHelpers.cs
Normal file
39
src/UglyToad.PdfPig.Tests/Images/ImageHelpers.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user