summaryrefslogtreecommitdiffstats
path: root/WebKitTools/DumpRenderTree/cg
diff options
context:
space:
mode:
authorUpstream <upstream-import@none>1970-01-12 13:46:40 +0000
committerUpstream <upstream-import@none>1970-01-12 13:46:40 +0000
commitd8543bb6618c17b12da906afa77d216f58cf4058 (patch)
treec58dc05ed86825bd0ef8d305d58c8205106b540f /WebKitTools/DumpRenderTree/cg
downloadexternal_webkit-d8543bb6618c17b12da906afa77d216f58cf4058.zip
external_webkit-d8543bb6618c17b12da906afa77d216f58cf4058.tar.gz
external_webkit-d8543bb6618c17b12da906afa77d216f58cf4058.tar.bz2
external/webkit r30707
Diffstat (limited to 'WebKitTools/DumpRenderTree/cg')
-rw-r--r--WebKitTools/DumpRenderTree/cg/ImageDiffCG.cpp208
-rw-r--r--WebKitTools/DumpRenderTree/cg/PixelDumpSupportCG.cpp126
-rw-r--r--WebKitTools/DumpRenderTree/cg/PixelDumpSupportCG.h55
3 files changed, 389 insertions, 0 deletions
diff --git a/WebKitTools/DumpRenderTree/cg/ImageDiffCG.cpp b/WebKitTools/DumpRenderTree/cg/ImageDiffCG.cpp
new file mode 100644
index 0000000..6a84b79
--- /dev/null
+++ b/WebKitTools/DumpRenderTree/cg/ImageDiffCG.cpp
@@ -0,0 +1,208 @@
+/*
+ * Copyright (C) 2005, 2007 Apple Inc. All rights reserved.
+ * Copyright (C) 2005 Ben La Monica <ben.lamonica@gmail.com>. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#define min min
+
+#include <CoreGraphics/CGBitmapContext.h>
+#include <CoreGraphics/CGImage.h>
+#include <ImageIO/CGImageDestination.h>
+#include <stdio.h>
+#include <wtf/Platform.h>
+#include <wtf/RetainPtr.h>
+
+#if PLATFORM(WIN)
+#include <fcntl.h>
+#include <io.h>
+#endif
+
+#if PLATFORM(MAC)
+#include <LaunchServices/UTCoreTypes.h>
+#endif
+
+#ifndef CGFLOAT_DEFINED
+#ifdef __LP64__
+typedef double CGFloat;
+#else
+typedef float CGFloat;
+#endif
+#define CGFLOAT_DEFINED 1
+#endif
+
+using namespace std;
+
+#if PLATFORM(WIN)
+static const CFStringRef kUTTypePNG = CFSTR("public.png");
+#endif
+
+static RetainPtr<CGImageRef> createImageFromStdin(int bytesRemaining)
+{
+ unsigned char buffer[2048];
+ RetainPtr<CFMutableDataRef> data(AdoptCF, CFDataCreateMutable(0, bytesRemaining));
+
+ while (bytesRemaining > 0) {
+ size_t bytesToRead = min(bytesRemaining, 2048);
+ size_t bytesRead = fread(buffer, 1, bytesToRead, stdin);
+ CFDataAppendBytes(data.get(), buffer, static_cast<CFIndex>(bytesRead));
+ bytesRemaining -= static_cast<int>(bytesRead);
+ }
+ RetainPtr<CGDataProviderRef> dataProvider(AdoptCF, CGDataProviderCreateWithCFData(data.get()));
+ return RetainPtr<CGImageRef>(AdoptCF, CGImageCreateWithPNGDataProvider(dataProvider.get(), 0, false, kCGRenderingIntentDefault));
+}
+
+static RetainPtr<CGContextRef> getDifferenceBitmap(CGImageRef testBitmap, CGImageRef referenceBitmap)
+{
+ // we must have both images to take diff
+ if (!testBitmap || !referenceBitmap)
+ return 0;
+
+ RetainPtr<CGColorSpaceRef> colorSpace(AdoptCF, CGColorSpaceCreateDeviceRGB());
+ static CFMutableDataRef data = CFDataCreateMutable(kCFAllocatorDefault, 0);
+ CFDataSetLength(data, CGImageGetHeight(testBitmap) * CGImageGetBytesPerRow(testBitmap));
+ RetainPtr<CGContextRef> context(AdoptCF, CGBitmapContextCreate(CFDataGetMutableBytePtr(data), CGImageGetWidth(testBitmap), CGImageGetHeight(testBitmap),
+ CGImageGetBitsPerComponent(testBitmap), CGImageGetBytesPerRow(testBitmap), colorSpace.get(), kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst));
+
+ CGContextSetBlendMode(context.get(), kCGBlendModeNormal);
+ CGContextDrawImage(context.get(), CGRectMake(0, 0, static_cast<CGFloat>(CGImageGetWidth(testBitmap)), static_cast<CGFloat>(CGImageGetHeight(testBitmap))), testBitmap);
+ CGContextSetBlendMode(context.get(), kCGBlendModeDifference);
+ CGContextDrawImage(context.get(), CGRectMake(0, 0, static_cast<CGFloat>(CGImageGetWidth(referenceBitmap)), static_cast<CGFloat>(CGImageGetHeight(referenceBitmap))), referenceBitmap);
+
+ return context;
+}
+
+/**
+ * Counts the number of non-black pixels, and returns the percentage
+ * of non-black pixels to total pixels in the image.
+ */
+static float computePercentageDifferent(CGContextRef diffBitmap, unsigned threshold)
+{
+ // if diffBiatmap is nil, then there was an error, and it didn't match.
+ if (!diffBitmap)
+ return 100.0f;
+
+ size_t pixelsHigh = CGBitmapContextGetHeight(diffBitmap);
+ size_t pixelsWide = CGBitmapContextGetWidth(diffBitmap);
+ size_t bytesPerRow = CGBitmapContextGetBytesPerRow(diffBitmap);
+ unsigned char* pixelRowData = static_cast<unsigned char*>(CGBitmapContextGetData(diffBitmap));
+ unsigned differences = 0;
+
+ // NOTE: This may not be safe when switching between ENDIAN types
+ for (unsigned row = 0; row < pixelsHigh; row++) {
+ for (unsigned col = 0; col < (pixelsWide * 4); col += 4) {
+ unsigned char* red = pixelRowData + col;
+ unsigned char* green = red + 1;
+ unsigned char* blue = red + 2;
+ unsigned distance = *red + *green + *blue;
+ if (distance > threshold) {
+ differences++;
+ // shift the pixels towards white to make them more visible
+ *red = static_cast<unsigned char>(min(UCHAR_MAX, *red + 100));
+ *green = static_cast<unsigned char>(min(UCHAR_MAX, *green + 100));
+ *blue = static_cast<unsigned char>(min(UCHAR_MAX, *blue + 100));
+ }
+ }
+ pixelRowData += bytesPerRow;
+ }
+
+ float totalPixels = static_cast<float>(pixelsHigh * pixelsWide);
+ return (differences * 100.f) / totalPixels;
+}
+
+static void compareImages(CGImageRef actualBitmap, CGImageRef baselineBitmap, unsigned threshold)
+{
+ // prepare the difference blend to check for pixel variations
+ RetainPtr<CGContextRef> diffBitmap = getDifferenceBitmap(actualBitmap, baselineBitmap);
+
+ float percentage = computePercentageDifferent(diffBitmap.get(), threshold);
+
+ percentage = (float)((int)(percentage * 100.0f)) / 100.0f; // round to 2 decimal places
+
+ // send message to let them know if an image was wrong
+ if (percentage > 0.0f) {
+ // since the diff might actually show something, send it to stdout
+ RetainPtr<CGImageRef> image(AdoptCF, CGBitmapContextCreateImage(diffBitmap.get()));
+ RetainPtr<CFMutableDataRef> imageData(AdoptCF, CFDataCreateMutable(0, 0));
+ RetainPtr<CGImageDestinationRef> imageDest(AdoptCF, CGImageDestinationCreateWithData(imageData.get(), kUTTypePNG, 1, 0));
+ CGImageDestinationAddImage(imageDest.get(), image.get(), 0);
+ CGImageDestinationFinalize(imageDest.get());
+ printf("Content-length: %lu\n", CFDataGetLength(imageData.get()));
+ fwrite(CFDataGetBytePtr(imageData.get()), 1, CFDataGetLength(imageData.get()), stdout);
+ fprintf(stdout, "diff: %01.2f%% failed\n", percentage);
+ } else
+ fprintf(stdout, "diff: %01.2f%% passed\n", percentage);
+}
+
+int main(int argc, const char* argv[])
+{
+#if PLATFORM(WIN)
+ _setmode(0, _O_BINARY);
+ _setmode(1, _O_BINARY);
+#endif
+
+ unsigned threshold = 0;
+
+ for (int i = 1; i < argc; ++i) {
+ if (!strcmp(argv[i], "-t") || !strcmp(argv[i], "--threshold")) {
+ if (i >= argc - 1)
+ exit(1);
+ threshold = strtol(argv[i + 1], 0, 0);
+ ++i;
+ continue;
+ }
+ }
+
+ char buffer[2048];
+ RetainPtr<CGImageRef> actualImage;
+ RetainPtr<CGImageRef> baselineImage;
+
+ while (fgets(buffer, sizeof(buffer), stdin)) {
+ // remove the CR
+ char* newLineCharacter = strchr(buffer, '\n');
+ if (newLineCharacter)
+ *newLineCharacter = '\0';
+
+ if (!strncmp("Content-length: ", buffer, 16)) {
+ strtok(buffer, " ");
+ int imageSize = strtol(strtok(0, " "), 0, 10);
+
+ if (imageSize > 0 && !actualImage)
+ actualImage = createImageFromStdin(imageSize);
+ else if (imageSize > 0 && !baselineImage)
+ baselineImage = createImageFromStdin(imageSize);
+ else
+ fputs("error, image size must be specified.\n", stdout);
+ }
+
+ if (actualImage && baselineImage) {
+ compareImages(actualImage.get(), baselineImage.get(), threshold);
+ actualImage = 0;
+ baselineImage = 0;
+ }
+
+ fflush(stdout);
+ }
+
+ return 0;
+}
diff --git a/WebKitTools/DumpRenderTree/cg/PixelDumpSupportCG.cpp b/WebKitTools/DumpRenderTree/cg/PixelDumpSupportCG.cpp
new file mode 100644
index 0000000..e2e790c
--- /dev/null
+++ b/WebKitTools/DumpRenderTree/cg/PixelDumpSupportCG.cpp
@@ -0,0 +1,126 @@
+/*
+ * Copyright (C) 2005, 2006, 2007 Apple Inc. All rights reserved.
+ * (C) 2007 Graham Dennis (graham.dennis@gmail.com)
+ * (C) 2007 Eric Seidel <eric@webkit.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
+ * its contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "DumpRenderTree.h"
+#include "PixelDumpSupportCG.h"
+
+#include "LayoutTestController.h"
+#include <ImageIO/CGImageDestination.h>
+#include <wtf/Assertions.h>
+#include <wtf/RetainPtr.h>
+#include <wtf/StringExtras.h>
+
+#if PLATFORM(WIN)
+#include "MD5.h"
+#elif PLATFORM(MAC)
+#include <LaunchServices/UTCoreTypes.h>
+#define COMMON_DIGEST_FOR_OPENSSL
+#include <CommonCrypto/CommonDigest.h>
+#endif
+
+#if PLATFORM(WIN)
+static const CFStringRef kUTTypePNG = CFSTR("public.png");
+#endif
+
+static void printPNG(CGImageRef image)
+{
+ RetainPtr<CFMutableDataRef> imageData(AdoptCF, CFDataCreateMutable(0, 0));
+ RetainPtr<CGImageDestinationRef> imageDest(AdoptCF, CGImageDestinationCreateWithData(imageData.get(), kUTTypePNG, 1, 0));
+ CGImageDestinationAddImage(imageDest.get(), image, 0);
+ CGImageDestinationFinalize(imageDest.get());
+ printf("Content-length: %lu\n", CFDataGetLength(imageData.get()));
+ fwrite(CFDataGetBytePtr(imageData.get()), 1, CFDataGetLength(imageData.get()), stdout);
+}
+
+static void getMD5HashStringForBitmap(CGContextRef bitmap, char string[33])
+{
+ MD5_CTX md5Context;
+ unsigned char hash[16];
+
+ size_t bitsPerPixel = CGBitmapContextGetBitsPerPixel(bitmap);
+ ASSERT(bitsPerPixel == 32); // ImageDiff assumes 32 bit RGBA, we must as well.
+ size_t bytesPerPixel = bitsPerPixel / 8;
+ size_t pixelsHigh = CGBitmapContextGetHeight(bitmap);
+ size_t pixelsWide = CGBitmapContextGetWidth(bitmap);
+ size_t bytesPerRow = CGBitmapContextGetBytesPerRow(bitmap);
+ ASSERT(bytesPerRow >= (pixelsWide * bytesPerPixel));
+
+ MD5_Init(&md5Context);
+ unsigned char* bitmapData = static_cast<unsigned char*>(CGBitmapContextGetData(bitmap));
+ for (unsigned row = 0; row < pixelsHigh; row++) {
+ MD5_Update(&md5Context, bitmapData, static_cast<unsigned>(pixelsWide * bytesPerPixel));
+ bitmapData += bytesPerRow;
+ }
+ MD5_Final(hash, &md5Context);
+
+ string[0] = '\0';
+ for (int i = 0; i < 16; i++)
+ snprintf(string, 33, "%s%02x", string, hash[i]);
+}
+
+void drawSelectionRect(CGContextRef context, const CGRect& rect)
+{
+ CGContextSaveGState(context);
+ CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
+ CGContextStrokeRect(context, rect);
+ CGContextRestoreGState(context);
+}
+
+void dumpWebViewAsPixelsAndCompareWithExpected(const char* /*currentTest*/, bool /*forceAllTestsToDumpPixels*/)
+{
+ RetainPtr<CGContextRef> context = getBitmapContextFromWebView();
+
+#if PLATFORM(MAC)
+ if (layoutTestController->testRepaint())
+ repaintWebView(context.get(), layoutTestController->testRepaintSweepHorizontally());
+ else
+ paintWebView(context.get());
+
+ if (layoutTestController->dumpSelectionRect())
+ drawSelectionRect(context.get(), getSelectionRect());
+#endif
+
+ // Compute the actual hash to compare to the expected image's hash.
+ char actualHash[33];
+ getMD5HashStringForBitmap(context.get(), actualHash);
+ printf("\nActualHash: %s\n", actualHash);
+
+ // FIXME: We should compare the actualHash to the expected hash here and
+ // only set dumpImage to true if they don't match, but DRT doesn't have
+ // enough information currently to find the expected checksum file.
+ bool dumpImage = true;
+
+ if (dumpImage) {
+ RetainPtr<CGImageRef> image(AdoptCF, CGBitmapContextCreateImage(context.get()));
+ printPNG(image.get());
+ }
+
+ printf("#EOF\n");
+}
diff --git a/WebKitTools/DumpRenderTree/cg/PixelDumpSupportCG.h b/WebKitTools/DumpRenderTree/cg/PixelDumpSupportCG.h
new file mode 100644
index 0000000..0abdea6
--- /dev/null
+++ b/WebKitTools/DumpRenderTree/cg/PixelDumpSupportCG.h
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2005, 2006, 2007 Apple Inc. All rights reserved.
+ * (C) 2007 Graham Dennis (graham.dennis@gmail.com)
+ * (C) 2007 Eric Seidel <eric@webkit.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
+ * its contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef PixelDumpSupportCG_h
+#define PixelDumpSupportCG_h
+
+#include <wtf/RetainPtr.h>
+
+#ifndef CGFLOAT_DEFINED
+#ifdef __LP64__
+typedef double CGFloat;
+#else
+typedef float CGFloat;
+#endif
+#define CGFLOAT_DEFINED 1
+#endif
+
+typedef struct CGContext* CGContextRef;
+struct CGRect;
+
+RetainPtr<CGContextRef> getBitmapContextFromWebView();
+CGRect getSelectionRect();
+
+void paintWebView(CGContextRef);
+void repaintWebView(CGContextRef context, bool horizontal);
+void drawSelectionRect(CGContextRef, const CGRect&);
+
+#endif // PixelDumpSupportCG_h