1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
4 * Copyright 2008-2010 VMware, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
43 static const int png_compression_level = Z_BEST_SPEED;
47 pngWriteCallback(png_structp png_ptr, png_bytep data, png_size_t length)
49 std::ostream *os = (std::ostream *) png_get_io_ptr(png_ptr);
50 os->write((const char *)data, length);
54 Image::writePNG(std::ostream &os) const
62 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
65 color_type = PNG_COLOR_TYPE_RGB;
68 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
71 color_type = PNG_COLOR_TYPE_GRAY;
78 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
82 info_ptr = png_create_info_struct(png_ptr);
84 png_destroy_write_struct(&png_ptr, NULL);
88 if (setjmp(png_jmpbuf(png_ptr))) {
89 png_destroy_write_struct(&png_ptr, &info_ptr);
93 png_set_write_fn(png_ptr, &os, pngWriteCallback, NULL);
95 png_set_IHDR(png_ptr, info_ptr, width, height, 8,
96 color_type, PNG_INTERLACE_NONE,
97 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
99 png_set_compression_level(png_ptr, png_compression_level);
101 png_write_info(png_ptr, info_ptr);
104 for (unsigned y = 0; y < height; ++y) {
105 png_bytep row = (png_bytep)(pixels + y*width*channels);
106 png_write_rows(png_ptr, &row, 1);
111 png_bytep row = (png_bytep)(pixels + y*width*channels);
112 png_write_rows(png_ptr, &row, 1);
116 png_write_end(png_ptr, info_ptr);
117 png_destroy_write_struct(&png_ptr, &info_ptr);
127 readPNG(const char *filename)
135 fp = fopen(filename, "rb");
139 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
143 info_ptr = png_create_info_struct(png_ptr);
145 png_destroy_read_struct(&png_ptr, NULL, NULL);
149 end_info = png_create_info_struct(png_ptr);
151 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
155 if (setjmp(png_jmpbuf(png_ptr))) {
156 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
160 png_init_io(png_ptr, fp);
162 png_read_info(png_ptr, info_ptr);
164 png_uint_32 width, height;
165 int bit_depth, color_type, interlace_type, compression_type, filter_method;
167 png_get_IHDR(png_ptr, info_ptr,
169 &bit_depth, &color_type, &interlace_type,
170 &compression_type, &filter_method);
172 image = new Image(width, height);
176 /* Convert to RGBA8 */
177 if (color_type == PNG_COLOR_TYPE_PALETTE)
178 png_set_palette_to_rgb(png_ptr);
179 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
180 png_set_expand_gray_1_2_4_to_8(png_ptr);
181 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
182 png_set_tRNS_to_alpha(png_ptr);
184 png_set_strip_16(png_ptr);
186 for (unsigned y = 0; y < height; ++y) {
187 png_bytep row = (png_bytep)(image->pixels + y*width*4);
188 png_read_row(png_ptr, row, NULL);
191 png_read_end(png_ptr, info_ptr);
192 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
197 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
206 } /* namespace image */