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