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 {
144 fp = fopen(filename, "wb");
148 /* Create and initialize the png_struct with the desired error handler
149 * functions. If you want to use the default stderr and longjump method,
150 * you can supply NULL for the last three parameters. We also check that
151 * the library version is compatible with the one used at compile time,
152 * in case we are using dynamically linked libraries. REQUIRED.
154 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
158 /* Allocate/initialize the image information data. REQUIRED */
159 info_ptr = png_create_info_struct(png_ptr);
161 png_destroy_write_struct(&png_ptr, NULL);
165 /* Set error handling. REQUIRED if you aren't supplying your own
166 * error handling functions in the png_create_write_struct() call.
168 if (setjmp(png_jmpbuf(png_ptr))) {
169 png_destroy_write_struct(&png_ptr, &info_ptr);
174 png_init_io(png_ptr, fp);
176 png_set_write_fn(png_ptr, (void *)user_io_ptr, user_write_fn,
177 user_IO_flush_function);
180 png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA,
181 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
183 png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
185 png_write_info(png_ptr, info_ptr);
188 for (unsigned y = 0; y < height; ++y) {
189 png_byte *row = (png_byte *)(pixels + y*width*4);
190 png_write_rows(png_ptr, &row, 1);
195 png_byte *row = (png_byte *)(pixels + y*width*4);
196 png_write_rows(png_ptr, &row, 1);
200 png_write_end(png_ptr, info_ptr);
201 png_destroy_write_struct(&png_ptr, &info_ptr);
212 } /* namespace Image */