-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzopfli-pnginator.c
446 lines (372 loc) · 14 KB
/
zopfli-pnginator.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
/*
zopfli-pnginator: Embbed javascript code in PNG and add custom chunk with a
"html unpacking" script. Image data (= js code) compression done via standard
deflate or zopfli.
clang -lz -lzopfli -std=c17 -Wall -Wextra -pedantic zopfli-pnginator.c
Based on:
https://daeken.dev/blog/2011-08-31_Superpacking_JS_Demos.html
https://gist.github.com/gasman/2560551
Uses:
https://github.com/google/zopfli
https://github.com/madler/zlib
*/
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#ifdef _WIN32
#include <winsock.h>
#else
#include <arpa/inet.h>
#endif
#include "zlib.h"
#include "zopfli.h"
#define min(a, b) (((a) < (b)) ? (a) : (b))
typedef struct PNG_IHDR {
unsigned int width;
unsigned int height;
unsigned char bit_depth;
unsigned char color_type;
unsigned char compression_method;
unsigned char filter_method;
unsigned char interlace_method;
} PNG_IHDR;
typedef struct IMAGE {
unsigned char *data;
size_t size;
size_t width;
size_t height;
} IMAGE;
typedef struct USER_OPTIONS {
char *javascript_path;
char *png_path;
bool no_zopfli;
int zopfli_iterations;
bool no_blocksplitting;
bool apply_format_hacks;
bool no_statistics;
} USER_OPTIONS;
typedef struct COMPRESSION_STATISTICS {
size_t javascript_size;
size_t png_size;
bool multi_row_image;
} COMPRESSION_STATISTICS;
// Command line option names
const char *NO_ZOPFLI = "--no_zopfli";
const char *ZOPFLI_ITERATIONS = "--zopfli_iterations=";
const char *NO_BLOCK_SPLITTING = "--no_blocksplitting";
const char *NO_FORMAT_HACKS = "--no_format_hacks";
const char *NO_STATISTICS = "--no_statistics";
const unsigned char PNG_HEADER[] = {0x89, 0x50, 0x4e, 0x47,
0x0d, 0x0a, 0x1a, 0x0a};
// Javascript code fits on a single row in the PNG
const int SINGLE_ROW_MAX_LENGTH = 4096;
// p01's single-pixel-row bootstrap (requires an 0x00 end marker on the js
// string) (edit by Gasman: move drawImage out of getImageData params (it
// returns undef, which is invalid) and change eval to (1,eval) to force global
// evaluation)
const char *SINGLE_ROW_IMAGE_HTML_UNPACK =
"<canvas id=c><img "
"onload=with(c.getContext('2d'))for(p=e='';drawImage(this,p--,0),t="
"getImageData(0,0,1,1).data[0];)e+=String.fromCharCode(t);(1,eval)(e) "
"src=#>";
// p01's multiple-pixel-row bootstrap (requires a dummy first byte on the js
// string) (edit by Gasman: set explicit canvas width to support widths above
// 300; move drawImage out of getImageData params; change eval to (1,eval) to
// force global evaluation)
const char *MULTI_ROW_IMAGE_HTML_UNPACK =
"<canvas id=c><img "
"onload=for(w=c.width=4096,a=c.getContext('2d'),a.drawImage(this,p=0,0),"
"e='"
"',d=a.getImageData(0,0,w,%u).data;t=d[p+=4];)e+=String.fromCharCode(t);"
"(1,"
"eval)(e) src=#>";
char *read_text_file(const char *file_path) {
char *text = NULL;
FILE *file = fopen(file_path, "rt");
if (file != NULL) {
fseek(file, 0, SEEK_END);
size_t size = ftell(file);
rewind(file);
// Add an additional byte for null terminating the string
text = calloc(size + 1, 1);
if (fread(text, 1, size, file) != size) {
printf("Failed to read source file '%s'\n", file_path);
free(text);
text = NULL;
}
fclose(file);
} else {
printf("Failed to open javascript source file '%s'\n", file_path);
}
return text;
}
IMAGE *embbed_javascript_in_image(
char *javascript, COMPRESSION_STATISTICS *compression_statistics) {
// Get string length of our javascript source
size_t javascript_length = strlen(javascript);
compression_statistics->javascript_size = javascript_length;
// Create our image
IMAGE *image = malloc(sizeof(IMAGE));
// Javascript source either fits on a single image row or needs multiple rows
if (javascript_length < SINGLE_ROW_MAX_LENGTH) {
// Size of single row in the image is length of javascript + 1 byte for
// dummy marker added at the end
image->width = javascript_length + 1;
image->height = 1;
// Image data size is it's width plus 1 byte for 'no filtering' indicator
// at the beginning of the row
image->size = image->width + 1;
image->data = calloc(image->size, 1);
// Copy javascript string into destination buffer, account for 'no
// filtering' \0 indicator at begin of row
memcpy(image->data + 1, javascript, javascript_length);
// Dummy marker \0 at end of javascript string (required by unpacking) is
// already there due to calloc
} else {
// Multi row image has maximum row width
image->width = SINGLE_ROW_MAX_LENGTH;
// Account for dummy byte required by unpacking to calculate number of rows
image->height = (size_t)ceil((javascript_length + 1) /
(float)SINGLE_ROW_MAX_LENGTH);
// Full data size includes 1 byte 'no filtering' indicator per row
image->size = ((image->width) + 1) * (image->height);
image->data = calloc(image->size, 1);
// Start at byte 2 of first row because of 'no filtering' \0 indicator
// and dummy byte \0 for unpacking
unsigned char *image_ptr = image->data + 2;
// Handle each row separately
for (size_t i = 0; i < javascript_length;) {
// Dummy byte for unpacking in first row was already "written", so
// width of first row is - 1 Length of last row is only the remaining
// bytes available in source data (the rest is already padded with \0)
size_t row_length =
min(javascript_length - i,
(i == 0 ? SINGLE_ROW_MAX_LENGTH - 1 : SINGLE_ROW_MAX_LENGTH));
// Copy the full row from source to destination
memcpy(image_ptr, javascript + i, row_length);
// Walk source data in row size increments
i += row_length;
// Destination data pointer increment accounts for additional byte
// from 'no filtering' indicator
image_ptr += row_length + 1;
}
}
return image;
}
bool write_png_chunk(char *chunk_identifier, unsigned char *data,
size_t data_size, FILE *outfile, bool no_crc,
bool overflow_data_in_crc) {
// Write data size
uint32_t data_size_out = htonl(data_size - (overflow_data_in_crc ? 4 : 0));
if (fwrite(&data_size_out, 1, 4, outfile) != 4) {
return false;
}
// Write chunk identifier
if (fwrite(chunk_identifier, 1, 4, outfile) != 4) {
return false;
}
// Write actual data (can be null)
if (fwrite(data, 1, data_size, outfile) != data_size) {
return false;
}
if (!no_crc) {
// Allocate our CRC32 buffer which contains chunk identifier + data
unsigned char *crc_buffer = malloc(4 + data_size);
// Copy chunk identifier and data into CRC32 buffer
memcpy(crc_buffer, chunk_identifier, 4);
memcpy(crc_buffer + 4, data, data_size);
// Calculate CRC32
unsigned long crc = crc32(0L, crc_buffer, 4 + data_size);
// Write CRC32 to file
uint32_t crc_out = htonl(crc);
if (fwrite(&crc_out, 1, 4, outfile) != 4) {
free(crc_buffer);
return false;
}
free(crc_buffer);
}
return true;
}
bool write_image_as_png(IMAGE *image, USER_OPTIONS *user_options,
COMPRESSION_STATISTICS *compression_statistics) {
FILE *outfile = fopen(user_options->png_path, "wb+");
if (outfile == NULL) {
printf("Failed to open destination png file '%s'\n",
user_options->png_path);
return false;
}
// Write PNG header
if (fwrite(PNG_HEADER, 1, sizeof(PNG_HEADER), outfile) != sizeof(PNG_HEADER)) {
printf("Failed to write destination png file '%s' (PNG header)\n",
user_options->png_path);
fclose(outfile);
return false;
}
// Write image header (IHDR) chunk
PNG_IHDR png_ihdr = {
htonl(image->width), htonl(image->height), 8, 0, 0, 0, 0};
if (!write_png_chunk("IHDR", (unsigned char *)&png_ihdr,
2 * sizeof(unsigned int) + 5, outfile, false, false)) {
printf("Failed to write destination png file '%s' (IHDR)\n",
user_options->png_path);
fclose(outfile);
return false;
}
// Prepare unpack code
char *unpack_code = NULL;
if (image->height == 1) {
// Unpack code for single row image can be stored as is
unpack_code = (char *)SINGLE_ROW_IMAGE_HTML_UNPACK;
compression_statistics->multi_row_image = false;
} else {
// Height of multi row image needs to be substituted in the unpack code
size_t max_unpack_code_length = strlen(MULTI_ROW_IMAGE_HTML_UNPACK) + 4;
unpack_code = malloc(max_unpack_code_length);
snprintf(unpack_code, max_unpack_code_length, MULTI_ROW_IMAGE_HTML_UNPACK,
image->height);
compression_statistics->multi_row_image = true;
}
// Write custom chunk with unpack code
if (!write_png_chunk("jawh", (unsigned char *)unpack_code, strlen(unpack_code),
outfile, user_options->apply_format_hacks,
user_options->apply_format_hacks)) {
printf("Failed to write destination png file '%s' (custom chunk)\n",
user_options->png_path);
fclose(outfile);
return false;
}
if (image->height > 1) {
free(unpack_code);
}
unsigned long compressed_data_size = 0;
unsigned char *compressed_data = NULL;
if (!user_options->no_zopfli) {
// Zopfli
ZopfliOptions zopfli_options;
ZopfliInitOptions(&zopfli_options);
zopfli_options.numiterations = user_options->zopfli_iterations;
zopfli_options.blocksplitting = !user_options->no_blocksplitting;
ZopfliCompress(&zopfli_options, ZOPFLI_FORMAT_ZLIB, image->data,
image->size, &compressed_data, &compressed_data_size);
} else {
// ZLIB deflate
compressed_data_size = compressBound(image->size);
compressed_data = malloc(compressed_data_size);
if (compress2(compressed_data, &compressed_data_size, image->data,
image->size, 9 /* level */) != Z_OK) {
printf("Failed to deflate image data\n");
free(compressed_data);
fclose(outfile);
return false;
}
}
if (!write_png_chunk("IDAT", compressed_data, compressed_data_size, outfile,
user_options->apply_format_hacks,
false)) {
printf("Failed to write destination png file '%s' (IDAT)\n",
user_options->png_path);
free(compressed_data);
fclose(outfile);
return false;
}
free(compressed_data);
if (!user_options->apply_format_hacks) {
// Write end (IEND) chunk
if (!write_png_chunk("IEND", NULL, 0, outfile, false, false)) {
printf("Failed to write destination png file '%s' (IEND)\n",
user_options->png_path);
fclose(outfile);
return false;
}
}
compression_statistics->png_size = ftell(outfile);
fclose(outfile);
return true;
}
void print_compression_statistics(
COMPRESSION_STATISTICS *compression_statistics) {
printf("Embedded image has %s\n", compression_statistics->multi_row_image
? "multiple rows"
: "single row");
printf("Input Javascript size: %lu bytes\n",
compression_statistics->javascript_size);
printf("Output PNG file size: %li bytes\n", compression_statistics->png_size);
printf("PNG is %3.2f percent of javascript\n",
compression_statistics->png_size /
(float)compression_statistics->javascript_size * 100.0f);
}
void print_usage_information() {
printf("Usage: zopfli-pnginator [options] infile.js outfile.png.html\n");
printf("\n");
printf("Options:\n");
printf("%s: Use standard zlib deflate instead of zopfli.\n", NO_ZOPFLI);
printf("%s[number]: Number of zopfli iterations. More ", ZOPFLI_ITERATIONS);
printf("iterations take\n more time but can provide slightly better ");
printf("compression. Default is 10.\n");
printf("%s: Do not use block splitting.\n", NO_BLOCK_SPLITTING);
printf("%s: Do not apply PNG format hacks (omit IEND ", NO_FORMAT_HACKS);
printf("chunk, custom chunk\n overflowing in CRC32, IDAT chunk w/o ");
printf("CRC32).\n");
printf("%s: Do not show statistics.\n", NO_STATISTICS);
}
void process_command_line(USER_OPTIONS *user_options, int argc, char *argv[]) {
if (argc < 3) {
print_usage_information();
return;
}
for (int i = 1; i < argc; i++) {
if (strncmp(argv[i], NO_ZOPFLI, strlen(NO_ZOPFLI)) == 0) {
user_options->no_zopfli = true;
continue;
}
if (strncmp(argv[i], ZOPFLI_ITERATIONS, strlen(ZOPFLI_ITERATIONS)) == 0) {
char iterations[strlen(argv[i]) - strlen(ZOPFLI_ITERATIONS)];
strncpy(iterations, argv[i] + strlen(ZOPFLI_ITERATIONS),
sizeof(iterations));
user_options->zopfli_iterations = atoi(iterations);
continue;
}
if (strncmp(argv[i], NO_BLOCK_SPLITTING, strlen(NO_BLOCK_SPLITTING)) == 0) {
user_options->no_blocksplitting = true;
continue;
}
if (strncmp(argv[i], NO_FORMAT_HACKS, strlen(NO_FORMAT_HACKS)) == 0) {
user_options->apply_format_hacks = false;
continue;
}
if (strncmp(argv[i], NO_STATISTICS, strlen(NO_STATISTICS)) == 0) {
user_options->no_statistics = true;
continue;
}
if (user_options->javascript_path == NULL) {
user_options->javascript_path = argv[i];
} else {
user_options->png_path = argv[i];
}
}
}
int main(int argc, char *argv[]) {
printf("zopfli-pnginator\n\n");
USER_OPTIONS user_options = {NULL, NULL, false, 10, false, true, false};
process_command_line(&user_options, argc, argv);
if (user_options.javascript_path == NULL || user_options.png_path == NULL) {
exit(EXIT_FAILURE);
}
char *javascript = read_text_file(user_options.javascript_path);
if (javascript == NULL) {
exit(EXIT_FAILURE);
}
COMPRESSION_STATISTICS compression_statistics;
IMAGE *image =
embbed_javascript_in_image(javascript, &compression_statistics);
free(javascript);
bool success =
write_image_as_png(image, &user_options, &compression_statistics);
free(image->data);
free(image);
if (success && !user_options.no_statistics) {
print_compression_statistics(&compression_statistics);
}
return success ? EXIT_SUCCESS : EXIT_FAILURE;
}