forked from jrmuizel/minpng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminpng.h
401 lines (347 loc) · 9.69 KB
/
minpng.h
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
/*
* Copyright © 2009 Jeff Muizelaar
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Jeff Muizelaar not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. Jeff Muizelaar makes no representations about the
* suitability of this software for any purpose. It is provided "as is"
* without express or implied warranty.
*
* JEFF MUIZELAAR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL JEFF MUIZELAAR
* BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
/* PNG specification Annex D */
/* Table of CRCs of all 8-bit messages. */
unsigned long crc_table[256];
/* Flag: has the table been computed? Initially false. */
int crc_table_computed = 0;
/* Make the table for a fast CRC. */
void make_crc_table(void)
{
unsigned long c;
int n, k;
for (n = 0; n < 256; n++) {
c = (unsigned long) n;
for (k = 0; k < 8; k++) {
if (c & 1)
c = 0xedb88320L ^ (c >> 1);
else
c = c >> 1;
}
crc_table[n] = c;
}
crc_table_computed = 1;
}
/* Update a running CRC with the bytes buf[0..len-1]--the CRC
should be initialized to all 1's, and the transmitted value
is the 1's complement of the final running CRC (see the
crc() routine below). */
unsigned long update_crc(unsigned long crc, unsigned char *buf,
int len)
{
unsigned long c = crc;
int n;
if (!crc_table_computed)
make_crc_table();
for (n = 0; n < len; n++) {
c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8);
}
return c;
}
/* Return the CRC of the bytes buf[0..len-1]. */
unsigned long crc(unsigned char *buf, int len)
{
return update_crc(0xffffffffL, buf, len) ^ 0xffffffffL;
}
char *write_be32(char *buf, int a)
{
buf[0] = (a>>24) & 0xff;
buf[1] = (a>>16) & 0xff;
buf[2] = (a>>8) & 0xff;
buf[3] = (a>>0) & 0xff;
return &buf[4];
}
struct buf
{
char *data;
int len;
};
struct buf chunk(char *type, struct buf q)
{
struct buf b;
char *t = (char *)malloc(4 + 4 + q.len + 4);
char *chunk_start;
b.data = t;
t = write_be32(t, q.len);
chunk_start = t;
/* copy over type */
t[0] = type[0]; t[1] = type[1]; t[2] = type[2]; t[3] = type[3];
t = &t[4];
/* copy over data */
memcpy(t, q.data, q.len);
t += q.len;
free(q.data);
t = write_be32(t, crc((unsigned char *)chunk_start, q.len + 4));
b.len = 4 + 4 + q.len + 4;
return b;
}
struct buf buf_cat_str(struct buf b, char *d, int len)
{
struct buf r;
r.data = (char *)realloc(b.data, b.len + len);
memcpy(r.data + b.len, d, len);
r.len = b.len + len;
return r;
}
typedef struct buf (*data_cat_fn)(struct buf b, void *src, int len);
struct buf buf_cat_str_rgb(struct buf b, void *abstract_src, int len)
{
unsigned int *src = (unsigned int *)abstract_src;
struct buf r;
char *dest;
r.data = (char *)realloc(b.data, b.len + len);
dest = r.data + b.len;
r.len = b.len + len;
while (len) {
/* we probably need to unpremultiply here */
char alpha = 0xff;
char red = (*src >> 16) & 0xff;
char green = (*src >> 8) & 0xff;
char blue = (*src >> 0) & 0xff;
*dest++ = red;
*dest++ = green;
*dest++ = blue;
*dest++ = alpha;
len-=4;
src++;
}
return r;
}
struct buf buf_cat_str_argb(struct buf b, void *abstract_src, int len)
{
unsigned int *src = (unsigned int *)abstract_src;
struct buf r;
char *dest;
r.data = (char *)realloc(b.data, b.len + len);
dest = r.data + b.len;
r.len = b.len + len;
while (len) {
/* we probably need to unpremultiply here */
char alpha = (*src >> 24) & 0xff;
char red = (*src >> 16) & 0xff;
char green = (*src >> 8) & 0xff;
char blue = (*src >> 0) & 0xff;
*dest++ = red;
*dest++ = green;
*dest++ = blue;
*dest++ = alpha;
len-=4;
src++;
}
return r;
}
struct buf buf_cat_str_a8(struct buf b, void *abstract_src, int len)
{
struct buf r;
unsigned char *src = (unsigned char *)abstract_src;
char *dest;
r.data = (char *)realloc(b.data, b.len + len);
dest = r.data + b.len;
r.len = b.len + len;
while (len) {
/* we probably need to unpremultiply here */
unsigned char alpha = 0xff;
unsigned char red = *src;
unsigned char green = *src;
unsigned char blue = *src;
*dest++ = red;
*dest++ = green;
*dest++ = blue;
*dest++ = alpha;
len-=4;
src++;
}
return r;
}
struct buf buf_cat_str_565(struct buf b, void *abstract_src, int len)
{
struct buf r;
unsigned short *src = (unsigned short *)abstract_src;
char *dest;
r.data = (char *)realloc(b.data, b.len + len);
dest = r.data + b.len;
r.len = b.len + len;
while (len) {
/* we probably need to unpremultiply here */
unsigned char alpha = 0xff;
unsigned char red = ((*src >> 11)*255)/0x1f;
unsigned char green = (((*src >> 5) & 0x3f)*255)/0x3f;
unsigned char blue = (((*src >> 0) & 0x1f)*255)/0x1f;
*dest++ = red;
*dest++ = green;
*dest++ = blue;
*dest++ = alpha;
len-=4;
src++;
}
return r;
}
struct buf buf_cat(struct buf b, struct buf c)
{
return buf_cat_str(b, c.data, c.len);
}
struct buf be32(int a)
{
struct buf r;
r.data = (char *)malloc(4);
r.len = 4;
write_be32(r.data, a);
return r;
}
struct adler {
int a; // 1
int b; // 0
};
#define MOD_ADLER 65521
unsigned int adler32(unsigned char *data, size_t len) /* data: Pointer to the data to be summed; len is in bytes */
{
unsigned int a = 1, b = 0;
while (len != 0)
{
a = (a + *data++) % MOD_ADLER;
b = (b + a) % MOD_ADLER;
len--;
}
return (b << 16) | a;
}
/* From Wikipedia */
struct adler adler32_str(struct adler ctx, char *data, size_t len) /* data: Pointer to the data to be summed; len is in bytes */
{
while (len != 0)
{
ctx.a = (ctx.a + *data++) % MOD_ADLER;
ctx.b = (ctx.b + ctx.a) % MOD_ADLER;
len--;
}
return ctx;
}
struct adler adler32_buf(struct adler ctx, struct buf b) /* data: Pointer to the data to be summed; len is in bytes */
{
int i=0;
while (i != b.len)
{
ctx.a = (ctx.a + b.data[i++]) % MOD_ADLER;
ctx.b = (ctx.b + ctx.a) % MOD_ADLER;
}
return ctx;
}
struct adler adler32_init()
{
struct adler ret;
ret.a = 1;
ret.b = 0;
return ret;
}
unsigned int adler32_fin(struct adler ctx)
{
return (ctx.b << 16) | ctx.a;
}
/* 16bit length in little endian followed by
* ones compliment of length in little endian */
struct buf zlib_block_length(int length)
{
struct buf r;
r.data = (char *)malloc(4);
r.len = 4;
r.data[0] = length & 0xff;
r.data[1] = (length >> 8) & 0xff;
length = ~length;
r.data[2] = length & 0xff;
r.data[3] = (length >> 8) & 0xff;
return r;
}
/* inspired by "A use for uncompressed PNGs"
* http://drj11.wordpress.com/2007/11/20/a-use-for-uncompressed-pngs/
* by David Jones */
struct buf make_png(void *d, int width, int height, int stride, data_cat_fn buf_cat_str_data)
{
struct buf r = {0};
char predictor[] = {0x0};
char zlib_prefix[] = {0x78,0x9c};
char zlib_final_block_prefix[] = {0x01};
char zlib_block_prefix[] = {0x00};
char hdr_tail[] = {0x08,0x06,0x00,0x00,0x00};
char png_start[] = {0x89,'P','N','G','\r','\n',0x1A,'\n'};
struct buf ihdr = {0};
int block_length = (width*4 + 1);
struct buf idat = {0}, iend = {0};
int i;
assert(block_length <= 65535);
r = buf_cat_str(r, png_start, sizeof(png_start));
ihdr = buf_cat(ihdr, be32(width));
ihdr = buf_cat(ihdr, be32(height));
ihdr = buf_cat_str(ihdr, hdr_tail, sizeof(hdr_tail));
r = buf_cat(r, chunk("IHDR", ihdr));
idat = buf_cat_str(idat, zlib_prefix, sizeof(zlib_prefix));
struct adler chksum = adler32_init();
for (i=0; i<height; i++) {
struct buf row_data = {0};
if (i == height - 1)
idat = buf_cat_str(idat, zlib_final_block_prefix, sizeof(zlib_final_block_prefix));
else
idat = buf_cat_str(idat, zlib_block_prefix, sizeof(zlib_block_prefix));
idat = buf_cat(idat, zlib_block_length(block_length));
chksum = adler32_str(chksum, predictor, sizeof(predictor));
idat = buf_cat_str(idat, predictor, sizeof(predictor));
row_data = buf_cat_str_data(row_data, d, width*4);
chksum = adler32_buf(chksum, row_data);
idat = buf_cat_str_data(idat, d, width*4);
free(row_data.data);
d = (char*)d + stride;
}
// be32 leaks
idat = buf_cat(idat, be32(adler32_fin(chksum)));
r = buf_cat(r, chunk("IDAT", idat));
r = buf_cat(r, chunk("IEND", iend));
return r;
}
void write_png(const char *name, void *d, int width, int height) {
FILE *f = fopen(name, "wb+");
struct buf png = make_png(d, width, height, 4*width, buf_cat_str_argb);
fwrite(png.data, png.len, 1, f);
free(png.data);
fclose(f);
}
void write_png_a8(const char *name, void *d, int width, int height, int stride) {
FILE *f = fopen(name, "wb+");
struct buf png = make_png(d, width, height, stride, buf_cat_str_a8);
fwrite(png.data, png.len, 1, f);
free(png.data);
fclose(f);
}
void write_png_rgb(const char *name, void *d, int width, int height, int stride) {
FILE *f = fopen(name, "wb+");
struct buf png = make_png(d, width, height, stride, buf_cat_str_rgb);
fwrite(png.data, png.len, 1, f);
free(png.data);
fclose(f);
}
void write_png_565(const char *name, void *d, int width, int height, int stride) {
FILE *f = fopen(name, "wb+");
struct buf png = make_png(d, width, height, stride, buf_cat_str_565);
fwrite(png.data, png.len, 1, f);
free(png.data);
fclose(f);
}