@@ -3,6 +3,7 @@ package main
3
3
import (
4
4
"crypto/sha256"
5
5
"errors"
6
+ "flag"
6
7
"fmt"
7
8
"image"
8
9
"image/color"
@@ -13,15 +14,17 @@ import (
13
14
"strconv"
14
15
)
15
16
16
- // Structure for Pixel. Used as float to make operations more easily.
17
+ // Structure for Pixel.
17
18
type Pixel struct {
18
- r ,g ,b ,a float64
19
+ r , g , b , a uint8
20
+ modified bool
19
21
}
20
22
21
- func rgbaToPixel (r uint32 , g uint32 , b uint32 , a uint32 ) Pixel {
22
- return Pixel {float64 (r ), float64 (g ), float64 (b ), float64 (a )}
23
- }
23
+ var rmaster , gmaster , bmaster float64
24
24
25
+ func rgbaToPixel (r uint32 , g uint32 , b uint32 , a uint32 ) Pixel {
26
+ return Pixel {uint8 (r >> 8 ), uint8 (g >> 8 ), uint8 (b >> 8 ), uint8 (a >> 8 ), false }
27
+ }
25
28
26
29
func CreatePNG (PDFPath string ) {
27
30
@@ -60,21 +63,30 @@ func RetrievePixel(fileName string) ([][]Pixel, int, int) {
60
63
61
64
bounds := img .Bounds ()
62
65
width , height := bounds .Max .X , bounds .Max .Y
63
- var pixels [][]Pixel
66
+ pixels := make ( [][]Pixel , bounds . Max . Y )
64
67
for y := bounds .Min .Y ; y < height ; y ++ {
65
- var row []Pixel
68
+ row := make ( []Pixel , bounds . Max . X )
66
69
for x := bounds .Min .X ; x < width ; x ++ {
67
- row = append ( row , rgbaToPixel (img .At (x , y ).RGBA () ))
70
+ row [ x ] = rgbaToPixel (img .At (x , y ).RGBA ())
68
71
}
69
- pixels = append ( pixels , row )
72
+ pixels [ y ] = row
70
73
}
71
74
return pixels , width , height
72
75
}
73
76
74
77
func drawSection (row []Pixel ) {
78
+ alpha := 0.6
79
+ notalpha := float64 (1 - alpha )
80
+
75
81
for i := 0 ; i < len (row ); i ++ {
76
- row [i ].g = row [i ].g * 0.7
77
- row [i ].b = row [i ].b * 0.9
82
+
83
+ if ! row [i ].modified {
84
+ row [i ].r = uint8 (float64 (row [i ].r )* alpha + notalpha * rmaster )
85
+ row [i ].g = uint8 (float64 (row [i ].g )* alpha + notalpha * gmaster )
86
+ row [i ].b = uint8 (float64 (row [i ].b )* alpha + notalpha * bmaster )
87
+ row [i ].modified = true
88
+ }
89
+
78
90
}
79
91
}
80
92
@@ -84,7 +96,7 @@ func CompareSingleImage(path1 string, path2 string, i int) {
84
96
sha2 := ComputeSha256 (path2 )
85
97
86
98
// If the two images have the same hash, the two pages are the same.
87
- if sha1 == sha2 {
99
+ if sha1 == sha2 {
88
100
fmt .Printf ("The pages number %d are the same.\n " , i )
89
101
return
90
102
}
@@ -102,21 +114,23 @@ func CompareSingleImage(path1 string, path2 string, i int) {
102
114
103
115
for y := 0 ; y < len (pixel_1 ); y ++ {
104
116
for x := 0 ; x < len (pixel_1 [y ]); x ++ {
105
- result := compareSinglePixel (pixel_1 [y ][x ], pixel_2 [y ][x ])
106
- if ! result {
107
- drawSection (pixel_3 [y ])
117
+ if ! pixel_3 [y ][x ].modified {
118
+ result := compareSinglePixel (pixel_1 [y ][x ], pixel_2 [y ][x ])
119
+ if ! result {
120
+ drawSection (pixel_3 [y ])
121
+ }
108
122
}
109
123
}
110
124
}
111
125
112
- img := image .NewNRGBA (image .Rect (0 , 0 , x_1 , y_1 ))
126
+ img := image .NewRGBA (image .Rect (0 , 0 , x_1 , y_1 ))
113
127
for y := 0 ; y < y_1 ; y ++ {
114
128
for x := 0 ; x < x_1 ; x ++ {
115
129
img .Set (x , y , color.RGBA {
116
- R : uint8 ( pixel_3 [y ][x ].r ) ,
117
- G : uint8 ( pixel_3 [y ][x ].g ) ,
118
- B : uint8 ( pixel_3 [y ][x ].b ) ,
119
- A : uint8 ( pixel_3 [y ][x ].a ) ,
130
+ R : pixel_3 [y ][x ].r ,
131
+ G : pixel_3 [y ][x ].g ,
132
+ B : pixel_3 [y ][x ].b ,
133
+ A : pixel_3 [y ][x ].a ,
120
134
})
121
135
}
122
136
}
@@ -164,14 +178,16 @@ func ComputeSha256(filePath string) string {
164
178
}
165
179
166
180
func Compare (PDF1 string , PDF2 string ) {
167
- // Compares the two files
181
+ // Compares the two files
168
182
169
183
shaPDF1 := ComputeSha256 (PDF1 )
170
184
shaPDF2 := ComputeSha256 (PDF2 )
171
185
172
- err := os .Mkdir ("generated" , os .ModePerm )
173
- if err != nil {
174
- panic (err )
186
+ if _ , err := os .Stat ("generated" ); errors .Is (err , os .ErrNotExist ) {
187
+ err := os .Mkdir ("generated" , os .ModePerm )
188
+ if err != nil {
189
+ panic (err )
190
+ }
175
191
}
176
192
177
193
i := 1
@@ -182,37 +198,55 @@ func Compare(PDF1 string, PDF2 string) {
182
198
// pdf contains <= 999 pages => 001.. 002.. 003
183
199
184
200
o := fmt .Sprintf ("%d" , k )
185
- s := fmt .Sprintf ("%0" + o + "d" , i )
201
+ s := fmt .Sprintf ("%0" + o + "d" , i )
186
202
187
203
s_pdf1 := shaPDF1 + "/png_gen-" + s + ".png"
188
204
s_pdf2 := shaPDF2 + "/png_gen-" + s + ".png"
189
205
190
206
if _ , err := os .Stat (s_pdf1 ); errors .Is (err , os .ErrNotExist ) {
191
- // TODO: remove this println
192
- fmt .Println ("File " + s_pdf1 + " does not exist." )
193
207
k ++
194
- if k == 12 {
208
+ if k == 12 {
195
209
break
196
210
}
197
211
} else {
198
212
CompareSingleImage (s_pdf1 , s_pdf2 , i )
199
213
i ++
200
214
}
201
-
215
+
202
216
}
217
+ }
218
+
219
+ func hexToRGB (hexcolor string ) {
220
+ // converts a string to rgb values
221
+ values , _ := strconv .ParseUint (hexcolor , 16 , 32 )
222
+ rmaster = float64 (values >> 16 )
223
+ gmaster = float64 ((values >> 8 ) & 0xff )
224
+ bmaster = float64 ((values ) & 0xff )
225
+
226
+ fmt .Printf ("Color chosen: %f %f %f \n " , rmaster , gmaster , bmaster )
203
227
204
228
}
205
229
206
- func main (){
207
- fmt .Println ("pdf-diff: highlights the differences between two pdf files." )
208
- if len (os .Args ) < 2 {
209
- fmt .Println ("You need to specify two parameters!" )
230
+ func main () {
231
+
232
+ // flags
233
+
234
+ color := flag .String ("color" , "ff2010" , "hex value for the background color for highlighting" )
235
+ flag .Parse ()
236
+
237
+ arguments := flag .Args ()
238
+
239
+ if len (arguments ) < 2 {
240
+ fmt .Println ("pdf-diff: highlights the differences between two pdf files." )
241
+ fmt .Println ("Usage: pdf-diff pdf-file-1 pdf-file-2 [-color] hex-color" )
242
+ fmt .Println ()
243
+ flag .PrintDefaults ()
210
244
os .Exit (1 )
211
245
}
212
246
213
- CreatePNG ( os . Args [ 1 ] )
214
- CreatePNG (os . Args [ 2 ])
215
-
216
- Compare (os . Args [ 1 ], os . Args [ 2 ])
247
+ hexToRGB ( * color )
248
+ CreatePNG (arguments [ 0 ])
249
+ CreatePNG ( arguments [ 1 ])
250
+ Compare (arguments [ 0 ], arguments [ 1 ])
217
251
218
252
}
0 commit comments