skdiff_utils.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright 2012 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/codec/SkCodec.h"
  8. #include "include/core/SkBitmap.h"
  9. #include "include/core/SkData.h"
  10. #include "include/core/SkImageEncoder.h"
  11. #include "include/core/SkStream.h"
  12. #include "include/core/SkTypes.h"
  13. #include "tools/ToolUtils.h"
  14. #include "tools/skdiff/skdiff.h"
  15. #include "tools/skdiff/skdiff_utils.h"
  16. #include <memory>
  17. bool are_buffers_equal(SkData* skdata1, SkData* skdata2) {
  18. if ((nullptr == skdata1) || (nullptr == skdata2)) {
  19. return false;
  20. }
  21. if (skdata1->size() != skdata2->size()) {
  22. return false;
  23. }
  24. return (0 == memcmp(skdata1->data(), skdata2->data(), skdata1->size()));
  25. }
  26. sk_sp<SkData> read_file(const char* file_path) {
  27. sk_sp<SkData> data(SkData::MakeFromFileName(file_path));
  28. if (!data) {
  29. SkDebugf("WARNING: could not open file <%s> for reading\n", file_path);
  30. }
  31. return data;
  32. }
  33. bool get_bitmap(sk_sp<SkData> fileBits, DiffResource& resource, bool sizeOnly,
  34. bool ignoreColorSpace) {
  35. auto codec = SkCodec::MakeFromData(fileBits);
  36. if (!codec) {
  37. SkDebugf("ERROR: could not create codec for <%s>\n", resource.fFullPath.c_str());
  38. resource.fStatus = DiffResource::kCouldNotDecode_Status;
  39. return false;
  40. }
  41. // If we're "ignoring" color space, then we want the raw pixel values from each image, so we
  42. // decode to the original color space. If we want to account for color spaces, then we want to
  43. // decode each image to the same color space, so that colors that are the "same" (but encoded
  44. // differently) are transformed to some canonical representation prior to comparison.
  45. //
  46. // TODO: Use something wider than sRGB to avoid clipping with out-of-gamut colors.
  47. SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
  48. if (!ignoreColorSpace) {
  49. info = info.makeColorSpace(SkColorSpace::MakeSRGB());
  50. }
  51. if (!resource.fBitmap.setInfo(info.makeColorType(kN32_SkColorType))) {
  52. SkDebugf("ERROR: could not set bitmap info for <%s>\n", resource.fFullPath.c_str());
  53. resource.fStatus = DiffResource::kCouldNotDecode_Status;
  54. return false;
  55. }
  56. if (sizeOnly) {
  57. return true;
  58. }
  59. if (!resource.fBitmap.tryAllocPixels()) {
  60. SkDebugf("ERROR: could not allocate pixels for <%s>\n", resource.fFullPath.c_str());
  61. resource.fStatus = DiffResource::kCouldNotDecode_Status;
  62. return false;
  63. }
  64. if (SkCodec::kSuccess != codec->getPixels(resource.fBitmap.info(),
  65. resource.fBitmap.getPixels(), resource.fBitmap.rowBytes())) {
  66. SkDebugf("ERROR: codec failed for basePath <%s>\n", resource.fFullPath.c_str());
  67. resource.fStatus = DiffResource::kCouldNotDecode_Status;
  68. return false;
  69. }
  70. resource.fStatus = DiffResource::kDecoded_Status;
  71. return true;
  72. }
  73. /** Thanks to PNG, we need to force all pixels 100% opaque. */
  74. static void force_all_opaque(const SkBitmap& bitmap) {
  75. for (int y = 0; y < bitmap.height(); y++) {
  76. for (int x = 0; x < bitmap.width(); x++) {
  77. *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
  78. }
  79. }
  80. }
  81. bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
  82. SkBitmap copy;
  83. ToolUtils::copy_to(&copy, kN32_SkColorType, bitmap);
  84. force_all_opaque(copy);
  85. return ToolUtils::EncodeImageToFile(path.c_str(), copy, SkEncodedImageFormat::kPNG, 100);
  86. }
  87. /// Return a copy of the "input" string, within which we have replaced all instances
  88. /// of oldSubstring with newSubstring.
  89. ///
  90. /// TODO: If we like this, we should move it into the core SkString implementation,
  91. /// adding more checks and ample test cases, and paying more attention to efficiency.
  92. static SkString replace_all(const SkString &input,
  93. const char oldSubstring[], const char newSubstring[]) {
  94. SkString output;
  95. const char *input_cstr = input.c_str();
  96. const char *first_char = input_cstr;
  97. const char *match_char;
  98. size_t oldSubstringLen = strlen(oldSubstring);
  99. while ((match_char = strstr(first_char, oldSubstring))) {
  100. output.append(first_char, (match_char - first_char));
  101. output.append(newSubstring);
  102. first_char = match_char + oldSubstringLen;
  103. }
  104. output.append(first_char);
  105. return output;
  106. }
  107. static SkString filename_to_derived_filename(const SkString& filename, const char *suffix) {
  108. SkString diffName (filename);
  109. const char* cstring = diffName.c_str();
  110. size_t dotOffset = strrchr(cstring, '.') - cstring;
  111. diffName.remove(dotOffset, diffName.size() - dotOffset);
  112. diffName.append(suffix);
  113. // In case we recursed into subdirectories, replace slashes with something else
  114. // so the diffs will all be written into a single flat directory.
  115. diffName = replace_all(diffName, PATH_DIV_STR, "_");
  116. return diffName;
  117. }
  118. SkString filename_to_diff_filename(const SkString& filename) {
  119. return filename_to_derived_filename(filename, "-diff.png");
  120. }
  121. SkString filename_to_white_filename(const SkString& filename) {
  122. return filename_to_derived_filename(filename, "-white.png");
  123. }
  124. void create_and_write_diff_image(DiffRecord* drp,
  125. DiffMetricProc dmp,
  126. const int colorThreshold,
  127. const SkString& outputDir,
  128. const SkString& filename) {
  129. const int w = drp->fBase.fBitmap.width();
  130. const int h = drp->fBase.fBitmap.height();
  131. if (w != drp->fComparison.fBitmap.width() || h != drp->fComparison.fBitmap.height()) {
  132. drp->fResult = DiffRecord::kDifferentSizes_Result;
  133. } else {
  134. drp->fDifference.fBitmap.allocN32Pixels(w, h);
  135. drp->fWhite.fBitmap.allocN32Pixels(w, h);
  136. SkASSERT(DiffRecord::kUnknown_Result == drp->fResult);
  137. compute_diff(drp, dmp, colorThreshold);
  138. SkASSERT(DiffRecord::kUnknown_Result != drp->fResult);
  139. }
  140. if (outputDir.isEmpty()) {
  141. drp->fDifference.fStatus = DiffResource::kUnspecified_Status;
  142. drp->fWhite.fStatus = DiffResource::kUnspecified_Status;
  143. } else {
  144. drp->fDifference.fFilename = filename_to_diff_filename(filename);
  145. drp->fDifference.fFullPath = outputDir;
  146. drp->fDifference.fFullPath.append(drp->fDifference.fFilename);
  147. drp->fDifference.fStatus = DiffResource::kSpecified_Status;
  148. drp->fWhite.fFilename = filename_to_white_filename(filename);
  149. drp->fWhite.fFullPath = outputDir;
  150. drp->fWhite.fFullPath.append(drp->fWhite.fFilename);
  151. drp->fWhite.fStatus = DiffResource::kSpecified_Status;
  152. if (DiffRecord::kDifferentPixels_Result == drp->fResult) {
  153. if (write_bitmap(drp->fDifference.fFullPath, drp->fDifference.fBitmap)) {
  154. drp->fDifference.fStatus = DiffResource::kExists_Status;
  155. } else {
  156. drp->fDifference.fStatus = DiffResource::kDoesNotExist_Status;
  157. }
  158. if (write_bitmap(drp->fWhite.fFullPath, drp->fWhite.fBitmap)) {
  159. drp->fWhite.fStatus = DiffResource::kExists_Status;
  160. } else {
  161. drp->fWhite.fStatus = DiffResource::kDoesNotExist_Status;
  162. }
  163. }
  164. }
  165. }