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 **************************************************************************/
44 Image::writePNG(const char *filename) const {
49 fp = fopen(filename, "wb");
53 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
57 info_ptr = png_create_info_struct(png_ptr);
59 png_destroy_write_struct(&png_ptr, NULL);
63 if (setjmp(png_jmpbuf(png_ptr))) {
64 png_destroy_write_struct(&png_ptr, &info_ptr);
68 png_init_io(png_ptr, fp);
73 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
76 color_type = PNG_COLOR_TYPE_RGB;
79 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
82 color_type = PNG_COLOR_TYPE_GRAY;
89 png_set_IHDR(png_ptr, info_ptr, width, height, 8, color_type,
90 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
92 png_set_compression_level(png_ptr, Z_DEFAULT_COMPRESSION);
94 png_write_info(png_ptr, info_ptr);
97 for (unsigned y = 0; y < height; ++y) {
98 png_bytep row = (png_bytep)(pixels + y*width*channels);
99 png_write_rows(png_ptr, &row, 1);
104 png_bytep row = (png_bytep)(pixels + y*width*channels);
105 png_write_rows(png_ptr, &row, 1);
109 png_write_end(png_ptr, info_ptr);
110 png_destroy_write_struct(&png_ptr, &info_ptr);
123 readPNG(const char *filename)
131 fp = fopen(filename, "rb");
135 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
139 info_ptr = png_create_info_struct(png_ptr);
141 png_destroy_read_struct(&png_ptr, NULL, NULL);
145 end_info = png_create_info_struct(png_ptr);
147 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
151 if (setjmp(png_jmpbuf(png_ptr))) {
152 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
156 png_init_io(png_ptr, fp);
158 png_read_info(png_ptr, info_ptr);
160 png_uint_32 width, height;
161 int bit_depth, color_type, interlace_type, compression_type, filter_method;
163 png_get_IHDR(png_ptr, info_ptr,
165 &bit_depth, &color_type, &interlace_type,
166 &compression_type, &filter_method);
168 image = new Image(width, height);
172 /* Convert to RGBA8 */
173 if (color_type == PNG_COLOR_TYPE_PALETTE)
174 png_set_palette_to_rgb(png_ptr);
175 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
176 png_set_expand_gray_1_2_4_to_8(png_ptr);
177 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
178 png_set_tRNS_to_alpha(png_ptr);
180 png_set_strip_16(png_ptr);
182 for (unsigned y = 0; y < height; ++y) {
183 png_bytep row = (png_bytep)(image->pixels + y*width*4);
184 png_read_row(png_ptr, row, NULL);
187 png_read_end(png_ptr, info_ptr);
188 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
193 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
201 struct png_tmp_buffer
208 pngWriteCallback(png_structp png_ptr, png_bytep data, png_size_t length)
210 struct png_tmp_buffer *buf = (struct png_tmp_buffer*) png_get_io_ptr(png_ptr);
211 size_t nsize = buf->size + length;
213 /* allocate or grow buffer */
215 buf->buffer = (char*)realloc(buf->buffer, nsize);
217 buf->buffer = (char*)malloc(nsize);
220 png_error(png_ptr, "Buffer allocation error");
222 memcpy(buf->buffer + buf->size, data, length);
226 bool writePixelsToBuffer(unsigned char *pixels,
227 unsigned width, unsigned height, unsigned numChannels,
232 struct png_tmp_buffer png_mem;
237 png_mem.buffer = NULL;
240 switch (numChannels) {
242 type = PNG_COLOR_TYPE_RGB_ALPHA;
245 type = PNG_COLOR_TYPE_RGB;
248 type = PNG_COLOR_TYPE_GRAY_ALPHA;
251 type = PNG_COLOR_TYPE_GRAY;
257 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
261 info_ptr = png_create_info_struct(png_ptr);
263 png_destroy_write_struct(&png_ptr, NULL);
267 if (setjmp(png_jmpbuf(png_ptr))) {
268 png_destroy_write_struct(&png_ptr, &info_ptr);
272 png_set_write_fn(png_ptr, &png_mem, pngWriteCallback, NULL);
274 png_set_IHDR(png_ptr, info_ptr, width, height, 8,
275 type, PNG_INTERLACE_NONE,
276 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
278 png_set_compression_level(png_ptr, Z_DEFAULT_COMPRESSION);
280 png_write_info(png_ptr, info_ptr);
283 for (unsigned y = 0; y < height; ++y) {
284 png_bytep row = (png_bytep)(pixels + y*width*numChannels);
285 png_write_rows(png_ptr, &row, 1);
290 png_bytep row = (png_bytep)(pixels + y*width*numChannels);
291 png_write_rows(png_ptr, &row, 1);
295 png_write_end(png_ptr, info_ptr);
296 png_destroy_write_struct(&png_ptr, &info_ptr);
298 *buffer = png_mem.buffer;
299 *size = png_mem.size;
308 free(png_mem.buffer);
312 } /* namespace Image */