bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 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 "skdiff.h" |
| 8 | #include "skdiff_utils.h" |
Hal Canary | 248ff02 | 2016-11-22 09:03:03 -0700 | [diff] [blame^] | 9 | #include "sk_tool_utils.h" |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 10 | #include "SkBitmap.h" |
msarett | 667433f | 2016-03-17 07:17:54 -0700 | [diff] [blame] | 11 | #include "SkCodec.h" |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 12 | #include "SkData.h" |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 13 | #include "SkImageEncoder.h" |
| 14 | #include "SkStream.h" |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 15 | #include "SkTypes.h" |
| 16 | |
bungeman | f3c15b7 | 2015-08-19 11:56:48 -0700 | [diff] [blame] | 17 | #include <memory> |
| 18 | |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 19 | bool are_buffers_equal(SkData* skdata1, SkData* skdata2) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 20 | if ((nullptr == skdata1) || (nullptr == skdata2)) { |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 21 | return false; |
| 22 | } |
| 23 | if (skdata1->size() != skdata2->size()) { |
| 24 | return false; |
| 25 | } |
| 26 | return (0 == memcmp(skdata1->data(), skdata2->data(), skdata1->size())); |
| 27 | } |
| 28 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 29 | sk_sp<SkData> read_file(const char* file_path) { |
| 30 | sk_sp<SkData> data(SkData::MakeFromFileName(file_path)); |
reed | 9594da1 | 2014-09-12 12:12:27 -0700 | [diff] [blame] | 31 | if (!data) { |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 32 | SkDebugf("WARNING: could not open file <%s> for reading\n", file_path); |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 33 | } |
reed | 9594da1 | 2014-09-12 12:12:27 -0700 | [diff] [blame] | 34 | return data; |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 35 | } |
| 36 | |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 37 | bool get_bitmap(sk_sp<SkData> fileBits, DiffResource& resource, bool sizeOnly) { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 38 | std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(fileBits)); |
msarett | 667433f | 2016-03-17 07:17:54 -0700 | [diff] [blame] | 39 | if (!codec) { |
| 40 | SkDebugf("ERROR: could not create codec for <%s>\n", resource.fFullPath.c_str()); |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 41 | resource.fStatus = DiffResource::kCouldNotDecode_Status; |
| 42 | return false; |
| 43 | } |
| 44 | |
msarett | 667433f | 2016-03-17 07:17:54 -0700 | [diff] [blame] | 45 | if (!resource.fBitmap.setInfo(codec->getInfo().makeColorType(kN32_SkColorType))) { |
| 46 | SkDebugf("ERROR: could not set bitmap info for <%s>\n", resource.fFullPath.c_str()); |
| 47 | resource.fStatus = DiffResource::kCouldNotDecode_Status; |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | if (sizeOnly) { |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | if (!resource.fBitmap.tryAllocPixels()) { |
| 56 | SkDebugf("ERROR: could not allocate pixels for <%s>\n", resource.fFullPath.c_str()); |
| 57 | resource.fStatus = DiffResource::kCouldNotDecode_Status; |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | if (SkCodec::kSuccess != codec->getPixels(resource.fBitmap.info(), |
| 62 | resource.fBitmap.getPixels(), resource.fBitmap.rowBytes())) { |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 63 | SkDebugf("ERROR: codec failed for basePath <%s>\n", resource.fFullPath.c_str()); |
| 64 | resource.fStatus = DiffResource::kCouldNotDecode_Status; |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | resource.fStatus = DiffResource::kDecoded_Status; |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | /** Thanks to PNG, we need to force all pixels 100% opaque. */ |
| 73 | static void force_all_opaque(const SkBitmap& bitmap) { |
| 74 | SkAutoLockPixels lock(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 | |
| 82 | bool write_bitmap(const SkString& path, const SkBitmap& bitmap) { |
| 83 | SkBitmap copy; |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 84 | bitmap.copyTo(©, kN32_SkColorType); |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 85 | force_all_opaque(copy); |
Hal Canary | 248ff02 | 2016-11-22 09:03:03 -0700 | [diff] [blame^] | 86 | return sk_tool_utils::EncodeImageToFile(path.c_str(), copy, |
| 87 | SkEncodedImageFormat::kPNG, 100); |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /// Return a copy of the "input" string, within which we have replaced all instances |
| 91 | /// of oldSubstring with newSubstring. |
| 92 | /// |
| 93 | /// TODO: If we like this, we should move it into the core SkString implementation, |
| 94 | /// adding more checks and ample test cases, and paying more attention to efficiency. |
| 95 | static SkString replace_all(const SkString &input, |
| 96 | const char oldSubstring[], const char newSubstring[]) { |
| 97 | SkString output; |
| 98 | const char *input_cstr = input.c_str(); |
| 99 | const char *first_char = input_cstr; |
| 100 | const char *match_char; |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 101 | size_t oldSubstringLen = strlen(oldSubstring); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 102 | while ((match_char = strstr(first_char, oldSubstring))) { |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 103 | output.append(first_char, (match_char - first_char)); |
| 104 | output.append(newSubstring); |
| 105 | first_char = match_char + oldSubstringLen; |
| 106 | } |
| 107 | output.append(first_char); |
| 108 | return output; |
| 109 | } |
| 110 | |
| 111 | static SkString filename_to_derived_filename(const SkString& filename, const char *suffix) { |
| 112 | SkString diffName (filename); |
| 113 | const char* cstring = diffName.c_str(); |
robertphillips@google.com | 8b16931 | 2013-10-15 17:47:36 +0000 | [diff] [blame] | 114 | size_t dotOffset = strrchr(cstring, '.') - cstring; |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 115 | diffName.remove(dotOffset, diffName.size() - dotOffset); |
| 116 | diffName.append(suffix); |
| 117 | |
| 118 | // In case we recursed into subdirectories, replace slashes with something else |
| 119 | // so the diffs will all be written into a single flat directory. |
| 120 | diffName = replace_all(diffName, PATH_DIV_STR, "_"); |
| 121 | return diffName; |
| 122 | } |
| 123 | |
| 124 | SkString filename_to_diff_filename(const SkString& filename) { |
| 125 | return filename_to_derived_filename(filename, "-diff.png"); |
| 126 | } |
| 127 | |
| 128 | SkString filename_to_white_filename(const SkString& filename) { |
| 129 | return filename_to_derived_filename(filename, "-white.png"); |
| 130 | } |
| 131 | |
| 132 | void create_and_write_diff_image(DiffRecord* drp, |
| 133 | DiffMetricProc dmp, |
| 134 | const int colorThreshold, |
| 135 | const SkString& outputDir, |
| 136 | const SkString& filename) { |
| 137 | const int w = drp->fBase.fBitmap.width(); |
| 138 | const int h = drp->fBase.fBitmap.height(); |
| 139 | |
| 140 | if (w != drp->fComparison.fBitmap.width() || h != drp->fComparison.fBitmap.height()) { |
| 141 | drp->fResult = DiffRecord::kDifferentSizes_Result; |
| 142 | } else { |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 143 | drp->fDifference.fBitmap.allocN32Pixels(w, h); |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 144 | |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 145 | drp->fWhite.fBitmap.allocN32Pixels(w, h); |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 146 | |
| 147 | SkASSERT(DiffRecord::kUnknown_Result == drp->fResult); |
| 148 | compute_diff(drp, dmp, colorThreshold); |
| 149 | SkASSERT(DiffRecord::kUnknown_Result != drp->fResult); |
| 150 | } |
| 151 | |
| 152 | if (outputDir.isEmpty()) { |
| 153 | drp->fDifference.fStatus = DiffResource::kUnspecified_Status; |
| 154 | drp->fWhite.fStatus = DiffResource::kUnspecified_Status; |
| 155 | |
| 156 | } else { |
| 157 | drp->fDifference.fFilename = filename_to_diff_filename(filename); |
| 158 | drp->fDifference.fFullPath = outputDir; |
| 159 | drp->fDifference.fFullPath.append(drp->fDifference.fFilename); |
| 160 | drp->fDifference.fStatus = DiffResource::kSpecified_Status; |
| 161 | |
| 162 | drp->fWhite.fFilename = filename_to_white_filename(filename); |
| 163 | drp->fWhite.fFullPath = outputDir; |
| 164 | drp->fWhite.fFullPath.append(drp->fWhite.fFilename); |
| 165 | drp->fWhite.fStatus = DiffResource::kSpecified_Status; |
| 166 | |
| 167 | if (DiffRecord::kDifferentPixels_Result == drp->fResult) { |
| 168 | if (write_bitmap(drp->fDifference.fFullPath, drp->fDifference.fBitmap)) { |
| 169 | drp->fDifference.fStatus = DiffResource::kExists_Status; |
| 170 | } else { |
| 171 | drp->fDifference.fStatus = DiffResource::kDoesNotExist_Status; |
| 172 | } |
| 173 | if (write_bitmap(drp->fWhite.fFullPath, drp->fWhite.fBitmap)) { |
| 174 | drp->fWhite.fStatus = DiffResource::kExists_Status; |
| 175 | } else { |
| 176 | drp->fWhite.fStatus = DiffResource::kDoesNotExist_Status; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |