-
Notifications
You must be signed in to change notification settings - Fork 0
/
pngreader.h
410 lines (341 loc) · 12.5 KB
/
pngreader.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
402
403
404
405
406
407
408
409
410
//#pragma comment (lib, "../../Image/PNG/libpng13.lib") // FIXME: incorrect path for all occasions
#ifndef PNGIO_H
#define PNGIO_H
#include <png.h>
class PNGIO
{
public:
static const int ERR_NOERR = 1;
static const int ERR_FOPEN = -1;
static const int ERR_PNG_WRITE_STRUCT = -2;
static const int ERR_PNG_READ_STRUCT = -3;
static const int ERR_PNG_INFO_STRUCT = -4;
static const int ERR_PNG_JUMP_HIT = -5;
static const int ERR_BAD_COLOUR_TYPE = -6;
static const int ERR_INTERLACED_FORMAT = -7;
static const int ERR_INCORRECT_BUFFER_FORMAT = -8;
private:
// custom read function and struct required by libpng incase the CRT libraries we use are incompatible
static void png_custom_read_fn (
png_structp png_ptr,
png_bytep data,
png_size_t length)
{
FILE *f = (FILE *) png_get_io_ptr (png_ptr);
if (f == NULL)
return;
fread (data, 1, length, f);
}
static void png_custom_write_fn (
png_structp png_ptr,
png_bytep data,
png_size_t length)
{
FILE *f = (FILE *) png_get_io_ptr (png_ptr);
if (f == NULL)
return;
fwrite (data, length, 1, f);
}
public:
#ifdef IMAGE_H
// reads a png file from disk and creates an Image from it
static Image<unsigned char, 2, 3> *readPNG (const char *filename)
{
FILE *f = NULL;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
int bd, ct, it;
png_uint_32 w, h;
Image<unsigned char, 2, 3> *image = NULL;
// open the file
f = fopen (filename, "rb\0");
if (f == NULL)
return NULL;
// create the nessecary read structures
if ((png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) == NULL)
{
fclose (f);
return NULL;
}
if ((info_ptr = png_create_info_struct (png_ptr)) == NULL)
{
png_destroy_read_struct (&png_ptr, NULL, NULL);
fclose (f);
return NULL;
}
// basic error handling.
if (setjmp (png_jmpbuf(png_ptr)))
{
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
fclose (f);
return NULL;
}
png_set_read_fn (png_ptr, (voidp) f, png_custom_read_fn); // set our custom read function
png_read_info (png_ptr, info_ptr); // start with the header
png_get_IHDR (png_ptr, info_ptr, &w, &h, &bd, &ct, &it, NULL, NULL); // dimensions
png_byte *data;
// if the png can be read into our limited Image, read it
if ((bd == 8) && (ct == PNG_COLOR_TYPE_RGB) && (it == PNG_INTERLACE_NONE))
{
data = new png_byte[w*h*3]; // initialise the internal image data array
png_byte **row_pointers = new png_byte*[h]; // list of pointers to the begining of each row
for (unsigned int i=0; i<h; i++)
{
row_pointers[i] = &data[i*(w*3)];
}
png_read_image (png_ptr, row_pointers); // read the entire image into the array
delete[] row_pointers; // free the row pointers
}
png_read_end (png_ptr, info_ptr); // finish reading
png_destroy_read_struct (&png_ptr, &info_ptr, NULL); // clean up
fclose (f); // close the file
image = new Image<unsigned char, 2, 3> (w*h); // create the image
image->setSize (image->WIDTH, w); // set the dimensions
image->setSize (image->HEIGHT, h);
for (unsigned int i=0; i<(w*h); i++) // copy the data
{
// image->samples[i].set (data[(i*3)+0], data[(i*3)+1], data[(i*3)+2]);
image->samples[i].set (&data[(i*3)]);
}
delete[] data; // delete the buffer
return image; // return the image
}
static void writePNG (
const Image<unsigned char, 2, 3> *image,
const char *filename)
{
FILE *f = NULL;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
f = fopen (filename, "wb\0");
if (f == NULL)
return;
// create the write stucture
if ((png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) == NULL)
{
fclose (f);
return;
}
// create the info structure
if ((info_ptr = png_create_info_struct (png_ptr)) == NULL)
{
png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
fclose (f);
return;
}
// setjump for pnglib to return to this function on fail (it calls longjump)
if (setjmp (png_jmpbuf (png_ptr)))
{
png_destroy_write_struct (&png_ptr, &info_ptr);
fclose (f);
return;
}
// A problem with the pnglib: by using the FILE structure we pass, we may have a different size
// struct (depending on the link lib. So override the png write functions and provide our own.
png_set_write_fn (png_ptr, (void *)f, png_custom_write_fn, NULL);
png_set_IHDR (png_ptr, info_ptr, image->getSize (image->WIDTH), image->getSize (image->HEIGHT),
8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info (png_ptr, info_ptr); // start writing the file
unsigned char *row = new unsigned char[image->getSize (image->WIDTH) * 3]; // buffer for a row of image data
for (unsigned int i=0; i<image->getSize (image->HEIGHT); i++)
{
for (unsigned int j=0; j<image->getSize (image->WIDTH); j++)
image->samples[j+(i*image->getSize (image->WIDTH))].get (&row[j*3]); // get the pixel data
png_write_row (png_ptr, row); // write the row to disk
}
delete row; // delete the buffer
png_write_end (png_ptr, info_ptr); // end writing
png_destroy_write_struct (&png_ptr, &info_ptr); // cleanup
fclose (f); // close the file
}
#endif
// image buffer functions
/*===============================================
write a pixel buffer to a png file on disk
===============================================*/
static int writePixelBuffer (
const char *filename,
const unsigned char *buffer,
const unsigned int width,
const unsigned int height,
const unsigned int bitdepth = 8,
const unsigned int depth = 3,
bool flip = false)
{
FILE *f = NULL;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
int colourType = 0;
f = fopen (filename, "wb\0");
if (f == NULL)
{
return ERR_FOPEN;
}
// create the write stucture
if ((png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) == NULL)
{
fclose (f);
return ERR_PNG_WRITE_STRUCT;
}
// create the info structure
if ((info_ptr = png_create_info_struct (png_ptr)) == NULL)
{
png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
fclose (f);
return ERR_PNG_INFO_STRUCT;
}
// setjump for pnglib to return to this function on fail (it calls longjump)
if (setjmp (png_jmpbuf (png_ptr)))
{
png_destroy_write_struct (&png_ptr, &info_ptr);
fclose (f);
return ERR_PNG_JUMP_HIT;
}
/* A problem with the pnglib: by using the FILE structure we pass, we may have a
different size struct (depending on the link lib. we override the png write
functions and provide our own.*/
png_set_write_fn (png_ptr, (void *)f, png_custom_write_fn, NULL);
switch (depth)
{
case 1: colourType = PNG_COLOR_TYPE_GRAY; break;
case 2: colourType = PNG_COLOR_TYPE_GRAY_ALPHA; break;
case 3: colourType = PNG_COLOR_TYPE_RGB; break;
case 4: colourType = PNG_COLOR_TYPE_RGBA; break;
default:
png_destroy_write_struct (&png_ptr, &info_ptr);
fclose (f);
return ERR_BAD_COLOUR_TYPE;
}
// png_set_IHDR (png_ptr, info_ptr, width, height, 8, colourType, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_set_IHDR (png_ptr, info_ptr, width, height, bitdepth, colourType, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info (png_ptr, info_ptr); // start writing the file
if (flip)
{
for (unsigned int i=height; i>0; i--)
png_write_row (png_ptr, const_cast<png_bytep>(&buffer[(i-1)*width*depth])); // write the row to disk
}
else
{
for (unsigned int i=0; i<height; i++)
png_write_row (png_ptr, const_cast<png_bytep>(&buffer[i*width*depth])); // write the row to disk
}
png_write_end (png_ptr, info_ptr); // end writing
png_destroy_write_struct (&png_ptr, &info_ptr); // cleanup
fclose (f); // close the file
return ERR_NOERR;
}
/*===============================================
read a pixel buffer from a png file on disk.
Note, buffer must be large enough to hold the
file. If buffer == NULL, the parameters will
be filled so a correctly sized buffer can be
allocated.
===============================================*/
static const unsigned int GS = PNG_COLOR_TYPE_GRAY;
static const unsigned int RGB = PNG_COLOR_TYPE_RGB;
static const unsigned int RGBA = PNG_COLOR_TYPE_RGB_ALPHA;
static int readPixelBuffer (
const char *filename,
unsigned char *buffer,
unsigned int& width,
unsigned int& height,
unsigned int& bitdepth,
unsigned int& colourType,
const bool flip = true)
{
FILE *f = NULL;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
int bd, d, ct, it;
png_uint_32 w, h;
// open the file
f = fopen (filename, "rb\0");
if (f == NULL)
{
return ERR_FOPEN;
}
// create the nessecary read structures
if ((png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) == NULL)
{
fclose (f);
return ERR_PNG_READ_STRUCT;
}
if ((info_ptr = png_create_info_struct (png_ptr)) == NULL)
{
png_destroy_read_struct (&png_ptr, NULL, NULL);
fclose (f);
return ERR_PNG_INFO_STRUCT;
}
// basic error handling.
if (setjmp (png_jmpbuf (png_ptr)))
{
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
fclose (f);
return ERR_PNG_JUMP_HIT;
}
// set the custom read function and read the header
png_set_read_fn (png_ptr, (void*)f, png_custom_read_fn);
png_read_info (png_ptr, info_ptr);
// get the dimensions of the image
png_get_IHDR (png_ptr, info_ptr, &w, &h, &bd, &ct, &it, NULL, NULL); // dimensions
// caller wants the image information, not the image
if (buffer == NULL)
{
width = w;
height = h;
bitdepth = bd;
colourType = ct;
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
fclose (f);
return ERR_NOERR;
}
else // caller wants the image, check the details are correct
{
if ((width != w) ||
(height != h) ||
(bitdepth != static_cast<unsigned int>(bd)) ||
(colourType != static_cast<unsigned int>(ct)))
{
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
fclose (f);
return ERR_INCORRECT_BUFFER_FORMAT;
}
switch (ct)
{
case GS: d = 1; break;
case RGB: d = 3; break;
case RGBA: d = 4; break;
}
}
// can't handle interlaced files yet (I don't know what the difference is)
if (it != PNG_INTERLACE_NONE)
{
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
fclose (f);
return ERR_INTERLACED_FORMAT;
}
// if the png can be read into the buffer
// data = new png_byte[w*h*3]; // initialise the internal image data array
png_byte **row_pointers = new png_byte*[h]; // list of pointers to the begining of each row
if (flip)
{
for (int i=h-1; i>0-1; i--)
row_pointers[i] = &buffer[i*(w*d)]; // FIXME: '3' is the colour type * bitdepth or something
}
else
{
for (unsigned int i=0; i<h; i++)
row_pointers[i] = &buffer[i*(w*d)]; // FIXME: '3' is the colour type * bitdepth or something
}
png_read_image (png_ptr, row_pointers); // read the entire image into the array
// cleanup
delete[] row_pointers; // free the row pointers
row_pointers = NULL;
png_read_end (png_ptr, info_ptr); // finish reading
png_destroy_read_struct (&png_ptr, &info_ptr, NULL); // clean up
fclose (f); // close the file
// return success
return ERR_NOERR;
}
};
#endif