1 /* 2 * Copyright (C) 2009 Apple Inc. All rights reserved. 3 * (C) 2009 Brent Fulgham <bfulgham (at) webkit.org> 4 * (C) 2010 Igalia S.L 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 16 * its contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "config.h" 32 #include "PixelDumpSupportCairo.h" 33 34 #include "DumpRenderTree.h" 35 #include "PixelDumpSupport.h" 36 #include <algorithm> 37 #include <ctype.h> 38 #include <wtf/Assertions.h> 39 #include <wtf/MD5.h> 40 #include <wtf/RefPtr.h> 41 #include <wtf/StringExtras.h> 42 43 using namespace std; 44 45 static cairo_status_t writeFunction(void* closure, const unsigned char* data, unsigned int length) 46 { 47 Vector<unsigned char>* in = reinterpret_cast<Vector<unsigned char>*>(closure); 48 in->append(data, length); 49 return CAIRO_STATUS_SUCCESS; 50 } 51 52 static void printPNG(cairo_surface_t* image, const char* checksum) 53 { 54 Vector<unsigned char> pixelData; 55 // Only PNG output is supported for now. 56 cairo_surface_write_to_png_stream(image, writeFunction, &pixelData); 57 58 const size_t dataLength = pixelData.size(); 59 const unsigned char* data = pixelData.data(); 60 61 printPNG(data, dataLength, checksum); 62 } 63 64 void computeMD5HashStringForBitmapContext(BitmapContext* context, char hashString[33]) 65 { 66 cairo_t* bitmapContext = context->cairoContext(); 67 cairo_surface_t* surface = cairo_get_target(bitmapContext); 68 69 ASSERT(cairo_image_surface_get_format(surface) == CAIRO_FORMAT_ARGB32); // ImageDiff assumes 32 bit RGBA, we must as well. 70 71 size_t pixelsHigh = cairo_image_surface_get_height(surface); 72 size_t pixelsWide = cairo_image_surface_get_width(surface); 73 size_t bytesPerRow = cairo_image_surface_get_stride(surface); 74 75 MD5 md5Context; 76 unsigned char* bitmapData = static_cast<unsigned char*>(cairo_image_surface_get_data(surface)); 77 for (unsigned row = 0; row < pixelsHigh; row++) { 78 md5Context.addBytes(bitmapData, 4 * pixelsWide); 79 bitmapData += bytesPerRow; 80 } 81 Vector<uint8_t, 16> hash; 82 md5Context.checksum(hash); 83 84 snprintf(hashString, 33, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", 85 hash[0], hash[1], hash[2], hash[3], hash[4], hash[5], hash[6], hash[7], 86 hash[8], hash[9], hash[10], hash[11], hash[12], hash[13], hash[14], hash[15]); 87 } 88 89 void dumpBitmap(BitmapContext* context, const char* checksum) 90 { 91 cairo_surface_t* surface = cairo_get_target(context->cairoContext()); 92 printPNG(surface, checksum); 93 } 94