-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpng_util.c
300 lines (235 loc) · 7.27 KB
/
png_util.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
/****************************************************************************
png.c - read and write png images using libpng routines.
Distributed with Xplanet.
Copyright (C) 2002 Hari Nair <[email protected]>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
****************************************************************************/
#include <png_util.h>
int
read_png(const char *filename, int *width, int *height, unsigned char **rgb,
unsigned char **alpha)
{
FILE *infile = fopen(filename, "rb");
png_structp png_ptr;
png_infop info_ptr;
png_bytepp row_pointers;
unsigned char *ptr = NULL;
png_uint_32 w, h;
int bit_depth, color_type, interlace_type;
int i;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
(png_voidp) NULL,
(png_error_ptr) NULL,
(png_error_ptr) NULL);
if (!png_ptr)
{
fclose(infile);
return(0);
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
png_destroy_read_struct(&png_ptr, (png_infopp) NULL,
(png_infopp) NULL);
fclose(infile);
return(0);
}
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
fclose(infile);
return(0);
}
png_init_io(png_ptr, infile);
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type,
&interlace_type, (int *) NULL, (int *) NULL);
*width = (int) w;
*height = (int) h;
*alpha = NULL;
if (color_type == PNG_COLOR_TYPE_RGB_ALPHA
|| color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
{
alpha[0] = malloc(*width * *height);
if (alpha[0] == NULL)
{
fprintf(stderr, "Can't allocate memory for alpha channel in PNG file.\n");
return(0);
}
}
/* Change a paletted/grayscale image to RGB */
if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8)
png_set_expand(png_ptr);
/* Change a grayscale image to RGB */
if (color_type == PNG_COLOR_TYPE_GRAY
|| color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr);
/* If the PNG file has 16 bits per channel, strip them down to 8 */
if (bit_depth == 16) png_set_strip_16(png_ptr);
/* use 1 byte per pixel */
png_set_packing(png_ptr);
row_pointers = malloc(*height * sizeof(png_bytep));
if (row_pointers == NULL)
{
fprintf(stderr, "Can't allocate memory for PNG file.\n");
return(0);
}
for (i = 0; i < *height; i++)
{
row_pointers[i] = malloc(4 * *width);
if (row_pointers == NULL)
{
fprintf(stderr, "Can't allocate memory for PNG line.\n");
return(0);
}
}
png_read_image(png_ptr, row_pointers);
rgb[0] = malloc(3 * *width * *height);
if (rgb[0] == NULL)
{
fprintf(stderr, "Can't allocate memory for PNG file.\n");
return(0);
}
if (alpha[0] == NULL)
{
ptr = rgb[0];
for (i = 0; i < *height; i++)
{
memcpy(ptr, row_pointers[i], 3 * *width);
ptr += 3 * *width;
}
}
else
{
int j;
ptr = rgb[0];
for (i = 0; i < *height; i++)
{
int ipos = 0;
for (j = 0; j < *width; j++)
{
*ptr++ = row_pointers[i][ipos++];
*ptr++ = row_pointers[i][ipos++];
*ptr++ = row_pointers[i][ipos++];
alpha[0][i * *width + j] = row_pointers[i][ipos++];
}
}
}
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
for (i = 0; i < *height; i++) free(row_pointers[i]);
free(row_pointers);
fclose(infile);
return(1);
}
int write_png(FILE *outfile, int width, int height, unsigned char *rgb,
unsigned char *alpha)
{
png_structp png_ptr;
png_infop info_ptr;
png_bytep row_ptr;
int i;
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
(png_voidp) NULL,
(png_error_ptr) NULL,
(png_error_ptr) NULL);
if (!png_ptr) return(0);
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
return(0);
}
png_init_io(png_ptr, outfile);
if (alpha == NULL)
{
png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
png_write_info(png_ptr, info_ptr);
for (i = 0; i < height; i++)
{
row_ptr = rgb + 3 * i * width;
png_write_rows(png_ptr, &row_ptr, 1);
}
}
else
{
int irgb = 0;
int irgba = 0;
int area = width * height;
unsigned char *rgba = malloc(4 * area);
png_set_IHDR(png_ptr, info_ptr, width, height, 8,
PNG_COLOR_TYPE_RGB_ALPHA,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
png_write_info(png_ptr, info_ptr);
for (i = 0; i < area; i++)
{
rgba[irgba++] = rgb[irgb++];
rgba[irgba++] = rgb[irgb++];
rgba[irgba++] = rgb[irgb++];
rgba[irgba++] = alpha[i];
}
for (i = 0; i < height; i++)
{
row_ptr = rgba + 4 * i * width;
png_write_rows(png_ptr, &row_ptr, 1);
}
free(rgba);
}
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
return(1);
}
int write_gray_png(FILE *outfile, int width, int height, float *img, float minI, float maxI){
int n, m;
unsigned char *rgb = (unsigned char*) calloc(3*width*height, sizeof(unsigned char));
for(n=0;n<height;++n){
for(m=0;m<width;++m){
int id = m+n*width;
/* scale intensity into [0,255] */
unsigned int I = (unsigned int) (255*(img[id]-minI)/(maxI-minI));
/* use same intensity in each red-green-blue channel */
rgb[3*id+0] = I;
rgb[3*id+1] = I;
rgb[3*id+2] = I;
}
}
write_png(outfile, width, height, rgb, NULL);
free(rgb);
}
int write_hot_png(FILE *outfile, int width, int height, float *img, float minI, float maxI){
int n, m;
unsigned char *rgb = (unsigned char*) calloc(3*width*height, sizeof(unsigned char));
for(n=0;n<height;++n){
for(m=0;m<width;++m){
int id = m+n*width;
#if 0
int I = (int) (768*sqrt((double)(img[id]-minI)/(maxI-minI)));
if(I<256) rgb[3*id+0] = I;
else if(I<512) rgb[3*id+1] = I-256;
else if(I<768) rgb[3*id+2] = I-512;
#else
int I = (int) (2048*sqrt((double)(img[id]-minI)/(maxI-minI)));
if(I<256) rgb[3*id+2] = 255-I;
else if(I<512) rgb[3*id+1] = 511-I;
else if(I<768) rgb[3*id+0] = 767-I;
else if(I<1024) rgb[3*id+0] = 1023-I;
else if(I<1536) rgb[3*id+1] = 1535-I;
else if(I<2048) rgb[3*id+2] = 2047-I;
#endif
}
}
write_png(outfile, width, height, rgb, NULL);
free(rgb);
}