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