-
Notifications
You must be signed in to change notification settings - Fork 122
/
jpeg-recompress.c
608 lines (530 loc) · 17.4 KB
/
jpeg-recompress.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/*
Recompress a JPEG file while attempting to keep visual quality the same
by using structural similarity (SSIM) as a metric. Does a binary search
between JPEG quality 40 and 95 to find the best match. Also makes sure
that huffman tables are optimized if they weren't already.
*/
#include <getopt.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "src/edit.h"
#include "src/iqa/include/iqa.h"
#include "src/smallfry.h"
#include "src/util.h"
#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#endif
const char *COMMENT = "Compressed by jpeg-recompress";
// Comparison method
enum METHOD {
UNKNOWN,
SSIM,
MS_SSIM,
SMALLFRY,
MPE
};
int method = SSIM;
// Number of binary search steps
int attempts = 6;
// Target quality (SSIM) value
enum QUALITY_PRESET {
LOW,
MEDIUM,
HIGH,
VERYHIGH
};
float target = 0;
int preset = MEDIUM;
// Min/max JPEG quality
int jpegMin = 40;
int jpegMax = 95;
// Strip metadata from the file?
int strip = 0;
// Disable progressive mode?
int noProgressive = 0;
// Defish the image?
float defishStrength = 0.0;
float defishZoom = 1.0;
// Input format
enum filetype inputFiletype = FILETYPE_AUTO;
// Whether to copy files that cannot be compressed
int copyFiles = 1;
// Whether to favor accuracy over speed
int accurate = 0;
// Chroma subsampling method
int subsample = SUBSAMPLE_DEFAULT;
// Quiet mode (less output)
int quiet = 0;
static enum QUALITY_PRESET parseQuality(const char *s) {
if (!strcmp("low", s))
return LOW;
else if (!strcmp("medium", s))
return MEDIUM;
else if (!strcmp("high", s))
return HIGH;
else if (!strcmp("veryhigh", s))
return VERYHIGH;
error("unknown quality preset: %s", s);
return MEDIUM;
}
static enum METHOD parseMethod(const char *s) {
if (!strcmp("ssim", s))
return SSIM;
else if (!strcmp("ms-ssim", s))
return MS_SSIM;
else if (!strcmp("smallfry", s))
return SMALLFRY;
else if (!strcmp("mpe", s))
return MPE;
return UNKNOWN;
}
static enum filetype parseInputFiletype(const char *s) {
if (!strcmp("auto", s))
return FILETYPE_AUTO;
if (!strcmp("jpeg", s))
return FILETYPE_JPEG;
if (!strcmp("ppm", s))
return FILETYPE_PPM;
return FILETYPE_UNKNOWN;
}
static void setTargetFromPreset() {
switch (method) {
case SSIM:
switch (preset) {
case LOW:
target = 0.999;
break;
case MEDIUM:
target = 0.9999;
break;
case HIGH:
target = 0.99995;
break;
case VERYHIGH:
target = 0.99999;
break;
}
break;
case MS_SSIM:
switch (preset) {
case LOW:
target = 0.85;
break;
case MEDIUM:
target = 0.94;
break;
case HIGH:
target = 0.96;
break;
case VERYHIGH:
target = 0.98;
break;
}
break;
case SMALLFRY:
switch (preset) {
case LOW:
target = 100.75;
break;
case MEDIUM:
target = 102.25;
break;
case HIGH:
target = 103.8;
break;
case VERYHIGH:
target = 105.5;
break;
}
break;
case MPE:
switch (preset) {
case LOW:
target = 1.5;
break;
case MEDIUM:
target = 1.0;
break;
case HIGH:
target = 0.8;
break;
case VERYHIGH:
target = 0.6;
break;
}
break;
}
}
static int parseSubsampling(const char *s) {
if (!strcmp("default", s))
return SUBSAMPLE_DEFAULT;
else if (!strcmp("disable", s))
return SUBSAMPLE_444;
error("unknown sampling method: %s", s);
return SUBSAMPLE_DEFAULT;
}
// Open a file for writing
FILE *openOutput(char *name) {
if (strcmp("-", name) == 0) {
#ifdef _WIN32
setmode(fileno(stdout), O_BINARY);
#endif
return stdout;
} else {
return fopen(name, "wb");
}
}
// Logs an informational message, taking quiet mode into account
void info(const char *format, ...) {
va_list argptr;
if (!quiet) {
va_start(argptr, format);
vfprintf(stderr, format, argptr);
va_end(argptr);
}
}
void usage(void) {
printf("usage: %s [options] input.jpg output.jpg\n\n", progname);
printf("options:\n\n");
printf(" -V, --version output program version\n");
printf(" -h, --help output program help\n");
printf(" -t, --target [arg] set target quality [0.9999]\n");
printf(" -q, --quality [arg] set a quality preset: low, medium, high, veryhigh [medium]\n");
printf(" -n, --min [arg] minimum JPEG quality [40]\n");
printf(" -x, --max [arg] maximum JPEG quality [95]\n");
printf(" -l, --loops [arg] set the number of runs to attempt [6]\n");
printf(" -a, --accurate favor accuracy over speed\n");
printf(" -m, --method [arg] set comparison method to one of 'mpe', 'ssim', 'ms-ssim', 'smallfry' [ssim]\n");
printf(" -s, --strip strip metadata\n");
printf(" -d, --defish [arg] set defish strength [0.0]\n");
printf(" -z, --zoom [arg] set defish zoom [1.0]\n");
printf(" -r, --ppm parse input as PPM\n");
printf(" -c, --no-copy disable copying files that will not be compressed\n");
printf(" -p, --no-progressive disable progressive encoding\n");
printf(" -S, --subsample [arg] set subsampling method to one of 'default', 'disable' [default]\n");
printf(" -T, --input-filetype [arg] set input file type to one of 'auto', 'jpeg', 'ppm' [auto]\n");
printf(" -Q, --quiet only print out errors\n");
}
int main (int argc, char **argv) {
const char *optstring = "Vht:q:n:x:l:am:sd:z:rcpS:T:Q";
static const struct option opts[] = {
{ "version", no_argument, 0, 'V' },
{ "help", no_argument, 0, 'h' },
{ "target", required_argument, 0, 't' },
{ "quality", required_argument, 0, 'q' },
{ "min", required_argument, 0, 'n' },
{ "max", required_argument, 0, 'x' },
{ "loops", required_argument, 0, 'l' },
{ "accurate", no_argument, 0, 'a' },
{ "method", required_argument, 0, 'm' },
{ "strip", no_argument, 0, 's' },
{ "defish", required_argument, 0, 'd' },
{ "zoom", required_argument, 0, 'z' },
{ "ppm", no_argument, 0, 'r' },
{ "no-copy", no_argument, 0, 'c' },
{ "no-progressive", no_argument, 0, 'p' },
{ "subsample", required_argument, 0, 'S' },
{ "input-filetype", required_argument, 0, 'T' },
{ "quiet", no_argument, 0, 'Q' },
{ 0, 0, 0, 0 }
};
int opt, longind = 0;
progname = "jpeg-recompress";
while ((opt = getopt_long(argc, argv, optstring, opts, &longind)) != -1) {
switch (opt) {
case 'V':
version();
return 0;
case 'h':
usage();
return 0;
case 't':
target = atof(optarg);
break;
case 'q':
preset = parseQuality(optarg);
break;
case 'n':
jpegMin = atoi(optarg);
break;
case 'x':
jpegMax = atoi(optarg);
break;
case 'l':
attempts = atoi(optarg);
break;
case 'a':
accurate = 1;
break;
case 'm':
method = parseMethod(optarg);
break;
case 's':
strip = 1;
break;
case 'd':
defishStrength = atof(optarg);
break;
case 'z':
defishZoom = atof(optarg);
break;
case 'r':
inputFiletype = FILETYPE_PPM;
break;
case 'c':
copyFiles = 0;
break;
case 'p':
noProgressive = 1;
break;
case 'S':
subsample = parseSubsampling(optarg);
break;
case 'T':
if (inputFiletype != FILETYPE_AUTO) {
error("multiple file types specified for the input file");
return 1;
}
inputFiletype = parseInputFiletype(optarg);
break;
case 'Q':
quiet = 1;
break;
};
}
if (argc - optind != 2) {
usage();
return 255;
}
if (method == UNKNOWN) {
error("invalid method!");
usage();
return 255;
}
// No target passed, use preset!
if (!target) {
setTargetFromPreset();
}
unsigned char *buf;
long bufSize = 0;
unsigned char *original;
long originalSize = 0;
unsigned char *originalGray = NULL;
long originalGraySize = 0;
unsigned char *compressed = NULL;
unsigned long compressedSize = 0;
unsigned char *compressedGray;
long compressedGraySize = 0;
unsigned char *tmpImage;
int width, height;
unsigned char *metaBuf;
unsigned int metaSize = 0;
FILE *file;
char *inputPath = argv[optind];
char *outputPath = argv[optind + 1];
/* Read the input into a buffer. */
bufSize = readFile(inputPath, (void **) &buf);
/* Detect input file type. */
if (inputFiletype == FILETYPE_AUTO)
inputFiletype = detectFiletypeFromBuffer(buf, bufSize);
/*
* Read original image and decode. We need the raw buffer contents and its
* size to obtain meta data and the original file size later.
*/
originalSize = decodeFileFromBuffer(buf, bufSize, &original, inputFiletype, &width, &height, JCS_RGB);
if (!originalSize) {
error("invalid input file: %s", inputPath);
return 1;
}
if (defishStrength) {
info("Defishing...\n");
tmpImage = malloc(width * height * 3);
defish(original, tmpImage, width, height, 3, defishStrength, defishZoom);
free(original);
original = tmpImage;
}
// Convert RGB input into Y
originalGraySize = grayscale(original, &originalGray, width, height);
if (inputFiletype == FILETYPE_JPEG) {
// Read metadata (EXIF / IPTC / XMP tags)
if (getMetadata(buf, bufSize, &metaBuf, &metaSize, COMMENT)) {
if (copyFiles) {
info("File already processed by jpeg-recompress!\n");
file = openOutput(outputPath);
if (file == NULL) {
error("could not open output file: %s", outputPath);
return 1;
}
fwrite(buf, bufSize, 1, file);
fclose(file);
free(buf);
return 0;
} else {
error("file already processed by jpeg-recompress!");
free(buf);
return 2;
}
}
}
if (strip) {
// Pretend we have no metadata
metaSize = 0;
} else {
info("Metadata size is %ukb\n", metaSize / 1024);
}
if (!originalSize || !originalGraySize) { return 1; }
if (jpegMin > jpegMax) {
error("maximum JPEG quality must not be smaller than minimum JPEG quality!");
return 1;
}
// Do a binary search to find the optimal encoding quality for the
// given target SSIM value.
int min = jpegMin, max = jpegMax;
for (int attempt = attempts - 1; attempt >= 0; --attempt) {
float metric;
int quality = min + (max - min) / 2;
/* Terminate early once bisection interval is a singleton. */
if (min == max)
attempt = 0;
int progressive = attempt ? 0 : !noProgressive;
int optimize = accurate ? 1 : (attempt ? 0 : 1);
// Recompress to a new quality level, without optimizations (for speed)
compressedSize = encodeJpeg(&compressed, original, width, height, JCS_RGB, quality, progressive, optimize, subsample);
// Load compressed luma for quality comparison
compressedGraySize = decodeJpeg(compressed, compressedSize, &compressedGray, &width, &height, JCS_GRAYSCALE);
if (!compressedGraySize) {
error("unable to decode file that was just encoded!");
return 1;
}
if (!attempt) {
info("Final optimized ");
}
// Measure quality difference
switch (method) {
case MS_SSIM:
metric = iqa_ms_ssim(originalGray, compressedGray, width, height, width, 0);
info("ms-ssim");
break;
case SMALLFRY:
metric = smallfry_metric(originalGray, compressedGray, width, height);
info("smallfry");
break;
case MPE:
metric = meanPixelError(originalGray, compressedGray, width, height, 1);
info("mpe");
break;
case SSIM: default:
metric = iqa_ssim(originalGray, compressedGray, width, height, width, 0, 0);
info("ssim");
break;
}
if (attempt) {
info(" at q=%i (%i - %i): %f\n", quality, min, max, metric);
} else {
info(" at q=%i: %f\n", quality, metric);
}
if (metric < target) {
if (compressedSize >= bufSize) {
free(compressed);
free(compressedGray);
if (copyFiles) {
info("Output file would be larger than input!\n");
file = openOutput(outputPath);
if (file == NULL) {
error("could not open output file: %s", outputPath);
return 1;
}
fwrite(buf, bufSize, 1, file);
fclose(file);
free(buf);
return 0;
} else {
error("output file would be larger than input!");
free(buf);
return 1;
}
}
switch (method) {
case SSIM: case MS_SSIM: case SMALLFRY:
// Too distorted, increase quality
min = MIN(quality + 1, max);
break;
case MPE:
// Higher than required, decrease quality
max = MAX(quality - 1, min);
break;
}
} else {
switch (method) {
case SSIM: case MS_SSIM: case SMALLFRY:
// Higher than required, decrease quality
max = MAX(quality - 1, min);
break;
case MPE:
// Too distorted, increase quality
min = MIN(quality + 1, max);
break;
}
}
// If we aren't done yet, then free the image data
if (attempt) {
free(compressed);
free(compressedGray);
}
}
free(buf);
// Calculate and show savings, if any
int percent = (compressedSize + metaSize) * 100 / bufSize;
unsigned long saved = (bufSize > compressedSize) ? bufSize - compressedSize - metaSize : 0;
info("New size is %i%% of original (saved %lu kb)\n", percent, saved / 1024);
if (compressedSize >= bufSize) {
error("output file is larger than input, aborting!");
return 1;
}
// Open output file for writing
file = openOutput(outputPath);
if (file == NULL) {
error("could not open output file");
return 1;
}
/* Check that the metadata starts with a SOI marker. */
if (!checkJpegMagic(compressed, compressedSize)) {
error("missing SOI marker, aborting!");
return 1;
}
/* Make sure APP0 is recorded immediately after the SOI marker. */
if (compressed[2] != 0xff || compressed[3] != 0xe0) {
error("missing APP0 marker, aborting!");
return 1;
}
/* Write SOI marker and APP0 metadata to the output file. */
int app0_len = (compressed[4] << 8) + compressed[5];
fwrite(compressed, 4 + app0_len, 1, file);
/*
* Write comment (COM metadata) so we know not to reprocess this file in
* the future if it gets passed in again.
*/
fputc(0xff, file);
fputc(0xfe, file);
fputc(0x00, file);
fputc(strlen(COMMENT) + 2, file);
fwrite(COMMENT, strlen(COMMENT), 1, file);
/* Write additional metadata markers. */
if (inputFiletype == FILETYPE_JPEG && !strip) {
fwrite(metaBuf, metaSize, 1, file);
}
/* Write image data. */
fwrite(compressed + 4 + app0_len, compressedSize - 4 - app0_len, 1, file);
fclose(file);
if (inputFiletype == FILETYPE_JPEG && !strip) {
free(metaBuf);
}
free(compressed);
free(original);
free(originalGray);
return 0;
}