PixelDumpSupportCairo.cpp 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C) 2009 Apple Inc. All rights reserved.
  3. * (C) 2009 Brent Fulgham <bfulgham@webkit.org>
  4. * (C) 2010 Igalia S.L
  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 "PixelDumpSupportCairo.h"
  32. #include "DumpRenderTree.h"
  33. #include "PixelDumpSupport.h"
  34. #include <algorithm>
  35. #include <ctype.h>
  36. #include <wtf/Assertions.h>
  37. #include <wtf/MD5.h>
  38. #include <wtf/RefPtr.h>
  39. #include <wtf/StringExtras.h>
  40. using namespace std;
  41. static cairo_status_t writeFunction(void* closure, const unsigned char* data, unsigned int length)
  42. {
  43. Vector<unsigned char>* in = reinterpret_cast<Vector<unsigned char>*>(closure);
  44. in->append(data, length);
  45. return CAIRO_STATUS_SUCCESS;
  46. }
  47. static void printPNG(cairo_surface_t* image, const char* checksum)
  48. {
  49. Vector<unsigned char> pixelData;
  50. // Only PNG output is supported for now.
  51. cairo_surface_write_to_png_stream(image, writeFunction, &pixelData);
  52. const size_t dataLength = pixelData.size();
  53. const unsigned char* data = pixelData.data();
  54. printPNG(data, dataLength, checksum);
  55. }
  56. void computeMD5HashStringForBitmapContext(BitmapContext* context, char hashString[33])
  57. {
  58. cairo_t* bitmapContext = context->cairoContext();
  59. cairo_surface_t* surface = cairo_get_target(bitmapContext);
  60. ASSERT(cairo_image_surface_get_format(surface) == CAIRO_FORMAT_ARGB32); // ImageDiff assumes 32 bit RGBA, we must as well.
  61. size_t pixelsHigh = cairo_image_surface_get_height(surface);
  62. size_t pixelsWide = cairo_image_surface_get_width(surface);
  63. size_t bytesPerRow = cairo_image_surface_get_stride(surface);
  64. MD5 md5Context;
  65. unsigned char* bitmapData = static_cast<unsigned char*>(cairo_image_surface_get_data(surface));
  66. for (unsigned row = 0; row < pixelsHigh; row++) {
  67. md5Context.addBytes(bitmapData, 4 * pixelsWide);
  68. bitmapData += bytesPerRow;
  69. }
  70. Vector<uint8_t, 16> hash;
  71. md5Context.checksum(hash);
  72. snprintf(hashString, 33, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  73. hash[0], hash[1], hash[2], hash[3], hash[4], hash[5], hash[6], hash[7],
  74. hash[8], hash[9], hash[10], hash[11], hash[12], hash[13], hash[14], hash[15]);
  75. }
  76. void dumpBitmap(BitmapContext* context, const char* checksum)
  77. {
  78. cairo_surface_t* surface = cairo_get_target(context->cairoContext());
  79. printPNG(surface, checksum);
  80. }