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