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 | */ |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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" |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 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 | |
Brian Osman | 9889c24 | 2018-07-17 16:45:40 -0400 | [diff] [blame] | 37 | bool get_bitmap(sk_sp<SkData> fileBits, DiffResource& resource, bool sizeOnly, |
| 38 | bool ignoreColorSpace) { |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 39 | auto codec = SkCodec::MakeFromData(fileBits); |
msarett | 667433f | 2016-03-17 07:17:54 -0700 | [diff] [blame] | 40 | if (!codec) { |
| 41 | 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] | 42 | resource.fStatus = DiffResource::kCouldNotDecode_Status; |
| 43 | return false; |
| 44 | } |
| 45 | |
Brian Osman | 9889c24 | 2018-07-17 16:45:40 -0400 | [diff] [blame] | 46 | // If we're "ignoring" color space, then we want the raw pixel values from each image, so we |
| 47 | // decode to the original color space. If we want to account for color spaces, then we want to |
| 48 | // decode each image to the same color space, so that colors that are the "same" (but encoded |
| 49 | // differently) are transformed to some canonical representation prior to comparison. |
| 50 | // |
| 51 | // TODO: Use something wider than sRGB to avoid clipping with out-of-gamut colors. |
| 52 | SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType); |
| 53 | if (!ignoreColorSpace) { |
| 54 | info = info.makeColorSpace(SkColorSpace::MakeSRGB()); |
| 55 | } |
| 56 | |
| 57 | if (!resource.fBitmap.setInfo(info.makeColorType(kN32_SkColorType))) { |
msarett | 667433f | 2016-03-17 07:17:54 -0700 | [diff] [blame] | 58 | SkDebugf("ERROR: could not set bitmap info for <%s>\n", resource.fFullPath.c_str()); |
| 59 | resource.fStatus = DiffResource::kCouldNotDecode_Status; |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | if (sizeOnly) { |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | if (!resource.fBitmap.tryAllocPixels()) { |
| 68 | SkDebugf("ERROR: could not allocate pixels for <%s>\n", resource.fFullPath.c_str()); |
| 69 | resource.fStatus = DiffResource::kCouldNotDecode_Status; |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | if (SkCodec::kSuccess != codec->getPixels(resource.fBitmap.info(), |
| 74 | resource.fBitmap.getPixels(), resource.fBitmap.rowBytes())) { |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 75 | SkDebugf("ERROR: codec failed for basePath <%s>\n", resource.fFullPath.c_str()); |
| 76 | resource.fStatus = DiffResource::kCouldNotDecode_Status; |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | resource.fStatus = DiffResource::kDecoded_Status; |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | /** Thanks to PNG, we need to force all pixels 100% opaque. */ |
| 85 | static void force_all_opaque(const SkBitmap& bitmap) { |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 86 | for (int y = 0; y < bitmap.height(); y++) { |
| 87 | for (int x = 0; x < bitmap.width(); x++) { |
| 88 | *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | bool write_bitmap(const SkString& path, const SkBitmap& bitmap) { |
| 94 | SkBitmap copy; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 95 | ToolUtils::copy_to(©, kN32_SkColorType, bitmap); |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 96 | force_all_opaque(copy); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 97 | return ToolUtils::EncodeImageToFile(path.c_str(), copy, SkEncodedImageFormat::kPNG, 100); |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /// Return a copy of the "input" string, within which we have replaced all instances |
| 101 | /// of oldSubstring with newSubstring. |
| 102 | /// |
| 103 | /// TODO: If we like this, we should move it into the core SkString implementation, |
| 104 | /// adding more checks and ample test cases, and paying more attention to efficiency. |
| 105 | static SkString replace_all(const SkString &input, |
| 106 | const char oldSubstring[], const char newSubstring[]) { |
| 107 | SkString output; |
| 108 | const char *input_cstr = input.c_str(); |
| 109 | const char *first_char = input_cstr; |
| 110 | const char *match_char; |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 111 | size_t oldSubstringLen = strlen(oldSubstring); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 112 | while ((match_char = strstr(first_char, oldSubstring))) { |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 113 | output.append(first_char, (match_char - first_char)); |
| 114 | output.append(newSubstring); |
| 115 | first_char = match_char + oldSubstringLen; |
| 116 | } |
| 117 | output.append(first_char); |
| 118 | return output; |
| 119 | } |
| 120 | |
| 121 | static SkString filename_to_derived_filename(const SkString& filename, const char *suffix) { |
| 122 | SkString diffName (filename); |
| 123 | const char* cstring = diffName.c_str(); |
robertphillips@google.com | 8b16931 | 2013-10-15 17:47:36 +0000 | [diff] [blame] | 124 | size_t dotOffset = strrchr(cstring, '.') - cstring; |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 125 | diffName.remove(dotOffset, diffName.size() - dotOffset); |
| 126 | diffName.append(suffix); |
| 127 | |
| 128 | // In case we recursed into subdirectories, replace slashes with something else |
| 129 | // so the diffs will all be written into a single flat directory. |
| 130 | diffName = replace_all(diffName, PATH_DIV_STR, "_"); |
| 131 | return diffName; |
| 132 | } |
| 133 | |
| 134 | SkString filename_to_diff_filename(const SkString& filename) { |
| 135 | return filename_to_derived_filename(filename, "-diff.png"); |
| 136 | } |
| 137 | |
| 138 | SkString filename_to_white_filename(const SkString& filename) { |
| 139 | return filename_to_derived_filename(filename, "-white.png"); |
| 140 | } |
| 141 | |
| 142 | void create_and_write_diff_image(DiffRecord* drp, |
| 143 | DiffMetricProc dmp, |
| 144 | const int colorThreshold, |
| 145 | const SkString& outputDir, |
| 146 | const SkString& filename) { |
| 147 | const int w = drp->fBase.fBitmap.width(); |
| 148 | const int h = drp->fBase.fBitmap.height(); |
| 149 | |
| 150 | if (w != drp->fComparison.fBitmap.width() || h != drp->fComparison.fBitmap.height()) { |
| 151 | drp->fResult = DiffRecord::kDifferentSizes_Result; |
| 152 | } else { |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 153 | drp->fDifference.fBitmap.allocN32Pixels(w, h); |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 154 | |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 155 | drp->fWhite.fBitmap.allocN32Pixels(w, h); |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 156 | |
| 157 | SkASSERT(DiffRecord::kUnknown_Result == drp->fResult); |
| 158 | compute_diff(drp, dmp, colorThreshold); |
| 159 | SkASSERT(DiffRecord::kUnknown_Result != drp->fResult); |
| 160 | } |
| 161 | |
| 162 | if (outputDir.isEmpty()) { |
| 163 | drp->fDifference.fStatus = DiffResource::kUnspecified_Status; |
| 164 | drp->fWhite.fStatus = DiffResource::kUnspecified_Status; |
| 165 | |
| 166 | } else { |
| 167 | drp->fDifference.fFilename = filename_to_diff_filename(filename); |
| 168 | drp->fDifference.fFullPath = outputDir; |
| 169 | drp->fDifference.fFullPath.append(drp->fDifference.fFilename); |
| 170 | drp->fDifference.fStatus = DiffResource::kSpecified_Status; |
| 171 | |
| 172 | drp->fWhite.fFilename = filename_to_white_filename(filename); |
| 173 | drp->fWhite.fFullPath = outputDir; |
| 174 | drp->fWhite.fFullPath.append(drp->fWhite.fFilename); |
| 175 | drp->fWhite.fStatus = DiffResource::kSpecified_Status; |
| 176 | |
| 177 | if (DiffRecord::kDifferentPixels_Result == drp->fResult) { |
| 178 | if (write_bitmap(drp->fDifference.fFullPath, drp->fDifference.fBitmap)) { |
| 179 | drp->fDifference.fStatus = DiffResource::kExists_Status; |
| 180 | } else { |
| 181 | drp->fDifference.fStatus = DiffResource::kDoesNotExist_Status; |
| 182 | } |
| 183 | if (write_bitmap(drp->fWhite.fFullPath, drp->fWhite.fBitmap)) { |
| 184 | drp->fWhite.fStatus = DiffResource::kExists_Status; |
| 185 | } else { |
| 186 | drp->fWhite.fStatus = DiffResource::kDoesNotExist_Status; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | } |