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
|
/*
render/pngfile.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 "png.h"
#include <string.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);
png_destroy_read_struct(&png_ptr, 0, 0);
return 0;
}
if (setjmp(png_jmpbuf(png_ptr))) {
con_warn << "Error reading " << filename << ": error during init_io!" << std::endl;
filesystem::close(png_file);
png_destroy_read_struct(&png_ptr, &info_ptr, 0);
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);
png_destroy_read_struct(&png_ptr, &info_ptr, 0);
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;
png_destroy_read_struct(&png_ptr, &info_ptr, 0);
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];
// read pixel data
png_read_image(png_ptr, row_pointers);
filesystem::close(png_file);
png_destroy_read_struct(&png_ptr, &info_ptr, 0);
con_debug << " " << filename << " " << image->width() << "x" << image->height() << "x" << image->bpp() << "bpp" << std::endl;
return image;
}
void PNG::save(const char *filename, Image & image)
{
FILE *png_file = fopen(filename, "wb");
if (!png_file) {
con_warn << "Could not write " << filename << std::endl;
return;
}
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
fclose(png_file);
con_warn << "Error writing " << filename << ": png_create_write_struct failed!" << std::endl;
return;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
con_warn << "Error writing " << filename << ": png_create_info_struct failed!" << std::endl;
fclose(png_file);
png_destroy_write_struct(&png_ptr, 0);
return;
}
if (setjmp(png_jmpbuf(png_ptr))) {
con_warn << "Error reading " << filename << ": error during init_io!" << std::endl;
fclose(png_file);
png_destroy_write_struct(&png_ptr, &info_ptr);
return;
}
png_init_io(png_ptr, png_file);
/* write header */
if (setjmp(png_jmpbuf(png_ptr))) {
con_warn << "Error writing " << filename << ": error writing header!" << std::endl;
fclose(png_file);
png_destroy_write_struct(&png_ptr, &info_ptr);
return;
}
png_set_IHDR(png_ptr, info_ptr, image.width(), image.height(), 8, PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
/* write image data */
if (setjmp(png_jmpbuf(png_ptr))) {
con_warn << "Error writing " << filename << ": error writing header!" << std::endl;
fclose(png_file);
png_destroy_write_struct(&png_ptr, &info_ptr);
return;
}
png_bytep row_pointers[image.height()];
for (size_t i = 0; i < image.height(); i++)
row_pointers[i] = (png_bytep) image[i * image.width() * image.channels()];
png_write_image(png_ptr, row_pointers);
/* end write */
if (setjmp(png_jmpbuf(png_ptr))) {
con_warn << "Error writing " << filename << std::endl;
fclose(png_file);
png_destroy_write_struct(&png_ptr, &info_ptr);
return;
}
png_write_end(png_ptr, NULL);
fclose(png_file);
png_destroy_write_struct(&png_ptr, &info_ptr);
con_print << "Wrote " << filename << std::endl;
}
}
|