2 //////////////////////////////////////////////////////////////////////////////////
3 // This program is free software: you can redistribute it and/or modify //
4 // it under the terms of the version 3 of the GNU General Public License //
5 // as published by the Free Software Foundation. //
7 // This program is distributed in the hope that it will be useful, but //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
10 // General Public License for more details. //
12 // You should have received a copy of the GNU General Public License //
13 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
15 // Written by Francois Fleuret //
16 // (C) Ecole Polytechnique Federale de Lausanne //
17 // Contact <pom@epfl.ch> for comments & bug reports //
18 //////////////////////////////////////////////////////////////////////////////////
26 #include <libpng/png.h>
28 #include "rgb_image.h"
30 void RGBImage::allocate() {
31 _bit_plans = new unsigned char **[RGB_DEPTH];
32 _bit_lines = new unsigned char *[RGB_DEPTH * _height];
33 _bit_map = new unsigned char [_width * _height * RGB_DEPTH];
34 for(int k = 0; k < RGB_DEPTH; k++) _bit_plans[k] = _bit_lines + k * _height;
35 for(int k = 0; k < RGB_DEPTH * _height; k++) _bit_lines[k] = _bit_map + k * _width;
38 void RGBImage::deallocate() {
44 RGBImage::RGBImage() : _bit_plans(0), _bit_lines(0), _bit_map(0) { }
46 RGBImage::RGBImage(int width, int height) : _width(width), _height(height) {
48 memset(_bit_map, 0, _width * _height * RGB_DEPTH * sizeof(unsigned char));
51 RGBImage::~RGBImage() {
55 void RGBImage::write_ppm(const char *filename) {
58 if ((outfile = fopen (filename, "wb")) == 0) {
59 fprintf (stderr, "Can't open %s for reading\n", filename);
63 fprintf(outfile, "P6\n%d %d\n255\n", _width, _height);
65 char *raw = new char[_width * _height * 3];
68 for(int y = 0; y < _height; y++) for(int x = 0; x < _width; x++) {
69 raw[k++] = _bit_map[x + _width * (y + _height * RED)];
70 raw[k++] = _bit_map[x + _width * (y + _height * GREEN)];
71 raw[k++] = _bit_map[x + _width * (y + _height * BLUE)];
74 fwrite((void *) raw, sizeof(unsigned char), _width * _height * 3, outfile);
80 void RGBImage::read_ppm(const char *filename) {
81 const int buffer_size = 1024;
83 char buffer[buffer_size];
88 if((infile = fopen (filename, "r")) == 0) {
89 fprintf (stderr, "Can't open %s for reading\n", filename);
93 fgets(buffer, buffer_size, infile);
95 if(strncmp(buffer, "P6", 2) == 0) {
98 fgets(buffer, buffer_size, infile);
99 } while((buffer[0] < '0') || (buffer[0] > '9'));
100 sscanf(buffer, "%d %d", &_width, &_height);
101 fgets(buffer, buffer_size, infile);
102 sscanf(buffer, "%d", &max);
106 unsigned char *raw = new unsigned char[_width * _height * RGB_DEPTH];
107 fread(raw, sizeof(unsigned char), _width * _height * RGB_DEPTH, infile);
110 for(int y = 0; y < _height; y++) for(int x = 0; x < _width; x++) {
111 _bit_plans[RED][y][x] = raw[k++];
112 _bit_plans[GREEN][y][x] = raw[k++];
113 _bit_plans[BLUE][y][x] = raw[k++];
118 } else if(strncmp(buffer, "P5", 2) == 0) {
121 fgets(buffer, buffer_size, infile);
122 } while((buffer[0] < '0') || (buffer[0] > '9'));
123 sscanf(buffer, "%d %d", &_width, &_height);
124 fgets(buffer, buffer_size, infile);
125 sscanf(buffer, "%d", &max);
129 unsigned char *pixbuf = new unsigned char[_width * _height];
130 fread(buffer, sizeof(unsigned char), _width * _height, infile);
133 for(int y = 0; y < _height; y++) for(int x = 0; x < _width; x++) {
134 unsigned char c = pixbuf[k++];
143 cerr << "Can not read ppm of type [" << buffer << "] from " << filename << ".\n";
148 void RGBImage::read_png(const char* filename) {
149 // This is the number of bytes the read_png routine will read to
150 // decide if the file is a PNG or not. According to the png
151 // documentation, it can be 1 to 8 bytes, 8 being the max and the
154 const int header_size = 8;
156 png_byte header[header_size];
157 png_bytep *row_pointers;
162 FILE *fp = fopen(filename, "rb");
164 cerr << "Unable to open file " << filename << " for reading.\n";
169 fread(header, 1, header_size, fp);
170 if (png_sig_cmp(header, 0, header_size)) {
171 cerr << "File " << filename << " does not look like PNG.\n";
176 // create png pointer
177 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
179 cerr << "png_create_read_struct failed\n";
184 // create png info struct
185 png_infop info_ptr = png_create_info_struct(png_ptr);
187 png_destroy_read_struct(&png_ptr, (png_infopp) 0, (png_infopp) 0);
188 cerr << "png_create_info_struct failed\n";
194 png_init_io(png_ptr, fp);
195 png_set_sig_bytes(png_ptr, header_size);
196 png_read_info(png_ptr, info_ptr);
198 _width = info_ptr->width;
199 _height = info_ptr->height;
201 png_byte bit_depth, color_type, channels;
202 color_type = info_ptr->color_type;
203 bit_depth = info_ptr->bit_depth;
204 channels = info_ptr->channels;
207 cerr << "Can only read 8-bits PNG images." << endl;
211 // allocate image pointer
212 row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * _height);
213 for (int y = 0; y < _height; y++)
214 row_pointers[y] = (png_byte*) malloc(info_ptr->rowbytes);
219 png_read_image(png_ptr, row_pointers);
221 // send image to red, green and blue buffers
222 switch (color_type) {
223 case PNG_COLOR_TYPE_GRAY:
225 unsigned char pixel = 0;
226 for (int y = 0; y < _height; y++) for (int x = 0; x < _width; x++) {
227 pixel = row_pointers[y][x];
228 _bit_plans[RED][y][x] = pixel;
229 _bit_plans[GREEN][y][x] = pixel;
230 _bit_plans[BLUE][y][x] = pixel;
235 case PNG_COLOR_TYPE_GRAY_ALPHA:
236 cerr << "PNG type GRAY_ALPHA not supported.\n";
240 case PNG_COLOR_TYPE_PALETTE:
241 cerr << "PNG type PALETTE not supported.\n";
245 case PNG_COLOR_TYPE_RGB:
247 if(channels != RGB_DEPTH) {
248 cerr << "Unsupported number of channels for RGB type\n";
252 for (int y = 0; y < _height; y++) {
254 for (int x = 0; x < _width; x++) {
255 _bit_plans[RED][y][x] = row_pointers[y][k++];
256 _bit_plans[GREEN][y][x] = row_pointers[y][k++];
257 _bit_plans[BLUE][y][x] = row_pointers[y][k++];
263 case PNG_COLOR_TYPE_RGB_ALPHA:
264 cerr << "PNG type RGB_ALPHA not supported.\n";
269 cerr << "Unknown PNG type\n";
274 png_destroy_read_struct(&png_ptr, &info_ptr, 0);
276 for (int y = 0; y < _height; y++) free(row_pointers[y]);
282 void RGBImage::write_png(const char *filename) {
283 png_bytep *row_pointers;
286 FILE *fp = fopen(filename, "wb");
289 cerr << "Unable to create image '" << filename << "'\n";
293 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
296 cerr << "png_create_write_struct failed\n";
301 png_infop info_ptr = png_create_info_struct(png_ptr);
303 cerr << "png_create_info_struct failed\n";
308 png_init_io(png_ptr, fp);
310 png_set_IHDR(png_ptr, info_ptr, _width, _height,
311 8, 2, PNG_INTERLACE_NONE,
312 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
314 png_write_info(png_ptr, info_ptr);
317 row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * _height);
318 for (int y = 0; y < _height; y++)
319 row_pointers[y] = (png_byte*) malloc(info_ptr->rowbytes);
322 for (int y = 0; y < _height; y++) {
324 for (int x = 0; x < _width; x++) {
325 row_pointers[y][k++] = _bit_map[x + _width * (y + _height * RED)];
326 row_pointers[y][k++] = _bit_map[x + _width * (y + _height * GREEN)];
327 row_pointers[y][k++] = _bit_map[x + _width * (y + _height * BLUE)];
331 png_write_image(png_ptr, row_pointers);
332 png_write_end(png_ptr, 0);
334 png_destroy_write_struct(&png_ptr, &info_ptr);
336 // cleanup heap allocation
337 for (int y = 0; y < _height; y++) free(row_pointers[y]);