PixelDumpSupportCG.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C) 2005, 2006, 2007 Apple Inc. All rights reserved.
  3. * (C) 2007 Graham Dennis (graham.dennis@gmail.com)
  4. * (C) 2007 Eric Seidel <eric@webkit.org>
  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. #include "config.h"
  31. #include "PixelDumpSupportCG.h"
  32. #include "DumpRenderTree.h"
  33. #include "PixelDumpSupport.h"
  34. #include <ImageIO/CGImageDestination.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. #if PLATFORM(WIN)
  42. #include "MD5.h"
  43. #elif PLATFORM(MAC)
  44. #include <LaunchServices/UTCoreTypes.h>
  45. #define COMMON_DIGEST_FOR_OPENSSL
  46. #include <CommonCrypto/CommonDigest.h>
  47. #endif
  48. using namespace std;
  49. #if PLATFORM(WIN)
  50. static const CFStringRef kUTTypePNG = CFSTR("public.png");
  51. #endif
  52. static void printPNG(CGImageRef image, const char* checksum)
  53. {
  54. RetainPtr<CFMutableDataRef> imageData = adoptCF(CFDataCreateMutable(0, 0));
  55. RetainPtr<CGImageDestinationRef> imageDest = adoptCF(CGImageDestinationCreateWithData(imageData.get(), kUTTypePNG, 1, 0));
  56. CGImageDestinationAddImage(imageDest.get(), image, 0);
  57. CGImageDestinationFinalize(imageDest.get());
  58. const UInt8* data = CFDataGetBytePtr(imageData.get());
  59. CFIndex dataLength = CFDataGetLength(imageData.get());
  60. printPNG(static_cast<const unsigned char*>(data), static_cast<size_t>(dataLength), checksum);
  61. }
  62. void computeMD5HashStringForBitmapContext(BitmapContext* context, char hashString[33])
  63. {
  64. CGContextRef bitmapContext = context->cgContext();
  65. ASSERT(CGBitmapContextGetBitsPerPixel(bitmapContext) == 32); // ImageDiff assumes 32 bit RGBA, we must as well.
  66. size_t pixelsHigh = CGBitmapContextGetHeight(bitmapContext);
  67. size_t pixelsWide = CGBitmapContextGetWidth(bitmapContext);
  68. size_t bytesPerRow = CGBitmapContextGetBytesPerRow(bitmapContext);
  69. // We need to swap the bytes to ensure consistent hashes independently of endianness
  70. MD5_CTX md5Context;
  71. MD5_Init(&md5Context);
  72. unsigned char* bitmapData = static_cast<unsigned char*>(CGBitmapContextGetData(bitmapContext));
  73. #if PLATFORM(MAC)
  74. if ((CGBitmapContextGetBitmapInfo(bitmapContext) & kCGBitmapByteOrderMask) == kCGBitmapByteOrder32Big) {
  75. for (unsigned row = 0; row < pixelsHigh; row++) {
  76. uint32_t buffer[pixelsWide];
  77. for (unsigned column = 0; column < pixelsWide; column++)
  78. buffer[column] = OSReadLittleInt32(bitmapData, 4 * column);
  79. MD5_Update(&md5Context, buffer, 4 * pixelsWide);
  80. bitmapData += bytesPerRow;
  81. }
  82. } else
  83. #endif
  84. {
  85. for (unsigned row = 0; row < pixelsHigh; row++) {
  86. MD5_Update(&md5Context, bitmapData, 4 * pixelsWide);
  87. bitmapData += bytesPerRow;
  88. }
  89. }
  90. unsigned char hash[16];
  91. MD5_Final(hash, &md5Context);
  92. hashString[0] = '\0';
  93. for (int i = 0; i < 16; i++)
  94. snprintf(hashString, 33, "%s%02x", hashString, hash[i]);
  95. }
  96. void dumpBitmap(BitmapContext* context, const char* checksum)
  97. {
  98. RetainPtr<CGImageRef> image = adoptCF(CGBitmapContextCreateImage(context->cgContext()));
  99. printPNG(image.get(), checksum);
  100. }