forked from floft/sprouts
-
Notifications
You must be signed in to change notification settings - Fork 3
/
png_decoder.cpp
134 lines (110 loc) · 3.07 KB
/
png_decoder.cpp
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
#include "png_decoder.h"
#include <png.h>
#include <cstdio>
#include <cstdlib>
#include <cerrno>
#include "util.h"
using namespace std;
/**************
*Description:
*Input:
*Output:
**************/
namespace
{
void pngloaderror(png_structp png_ptr, png_const_charp msg)
{
*(string *)png_get_error_ptr(png_ptr) = msg;
longjmp(png_jmpbuf(png_ptr), 1);
}
void pngloadwarning(png_structp, png_const_charp)
{
// do nothing
}
void readBytes(png_structp png_ptr, png_bytep output, png_size_t count)
{
png_voidp user_data = png_get_io_ptr(png_ptr);
Reader & reader = *(Reader *)user_data;
try
{
reader.readBytes((uint8_t *)output, count);
}
catch(IOException & e)
{
*(string *)png_get_error_ptr(png_ptr) = e.what();
longjmp(png_jmpbuf(png_ptr), 1);
}
}
inline bool LoadPNG(Reader * preader, uint8_t *&pixels, unsigned &width, unsigned &height, string &errorMsg)
{
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (void *)&errorMsg, pngloaderror, pngloadwarning);
if(!png_ptr)
{
errorMsg = "can't create png read struct";
return false;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if(!info_ptr)
{
png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
errorMsg = "can't create png info struct";
return false;
}
uint8_t *volatile retval = NULL; // so that it isn't messed up by longjmp
volatile png_bytepp rows = NULL;
if(setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
delete []retval;
delete []rows;
return false;
}
png_set_read_fn(png_ptr, (png_voidp)preader, readBytes);
png_read_info(png_ptr, info_ptr);
png_uint_32 XRes, YRes;
int bit_depth, color_type, interlace_type;
png_get_IHDR(png_ptr, info_ptr, &XRes, &YRes, &bit_depth, &color_type, &interlace_type, int_p_NULL, int_p_NULL);
png_set_strip_16(png_ptr);
png_set_packing(png_ptr);
if(color_type == PNG_COLOR_TYPE_PALETTE)
{
png_set_palette_to_rgb(png_ptr);
}
if((color_type & PNG_COLOR_MASK_ALPHA) == 0)
{
png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
}
width = XRes;
height = YRes;
retval = new uint8_t[width * height * 4];
if(!retval)
{
errorMsg = "can't allocate image data array";
longjmp(png_jmpbuf(png_ptr), 1);
}
rows = (png_bytepp)new png_bytep[YRes];
if(!rows)
{
errorMsg = "can't allocate image row indirection array";
longjmp(png_jmpbuf(png_ptr), 1);
}
for(unsigned y = 0; y < YRes; y++)
{
rows[y] = (png_bytep)&retval[y * XRes * 4];
}
png_read_image(png_ptr, rows);
png_read_end(png_ptr, info_ptr);
delete []rows;
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
pixels = (uint8_t *)retval;
return true;
}
}
PngDecoder::PngDecoder(Reader & reader)
{
string errorMsg;
if(!LoadPNG(&reader, data, w, h, errorMsg))
{
throw PngLoadError(errorMsg);
}
}