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 **************************************************************************/
60 uint32_t biCompression;
62 int32_t biXPelsPerMeter;
63 int32_t biYPelsPerMeter;
65 uint32_t biClrImportant;
77 Image::writeBMP(const char *filename) const {
78 assert(channels == 4);
80 struct FileHeader bmfh;
81 struct InfoHeader bmih;
85 bmfh.bfSize = 14 + 40 + height*width*4;
88 bmfh.bfOffBits = 14 + 40;
92 bmih.biHeight = height;
95 bmih.biCompression = 0;
96 bmih.biSizeImage = height*width*4;
97 bmih.biXPelsPerMeter = 0;
98 bmih.biYPelsPerMeter = 0;
100 bmih.biClrImportant = 0;
102 std::ofstream stream(filename, std::ofstream::binary);
107 stream.write((const char *)&bmfh, 14);
108 stream.write((const char *)&bmih, 40);
110 unsigned stride = width*4;
113 for (y = 0; y < height; ++y) {
114 const unsigned char *ptr = pixels + y * stride;
115 for (x = 0; x < width; ++x) {
117 pixel.rgbRed = ptr[x*4 + 0];
118 pixel.rgbGreen = ptr[x*4 + 1];
119 pixel.rgbBlue = ptr[x*4 + 2];
120 pixel.rgbAlpha = ptr[x*4 + 3];
121 stream.write((const char *)&pixel, 4);
127 const unsigned char *ptr = pixels + y * stride;
128 for (x = 0; x < width; ++x) {
130 pixel.rgbRed = ptr[x*4 + 0];
131 pixel.rgbGreen = ptr[x*4 + 1];
132 pixel.rgbBlue = ptr[x*4 + 2];
133 pixel.rgbAlpha = ptr[x*4 + 3];
134 stream.write((const char *)&pixel, 4);
145 Image::writePNG(const char *filename) const {
150 fp = fopen(filename, "wb");
154 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
158 info_ptr = png_create_info_struct(png_ptr);
160 png_destroy_write_struct(&png_ptr, NULL);
164 if (setjmp(png_jmpbuf(png_ptr))) {
165 png_destroy_write_struct(&png_ptr, &info_ptr);
169 png_init_io(png_ptr, fp);
174 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
177 color_type = PNG_COLOR_TYPE_RGB;
180 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
183 color_type = PNG_COLOR_TYPE_GRAY;
190 png_set_IHDR(png_ptr, info_ptr, width, height, 8, color_type,
191 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
193 png_set_compression_level(png_ptr, Z_DEFAULT_COMPRESSION);
195 png_write_info(png_ptr, info_ptr);
198 for (unsigned y = 0; y < height; ++y) {
199 png_bytep row = (png_bytep)(pixels + y*width*channels);
200 png_write_rows(png_ptr, &row, 1);
205 png_bytep row = (png_bytep)(pixels + y*width*channels);
206 png_write_rows(png_ptr, &row, 1);
210 png_write_end(png_ptr, info_ptr);
211 png_destroy_write_struct(&png_ptr, &info_ptr);
224 readPNG(const char *filename)
232 fp = fopen(filename, "rb");
236 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
240 info_ptr = png_create_info_struct(png_ptr);
242 png_destroy_read_struct(&png_ptr, NULL, NULL);
246 end_info = png_create_info_struct(png_ptr);
248 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
252 if (setjmp(png_jmpbuf(png_ptr))) {
253 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
257 png_init_io(png_ptr, fp);
259 png_read_info(png_ptr, info_ptr);
261 png_uint_32 width, height;
262 int bit_depth, color_type, interlace_type, compression_type, filter_method;
264 png_get_IHDR(png_ptr, info_ptr,
266 &bit_depth, &color_type, &interlace_type,
267 &compression_type, &filter_method);
269 image = new Image(width, height);
273 /* Convert to RGBA8 */
274 if (color_type == PNG_COLOR_TYPE_PALETTE)
275 png_set_palette_to_rgb(png_ptr);
276 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
277 png_set_expand_gray_1_2_4_to_8(png_ptr);
278 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
279 png_set_tRNS_to_alpha(png_ptr);
281 png_set_strip_16(png_ptr);
283 for (unsigned y = 0; y < height; ++y) {
284 png_bytep row = (png_bytep)(image->pixels + y*width*4);
285 png_read_row(png_ptr, row, NULL);
288 png_read_end(png_ptr, info_ptr);
289 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
294 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
302 double Image::compare(Image &ref)
304 if (width != ref.width ||
305 height != ref.height ||
306 channels != ref.channels) {
310 const unsigned char *pSrc = start();
311 const unsigned char *pRef = ref.start();
313 assert(channels >= 3);
315 unsigned long long error = 0;
316 for (unsigned y = 0; y < height; ++y) {
317 for (unsigned x = 0; x < width; ++x) {
318 // FIXME: Ignore alpha channel until we are able to pick a visual
319 // that matches the traces
320 for (unsigned c = 0; c < 3; ++c) {
321 int delta = pSrc[x*channels + c] - pRef[x*channels + c];
322 error += delta*delta;
327 pRef += ref.stride();
330 double numerator = error*2 + 1;
331 double denominator = height*width*3ULL*255ULL*255ULL*2;
332 double quotient = numerator/denominator;
335 double precision = -log(quotient)/log(2.0);
340 struct png_tmp_buffer
347 pngWriteCallback(png_structp png_ptr, png_bytep data, png_size_t length)
349 struct png_tmp_buffer *buf = (struct png_tmp_buffer*) png_get_io_ptr(png_ptr);
350 size_t nsize = buf->size + length;
352 /* allocate or grow buffer */
354 buf->buffer = (char*)realloc(buf->buffer, nsize);
356 buf->buffer = (char*)malloc(nsize);
359 png_error(png_ptr, "Buffer allocation error");
361 memcpy(buf->buffer + buf->size, data, length);
365 bool writePixelsToBuffer(unsigned char *pixels,
366 unsigned width, unsigned height, unsigned numChannels,
371 struct png_tmp_buffer png_mem;
376 png_mem.buffer = NULL;
379 switch (numChannels) {
381 type = PNG_COLOR_TYPE_RGB_ALPHA;
384 type = PNG_COLOR_TYPE_RGB;
387 type = PNG_COLOR_TYPE_GRAY_ALPHA;
390 type = PNG_COLOR_TYPE_GRAY;
396 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
400 info_ptr = png_create_info_struct(png_ptr);
402 png_destroy_write_struct(&png_ptr, NULL);
406 if (setjmp(png_jmpbuf(png_ptr))) {
407 png_destroy_write_struct(&png_ptr, &info_ptr);
411 png_set_write_fn(png_ptr, &png_mem, pngWriteCallback, NULL);
413 png_set_IHDR(png_ptr, info_ptr, width, height, 8,
414 type, PNG_INTERLACE_NONE,
415 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
417 png_set_compression_level(png_ptr, Z_DEFAULT_COMPRESSION);
419 png_write_info(png_ptr, info_ptr);
422 for (unsigned y = 0; y < height; ++y) {
423 png_bytep row = (png_bytep)(pixels + y*width*numChannels);
424 png_write_rows(png_ptr, &row, 1);
429 png_bytep row = (png_bytep)(pixels + y*width*numChannels);
430 png_write_rows(png_ptr, &row, 1);
434 png_write_end(png_ptr, info_ptr);
435 png_destroy_write_struct(&png_ptr, &info_ptr);
437 *buffer = png_mem.buffer;
438 *size = png_mem.size;
447 free(png_mem.buffer);
451 } /* namespace Image */