1 /**************************************************************************
3 * Copyright 2008-2010 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
55 uint32_t biCompression;
57 int32_t biXPelsPerMeter;
58 int32_t biYPelsPerMeter;
60 uint32_t biClrImportant;
72 Image::writeBMP(const char *filename) const {
73 struct FileHeader bmfh;
74 struct InfoHeader bmih;
78 bmfh.bfSize = 14 + 40 + height*width*4;
81 bmfh.bfOffBits = 14 + 40;
85 bmih.biHeight = height;
88 bmih.biCompression = 0;
89 bmih.biSizeImage = height*width*4;
90 bmih.biXPelsPerMeter = 0;
91 bmih.biYPelsPerMeter = 0;
93 bmih.biClrImportant = 0;
95 std::ofstream stream(filename, std::ofstream::binary);
100 stream.write((const char *)&bmfh, 14);
101 stream.write((const char *)&bmih, 40);
103 unsigned stride = width*4;
106 for (y = 0; y < height; ++y) {
107 const unsigned char *ptr = pixels + y * stride;
108 for (x = 0; x < width; ++x) {
110 pixel.rgbRed = ptr[x*4 + 0];
111 pixel.rgbGreen = ptr[x*4 + 1];
112 pixel.rgbBlue = ptr[x*4 + 2];
113 pixel.rgbAlpha = ptr[x*4 + 3];
114 stream.write((const char *)&pixel, 4);
120 const unsigned char *ptr = pixels + y * stride;
121 for (x = 0; x < width; ++x) {
123 pixel.rgbRed = ptr[x*4 + 0];
124 pixel.rgbGreen = ptr[x*4 + 1];
125 pixel.rgbBlue = ptr[x*4 + 2];
126 pixel.rgbAlpha = ptr[x*4 + 3];
127 stream.write((const char *)&pixel, 4);
138 Image::writePNG(const char *filename) const {
143 fp = fopen(filename, "wb");
147 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
151 info_ptr = png_create_info_struct(png_ptr);
153 png_destroy_write_struct(&png_ptr, NULL);
157 if (setjmp(png_jmpbuf(png_ptr))) {
158 png_destroy_write_struct(&png_ptr, &info_ptr);
162 png_init_io(png_ptr, fp);
164 png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA,
165 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
167 png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
169 png_write_info(png_ptr, info_ptr);
172 for (unsigned y = 0; y < height; ++y) {
173 png_bytep row = (png_bytep)(pixels + y*width*4);
174 png_write_rows(png_ptr, &row, 1);
179 png_bytep row = (png_bytep)(pixels + y*width*4);
180 png_write_rows(png_ptr, &row, 1);
184 png_write_end(png_ptr, info_ptr);
185 png_destroy_write_struct(&png_ptr, &info_ptr);
198 readPNG(const char *filename)
206 fp = fopen(filename, "rb");
210 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
214 info_ptr = png_create_info_struct(png_ptr);
216 png_destroy_read_struct(&png_ptr, NULL, NULL);
220 end_info = png_create_info_struct(png_ptr);
222 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
226 if (setjmp(png_jmpbuf(png_ptr))) {
227 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
231 png_init_io(png_ptr, fp);
233 png_read_info(png_ptr, info_ptr);
235 png_uint_32 width, height;
236 int bit_depth, color_type, interlace_type, compression_type, filter_method;
238 png_get_IHDR(png_ptr, info_ptr,
240 &bit_depth, &color_type, &interlace_type,
241 &compression_type, &filter_method);
243 image = new Image(width, height);
247 /* Convert to RGBA8 */
248 if (color_type == PNG_COLOR_TYPE_PALETTE)
249 png_set_palette_to_rgb(png_ptr);
250 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
251 png_set_expand_gray_1_2_4_to_8(png_ptr);
252 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
253 png_set_tRNS_to_alpha(png_ptr);
255 png_set_strip_16(png_ptr);
257 for (unsigned y = 0; y < height; ++y) {
258 png_bytep row = (png_bytep)(image->pixels + y*width*4);
259 png_read_row(png_ptr, row, NULL);
262 png_read_end(png_ptr, info_ptr);
263 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
268 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
276 } /* namespace Image */