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
56 assert(channelType == TYPE_UNORM8);
64 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
67 color_type = PNG_COLOR_TYPE_RGB;
70 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
73 color_type = PNG_COLOR_TYPE_GRAY;
80 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
84 info_ptr = png_create_info_struct(png_ptr);
86 png_destroy_write_struct(&png_ptr, NULL);
90 if (setjmp(png_jmpbuf(png_ptr))) {
91 png_destroy_write_struct(&png_ptr, &info_ptr);
95 png_set_write_fn(png_ptr, &os, pngWriteCallback, NULL);
97 png_set_IHDR(png_ptr, info_ptr, width, height, 8,
98 color_type, PNG_INTERLACE_NONE,
99 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
101 png_set_compression_level(png_ptr, png_compression_level);
103 png_write_info(png_ptr, info_ptr);
106 for (unsigned y = 0; y < height; ++y) {
107 png_bytep row = (png_bytep)(pixels + y*width*channels);
108 png_write_rows(png_ptr, &row, 1);
113 png_bytep row = (png_bytep)(pixels + y*width*channels);
114 png_write_rows(png_ptr, &row, 1);
118 png_write_end(png_ptr, info_ptr);
119 png_destroy_write_struct(&png_ptr, &info_ptr);
129 Image::writePNG(const char *filename) const
131 std::ofstream os(filename, std::ofstream::binary);
140 pngReadCallback(png_structp png_ptr, png_bytep data, png_size_t length)
142 std::istream *os = (std::istream *) png_get_io_ptr(png_ptr);
143 os->read((char *)data, length);
148 readPNG(std::istream &is)
156 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
160 info_ptr = png_create_info_struct(png_ptr);
162 png_destroy_read_struct(&png_ptr, NULL, NULL);
166 end_info = png_create_info_struct(png_ptr);
168 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
172 if (setjmp(png_jmpbuf(png_ptr))) {
173 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
177 png_set_read_fn(png_ptr, &is, pngReadCallback);
179 png_read_info(png_ptr, info_ptr);
181 png_uint_32 width, height;
182 int bit_depth, color_type, interlace_type, compression_type, filter_method;
184 png_get_IHDR(png_ptr, info_ptr,
186 &bit_depth, &color_type, &interlace_type,
187 &compression_type, &filter_method);
189 /* Convert to RGBA8 */
190 if (color_type == PNG_COLOR_TYPE_PALETTE)
191 png_set_palette_to_rgb(png_ptr);
192 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
193 png_set_expand_gray_1_2_4_to_8(png_ptr);
194 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
195 png_set_tRNS_to_alpha(png_ptr);
197 png_set_strip_16(png_ptr);
199 channels = png_get_channels(png_ptr, info_ptr);
200 image = new Image(width, height, channels);
204 assert(png_get_rowbytes(png_ptr, info_ptr) == width*channels);
205 for (unsigned y = 0; y < height; ++y) {
206 png_bytep row = (png_bytep)(image->pixels + y*width*channels);
207 png_read_row(png_ptr, row, NULL);
210 png_read_end(png_ptr, info_ptr);
211 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
216 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
222 readPNG(const char *filename)
224 std::ifstream is(filename, std::ifstream::binary);
228 return readPNG(filename);
232 } /* namespace image */