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 **************************************************************************/
56 uint32_t biCompression;
58 int32_t biXPelsPerMeter;
59 int32_t biYPelsPerMeter;
61 uint32_t biClrImportant;
73 Image::writeBMP(const char *filename) const {
74 struct FileHeader bmfh;
75 struct InfoHeader bmih;
79 bmfh.bfSize = 14 + 40 + height*width*4;
82 bmfh.bfOffBits = 14 + 40;
86 bmih.biHeight = height;
89 bmih.biCompression = 0;
90 bmih.biSizeImage = height*width*4;
91 bmih.biXPelsPerMeter = 0;
92 bmih.biYPelsPerMeter = 0;
94 bmih.biClrImportant = 0;
96 std::ofstream stream(filename, std::ofstream::binary);
101 stream.write((const char *)&bmfh, 14);
102 stream.write((const char *)&bmih, 40);
104 unsigned stride = width*4;
107 for (y = 0; y < height; ++y) {
108 const unsigned char *ptr = pixels + y * stride;
109 for (x = 0; x < width; ++x) {
111 pixel.rgbRed = ptr[x*4 + 0];
112 pixel.rgbGreen = ptr[x*4 + 1];
113 pixel.rgbBlue = ptr[x*4 + 2];
114 pixel.rgbAlpha = ptr[x*4 + 3];
115 stream.write((const char *)&pixel, 4);
121 const unsigned char *ptr = pixels + y * stride;
122 for (x = 0; x < width; ++x) {
124 pixel.rgbRed = ptr[x*4 + 0];
125 pixel.rgbGreen = ptr[x*4 + 1];
126 pixel.rgbBlue = ptr[x*4 + 2];
127 pixel.rgbAlpha = ptr[x*4 + 3];
128 stream.write((const char *)&pixel, 4);
139 Image::writePNG(const char *filename) const {
144 fp = fopen(filename, "wb");
148 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
152 info_ptr = png_create_info_struct(png_ptr);
154 png_destroy_write_struct(&png_ptr, NULL);
158 if (setjmp(png_jmpbuf(png_ptr))) {
159 png_destroy_write_struct(&png_ptr, &info_ptr);
163 png_init_io(png_ptr, fp);
165 png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA,
166 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
168 png_set_compression_level(png_ptr, Z_DEFAULT_COMPRESSION);
170 png_write_info(png_ptr, info_ptr);
173 for (unsigned y = 0; y < height; ++y) {
174 png_bytep row = (png_bytep)(pixels + y*width*4);
175 png_write_rows(png_ptr, &row, 1);
180 png_bytep row = (png_bytep)(pixels + y*width*4);
181 png_write_rows(png_ptr, &row, 1);
185 png_write_end(png_ptr, info_ptr);
186 png_destroy_write_struct(&png_ptr, &info_ptr);
199 readPNG(const char *filename)
207 fp = fopen(filename, "rb");
211 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
215 info_ptr = png_create_info_struct(png_ptr);
217 png_destroy_read_struct(&png_ptr, NULL, NULL);
221 end_info = png_create_info_struct(png_ptr);
223 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
227 if (setjmp(png_jmpbuf(png_ptr))) {
228 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
232 png_init_io(png_ptr, fp);
234 png_read_info(png_ptr, info_ptr);
236 png_uint_32 width, height;
237 int bit_depth, color_type, interlace_type, compression_type, filter_method;
239 png_get_IHDR(png_ptr, info_ptr,
241 &bit_depth, &color_type, &interlace_type,
242 &compression_type, &filter_method);
244 image = new Image(width, height);
248 /* Convert to RGBA8 */
249 if (color_type == PNG_COLOR_TYPE_PALETTE)
250 png_set_palette_to_rgb(png_ptr);
251 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
252 png_set_expand_gray_1_2_4_to_8(png_ptr);
253 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
254 png_set_tRNS_to_alpha(png_ptr);
256 png_set_strip_16(png_ptr);
258 for (unsigned y = 0; y < height; ++y) {
259 png_bytep row = (png_bytep)(image->pixels + y*width*4);
260 png_read_row(png_ptr, row, NULL);
263 png_read_end(png_ptr, info_ptr);
264 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
269 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
277 double Image::compare(Image &ref)
279 if (width != ref.width ||
280 height != ref.height) {
284 const unsigned char *pSrc = start();
285 const unsigned char *pRef = ref.start();
287 unsigned long long error = 0;
288 for (unsigned y = 0; y < height; ++y) {
289 for (unsigned x = 0; x < width; ++x) {
290 // FIXME: Ignore alpha channel until we are able to pick a visual
291 // that matches the traces
292 for (unsigned c = 0; c < 3; ++c) {
293 int delta = pSrc[x*4 + c] - pRef[x*4 + c];
294 error += delta*delta;
299 pRef += ref.stride();
302 double numerator = error*2 + 1;
303 double denominator = height*width*3ULL*255ULL*255ULL*2;
304 double quotient = numerator/denominator;
307 double precision = -log(quotient)/log(2.0);
314 } /* namespace Image */