blob: 18682c02b7199d224923518424b8f95c270026f0 [file] [log] [blame]
bungeman@google.come3c8ddf2012-12-05 20:13:12 +00001/*
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 Canarydb683012016-11-23 08:55:18 -07009#include "sk_tool_utils.h"
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000010#include "SkBitmap.h"
msarett667433f2016-03-17 07:17:54 -070011#include "SkCodec.h"
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000012#include "SkData.h"
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000013#include "SkImageEncoder.h"
14#include "SkStream.h"
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000015#include "SkTypes.h"
16
bungemanf3c15b72015-08-19 11:56:48 -070017#include <memory>
18
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000019bool are_buffers_equal(SkData* skdata1, SkData* skdata2) {
halcanary96fcdcc2015-08-27 07:41:13 -070020 if ((nullptr == skdata1) || (nullptr == skdata2)) {
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000021 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
bungeman38d909e2016-08-02 14:40:46 -070029sk_sp<SkData> read_file(const char* file_path) {
30 sk_sp<SkData> data(SkData::MakeFromFileName(file_path));
reed9594da12014-09-12 12:12:27 -070031 if (!data) {
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000032 SkDebugf("WARNING: could not open file <%s> for reading\n", file_path);
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000033 }
reed9594da12014-09-12 12:12:27 -070034 return data;
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000035}
36
Brian Osman9889c242018-07-17 16:45:40 -040037bool get_bitmap(sk_sp<SkData> fileBits, DiffResource& resource, bool sizeOnly,
38 bool ignoreColorSpace) {
Mike Reedede7bac2017-07-23 15:30:02 -040039 auto codec = SkCodec::MakeFromData(fileBits);
msarett667433f2016-03-17 07:17:54 -070040 if (!codec) {
41 SkDebugf("ERROR: could not create codec for <%s>\n", resource.fFullPath.c_str());
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000042 resource.fStatus = DiffResource::kCouldNotDecode_Status;
43 return false;
44 }
45
Brian Osman9889c242018-07-17 16:45:40 -040046 // 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))) {
msarett667433f2016-03-17 07:17:54 -070058 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.come3c8ddf2012-12-05 20:13:12 +000075 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. */
85static void force_all_opaque(const SkBitmap& bitmap) {
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000086 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
93bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
94 SkBitmap copy;
Matt Sarett68b8e3d2017-04-28 11:15:22 -040095 sk_tool_utils::copy_to(&copy, kN32_SkColorType, bitmap);
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000096 force_all_opaque(copy);
Hal Canarydb683012016-11-23 08:55:18 -070097 return sk_tool_utils::EncodeImageToFile(path.c_str(), copy,
98 SkEncodedImageFormat::kPNG, 100);
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000099}
100
101/// Return a copy of the "input" string, within which we have replaced all instances
102/// of oldSubstring with newSubstring.
103///
104/// TODO: If we like this, we should move it into the core SkString implementation,
105/// adding more checks and ample test cases, and paying more attention to efficiency.
106static SkString replace_all(const SkString &input,
107 const char oldSubstring[], const char newSubstring[]) {
108 SkString output;
109 const char *input_cstr = input.c_str();
110 const char *first_char = input_cstr;
111 const char *match_char;
robertphillips@google.comadacc702013-10-14 21:53:24 +0000112 size_t oldSubstringLen = strlen(oldSubstring);
bsalomon49f085d2014-09-05 13:34:00 -0700113 while ((match_char = strstr(first_char, oldSubstring))) {
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000114 output.append(first_char, (match_char - first_char));
115 output.append(newSubstring);
116 first_char = match_char + oldSubstringLen;
117 }
118 output.append(first_char);
119 return output;
120}
121
122static SkString filename_to_derived_filename(const SkString& filename, const char *suffix) {
123 SkString diffName (filename);
124 const char* cstring = diffName.c_str();
robertphillips@google.com8b169312013-10-15 17:47:36 +0000125 size_t dotOffset = strrchr(cstring, '.') - cstring;
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000126 diffName.remove(dotOffset, diffName.size() - dotOffset);
127 diffName.append(suffix);
128
129 // In case we recursed into subdirectories, replace slashes with something else
130 // so the diffs will all be written into a single flat directory.
131 diffName = replace_all(diffName, PATH_DIV_STR, "_");
132 return diffName;
133}
134
135SkString filename_to_diff_filename(const SkString& filename) {
136 return filename_to_derived_filename(filename, "-diff.png");
137}
138
139SkString filename_to_white_filename(const SkString& filename) {
140 return filename_to_derived_filename(filename, "-white.png");
141}
142
143void create_and_write_diff_image(DiffRecord* drp,
144 DiffMetricProc dmp,
145 const int colorThreshold,
146 const SkString& outputDir,
147 const SkString& filename) {
148 const int w = drp->fBase.fBitmap.width();
149 const int h = drp->fBase.fBitmap.height();
150
151 if (w != drp->fComparison.fBitmap.width() || h != drp->fComparison.fBitmap.height()) {
152 drp->fResult = DiffRecord::kDifferentSizes_Result;
153 } else {
reed6c225732014-06-09 19:52:07 -0700154 drp->fDifference.fBitmap.allocN32Pixels(w, h);
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000155
reed6c225732014-06-09 19:52:07 -0700156 drp->fWhite.fBitmap.allocN32Pixels(w, h);
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000157
158 SkASSERT(DiffRecord::kUnknown_Result == drp->fResult);
159 compute_diff(drp, dmp, colorThreshold);
160 SkASSERT(DiffRecord::kUnknown_Result != drp->fResult);
161 }
162
163 if (outputDir.isEmpty()) {
164 drp->fDifference.fStatus = DiffResource::kUnspecified_Status;
165 drp->fWhite.fStatus = DiffResource::kUnspecified_Status;
166
167 } else {
168 drp->fDifference.fFilename = filename_to_diff_filename(filename);
169 drp->fDifference.fFullPath = outputDir;
170 drp->fDifference.fFullPath.append(drp->fDifference.fFilename);
171 drp->fDifference.fStatus = DiffResource::kSpecified_Status;
172
173 drp->fWhite.fFilename = filename_to_white_filename(filename);
174 drp->fWhite.fFullPath = outputDir;
175 drp->fWhite.fFullPath.append(drp->fWhite.fFilename);
176 drp->fWhite.fStatus = DiffResource::kSpecified_Status;
177
178 if (DiffRecord::kDifferentPixels_Result == drp->fResult) {
179 if (write_bitmap(drp->fDifference.fFullPath, drp->fDifference.fBitmap)) {
180 drp->fDifference.fStatus = DiffResource::kExists_Status;
181 } else {
182 drp->fDifference.fStatus = DiffResource::kDoesNotExist_Status;
183 }
184 if (write_bitmap(drp->fWhite.fFullPath, drp->fWhite.fBitmap)) {
185 drp->fWhite.fStatus = DiffResource::kExists_Status;
186 } else {
187 drp->fWhite.fStatus = DiffResource::kDoesNotExist_Status;
188 }
189 }
190 }
191}