-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpngw.cpp
237 lines (221 loc) · 7.32 KB
/
pngw.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
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
/*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <png.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "pngw.h"
#define FH_ERROR_OK 0
#define FH_ERROR_FILE 1 /* read/access error */
#define FH_ERROR_FORMAT 2 /* file format error */
#define FH_ERROR_MALLOC 3 /* error during malloc */
#define PNG_BYTES_TO_CHECK 4
#define min(x,y) ((x) < (y) ? (x) : (y))
int fh_png_load(const char *name, unsigned char **buffer, int* xp, int* yp);
int fh_png_id(const char *name)
{
int fd;
char id[4];
fd=open(name,O_RDONLY); if(fd==-1) return(0);
read(fd,id,4);
close(fd);
if(id[1]=='P' && id[2]=='N' && id[3]=='G') return(1);
return(0);
}
int fh_png_load(const char *name, unsigned char **buffer, int* xp, int* yp);
int int_png_load(const char *name, unsigned char **buffer, int* xp, int* yp, int* bpp, bool alpha)
{
static const png_color_16 my_background = {0, 0, 0, 0, 0};
png_structp png_ptr;
png_infop info_ptr;
png_uint_32 width, height;
unsigned int i;
int bit_depth, color_type, interlace_type, number_passes, pass, int_bpp;
png_byte * fbptr;
FILE * fh;
bool updateInfo_alreadyRead;
if(!(fh=fopen(name,"rb")))
return(FH_ERROR_FILE);
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(png_ptr == NULL) {
fclose(fh);
return(FH_ERROR_FORMAT);
}
info_ptr = png_create_info_struct(png_ptr);
if(info_ptr == NULL)
{
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
fclose(fh);
return(FH_ERROR_FORMAT);
}
#if (PNG_LIBPNG_VER < 10500)
if (setjmp(png_ptr->jmpbuf))
#else
if (setjmp(png_jmpbuf(png_ptr)))
#endif
{
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
fclose(fh);
return(FH_ERROR_FORMAT);
}
png_init_io(png_ptr,fh);
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL);
updateInfo_alreadyRead = false;
if (alpha) // 24bit or gray scale PNGs with alpha-channel
{
*bpp = png_get_channels(png_ptr, info_ptr);
if ((*bpp == 2) && (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) {
if (bit_depth < 8) {
/* Extract multiple pixels with bit depths of 1, 2, and 4
from a single byte into separate bytes
(useful for paletted and grayscale images). */
png_set_packing(png_ptr);
/* Expand grayscale images to the full 8 bits
from 1, 2, or 4 bits/pixel */
#if PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR <= 2 && PNG_LIBPNG_VER_RELEASE < 9
png_set_gray_1_2_4_to_8(png_ptr);
#else
png_set_expand_gray_1_2_4_to_8(png_ptr);
#endif
}
/* Expand the grayscale to 24-bit RGB if necessary. */
png_set_gray_to_rgb(png_ptr);
/* Update the users info structure */
png_read_update_info(png_ptr, info_ptr);
updateInfo_alreadyRead = true;
*bpp = png_get_channels(png_ptr, info_ptr);
if (*bpp != 4) {
/* No 4 channels found
load PNG without alpha channel */
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
fclose(fh);
return fh_png_load(name, buffer, xp, yp);
}
}
else if ((*bpp != 4) || !(color_type & PNG_COLOR_MASK_ALPHA)) {
/* No 4 channels & not an alpha channel found
load PNG without alpha channel */
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
fclose(fh);
return fh_png_load(name, buffer, xp, yp);
}
int_bpp = 4;
/* Expand paletted or RGB images with transparency
to full alpha channels so the data will
be available as RGBA quartets. */
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_tRNS_to_alpha(png_ptr);
}else // All other PNGs
{
if (color_type == PNG_COLOR_TYPE_PALETTE)
{
png_set_palette_to_rgb(png_ptr);
png_set_background(png_ptr, (png_color_16*)&my_background, PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
/* other possibility for png_set_background: use png_get_bKGD */
}
if (color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
{
png_set_gray_to_rgb(png_ptr);
png_set_background(png_ptr, (png_color_16*)&my_background, PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
}
/* this test does not trigger for 8bit-paletted PNGs with newer libpng (1.2.36 at least),
but the data delivered is with alpha channel anyway, so always strip alpha for now
*/
#if PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR <= 2 && PNG_LIBPNG_VER_RELEASE < 36
if (color_type & PNG_COLOR_MASK_ALPHA)
#endif
png_set_strip_alpha(png_ptr);
if (bit_depth < 8)
png_set_packing(png_ptr);
int_bpp = 3;
}
if (bit_depth == 16)
png_set_strip_16(png_ptr);
number_passes = png_set_interlace_handling(png_ptr);
/* Update the users info structure */
if (updateInfo_alreadyRead == 0)
png_read_update_info(png_ptr,info_ptr);
unsigned long rowbytes = png_get_rowbytes(png_ptr, info_ptr);
if (width * int_bpp != rowbytes)
{
printf("[pngw.cpp]: Error processing %s - please report (including image).\n", name);
printf(" width: %lu rowbytes: %lu\n", (unsigned long)width, (unsigned long)rowbytes);
fclose(fh);
return(FH_ERROR_FORMAT);
}
for (pass = 0; pass < number_passes; pass++)
{
fbptr = (png_byte *)(*buffer);
for (i = 0; i < height; i++, fbptr += width * int_bpp)
png_read_row(png_ptr, fbptr, NULL);
}
png_read_end(png_ptr, info_ptr);
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
fclose(fh);
return(FH_ERROR_OK);
}
int png_load_ext(const char *name, unsigned char **buffer, int* xp, int* yp, int* bpp)
{
return int_png_load(name, buffer, xp, yp, bpp, true);
}
int fh_png_load(const char *name, unsigned char **buffer, int* xp, int* yp)
{
return int_png_load(name, buffer, xp, yp, NULL, false);
}
int fh_png_getsize(const char *name,int *x,int *y)
{
png_structp png_ptr;
png_infop info_ptr;
png_uint_32 width, height;
int bit_depth, color_type, interlace_type;
FILE *fh;
if(!(fh=fopen(name,"rb"))) return(FH_ERROR_FILE);
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
if(png_ptr == NULL) {
fclose(fh);
return(FH_ERROR_FORMAT);
}
info_ptr = png_create_info_struct(png_ptr);
if(info_ptr == NULL)
{
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
fclose(fh);
return(FH_ERROR_FORMAT);
}
#if (PNG_LIBPNG_VER < 10500)
if (setjmp(png_ptr->jmpbuf))
#else
if (setjmp(png_jmpbuf(png_ptr)))
#endif
{
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
fclose(fh);
return(FH_ERROR_FORMAT);
}
png_init_io(png_ptr,fh);
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,&interlace_type, NULL, NULL);
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
*x=width;
*y=height;
fclose(fh);
return(FH_ERROR_OK);
}