blob: 1e2a727945b921892404788349f391f560966528 [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"
9#include "SkBitmap.h"
10#include "SkData.h"
11#include "SkImageDecoder.h"
12#include "SkImageEncoder.h"
13#include "SkStream.h"
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000014#include "SkTypes.h"
15
bungemanf3c15b72015-08-19 11:56:48 -070016#include <memory>
17
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000018bool are_buffers_equal(SkData* skdata1, SkData* skdata2) {
halcanary96fcdcc2015-08-27 07:41:13 -070019 if ((nullptr == skdata1) || (nullptr == skdata2)) {
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000020 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
28SkData* read_file(const char* file_path) {
reed9594da12014-09-12 12:12:27 -070029 SkData* data = SkData::NewFromFileName(file_path);
30 if (!data) {
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000031 SkDebugf("WARNING: could not open file <%s> for reading\n", file_path);
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000032 }
reed9594da12014-09-12 12:12:27 -070033 return data;
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000034}
35
36bool get_bitmap(SkData* fileBits, DiffResource& resource, SkImageDecoder::Mode mode) {
37 SkMemoryStream stream(fileBits->data(), fileBits->size());
38
bungemanf3c15b72015-08-19 11:56:48 -070039 // In debug, the DLL will automatically be unloaded when this is deleted,
40 // but that shouldn't be a problem in release mode.
41 std::unique_ptr<SkImageDecoder> codec(SkImageDecoder::Factory(&stream));
halcanary96fcdcc2015-08-27 07:41:13 -070042 if (nullptr == codec) {
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000043 SkDebugf("ERROR: no codec found for <%s>\n", resource.fFullPath.c_str());
44 resource.fStatus = DiffResource::kCouldNotDecode_Status;
45 return false;
46 }
47
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000048 stream.rewind();
reedbfefc7c2014-06-12 17:40:00 -070049 if (!codec->decode(&stream, &resource.fBitmap, kN32_SkColorType, mode)) {
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000050 SkDebugf("ERROR: codec failed for basePath <%s>\n", resource.fFullPath.c_str());
51 resource.fStatus = DiffResource::kCouldNotDecode_Status;
52 return false;
53 }
54
55 resource.fStatus = DiffResource::kDecoded_Status;
56 return true;
57}
58
59/** Thanks to PNG, we need to force all pixels 100% opaque. */
60static void force_all_opaque(const SkBitmap& bitmap) {
61 SkAutoLockPixels lock(bitmap);
62 for (int y = 0; y < bitmap.height(); y++) {
63 for (int x = 0; x < bitmap.width(); x++) {
64 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
65 }
66 }
67}
68
69bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
70 SkBitmap copy;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000071 bitmap.copyTo(&copy, kN32_SkColorType);
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000072 force_all_opaque(copy);
73 return SkImageEncoder::EncodeFile(path.c_str(), copy,
74 SkImageEncoder::kPNG_Type, 100);
75}
76
77/// Return a copy of the "input" string, within which we have replaced all instances
78/// of oldSubstring with newSubstring.
79///
80/// TODO: If we like this, we should move it into the core SkString implementation,
81/// adding more checks and ample test cases, and paying more attention to efficiency.
82static SkString replace_all(const SkString &input,
83 const char oldSubstring[], const char newSubstring[]) {
84 SkString output;
85 const char *input_cstr = input.c_str();
86 const char *first_char = input_cstr;
87 const char *match_char;
robertphillips@google.comadacc702013-10-14 21:53:24 +000088 size_t oldSubstringLen = strlen(oldSubstring);
bsalomon49f085d2014-09-05 13:34:00 -070089 while ((match_char = strstr(first_char, oldSubstring))) {
bungeman@google.come3c8ddf2012-12-05 20:13:12 +000090 output.append(first_char, (match_char - first_char));
91 output.append(newSubstring);
92 first_char = match_char + oldSubstringLen;
93 }
94 output.append(first_char);
95 return output;
96}
97
98static SkString filename_to_derived_filename(const SkString& filename, const char *suffix) {
99 SkString diffName (filename);
100 const char* cstring = diffName.c_str();
robertphillips@google.com8b169312013-10-15 17:47:36 +0000101 size_t dotOffset = strrchr(cstring, '.') - cstring;
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000102 diffName.remove(dotOffset, diffName.size() - dotOffset);
103 diffName.append(suffix);
104
105 // In case we recursed into subdirectories, replace slashes with something else
106 // so the diffs will all be written into a single flat directory.
107 diffName = replace_all(diffName, PATH_DIV_STR, "_");
108 return diffName;
109}
110
111SkString filename_to_diff_filename(const SkString& filename) {
112 return filename_to_derived_filename(filename, "-diff.png");
113}
114
115SkString filename_to_white_filename(const SkString& filename) {
116 return filename_to_derived_filename(filename, "-white.png");
117}
118
119void create_and_write_diff_image(DiffRecord* drp,
120 DiffMetricProc dmp,
121 const int colorThreshold,
122 const SkString& outputDir,
123 const SkString& filename) {
124 const int w = drp->fBase.fBitmap.width();
125 const int h = drp->fBase.fBitmap.height();
126
127 if (w != drp->fComparison.fBitmap.width() || h != drp->fComparison.fBitmap.height()) {
128 drp->fResult = DiffRecord::kDifferentSizes_Result;
129 } else {
reed6c225732014-06-09 19:52:07 -0700130 drp->fDifference.fBitmap.allocN32Pixels(w, h);
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000131
reed6c225732014-06-09 19:52:07 -0700132 drp->fWhite.fBitmap.allocN32Pixels(w, h);
bungeman@google.come3c8ddf2012-12-05 20:13:12 +0000133
134 SkASSERT(DiffRecord::kUnknown_Result == drp->fResult);
135 compute_diff(drp, dmp, colorThreshold);
136 SkASSERT(DiffRecord::kUnknown_Result != drp->fResult);
137 }
138
139 if (outputDir.isEmpty()) {
140 drp->fDifference.fStatus = DiffResource::kUnspecified_Status;
141 drp->fWhite.fStatus = DiffResource::kUnspecified_Status;
142
143 } else {
144 drp->fDifference.fFilename = filename_to_diff_filename(filename);
145 drp->fDifference.fFullPath = outputDir;
146 drp->fDifference.fFullPath.append(drp->fDifference.fFilename);
147 drp->fDifference.fStatus = DiffResource::kSpecified_Status;
148
149 drp->fWhite.fFilename = filename_to_white_filename(filename);
150 drp->fWhite.fFullPath = outputDir;
151 drp->fWhite.fFullPath.append(drp->fWhite.fFilename);
152 drp->fWhite.fStatus = DiffResource::kSpecified_Status;
153
154 if (DiffRecord::kDifferentPixels_Result == drp->fResult) {
155 if (write_bitmap(drp->fDifference.fFullPath, drp->fDifference.fBitmap)) {
156 drp->fDifference.fStatus = DiffResource::kExists_Status;
157 } else {
158 drp->fDifference.fStatus = DiffResource::kDoesNotExist_Status;
159 }
160 if (write_bitmap(drp->fWhite.fFullPath, drp->fWhite.fBitmap)) {
161 drp->fWhite.fStatus = DiffResource::kExists_Status;
162 } else {
163 drp->fWhite.fStatus = DiffResource::kDoesNotExist_Status;
164 }
165 }
166 }
167}