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