Bug #: 6155
Submitted by: eseidel
Reviewed by: darin
        DumpRenderTree should set a consistent color profile while running
        http://bugzilla.opendarwin.org/show_bug.cgi?id=6155

        Creates consistent colormatched renderings on every test machine
        using the only way possible with Tiger APIs: by setting the
        system color profile on the test machine for the duration of the
        tests.  This will (unfortunately) cause colors to change while
        running DumpRenderTree.  This can also cause "permanent" color
        changes to occur if DRT is to crash (SIGSEGV, etc.) while running.
        This is far from ideal, but it's be best way we've found to deal
        with the issue for now.

        * DumpRenderTree/DumpRenderTree.m:
        (restoreColorSpace):
        (setDefaultColorProfileToRGB):
        (main):


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@11824 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/WebKitTools/ChangeLog b/WebKitTools/ChangeLog
index 32a68ff..b717eff 100644
--- a/WebKitTools/ChangeLog
+++ b/WebKitTools/ChangeLog
@@ -1,3 +1,24 @@
+2005-12-30  Eric Seidel  <eseidel@apple.com>
+
+        Reviewed by darin.
+
+        DumpRenderTree should set a consistent color profile while running
+        http://bugzilla.opendarwin.org/show_bug.cgi?id=6155
+
+        Creates consistent colormatched renderings on every test machine
+        using the only way possible with Tiger APIs: by setting the
+        system color profile on the test machine for the duration of the
+        tests.  This will (unfortunately) cause colors to change while
+        running DumpRenderTree.  This can also cause "permanent" color
+        changes to occur if DRT is to crash (SIGSEGV, etc.) while running.
+        This is far from ideal, but it's be best way we've found to deal
+        with the issue for now.
+
+        * DumpRenderTree/DumpRenderTree.m:
+        (restoreColorSpace):
+        (setDefaultColorProfileToRGB):
+        (main):
+
 2005-12-20  Alexey Proskuryakov  <ap@nypop.com>
 
         Reviewed by Darin Adler.
diff --git a/WebKitTools/DumpRenderTree/DumpRenderTree.m b/WebKitTools/DumpRenderTree/DumpRenderTree.m
index 6106f86..f6559a3 100644
--- a/WebKitTools/DumpRenderTree/DumpRenderTree.m
+++ b/WebKitTools/DumpRenderTree/DumpRenderTree.m
@@ -35,12 +35,12 @@
 #import <WebKit/WebPreferences.h>
 #import <WebKit/WebView.h>
 
-#import <Carbon/Carbon.h> // for GetCurrentEventTime()
-
-#import <objc/objc-runtime.h> // for class_poseAs
+#import <Carbon/Carbon.h>                           // for GetCurrentEventTime()
+#import <ApplicationServices/ApplicationServices.h> // for CMSetDefaultProfileBySpace
+#import <objc/objc-runtime.h>                       // for class_poseAs
 
 #define COMMON_DIGEST_FOR_OPENSSL
-#import <CommonCrypto/CommonDigest.h> // for MD5 functions
+#import <CommonCrypto/CommonDigest.h>               // for MD5 functions
 
 #import <getopt.h>
 
@@ -83,6 +83,52 @@
 static NSString *currentTest = nil;
 static NSPasteboard *localPasteboard;
 
+static CMProfileRef currentColorProfile = 0;
+static void restoreColorSpace(int ignored)
+{
+    if (currentColorProfile) {
+        int error = CMSetDefaultProfileByUse(cmDisplayUse, currentColorProfile);
+        if (error)
+            fprintf(stderr, "Failed to retore previous color profile!  You may need to open System Preferences : Displays : Color and manually restore your color settings.  (Error: %i)", error);
+        currentColorProfile = 0;
+    }
+}
+
+static void setDefaultColorProfileToRGB(void)
+{
+    CMProfileRef genericProfile = [[NSColorSpace genericRGBColorSpace] colorSyncProfile];
+    CMProfileRef previousProfile;
+    int error = CMGetDefaultProfileByUse(cmDisplayUse, &previousProfile);
+    if (error) {
+        fprintf(stderr, "Failed to get current color profile.  I will not be able to restore your current profile, thus I'm not changing it.  Many pixel tests may fail as a result.  (Error: %i)\n", error);
+        return;
+    }
+    if (previousProfile == genericProfile)
+        return;
+    CFStringRef previousProfileName;
+    CFStringRef genericProfileName;
+    CMCopyProfileDescriptionString(previousProfile, &previousProfileName);
+    CMCopyProfileDescriptionString(genericProfile, &genericProfileName);
+    
+    fprintf(stderr, "\n\nWARNING: Temporarily changing your system color profile from \"%s\" to \"%s\".\n",
+        CFStringGetCStringPtr(previousProfileName, kCFStringEncodingMacRoman),
+        CFStringGetCStringPtr(genericProfileName, kCFStringEncodingMacRoman));
+    fprintf(stderr, "This allows the WebKit pixel-based regression tests to have consistent color values across all machines.\n");
+    fprintf(stderr, "The colors on your screen will change for the duration of the testing.\n\n");
+    
+    if ((error = CMSetDefaultProfileByUse(cmDisplayUse, genericProfile)))
+        fprintf(stderr, "Failed to set color profile to \"%s\"! Many pixel tests will fail as a result.  (Error: %i)",
+            CFStringGetCStringPtr(genericProfileName, kCFStringEncodingMacRoman), error);
+    else {
+        currentColorProfile = previousProfile;
+        signal(SIGINT, restoreColorSpace);
+        signal(SIGHUP, restoreColorSpace);
+        signal(SIGTERM, restoreColorSpace);
+    }
+    CFRelease(genericProfileName);
+    CFRelease(previousProfileName);
+}
+
 int main(int argc, const char *argv[])
 {
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@@ -152,6 +198,9 @@
         exit(1);
     }
     
+    if (dumpPixels)
+        setDefaultColorProfileToRGB();
+    
     localPasteboard = [NSPasteboard pasteboardWithUniqueName];
 
     WebView *webView = [[WebView alloc] initWithFrame:NSMakeRect(0, 0, width, height)];
@@ -199,6 +248,9 @@
     [localPasteboard releaseGlobally];
     localPasteboard = nil;
     
+    if (dumpPixels)
+        restoreColorSpace(0);
+    
     [pool release];
 
     return 0;