-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpincrush.c
376 lines (328 loc) · 12.5 KB
/
pincrush.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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <unistd.h>
#include <png.h>
#define RDERR(...) { debuglog(LEVEL_ERROR, infilename, __VA_ARGS__); goto out; }
#define WRERR(...) { debuglog(LEVEL_ERROR, inplace?infilename:outfilename, __VA_ARGS__); goto out; }
#define INFO(level, ...) { if(verbose) debuglog(level, infilename, __VA_ARGS__); }
#ifndef PATH_MAX
#include <linux/limits.h>
#endif
#if TARGET_LINUX == 1
#include <getopt.h>
extern char *strdup (const char *s);
#endif
enum debuglevels {
LEVEL_ERROR = -1,
LEVEL_NONE = 0,
LEVEL_INFO, LEVEL_RIDICULOUS, LEVEL_INSANE
};
static bool inplace = false;
static unsigned int verbose = 0;
static bool chunked = true;
static unsigned int global_num_rows = 8;
void debuglog(int level, char *filename, const char *format, ...) {
if(level > verbose && level != LEVEL_ERROR) return;
va_list args;
va_start(args, format);
char *out = (char *)malloc(PATH_MAX);
vsprintf(out, format, args);
if(!filename)
fprintf(stderr, "%s", out);
else
fprintf(stderr, "%s: %s", filename, out);
if(out[strlen(out)-1] != '\n')
fprintf(stderr, "\n");
free(out);
va_end(args);
}
void png_warning_cb(png_structp ptr, png_const_charp msg) {
char *filename = (char *)png_get_error_ptr(ptr);
if(verbose)
debuglog(LEVEL_INFO, filename, msg);
}
void copy(char *inf, char *outf) {
FILE *in, *out;
in = fopen(inf, "rb");
out = fopen(outf, "wb");
fseek(in, 0, SEEK_END);
int len = ftell(in);
rewind(in);
char *filebuf = malloc(len);
fread(filebuf, 1, len, in);
fclose(in);
fwrite(filebuf, 1, len, out);
fclose(out);
free(filebuf);
}
int unknown_chunk_read_cb(png_structp ptr, png_unknown_chunkp chunk) {
//png_uint_32 *my_chunk_data = (png_uint_32*)png_get_user_chunk_ptr(ptr);
if(!strncmp((char*)chunk->name, "CgBI", 4)) {
fprintf(stderr, "Error: This file is already crushed and how the hell did you get here?\n");
exit(1);
}
return 1;
}
void swap_and_premultiply_alpha_transform(png_structp ptr, png_row_infop row_info, png_bytep data) {
for(unsigned int x = 0; x < row_info->width * 4; x += 4) {
png_byte r, g, b, a;
r = data[x+0]; g = data[x+1]; b = data[x+2]; a = data[x+3];
data[x+0] = (b*a) / 0xff;
data[x+1] = (g*a) / 0xff;
data[x+2] = (r*a) / 0xff;
}
}
void crush(char *infilename, char *outfilename) {
if(!outfilename) {
outfilename = (char *)malloc(PATH_MAX);
sprintf(outfilename, "%s.crush", infilename);
}
FILE *fp_in = NULL, *fp_out = NULL;
{
debuglog(LEVEL_INFO, NULL, "Crushing %s.\n", infilename);
fp_in = fopen(infilename, "rb");
if(!fp_in) {
RDERR("Error: could not open.\n", infilename);
}
png_byte header[8];
fread(header, 1, 8, fp_in);
if(png_sig_cmp(header, 0, 8) != 0) {
RDERR("Error: Not a valid PNG file.\n", infilename);
}
fseek(fp_in, 12, SEEK_SET);
png_byte cgbi[4];
fread(cgbi, 1, 4, fp_in);
if(!strncmp((char*)cgbi, "CgBI", 4)) {
fclose(fp_in); fp_in = NULL;
INFO(LEVEL_INFO, "Warning: This file is already crushed. Doing nothing.\n");
if(!inplace)
copy(infilename, outfilename);
goto out;
}
fseek(fp_in, 8, SEEK_SET);
png_structp read_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(!read_ptr) RDERR("Error: failed to init libpng for reading.\n");
png_set_error_fn(read_ptr, (void*)infilename, NULL, png_warning_cb);
png_infop read_info = png_create_info_struct(read_ptr);
if(!read_info) {
png_destroy_read_struct(&read_ptr, NULL, NULL);
RDERR("Error: failed to init libpng for reading.\n");
}
png_infop read_end = png_create_info_struct(read_ptr);
if(!read_end) {
png_destroy_read_struct(&read_ptr, &read_info, NULL);
RDERR("Error: failed to init libpng for reading.\n");
}
if(setjmp(png_jmpbuf(read_ptr))) {
RDERR("Error: error reading PNG.\n");
goto out;
}
png_init_io(read_ptr, fp_in);
png_set_sig_bytes(read_ptr, 8); // Already read the signature.
fp_out = fopen(outfilename, "wb");
if(!fp_out) {
WRERR("Error: could not open for writing.\n", outfilename);
}
png_structp write_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(!write_ptr) WRERR("Error: failed to init libpng for writing.\n");
png_set_error_fn(read_ptr, (void*)(inplace?infilename:outfilename), NULL, png_warning_cb);
png_infop write_info = png_create_info_struct(write_ptr);
if(!write_info) {
png_destroy_write_struct(&write_ptr, NULL);
WRERR("Error: failed to init libpng for writing.\n");
}
if(setjmp(png_jmpbuf(write_ptr))) {
unlink(outfilename);
WRERR("Error: error writing PNG.\n");
goto out;
}
// If we don't write the header first, png_write_chunk (for our custom CgBI chunk) doesn't add a header.
// png_write_sig() supposedly exists, even in the header file, but I can't for the life of me use it?
fwrite(header, 1, 8, fp_out);
png_init_io(write_ptr, fp_out);
png_set_sig_bytes(write_ptr, 8);
png_set_filter(write_ptr, 0, PNG_FILTER_NONE);
// The default window size is 15 bits. Setting it to -15 causes zlib to discard the header and crc information.
// This is critical to making a proper CgBI PNG
png_set_compression_window_bits(write_ptr, -15);
// Handle ALL unknown chunks.
png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS, NULL, 0);
png_set_read_user_chunk_fn(read_ptr, NULL, unknown_chunk_read_cb);
png_read_info(read_ptr, read_info);
png_uint_32 width, height;
int color_type, bitdepth, filter_method, compression_type, interlace_type;
int orig_color_type;
double gamma = 0.45455;
png_get_IHDR(read_ptr, read_info, &width, &height, &bitdepth, &color_type, &interlace_type, &compression_type, &filter_method);
orig_color_type = color_type;
if(color_type == PNG_COLOR_TYPE_PALETTE) {
INFO(LEVEL_INFO, "Converting Palette to RGB\n");
png_set_palette_to_rgb(read_ptr);
} else if(color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
if(bitdepth < 8)
png_set_expand_gray_1_2_4_to_8(read_ptr);
INFO(LEVEL_INFO, "Converting Greyscale image to RGB\n");
png_set_gray_to_rgb(read_ptr);
}
if(png_get_valid(read_ptr, read_info, PNG_INFO_tRNS)) {
INFO(LEVEL_INFO, "Converting tRNS chunk to Alpha values\n");
png_set_tRNS_to_alpha(read_ptr);
}
else if(!(color_type & PNG_COLOR_MASK_ALPHA)) {
// Expand, adding an opaque alpha channel.
INFO(LEVEL_INFO, "Adding opaque alpha channel.\n");
png_set_add_alpha(read_ptr, 0xff, PNG_FILLER_AFTER);
}
if(bitdepth == 16) {
INFO(LEVEL_INFO, "Stripping 16-bit samples.\n");
png_set_strip_16(read_ptr);
}
png_set_read_user_transform_fn(read_ptr, swap_and_premultiply_alpha_transform);
png_read_update_info(read_ptr, read_info);
png_set_user_transform_info(read_ptr, NULL, 8, 4);
if(png_get_valid(read_ptr, read_info, PNG_INFO_gAMA)) png_get_gAMA(read_ptr, read_info, &gamma);
if(png_get_valid(read_ptr, read_info, PNG_INFO_cHRM)) {
double wx,wy,rx,ry,gx,gy,bx,by;
png_get_cHRM(read_ptr, read_info, &wx, &wy, &rx, &ry, &gx, &gy, &bx, &by);
INFO(LEVEL_RIDICULOUS, "cHRM (chromaticities): %f, %f, %f, %f, %f, %f, %f, %f\n", wx,wy,rx,ry,gx,gy,bx,by);
}
if(png_get_valid(read_ptr, read_info, PNG_INFO_sRGB)) {
int intent;
png_get_sRGB(read_ptr, read_info, &intent);
INFO(LEVEL_RIDICULOUS, "sRGB intent: %d\n", intent);
}
if(png_get_valid(read_ptr, read_info, PNG_INFO_pHYs)) {
png_uint_32 res_x, res_y;
int unit_type;
png_get_pHYs(read_ptr, read_info, &res_x, &res_y, &unit_type);
INFO(LEVEL_RIDICULOUS, "pHYs info: xres %u yres %u unit type %d\n", res_x, res_y, unit_type);
}
// re-read the info
png_get_IHDR(read_ptr, read_info, &width, &height, &bitdepth, &color_type, &interlace_type, &compression_type, &filter_method);
INFO(LEVEL_INFO, "Dimensions: %ux%u.\n", height, width);
if(interlace_type != 0)
INFO(LEVEL_INFO, "Interlaced. Overriding chunked mode.\n");
//INFO("Bit Depth: %d\n", bitdepth);
//INFO("Colour Mode: %d\n", color_type);
png_set_IHDR(write_ptr, write_info, width, height, bitdepth, color_type, interlace_type, compression_type, filter_method);
// Standard Gamma
png_set_gAMA(write_ptr, write_info, 0.45455);
// Primary Chromaticities white_xy, red_xy, blue_xy, green_xy, in that order.
png_set_cHRM(write_ptr, write_info, 0.312700, 0.329000, 0.640000, 0.330000, 0.300000, 0.600000, 0.150000, 0.060000);
// Apple's PNGs have an sRGB intent of 0.
png_set_sRGB(write_ptr, write_info, 0);
// CgBI = 0x50 00 20 06
//png_unknown_chunkp cgbi = (png_unknown_chunkp)png_malloc(write_ptr, sizeof(png_unknown_chunk));
png_byte cname[] = {'C', 'g', 'B', 'I', '\0'};
png_byte cdata[] = {0x50, 0x00, 0x20, 0x06};
if(orig_color_type & PNG_COLOR_MASK_ALPHA || orig_color_type == PNG_COLOR_TYPE_PALETTE) {
// I'm not sure, but if the input colortype is alpha anything, CgBI[3] is 0x02 instead of 0x06.
// Strange, because 0x06 means RGBA and 0x02 does not.
// But, Mimick this behaviour. Otherwise, our alpha channel is ignored.
cdata[3] = 0x02;
}
png_write_chunk(write_ptr, cname, cdata, 4);
png_write_info(write_ptr, write_info);
int rowbytes = png_get_rowbytes(read_ptr, read_info); // We're always outputting 4bpp.
INFO(LEVEL_RIDICULOUS, "There are %d bytes per row. Don't let anybody tell you otherwise.\n", rowbytes);
if(interlace_type != 0 || !chunked) {
INFO(LEVEL_INSANE, "Reading whole image into memory: %d bytes.\n", rowbytes*height);
png_bytep read_data = (png_bytep)malloc(rowbytes * height);
png_bytep read_rows[height];
int bpr = png_get_rowbytes(read_ptr, read_info);
for(unsigned int i = 0; i < height; i++) {
read_rows[i] = read_data + i*bpr;
}
png_read_image(read_ptr, read_rows);
png_write_image(write_ptr, read_rows);
free(read_data);
} else {
int number_of_passes = 1;
if(interlace_type == PNG_INTERLACE_ADAM7) {
number_of_passes = png_set_interlace_handling(read_ptr);
png_set_interlace_handling(write_ptr);
}
int remaining_rows = height;
unsigned int numrows = remaining_rows > global_num_rows ? global_num_rows : remaining_rows;
INFO(LEVEL_INSANE, "Reading image into memory %d rows at a time: %d bytes.\n", numrows, rowbytes*numrows);
png_bytep row = png_malloc(read_ptr, rowbytes * numrows);
png_bytep rowpointers[numrows];
for(unsigned int i = 0; i < numrows; i++)
rowpointers[i] = row+(i*rowbytes);
for(int pass = 0; pass < number_of_passes; pass++) {
while(remaining_rows > 0) {
png_read_rows(read_ptr, rowpointers, NULL, numrows);
png_write_rows(write_ptr, rowpointers, numrows);
remaining_rows -= numrows;
numrows = remaining_rows > global_num_rows ? global_num_rows : remaining_rows;
}
}
png_free(read_ptr, row);
}
png_read_end(read_ptr, read_end);
png_write_end(write_ptr, write_info);
fclose(fp_in); fp_in = NULL;
fclose(fp_out); fp_out = NULL;
png_destroy_read_struct(&read_ptr, &read_info, &read_end);
png_destroy_write_struct(&write_ptr, &write_info);
if(inplace) {
unlink(infilename);
rename(outfilename, infilename);
}
}
out:
if(fp_in) fclose(fp_in);
if(fp_out) fclose(fp_out);
free(outfilename);
}
void usage(char *argv0) {
printf("pincrush %s\n", VERSION);
printf("Syntax: %s [-v] [-c#] -i <infile> [infile ...]\n", argv0);
printf(" %s [-v] [-c#] <infile> <outfile>\n\n", argv0);
printf(" -i In-place mode. One of -i or outfile is required.\n");
printf(" -v Verbose mode. More 'v's = more verbose! Up to three. Five is right out.\n");
printf(" -c# Process # rows at a time. The default is 8.\n");
printf(" Use 0 to disable chunk mode (uses more memory, reads the entire image into memory).\n");
printf(" -h Display this help text.\n\n");
printf("pincrush comes with no warranty, neither express nor implied. If it blows up\nyour themes, eats your children, or causes you bodily harm,\nall you'll get is a little pity and maybe an apology.\n");
printf("Please report any pincrush bugs to <[email protected]>, possibly including\na copy of the offending PNG file.\n");
}
int main(int argc, char **argv, char **envp) {
char *argv0 = argv[0];
char optflag;
while((optflag = getopt(argc, argv, "ivhc:")) != -1) {
switch(optflag) {
case 'i':
inplace = true;
break;
case 'v':
verbose++;
break;
case 'c':
chunked = true;
global_num_rows = (unsigned int)strtoul(optarg, NULL, 0);
if(global_num_rows == 0)
chunked = false;
break;
case '?':
case 'h':
default:
usage(argv0);
exit(1);
}
}
argc -= optind;
argv += optind;
if(argc < 1 || (!inplace && argc < 2)) {
usage(argv0);
exit(1);
}
if(inplace) {
for(int i = 0; i < argc; i++)
crush(argv[i], NULL);
} else
crush(argv[0], strdup(argv[1]));
return 0;
}