-
Notifications
You must be signed in to change notification settings - Fork 1
/
PDFCompare.cs
186 lines (165 loc) · 6.25 KB
/
PDFCompare.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#region
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Apitron.PDF.Rasterizer;
using Apitron.PDF.Rasterizer.Configuration;
using Rectangle = System.Drawing.Rectangle;
#endregion
namespace PDFCompare
{
internal static class ImageComparer
{
/// <summary>
/// Number of errors indicating comparison should stop
/// </summary>
public const int ErrorLimit = 50;
private const int AreaPixelThreshold = 4;
/// <summary>
/// Represents comparizon result in a organized form
/// </summary>
internal class DifferenceMap
{
private readonly IList<Rectangle> _areas;
private Rectangle _lastErrorArea;
private Rectangle _catchArea;
public DifferenceMap()
{
_areas = new List<Rectangle>();
_lastErrorArea = _catchArea = new Rectangle();
}
public IEnumerable<Rectangle> Areas
{
get { return _areas; }
}
public void RollupErrors()
{
if (!_lastErrorArea.IsEmpty)
_areas.Add(_lastErrorArea);
}
//Gets number of records in difference map
public int Size
{
get { return _areas.Count; }
}
public void AddPixel(int x, int y)
{
if (_catchArea.Contains(x, y))
{
if (!_lastErrorArea.Contains(x, y)) // expand error area
{
_lastErrorArea = Rectangle.Union(_lastErrorArea, new Rectangle(x, y, 1, 1));
UpdateCatchArea();
}
}
else
{
if (!_lastErrorArea.IsEmpty)
{
_areas.Add(_lastErrorArea);
}
_lastErrorArea = new Rectangle(x, y, 1, 1);
UpdateCatchArea();
}
}
private void UpdateCatchArea()
{
_catchArea = _lastErrorArea;
if (!_catchArea.IsEmpty)
_catchArea.Inflate(AreaPixelThreshold, 1);
}
}
/// <summary>
/// Compares generated image with the image stored in master file.
/// In case of difference writes comparison map as <actualFileName>.compare.png
/// </summary>
public static void CompareImages(string masterFileName, string actualFileName)
{
Bitmap master;
try
{
master = (Bitmap) Image.FromFile(masterFileName);
}
catch(FileNotFoundException ex)
{
Console.WriteLine("Master file {0} was not found", masterFileName);
return ;
}
var actual = (Bitmap) Image.FromFile(actualFileName);
DifferenceMap diff;
var areIdentical = CompareImages(master, actual, 0, out diff);
if (areIdentical) return;
var diffMapFileName = actualFileName + ".compared.png";
if (File.Exists(diffMapFileName))
File.Delete(diffMapFileName);
var diffImage = (Image) actual.Clone();
using (var diffMapGraphics = Graphics.FromImage(diffImage))
using (var diffAreaPen = new Pen(Color.FromArgb(128, Color.Red)))
{
foreach (var diffArea in diff.Areas)
{
diffMapGraphics.DrawRectangle(diffAreaPen, diffArea);
}
}
diffImage.Save(diffMapFileName, ImageFormat.Png);
}
/// <summary>
/// Two images comparer
/// </summary>
/// <returns> Gets true if images are equal </returns>
public static bool CompareImages(Bitmap expected, Bitmap actual, int errorsLimit, out DifferenceMap diffMap)
{
diffMap = CompareImages(expected, actual, errorsLimit);
return diffMap.Size == 0;
}
private static DifferenceMap CompareImages(Bitmap reference, Bitmap output, int errorsLimit)
{
var diffMap = new DifferenceMap();
//TODO: Check if output images has the same size
if ((int) reference.Width * (int) reference.Height > 0)
{
for (var y = 0; y < (int) reference.Height; y++)
{
for (var x = 0; x < (int) reference.Width; x++)
{
if (reference.GetPixel(x, y) == output.GetPixel(x, y)) continue;
diffMap.AddPixel(x, y);
}
if (errorsLimit > 0 && diffMap.Size > errorsLimit) break;
}
diffMap.RollupErrors();
}
return diffMap;
}
/// <summary>
/// Renders the sample (pdf file) from folder specified and compares it master image located in the same folder
/// In case of difference, writes comparison result to output folder
/// </summary>
public static void ImageComparator(string folder, string sample, Size resolution)
{
var sampleFile = Path.Combine(folder, sample );
var outputFile = Path.Combine(folder, sample + ".png");
var masterFile = Path.Combine(folder, sample + ".master.png");
using (var fs = new FileStream(sampleFile, FileMode.Open))
{
// it could be password protected
var document = new Document(fs);
for (var i = 0; i < document.Pages.Count; i++)
{
Page currentPage = document.Pages[0];
currentPage.Render(resolution.Width, resolution.Height, new RenderingSettings()).Save(outputFile, ImageFormat.Png);
CompareImages(masterFile, outputFile);
}
}
}
}
internal class PDFCompare
{
private static void Main(string[] args)
{
ImageComparer.ImageComparator(@"C:\", "3BigPreview.pdf", new Size(1200,1600));
}
}
}