-
Notifications
You must be signed in to change notification settings - Fork 19
/
quantsmooth.c
415 lines (375 loc) · 10.2 KB
/
quantsmooth.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
/*
* Copyright (C) 2016-2020 Kurdyukov Ilya
*
* This file is part of jpeg quantsmooth
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <math.h>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#define WASM
#endif
#ifdef WASM_MAIN
#ifndef __EMSCRIPTEN__
#define EMSCRIPTEN_KEEPALIVE
#define WASM
#endif
#endif
#include "jpeglib.h"
#ifdef WASM
#define logfmt(...) printf(__VA_ARGS__)
#else
#define logfmt(...) fprintf(stderr, __VA_ARGS__)
#endif
#define WITH_LOG
#include "quantsmooth.h"
#ifdef WASM
#define MEM_INPUT
#define MEM_OUTPUT
#endif
#ifdef MEM_INPUT
#if !defined(WASM) || defined(WASM_MAIN)
static uint8_t* loadfile(const char *fn, size_t *num) {
size_t n, j = 0; uint8_t *buf = 0;
FILE *fi = fopen(fn, "rb");
if (fi) {
fseek(fi, 0, SEEK_END);
n = ftell(fi);
fseek(fi, 0, SEEK_SET);
if (n) {
buf = (uint8_t*)malloc(n);
if (buf) j = fread(buf, 1, n, fi);
}
fclose(fi);
}
if (num) *num = j;
return buf;
}
#endif
void jpeg_init_source(j_decompress_ptr cinfo) { }
boolean jpeg_fill_input_buffer(j_decompress_ptr cinfo) { return FALSE; }
void jpeg_skip_input_data(j_decompress_ptr cinfo, long num_bytes) {
struct jpeg_source_mgr *src = cinfo->src;
if (num_bytes > src->bytes_in_buffer) num_bytes = src->bytes_in_buffer;
src->next_input_byte += (size_t)num_bytes;
src->bytes_in_buffer -= (size_t)num_bytes;
}
void jpeg_term_source(j_decompress_ptr cinfo) { }
#endif
#ifdef MEM_OUTPUT
struct jpeg_dest_mem {
struct jpeg_destination_mgr pub; /* public fields */
uint8_t *buffer; size_t size, bufsize, maxchunk;
};
static void jpeg_init_destination(j_compress_ptr cinfo) {
struct jpeg_dest_mem *p = (struct jpeg_dest_mem*)cinfo->dest;
if (!p->buffer) {
p->buffer = (uint8_t*)malloc(p->bufsize);
}
p->pub.next_output_byte = p->buffer;
p->pub.free_in_buffer = p->bufsize;
}
static boolean jpeg_empty_output_buffer(j_compress_ptr cinfo) {
struct jpeg_dest_mem *p = (struct jpeg_dest_mem*)cinfo->dest;
size_t offset = p->bufsize, next;
p->size = offset;
next = p->bufsize;
if (next > p->maxchunk) next = p->maxchunk;
p->bufsize += next;
p->buffer = (uint8_t*)realloc(p->buffer, p->bufsize);
p->pub.next_output_byte = p->buffer + offset;
p->pub.free_in_buffer = p->bufsize - offset;
return TRUE;
}
static void jpeg_term_destination(j_compress_ptr cinfo) {
struct jpeg_dest_mem *p = (struct jpeg_dest_mem*)cinfo->dest;
p->size = p->bufsize - p->pub.free_in_buffer;
}
#endif
#ifdef WASM
static char** make_argv(char *str, int *argc_ret) {
int i = 0, eol = 0, argc = 1; char **argv;
for (;;) {
char a = str[i++];
if (!a) break;
if (eol) {
if (a == eol) eol = 0;
} else {
if (a != ' ') {
eol = ' ';
if (a == '"' || a == '\'') eol = a;
argc++;
}
}
}
*argc_ret = argc;
argv = (char**)malloc(argc * sizeof(char*));
if (!argv) return argv;
argv[0] = NULL; eol = 0; argc = 1;
for (;;) {
char a = *str++;
if (!a) break;
if (eol) {
if (a == eol) { str[-1] = 0; eol = 0; }
} else {
if (a != ' ') {
eol = ' ';
if (a == '"' || a == '\'') { eol = a; str++; }
argv[argc++] = str - 1;
}
}
}
return argv;
}
EMSCRIPTEN_KEEPALIVE
int web_process(int64_t *params) {
char *cmdline = (char*)params[0];
#else
int main(int argc, char **argv) {
#endif
struct jpeg_decompress_struct srcinfo;
struct jpeg_compress_struct dstinfo;
struct jpeg_error_mgr jsrcerr, jdsterr;
jvirt_barray_ptr *src_coef_arrays;
#ifdef MEM_INPUT
size_t input_size = 0;
uint8_t *input_mem = NULL;
struct jpeg_source_mgr src_mgr = { 0 };
#else
FILE *input_file = stdin;
#endif
#ifdef MEM_OUTPUT
struct jpeg_dest_mem dest_mgr = { { 0 }, NULL, 0, 0x10000, 0x100000 };
#endif
#ifndef WASM
FILE *output_file = stdout;
#endif
int optimize = 0, verbose_level = 0, smooth_info = 0xf;
#ifdef WASM
int argc = 0;
char **argv_ptr = make_argv(cmdline, &argc), **argv = argv_ptr;
#else
const char *progname = "quantsmooth", *fn;
#endif
while (argc > 1) {
if (!strcmp(argv[1], "--optimize")) {
optimize = 1;
argv++; argc--;
}
else if (argc > 2 && !strcmp(argv[1], "--verbose")) {
verbose_level = atoi(argv[2]);
argv += 2; argc -= 2;
}
else if (argc > 2 && !strcmp(argv[1], "--info")) {
smooth_info = atoi(argv[2]);
argv += 2; argc -= 2;
}
else if (!strcmp(argv[1], "--")) {
argv++; argc--;
break;
}
else break;
}
#ifdef WASM
free(argv_ptr);
if (argc != 1) {
logfmt("Unrecognized command line option.\n");
return 1;
}
#else
if (argc != 3) {
logfmt(
"Usage:\n"
" %s [options] input.jpg output.jpg\n"
"Options:\n"
" --optimize Optimize Huffman table (smaller file, but slow compression)\n"
" --verbose level Print libjpeg debug output\n"
" --info flags Print quantsmooth debug output\n", progname);
return 1;
}
#endif
if (verbose_level) {
#ifdef LIBJPEG_TURBO_VERSION
#define TOSTR(s) #s
#define TOSTR1(s) TOSTR(s)
logfmt("using libjpeg-turbo version %s\n", TOSTR1(LIBJPEG_TURBO_VERSION));
#else
logfmt("using libjpeg version %d\n", JPEG_LIB_VERSION);
#endif
verbose_level--;
}
srcinfo.err = jpeg_std_error(&jsrcerr);
jpeg_create_decompress(&srcinfo);
dstinfo.err = jpeg_std_error(&jdsterr);
jpeg_create_compress(&dstinfo);
jsrcerr.trace_level = jdsterr.trace_level = verbose_level;
srcinfo.mem->max_memory_to_use = dstinfo.mem->max_memory_to_use;
#ifdef WASM
input_mem = (uint8_t*)params[1];
input_size = params[2];
#else
fn = argv[1];
#endif
#ifdef MEM_INPUT
#ifndef WASM
input_mem = loadfile(fn, &input_size);
if (!input_mem) {
logfmt("%s: can't open input file \"%s\"\n", progname, fn);
return 1;
}
#endif
#if 0 && (JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED))
jpeg_mem_src(&srcinfo, input_mem, input_size);
#else
srcinfo.src = &src_mgr;
src_mgr.init_source = jpeg_init_source;
src_mgr.fill_input_buffer = jpeg_fill_input_buffer;
src_mgr.skip_input_data = jpeg_skip_input_data;
src_mgr.resync_to_restart = jpeg_resync_to_restart; /* use default method */
src_mgr.term_source = jpeg_term_source;
src_mgr.next_input_byte = (const JOCTET*)input_mem;
src_mgr.bytes_in_buffer = input_size;
#endif
#else
if (strcmp(fn, "-")) {
if ((input_file = fopen(fn, "rb")) == NULL) {
logfmt("%s: can't open input file \"%s\"\n", progname, fn);
return 1;
}
}
jpeg_stdio_src(&srcinfo, input_file);
#endif
(void) jpeg_read_header(&srcinfo, TRUE);
src_coef_arrays = jpeg_read_coefficients(&srcinfo);
do_quantsmooth(&srcinfo, src_coef_arrays, smooth_info);
jpeg_copy_critical_parameters(&srcinfo, &dstinfo);
if (optimize) dstinfo.optimize_coding = TRUE;
#ifdef MEM_OUTPUT
// uint8_t *outbuffer; unsigned long outsize;
// jpeg_mem_dest(dstinfo, &outbuffer, &outsize);
dest_mgr.pub.init_destination = jpeg_init_destination;
dest_mgr.pub.empty_output_buffer = jpeg_empty_output_buffer;
dest_mgr.pub.term_destination = jpeg_term_destination;
dstinfo.dest = (struct jpeg_destination_mgr*)&dest_mgr;
#else
// If output opened after reading coefs, then we can write result to input file
fn = argv[2];
if (strcmp(fn, "-")) {
if ((output_file = fopen(fn, "wb")) == NULL) {
logfmt("%s: can't open output file \"%s\"\n", progname, fn);
return 1;
}
}
jpeg_stdio_dest(&dstinfo, output_file);
#endif
/* Start compressor (note no image data is actually written here) */
jpeg_write_coefficients(&dstinfo, src_coef_arrays);
/* Finish compression and release memory */
jpeg_finish_compress(&dstinfo);
jpeg_destroy_compress(&dstinfo);
(void) jpeg_finish_decompress(&srcinfo);
jpeg_destroy_decompress(&srcinfo);
/* Close files, if we opened them */
#ifndef MEM_INPUT
if (input_file != stdin) fclose(input_file);
#endif
#ifdef WASM
params[3] = (intptr_t)dest_mgr.buffer;
params[4] = dest_mgr.size;
#else
#ifdef MEM_OUTPUT
fn = argv[2];
if (strcmp(fn, "-")) {
if ((output_file = fopen(fn, "wb")) == NULL) {
logfmt("%s: can't open output file \"%s\"\n", progname, fn);
return 1;
}
}
if (dest_mgr.buffer) {
fwrite(dest_mgr.buffer, 1, dest_mgr.size, output_file);
free(dest_mgr.buffer);
}
#endif
if (output_file != stdout) fclose(output_file);
#endif
return jsrcerr.num_warnings + jdsterr.num_warnings ? 2 : 0;
}
// for testing purposes
#ifdef WASM_MAIN
int main(int argc, char **argv) {
int64_t params[5]; int i, n, ret;
char *cmdline = NULL; size_t cmd_size = 1;
const char *progname = "quantsmooth", *fn;
uint8_t *input_mem; size_t input_size;
for (n = 1; n < argc; ) {
const char *str = argv[n];
if (str[0] != '-') break;
if (str[1] != '-') break;
n++;
if (str[2] == 0) break;
cmd_size += strlen(str) + 3;
}
cmdline = malloc(cmd_size);
if (!cmdline) return 1;
cmd_size = 0;
for (i = 1; i < n; i++) {
const char *str = argv[i];
int len = strlen(str);
cmdline[cmd_size++] = '"';
memcpy(cmdline + cmd_size, str, len);
cmd_size += len;
cmdline[cmd_size++] = '"';
cmdline[cmd_size++] = ' ';
}
cmdline[cmd_size] = 0;
params[0] = (intptr_t)cmdline;
// printf("cmdline: %s\n", cmdline);
argv += n - 1;
argc -= n - 1;
if (argc != 3) {
logfmt("Unrecognized command line.\n");
return 1;
}
fn = argv[1];
input_mem = loadfile(fn, &input_size);
if (!input_mem) {
logfmt("%s: can't open input file \"%s\"\n", progname, fn);
return 1;
}
params[1] = (intptr_t)input_mem;
params[2] = input_size;
params[3] = 0;
params[4] = 0;
ret = web_process(params);
free(input_mem);
if (params[3]) {
FILE *output_file;
fn = argv[2];
if ((output_file = fopen(fn, "wb")) == NULL) {
logfmt("%s: can't open output file \"%s\"\n", progname, fn);
return 1;
}
fwrite((void*)params[3], 1, (size_t)params[4], output_file);
fclose(output_file);
free((void*)params[3]);
}
if (cmdline) free(cmdline);
return ret;
}
#endif