ImageDiffCairo.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (C) 2005, 2007 Apple Inc. All rights reserved.
  3. * Copyright (C) 2005 Ben La Monica <ben.lamonica@gmail.com>. All rights reserved.
  4. * Copyright (C) 2011 Brent Fulgham. All rights reserved.
  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. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
  19. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  23. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. // FIXME: We need to be able to include these defines from a config.h somewhere.
  28. #define JS_EXPORT_PRIVATE
  29. #define WTF_EXPORT_PRIVATE
  30. #include <cairo.h>
  31. #include <stdio.h>
  32. #include <wtf/Platform.h>
  33. #include <wtf/RefPtr.h>
  34. #include <wtf/RetainPtr.h>
  35. #if PLATFORM(WIN)
  36. #include <fcntl.h>
  37. #include <io.h>
  38. #include <windows.h>
  39. #include <wtf/MathExtras.h>
  40. #endif
  41. using namespace std;
  42. static const int s_bufferSize = 2048;
  43. static const int s_bytesPerPixel = 4;
  44. static cairo_user_data_key_t s_imageDataKey;
  45. #if PLATFORM(WIN)
  46. #undef min
  47. #undef max
  48. static inline float strtof(const char* inputString, char** endptr)
  49. {
  50. return strtod(inputString, endptr);
  51. }
  52. #endif
  53. static cairo_status_t readFromData(void* closure, unsigned char* data, unsigned int length)
  54. {
  55. CFMutableDataRef dataSource = reinterpret_cast<CFMutableDataRef>(closure);
  56. CFRange range = CFRangeMake(0, length);
  57. CFDataGetBytes(dataSource, range, data);
  58. CFDataDeleteBytes(dataSource, range);
  59. return CAIRO_STATUS_SUCCESS;
  60. }
  61. static cairo_surface_t* createImageFromStdin(int bytesRemaining)
  62. {
  63. unsigned char buffer[s_bufferSize];
  64. RetainPtr<CFMutableDataRef> data = adoptCF(CFDataCreateMutable(0, bytesRemaining));
  65. while (bytesRemaining > 0) {
  66. size_t bytesToRead = min(bytesRemaining, s_bufferSize);
  67. size_t bytesRead = fread(buffer, 1, bytesToRead, stdin);
  68. CFDataAppendBytes(data.get(), buffer, static_cast<CFIndex>(bytesRead));
  69. bytesRemaining -= static_cast<int>(bytesRead);
  70. }
  71. return cairo_image_surface_create_from_png_stream (static_cast<cairo_read_func_t>(readFromData), data.get());
  72. }
  73. static void releaseMallocBuffer(void* data)
  74. {
  75. free(data);
  76. }
  77. static inline float pixelDifference(float expected, float actual)
  78. {
  79. return (actual - expected) / max<float>(255 - expected, expected);
  80. }
  81. static inline void normalizeBuffer(float maxDistance, unsigned char* buffer, size_t length)
  82. {
  83. if (maxDistance >= 1)
  84. return;
  85. for (size_t p = 0; p < length; ++p)
  86. buffer[p] /= maxDistance;
  87. }
  88. static cairo_surface_t* createDifferenceImage(cairo_surface_t* baselineImage, cairo_surface_t* actualImage, float& difference)
  89. {
  90. size_t width = cairo_image_surface_get_width(baselineImage);
  91. size_t height = cairo_image_surface_get_height(baselineImage);
  92. unsigned char* baselinePixel = cairo_image_surface_get_data(baselineImage);
  93. unsigned char* actualPixel = cairo_image_surface_get_data(actualImage);
  94. // Compare the content of the 2 bitmaps
  95. void* diffBuffer = malloc(width * height);
  96. unsigned char* diffPixel = reinterpret_cast<unsigned char*>(diffBuffer);
  97. float count = 0;
  98. float sum = 0;
  99. float maxDistance = 0;
  100. for (size_t y = 0; y < height; ++y) {
  101. for (size_t x = 0; x < width; ++x) {
  102. float red = pixelDifference(baselinePixel[0], actualPixel[0]);
  103. float green = pixelDifference(baselinePixel[1], actualPixel[1]);
  104. float blue = pixelDifference(baselinePixel[2], actualPixel[2]);
  105. float alpha = pixelDifference(baselinePixel[3], actualPixel[3]);
  106. float distance = sqrtf(red * red + green * green + blue * blue + alpha * alpha) / 2.0;
  107. *diffPixel++ = static_cast<unsigned char>(distance * 255);
  108. if (distance >= 1.0 / 255.0) {
  109. ++count;
  110. sum += distance;
  111. if (distance > maxDistance)
  112. maxDistance = distance;
  113. }
  114. baselinePixel += s_bytesPerPixel;
  115. actualPixel += s_bytesPerPixel;
  116. }
  117. }
  118. // Compute the difference as a percentage combining both the number of different pixels and their difference amount i.e. the average distance over the entire image
  119. if (count > 0)
  120. difference = 100.0f * sum / (height * width);
  121. else
  122. difference = 0;
  123. if (!difference) {
  124. free(diffBuffer);
  125. return 0;
  126. }
  127. // Generate a normalized diff image
  128. normalizeBuffer(maxDistance, reinterpret_cast<unsigned char*>(diffBuffer), height * width);
  129. cairo_surface_t* diffImage = cairo_image_surface_create_for_data(diffPixel, CAIRO_FORMAT_ARGB32, width, height, width * s_bytesPerPixel);
  130. cairo_surface_set_user_data(diffImage, &s_imageDataKey, diffBuffer, releaseMallocBuffer);
  131. return diffImage;
  132. }
  133. static inline bool imageHasAlpha(cairo_surface_t* image)
  134. {
  135. return (cairo_image_surface_get_format(image) == CAIRO_FORMAT_ARGB32);
  136. }
  137. static cairo_status_t writeToData(void* closure, unsigned char* data, unsigned int length)
  138. {
  139. CFMutableDataRef dataTarget = reinterpret_cast<CFMutableDataRef>(closure);
  140. CFDataAppendBytes(dataTarget, data, length);
  141. return CAIRO_STATUS_SUCCESS;
  142. }
  143. int main(int argc, const char* argv[])
  144. {
  145. #if PLATFORM(WIN)
  146. _setmode(0, _O_BINARY);
  147. _setmode(1, _O_BINARY);
  148. #endif
  149. float tolerance = 0;
  150. for (int i = 1; i < argc; ++i) {
  151. if (!strcmp(argv[i], "-t") || !strcmp(argv[i], "--tolerance")) {
  152. if (i >= argc - 1)
  153. exit(1);
  154. tolerance = strtof(argv[i + 1], 0);
  155. ++i;
  156. continue;
  157. }
  158. }
  159. char buffer[s_bufferSize];
  160. cairo_surface_t* actualImage = 0;
  161. cairo_surface_t* baselineImage = 0;
  162. while (fgets(buffer, sizeof(buffer), stdin)) {
  163. char* newLineCharacter = strchr(buffer, '\n');
  164. if (newLineCharacter)
  165. *newLineCharacter = '\0';
  166. if (!strncmp("Content-Length: ", buffer, 16)) {
  167. strtok(buffer, " ");
  168. int imageSize = strtol(strtok(0, " "), 0, 10);
  169. if (imageSize > 0 && !actualImage)
  170. actualImage = createImageFromStdin(imageSize);
  171. else if (imageSize > 0 && !baselineImage)
  172. baselineImage = createImageFromStdin(imageSize);
  173. else
  174. fputs("error, image size must be specified.\n", stdout);
  175. }
  176. if (actualImage && baselineImage) {
  177. cairo_surface_t* diffImage = 0;
  178. float difference = 100.0;
  179. if ((cairo_image_surface_get_width(actualImage) == cairo_image_surface_get_width(baselineImage))
  180. && (cairo_image_surface_get_height(actualImage) == cairo_image_surface_get_height(baselineImage))
  181. && (imageHasAlpha(actualImage) == imageHasAlpha(baselineImage))) {
  182. diffImage = createDifferenceImage(actualImage, baselineImage, difference); // difference is passed by reference
  183. if (difference <= tolerance)
  184. difference = 0;
  185. else {
  186. difference = roundf(difference * 100.0) / 100.0;
  187. difference = max<float>(difference, 0.01); // round to 2 decimal places
  188. }
  189. } else
  190. fputs("error, test and reference image have different properties.\n", stderr);
  191. if (difference > 0.0) {
  192. if (diffImage) {
  193. RetainPtr<CFMutableDataRef> imageData = adoptCF(CFDataCreateMutable(0, 0));
  194. cairo_surface_write_to_png_stream(diffImage, (cairo_write_func_t)writeToData, imageData.get());
  195. printf("Content-Length: %lu\n", CFDataGetLength(imageData.get()));
  196. fwrite(CFDataGetBytePtr(imageData.get()), 1, CFDataGetLength(imageData.get()), stdout);
  197. cairo_surface_destroy(diffImage);
  198. diffImage = 0;
  199. }
  200. fprintf(stdout, "diff: %01.2f%% failed\n", difference);
  201. } else
  202. fprintf(stdout, "diff: %01.2f%% passed\n", difference);
  203. cairo_surface_destroy(actualImage);
  204. cairo_surface_destroy(baselineImage);
  205. actualImage = 0;
  206. baselineImage = 0;
  207. }
  208. fflush(stdout);
  209. }
  210. return 0;
  211. }