forked from danielgtaylor/jpeg-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjpeg-compare.c
194 lines (163 loc) · 5.09 KB
/
jpeg-compare.c
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
187
188
189
190
191
192
193
194
/*
Compare two JPEG images. Several methods are available. PSNR, SSIM,
and MS_SSIM require the images to be exactly the same size, while
FAST works on any sized image.
FAST compares two images and returns the difference on a
scale of 0 - 99, where 0 would mean the images are identical. The
comparison should be immune to exposure, saturation, white balance,
scaling, minor crops and similar modifications.
If the difference is 10 or less than the images are very likely
different versions of the same image (e.g. a thumbnail or black
and white edit) or just slightly different images. It is possible
to get false positives, in which case a slower PSNR or SSIM
comparison will help.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "src/commander.h"
#include "src/edit.h"
#include "src/hash.h"
#include "src/iqa/include/iqa.h"
#include "src/util.h"
// Comparison method
enum METHOD {
UNKNOWN,
FAST,
PSNR,
SSIM,
MS_SSIM
};
int method = FAST;
// Hash size when method is FAST
int size = 16;
// Use PPM input?
int ppm = 0;
static void setSize(command_t *self) {
size = atoi(self->arg);
}
static void setMethod(command_t *self) {
if (!strcmp("fast", self->arg)) {
method = FAST;
} else if (!strcmp("psnr", self->arg)) {
method = PSNR;
} else if (!strcmp("ssim", self->arg)) {
method = SSIM;
} else if (!strcmp("ms-ssim", self->arg)) {
method = MS_SSIM;
} else {
method = UNKNOWN;
}
}
static void setPpm(command_t *self) {
ppm = 1;
}
int compareFast(const char *filename1, const char *filename2) {
unsigned char *hash1, *hash2;
// Generate hashes
if (jpegHash(filename1, &hash1, size)) {
printf("Error hashing image 1!\n");
return 1;
}
if (jpegHash(filename2, &hash2, size)) {
printf("Error hashing image 2!\n");
return 1;
}
// Compare and print out hamming distance
printf("%u\n", hammingDist(hash1, hash2, size * size) * 100 / (size * size));
// Cleanup
free(hash1);
free(hash2);
return 0;
}
int compare(const char *filename1, const char *filename2) {
unsigned char *image1, *image2, *image1Gray = NULL;
int width1, width2, height1, height2;
int format, components;
float diff;
// Set requested pixel format
switch (method) {
case PSNR:
format = JCS_RGB;
components = 3;
break;
case SSIM: case MS_SSIM: default:
format = JCS_GRAYSCALE;
components = 1;
break;
}
// Decode files
if (ppm) {
if (!decodePpmFile(filename1, &image1, &width1, &height1)) {
printf("Error decoding %s\n", filename1);
return 1;
}
if (1 == components) {
grayscale(image1, &image1Gray, width1, height1);
free(image1);
image1 = image1Gray;
}
} else {
if (!decodeJpegFile(filename1, &image1, &width1, &height1, format)) {
printf("Error decoding %s\n", filename1);
return 1;
}
}
if (!decodeJpegFile(filename2, &image2, &width2, &height2, format)) {
printf("Error decoding %s\n", filename2);
return 1;
}
// Ensure width/height are equal
if (width1 != width2 || height1 != height2) {
printf("Images must be identical sizes for selected method!\n");
return 1;
}
// Calculate and print comparison
switch (method) {
case PSNR:
diff = iqa_psnr(image1, image2, width1, height1, width1 * components);
printf("PSNR: %f\n", diff);
break;
case MS_SSIM:
diff = iqa_ms_ssim(image1, image2, width1, height1, width1 * components, 0);
printf("MS-SSIM: %f\n", diff);
break;
case SSIM: default:
diff = iqa_ssim(image1, image2, width1, height1, width1 * components, 0, 0);
printf("SSIM: %f\n", diff);
break;
}
// Cleanup
free(image1);
free(image2);
return 0;
}
int main (int argc, char **argv) {
int error;
// Parse commandline options
command_t cmd;
command_init(&cmd, argv[0], VERSION);
cmd.usage = "[options] image1.jpg image2.jpg";
command_option(&cmd, "-s", "--size [arg]", "Set fast comparison image hash size", setSize);
command_option(&cmd, "-m", "--method [arg]", "Set comparison method to one of 'fast', 'psnr', 'ssim', or 'ms-ssim' [fast]", setMethod);
command_option(&cmd, "-r", "--ppm", "Parse first input as PPM instead of JPEG", setPpm);
command_parse(&cmd, argc, argv);
if (cmd.argc < 2) {
command_help(&cmd);
return 255;
}
// Calculate and print output
switch (method) {
case FAST:
error = compareFast(cmd.argv[0], cmd.argv[1]);
break;
case PSNR: case SSIM: case MS_SSIM:
error = compare(cmd.argv[0], cmd.argv[1]);
break;
default:
printf("Unknown comparison method!\n");
error = 255;
}
command_free(&cmd);
return error;
}