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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
|
/*
* pnglite.h - Interface for pnglite library
* Copyright (c) 2007 Daniel Karling
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*
* Daniel Karling
* daniel.karling@gmail.com
*/
pnglite
/*
* pnglite.c - pnglite library
* For conditions of distribution and use, see copyright notice in pnglite.h
*/
#ifdef _STANDALONE
#include <sys/cdefs.h>
#include <stand.h>
#else
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif
#include <zlib.h>
#include "pnglite.h"
#ifndef abs
#define abs(x) ((x) < 0? -(x):(x))
#endif
#define PNG_32b(b, s) ((uint32_t)(b) << (s))
#define PNG_U32(b1, b2, b3, b4) \
(PNG_32b(b1, 24) | PNG_32b(b2, 16) | PNG_32b(b3, 8) | PNG_32b(b4, 0))
#define png_IDAT PNG_U32(73, 68, 65, 84)
#define png_IEND PNG_U32(73, 69, 78, 68)
static ssize_t
file_read(png_t *png, void *out, size_t size, size_t numel)
{
ssize_t result;
off_t offset = (off_t)(size * numel);
if (offset < 0)
return (PNG_FILE_ERROR);
if (!out) {
result = lseek(png->fd, offset, SEEK_CUR);
} else {
result = read(png->fd, out, size * numel);
}
return (result);
}
static int
file_read_ul(png_t *png, unsigned *out)
{
uint8_t buf[4];
if (file_read(png, buf, 1, 4) != 4)
return (PNG_FILE_ERROR);
*out = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
return (PNG_NO_ERROR);
}
static unsigned
get_ul(uint8_t *buf)
{
unsigned result;
uint8_t foo[4];
memcpy(foo, buf, 4);
result = (foo[0]<<24) | (foo[1]<<16) | (foo[2]<<8) | foo[3];
return (result);
}
static int
png_get_bpp(png_t *png)
{
int bpp;
switch (png->color_type) {
case PNG_GREYSCALE:
bpp = 1; break;
case PNG_TRUECOLOR:
bpp = 3; break;
case PNG_INDEXED:
bpp = 1; break;
case PNG_GREYSCALE_ALPHA:
bpp = 2; break;
case PNG_TRUECOLOR_ALPHA:
bpp = 4; break;
default:
return (PNG_FILE_ERROR);
}
bpp *= png->depth / 8;
return (bpp);
}
static int
png_read_ihdr(png_t *png)
{
unsigned length = 0;
unsigned orig_crc;
unsigned calc_crc;
uint8_t ihdr[13+4]; /* length should be 13, make room for type (IHDR) */
if (file_read_ul(png, &length) != PNG_NO_ERROR)
return (PNG_FILE_ERROR);
if (length != 13)
return (PNG_CRC_ERROR);
if (file_read(png, ihdr, 1, 13+4) != 13+4)
return (PNG_EOF_ERROR);
if (file_read_ul(png, &orig_crc) != PNG_NO_ERROR)
return (PNG_FILE_ERROR);
calc_crc = crc32(0L, Z_NULL, 0);
calc_crc = crc32(calc_crc, ihdr, 13+4);
if (orig_crc != calc_crc) {
return (PNG_CRC_ERROR);
}
png->width = get_ul(ihdr+4);
png->height = get_ul(ihdr+8);
png->depth = ihdr[12];
png->color_type = ihdr[13];
png->compression_method = ihdr[14];
png->filter_method = ihdr[15];
png->interlace_method = ihdr[16];
if (png->color_type == PNG_INDEXED)
return (PNG_NOT_SUPPORTED);
if (png->depth != 8 && png->depth != 16)
return (PNG_NOT_SUPPORTED);
if (png->interlace_method)
return (PNG_NOT_SUPPORTED);
return (PNG_NO_ERROR);
}
void
png_print_info(png_t *png)
{
printf("PNG INFO:\n");
printf("\twidth:\t\t%d\n", png->width);
printf("\theight:\t\t%d\n", png->height);
printf("\tdepth:\t\t%d\n", png->depth);
printf("\tcolor:\t\t");
switch (png->color_type) {
case PNG_GREYSCALE:
printf("greyscale\n"); break;
case PNG_TRUECOLOR:
printf("truecolor\n"); break;
case PNG_INDEXED:
printf("palette\n"); break;
case PNG_GREYSCALE_ALPHA:
printf("greyscale with alpha\n"); break;
case PNG_TRUECOLOR_ALPHA:
printf("truecolor with alpha\n"); break;
default:
printf("unknown, this is not good\n"); break;
}
printf("\tcompression:\t%s\n",
png->compression_method?
"unknown, this is not good":"inflate/deflate");
printf("\tfilter:\t\t%s\n",
png->filter_method? "unknown, this is not good":"adaptive");
printf("\tinterlace:\t%s\n",
png->interlace_method? "interlace":"no interlace");
}
int
png_open(png_t *png, const char *filename)
{
char header[8];
int result;
png->image = NULL;
png->fd = open(filename, O_RDONLY);
if (png->fd == -1)
return (PNG_FILE_ERROR);
if (file_read(png, header, 1, 8) != 8) {
result = PNG_EOF_ERROR;
goto done;
}
if (memcmp(header, "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", 8) != 0) {
result = PNG_HEADER_ERROR;
goto done;
}
result = png_read_ihdr(png);
if (result == PNG_NO_ERROR) {
result = png_get_bpp(png);
if (result > 0) {
png->bpp = (uint8_t)result;
result = PNG_NO_ERROR;
}
}
done:
if (result == PNG_NO_ERROR) {
uint64_t size = png->width * png->height * png->bpp;
if (size < UINT_MAX)
png->image = malloc(size);
if (png->image == NULL)
result = PNG_MEMORY_ERROR;
}
if (result == PNG_NO_ERROR)
result = png_get_data(png, png->image);
if (result != PNG_NO_ERROR) {
free(png->image);
(void) close(png->fd);
png->fd = -1;
return (result);
}
return (result);
}
int
png_close(png_t *png)
{
(void) close(png->fd);
png->fd = -1;
free(png->image);
png->image = NULL;
return (PNG_NO_ERROR);
}
static int
png_init_inflate(png_t *png)
{
z_stream *stream;
png->zs = calloc(1, sizeof (z_stream));
stream = png->zs;
if (!stream)
return (PNG_MEMORY_ERROR);
if (inflateInit(stream) != Z_OK) {
free(png->zs);
png->zs = NULL;
return (PNG_ZLIB_ERROR);
}
stream->next_out = png->png_data;
stream->avail_out = png->png_datalen;
return (PNG_NO_ERROR);
}
static int
png_end_inflate(png_t *png)
{
z_stream *stream = png->zs;
int rc = PNG_NO_ERROR;
if (!stream)
return (PNG_MEMORY_ERROR);
if (inflateEnd(stream) != Z_OK) {
printf("ZLIB says: %s\n", stream->msg);
rc = PNG_ZLIB_ERROR;
}
free(png->zs);
png->zs = NULL;
return (rc);
}
static int
png_inflate(png_t *png, uint8_t *data, int len)
{
int result;
z_stream *stream = png->zs;
if (!stream)
return (PNG_MEMORY_ERROR);
stream->next_in = data;
stream->avail_in = len;
result = inflate(stream, Z_SYNC_FLUSH);
if (result != Z_STREAM_END && result != Z_OK) {
printf("%s\n", stream->msg);
return (PNG_ZLIB_ERROR);
}
if (stream->avail_in != 0)
return (PNG_ZLIB_ERROR);
return (PNG_NO_ERROR);
}
static int
png_read_idat(png_t *png, unsigned length)
{
unsigned orig_crc;
unsigned calc_crc;
ssize_t len = length;
if (!png->readbuf || png->readbuflen < length) {
png->readbuf = realloc(png->readbuf, length);
png->readbuflen = length;
}
if (!png->readbuf)
return (PNG_MEMORY_ERROR);
if (file_read(png, png->readbuf, 1, length) != len)
return (PNG_FILE_ERROR);
calc_crc = crc32(0L, Z_NULL, 0);
calc_crc = crc32(calc_crc, (uint8_t *)"IDAT", 4);
calc_crc = crc32(calc_crc, (uint8_t *)png->readbuf, length);
if (file_read_ul(png, &orig_crc) != PNG_NO_ERROR)
return (PNG_FILE_ERROR);
if (orig_crc != calc_crc)
return (PNG_CRC_ERROR);
return (png_inflate(png, png->readbuf, length));
}
static int
png_process_chunk(png_t *png)
{
int result = PNG_NO_ERROR;
unsigned type;
unsigned length;
if (file_read_ul(png, &length) != PNG_NO_ERROR)
return (PNG_FILE_ERROR);
if (file_read_ul(png, &type) != PNG_NO_ERROR)
return (PNG_FILE_ERROR);
/*
* if we found an idat, all other idats should be followed with no
* other chunks in between
*/
if (type == png_IDAT) {
if (!png->png_data) { /* first IDAT */
png->png_datalen = png->width * png->height *
png->bpp + png->height;
png->png_data = malloc(png->png_datalen);
}
if (!png->png_data)
return (PNG_MEMORY_ERROR);
if (!png->zs) {
result = png_init_inflate(png);
if (result != PNG_NO_ERROR)
return (result);
}
return (png_read_idat(png, length));
} else if (type == png_IEND)
return (PNG_DONE);
else
(void) file_read(png, 0, 1, length + 4); /* unknown chunk */
return (result);
}
static void
png_filter_sub(unsigned stride, uint8_t *in, uint8_t *out, unsigned len)
{
unsigned i;
uint8_t a = 0;
for (i = 0; i < len; i++) {
if (i >= stride)
a = out[i - stride];
out[i] = in[i] + a;
}
}
static void
png_filter_up(unsigned stride __unused, uint8_t *in, uint8_t *out,
uint8_t *prev_line, unsigned len)
{
unsigned i;
if (prev_line) {
for (i = 0; i < len; i++)
out[i] = in[i] + prev_line[i];
} else
memcpy(out, in, len);
}
static void
png_filter_average(unsigned stride, uint8_t *in, uint8_t *out,
uint8_t *prev_line, unsigned len)
{
unsigned int i;
uint8_t a = 0;
uint8_t b = 0;
unsigned int sum = 0;
for (i = 0; i < len; i++) {
if (prev_line)
b = prev_line[i];
if (i >= stride)
a = out[i - stride];
sum = a;
sum += b;
out[i] = in[i] + sum/2;
}
}
static uint8_t
png_paeth(uint8_t a, uint8_t b, uint8_t c)
{
int p = (int)a + b - c;
int pa = abs(p - a);
int pb = abs(p - b);
int pc = abs(p - c);
int pr;
if (pa <= pb && pa <= pc)
pr = a;
else if (pb <= pc)
pr = b;
else
pr = c;
return (pr);
}
static void
png_filter_paeth(unsigned stride, uint8_t *in, uint8_t *out, uint8_t *prev_line,
unsigned len)
{
unsigned i;
uint8_t a;
uint8_t b;
uint8_t c;
for (i = 0; i < len; i++) {
if (prev_line && i >= stride) {
a = out[i - stride];
b = prev_line[i];
c = prev_line[i - stride];
} else {
if (prev_line)
b = prev_line[i];
else
b = 0;
if (i >= stride)
a = out[i - stride];
else
a = 0;
c = 0;
}
out[i] = in[i] + png_paeth(a, b, c);
}
}
static int
png_unfilter(png_t *png, uint8_t *data)
{
unsigned i;
unsigned pos = 0;
unsigned outpos = 0;
uint8_t *filtered = png->png_data;
unsigned stride = png->bpp;
while (pos < png->png_datalen) {
uint8_t filter = filtered[pos];
pos++;
if (png->depth == 16) {
for (i = 0; i < png->width * stride; i += 2) {
*(short *)(filtered+pos+i) =
(filtered[pos+i] << 8) | filtered[pos+i+1];
}
}
switch (filter) {
case 0: /* none */
memcpy(data+outpos, filtered+pos, png->width * stride);
break;
case 1: /* sub */
png_filter_sub(stride, filtered+pos, data+outpos,
png->width * stride);
break;
case 2: /* up */
if (outpos) {
png_filter_up(stride, filtered+pos, data+outpos,
data + outpos - (png->width*stride),
png->width*stride);
} else {
png_filter_up(stride, filtered+pos, data+outpos,
0, png->width*stride);
}
break;
case 3: /* average */
if (outpos) {
png_filter_average(stride, filtered+pos,
data+outpos,
data + outpos - (png->width*stride),
png->width*stride);
} else {
png_filter_average(stride, filtered+pos,
data+outpos, 0, png->width*stride);
}
break;
case 4: /* paeth */
if (outpos) {
png_filter_paeth(stride, filtered+pos,
data+outpos,
data + outpos - (png->width*stride),
png->width*stride);
} else {
png_filter_paeth(stride, filtered+pos,
data+outpos, 0, png->width*stride);
}
break;
default:
return (PNG_UNKNOWN_FILTER);
}
outpos += png->width * stride;
pos += png->width * stride;
}
return (PNG_NO_ERROR);
}
int
png_get_data(png_t *png, uint8_t *data)
{
int result = PNG_NO_ERROR;
png->zs = NULL;
png->png_datalen = 0;
png->png_data = NULL;
png->readbuf = NULL;
png->readbuflen = 0;
while (result == PNG_NO_ERROR)
result = png_process_chunk(png);
if (png->readbuf) {
free(png->readbuf);
png->readbuflen = 0;
}
if (png->zs)
(void) png_end_inflate(png);
if (result != PNG_DONE) {
free(png->png_data);
return (result);
}
result = png_unfilter(png, data);
free(png->png_data);
return (result);
}
char *
png_error_string(int error)
{
switch (error) {
case PNG_NO_ERROR:
return ("No error");
case PNG_FILE_ERROR:
return ("Unknown file error.");
case PNG_HEADER_ERROR:
return ("No PNG header found. Are you sure this is a PNG?");
case PNG_IO_ERROR:
return ("Failure while reading file.");
case PNG_EOF_ERROR:
return ("Reached end of file.");
case PNG_CRC_ERROR:
return ("CRC or chunk length error.");
case PNG_MEMORY_ERROR:
return ("Could not allocate memory.");
case PNG_ZLIB_ERROR:
return ("zlib reported an error.");
case PNG_UNKNOWN_FILTER:
return ("Unknown filter method used in scanline.");
case PNG_DONE:
return ("PNG done");
case PNG_NOT_SUPPORTED:
return ("The PNG is unsupported by pnglite, too bad for you!");
case PNG_WRONG_ARGUMENTS:
return ("Wrong combination of arguments passed to png_open. "
"You must use either a read_function or supply a file "
"pointer to use.");
default:
return ("Unknown error.");
};
}
/*
* pnglite.h - Interface for pnglite library
* Copyright (c) 2007 Daniel Karling
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*
* Daniel Karling
* daniel.karling@gmail.com
*/
#ifndef _PNGLITE_H_
#define _PNGLITE_H_
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
* Enumerations for pnglite.
* Negative numbers are error codes and 0 and up are okay responses.
*/
enum {
PNG_DONE = 1,
PNG_NO_ERROR = 0,
PNG_FILE_ERROR = -1,
PNG_HEADER_ERROR = -2,
PNG_IO_ERROR = -3,
PNG_EOF_ERROR = -4,
PNG_CRC_ERROR = -5,
PNG_MEMORY_ERROR = -6,
PNG_ZLIB_ERROR = -7,
PNG_UNKNOWN_FILTER = -8,
PNG_NOT_SUPPORTED = -9,
PNG_WRONG_ARGUMENTS = -10
};
/*
* The five different kinds of color storage in PNG files.
*/
enum {
PNG_GREYSCALE = 0,
PNG_TRUECOLOR = 2,
PNG_INDEXED = 3,
PNG_GREYSCALE_ALPHA = 4,
PNG_TRUECOLOR_ALPHA = 6
};
typedef struct {
void *zs; /* pointer to z_stream */
int fd;
unsigned char *image;
unsigned char *png_data;
unsigned png_datalen;
unsigned width;
unsigned height;
unsigned char depth;
unsigned char color_type;
unsigned char compression_method;
unsigned char filter_method;
unsigned char interlace_method;
unsigned char bpp;
unsigned char *readbuf;
unsigned readbuflen;
} png_t;
/*
* Function: png_open
*
* This function is used to open a png file with the internal file
* IO system.
*
* Parameters:
* png - Empty png_t struct.
* filename - Filename of the file to be opened.
*
* Returns:
* PNG_NO_ERROR on success, otherwise an error code.
*/
int png_open(png_t *png, const char *filename);
/*
* Function: png_print_info
*
* This function prints some info about the opened png file to stdout.
*
* Parameters:
* png - png struct to get info from.
*/
void png_print_info(png_t *png);
/*
* Function: png_error_string
*
* This function translates an error code to a human readable string.
*
* Parameters:
* error - Error code.
*
* Returns:
* Pointer to string.
*/
char *png_error_string(int error);
/*
* Function: png_get_data
*
* This function decodes the opened png file and stores the result in data.
* data should be big enough to hold the decoded png.
* Required size will be:
*
* > width*height*(bytes per pixel)
*
* Parameters:
* data - Where to store result.
*
* Returns:
* PNG_NO_ERROR on success, otherwise an error code.
*/
int png_get_data(png_t *png, uint8_t *data);
/*
* Function: png_close
*
* Closes an open png file pointer.
*
* Parameters:
* png - png to close.
*
* Returns:
* PNG_NO_ERROR
*/
int png_close(png_t *png);
#ifdef __cplusplus
}
#endif
#endif /* _PNGLITE_H_ */
|