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
|
/*
render/png.cc
This file is part of the Osirion project and is distributed under
the terms of the GNU General Public License version 2
*/
/*
Notes:
http://www.zarb.org/~gc/html/libpng.html
*/
#include <string.h>
#include <png.h>
#include <iostream>
#include "filesystem/filesystem.h"
#include "render/pngfile.h"
#include "sys/sys.h"
namespace render {
Image *PNG::load(const char *filename)
{
Image *image = 0;
if (!filename)
return 0;
filesystem::File *png_file = filesystem::open(filename);
if (!png_file) {
//con_warn << "Could not open " << filename << std::endl;
return 0;
}
png_byte header[8];
memset(header, 0, sizeof(header));
if (!png_file->read(header, 8)) {
con_warn << "Error reading " << filename << std::endl;
filesystem::close(png_file);
return 0;
}
if (png_sig_cmp(header, 0, 8)) {
con_warn << "Error reading " << filename << ": not a PNG file!" << std::endl;
filesystem::close(png_file);
return 0;
}
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
con_warn << "Error reading " << filename << ": png_create_read_struct failed!" << std::endl;
filesystem::close(png_file);
return 0;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
con_warn << "Error reading " << filename << ": png_create_info_struct failed!" << std::endl;
filesystem::close(png_file);
return 0;
}
if (setjmp(png_jmpbuf(png_ptr))) {
con_warn << "Error reading " << filename << ": error during init_io!" << std::endl;
filesystem::close(png_file);
return 0;
}
/* read the PNG header */
png_init_io(png_ptr, png_file->handle());
png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr);
int png_width = info_ptr->width;
int png_height = info_ptr->height;
//int png_color_type = info_ptr->color_type;
int png_depth = info_ptr->bit_depth;
//int number_of_passes = png_set_interlace_handling(png_ptr);
png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);
if (png_depth != 8) {
con_warn << "Error reading " << filename << ": bits per channel must be 8!" << std::endl;
filesystem::close(png_file);
return 0;
}
unsigned int channels = info_ptr->rowbytes / png_width;
image = new Image(png_width, png_height, channels);
/* read image data */
if (setjmp(png_jmpbuf(png_ptr))) {
con_warn << "Error reading " << filename << std::endl;
filesystem::close(png_file);
delete image;
return 0;
}
png_bytep row_pointers[png_height];
for (size_t i=0; i < (size_t)png_height; i++)
row_pointers[i] = (png_bytep) (*image)[i * info_ptr->rowbytes];
png_read_image(png_ptr, row_pointers);
filesystem::close(png_file);
con_debug << " " << filename << " " << png_width << "x" << png_height << "x" << channels * png_depth << "bpp" << std::endl;
return image;
}
void PNG::save(const char *filename, Image & image)
{
con_warn << "PNG::save stub" << std::endl;
}
}
|