blob: 7a28fe13c95a3f6f47c5c8a10c961f2678f32e22 [file] [log] [blame]
zachr@google.com945708a2013-07-02 19:55:32 +00001/*
2 * Copyright 2013 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
8#include "SkBitmap.h"
9#include "SkImageDecoder.h"
10#include "SkOSFile.h"
11#include "SkStream.h"
edisonn@google.comc93c8ac2013-07-22 15:24:26 +000012#include "SkTDict.h"
zachr@google.com945708a2013-07-02 19:55:32 +000013
14#include "SkDiffContext.h"
15#include "SkImageDiffer.h"
16#include "skpdiff_util.h"
17
zachr@google.comee0f46d2013-07-23 12:57:52 +000018// Truncates the number of points of interests in JSON output to not freeze the parser
19static const int kMaxPOI = 100;
20
zachr@google.com945708a2013-07-02 19:55:32 +000021SkDiffContext::SkDiffContext() {
22 fRecords = NULL;
23 fDiffers = NULL;
24 fDifferCount = 0;
25}
26
27SkDiffContext::~SkDiffContext() {
28 // Delete the record linked list
29 DiffRecord* currentRecord = fRecords;
30 while (NULL != currentRecord) {
31 DiffRecord* nextRecord = currentRecord->fNext;
32 SkDELETE(currentRecord);
33 currentRecord = nextRecord;
34 }
35
36 if (NULL != fDiffers) {
37 SkDELETE_ARRAY(fDiffers);
38 }
39}
40
41void SkDiffContext::setDiffers(const SkTDArray<SkImageDiffer*>& differs) {
42 // Delete whatever the last array of differs was
43 if (NULL != fDiffers) {
44 SkDELETE_ARRAY(fDiffers);
45 fDiffers = NULL;
46 fDifferCount = 0;
47 }
48
49 // Copy over the new differs
50 fDifferCount = differs.count();
51 fDiffers = SkNEW_ARRAY(SkImageDiffer*, fDifferCount);
52 differs.copy(fDiffers);
53}
54
55void SkDiffContext::addDiff(const char* baselinePath, const char* testPath) {
56 // Load the images at the paths
57 SkBitmap baselineBitmap;
58 SkBitmap testBitmap;
59 if (!SkImageDecoder::DecodeFile(baselinePath, &baselineBitmap)) {
60 SkDebugf("Failed to load bitmap \"%s\"\n", baselinePath);
61 return;
62 }
63 if (!SkImageDecoder::DecodeFile(testPath, &testBitmap)) {
64 SkDebugf("Failed to load bitmap \"%s\"\n", testPath);
65 return;
66 }
67
68 // Setup a record for this diff
69 DiffRecord* newRecord = SkNEW(DiffRecord);
70 newRecord->fBaselinePath = baselinePath;
71 newRecord->fTestPath = testPath;
72 newRecord->fNext = fRecords;
73 fRecords = newRecord;
74
75 // Perform each diff
76 for (int differIndex = 0; differIndex < fDifferCount; differIndex++) {
77 SkImageDiffer* differ = fDiffers[differIndex];
78 int diffID = differ->queueDiff(&baselineBitmap, &testBitmap);
79 if (diffID >= 0) {
80
81 // Copy the results into data for this record
82 DiffData& diffData = newRecord->fDiffs.push_back();
83
84 diffData.fDiffName = differ->getName();
85 diffData.fResult = differ->getResult(diffID);
86
87 int poiCount = differ->getPointsOfInterestCount(diffID);
88 SkIPoint* poi = differ->getPointsOfInterest(diffID);
89 diffData.fPointsOfInterest.append(poiCount, poi);
90
91 // Because we are doing everything synchronously for now, we are done with the diff
92 // after reading it.
93 differ->deleteDiff(diffID);
94 }
95 }
96}
97
98
99void SkDiffContext::diffDirectories(const char baselinePath[], const char testPath[]) {
100 // Get the files in the baseline, we will then look for those inside the test path
101 SkTArray<SkString> baselineEntries;
102 if (!get_directory(baselinePath, &baselineEntries)) {
103 SkDebugf("Unable to open path \"%s\"\n", baselinePath);
104 return;
105 }
106
107 for (int baselineIndex = 0; baselineIndex < baselineEntries.count(); baselineIndex++) {
zachr@google.com92fe0732013-07-16 15:47:07 +0000108 SkDebugf("[%i/%i] ", baselineIndex, baselineEntries.count());
zachr@google.com945708a2013-07-02 19:55:32 +0000109 const char* baseFilename = baselineEntries[baselineIndex].c_str();
110
111 // Find the real location of each file to compare
112 SkString baselineFile = SkOSPath::SkPathJoin(baselinePath, baseFilename);
113 SkString testFile = SkOSPath::SkPathJoin(testPath, baseFilename);
114
115 // Check that the test file exists and is a file
116 if (sk_exists(testFile.c_str()) && !sk_isdir(testFile.c_str())) {
117 // Queue up the comparison with the differ
118 this->addDiff(baselineFile.c_str(), testFile.c_str());
119 } else {
120 SkDebugf("Baseline file \"%s\" has no corresponding test file\n", baselineFile.c_str());
121 }
122 }
123}
124
125
126void SkDiffContext::diffPatterns(const char baselinePattern[], const char testPattern[]) {
127 // Get the files in the baseline and test patterns. Because they are in sorted order, it's easy
128 // to find corresponding images by matching entry indices.
129
130 SkTArray<SkString> baselineEntries;
131 if (!glob_files(baselinePattern, &baselineEntries)) {
132 SkDebugf("Unable to get pattern \"%s\"\n", baselinePattern);
133 return;
134 }
135
136 SkTArray<SkString> testEntries;
137 if (!glob_files(testPattern, &testEntries)) {
138 SkDebugf("Unable to get pattern \"%s\"\n", testPattern);
139 return;
140 }
141
142 if (baselineEntries.count() != testEntries.count()) {
143 SkDebugf("Baseline and test patterns do not yield corresponding number of files\n");
144 return;
145 }
146
147 for (int entryIndex = 0; entryIndex < baselineEntries.count(); entryIndex++) {
zachr@google.com92fe0732013-07-16 15:47:07 +0000148 SkDebugf("[%i/%i] ", entryIndex, baselineEntries.count());
zachr@google.com945708a2013-07-02 19:55:32 +0000149 const char* baselineFilename = baselineEntries[entryIndex].c_str();
150 const char* testFilename = testEntries [entryIndex].c_str();
151
152 this->addDiff(baselineFilename, testFilename);
153 }
154}
155
zachr@google.coma95959c2013-07-08 15:04:45 +0000156void SkDiffContext::outputRecords(SkWStream& stream, bool useJSONP) {
zachr@google.com945708a2013-07-02 19:55:32 +0000157 DiffRecord* currentRecord = fRecords;
zachr@google.coma95959c2013-07-08 15:04:45 +0000158 if (useJSONP) {
159 stream.writeText("var SkPDiffRecords = {\n");
160 }
161 else
162 {
163 stream.writeText("{\n");
164 }
zachr@google.com945708a2013-07-02 19:55:32 +0000165 stream.writeText(" \"records\": [\n");
166 while (NULL != currentRecord) {
167 stream.writeText(" {\n");
168
169 stream.writeText(" \"baselinePath\": \"");
170 stream.writeText(currentRecord->fBaselinePath.c_str());
171 stream.writeText("\",\n");
172
173 stream.writeText(" \"testPath\": \"");
174 stream.writeText(currentRecord->fTestPath.c_str());
175 stream.writeText("\",\n");
176
177 stream.writeText(" \"diffs\": [\n");
178 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffIndex++) {
179 DiffData& data = currentRecord->fDiffs[diffIndex];
180 stream.writeText(" {\n");
181
182 stream.writeText(" \"differName\": \"");
183 stream.writeText(data.fDiffName);
184 stream.writeText("\",\n");
185
186 stream.writeText(" \"result\": ");
zachr@google.com35f02fb2013-07-22 17:05:24 +0000187 stream.writeScalarAsText((SkScalar)data.fResult);
zachr@google.com945708a2013-07-02 19:55:32 +0000188 stream.writeText(",\n");
189
190 stream.writeText(" \"pointsOfInterest\": [\n");
zachr@google.comee0f46d2013-07-23 12:57:52 +0000191 for (int poiIndex = 0; poiIndex < data.fPointsOfInterest.count() &&
192 poiIndex < kMaxPOI; poiIndex++) {
zachr@google.com945708a2013-07-02 19:55:32 +0000193 SkIPoint poi = data.fPointsOfInterest[poiIndex];
194 stream.writeText(" [");
195 stream.writeDecAsText(poi.x());
196 stream.writeText(",");
197 stream.writeDecAsText(poi.y());
198 stream.writeText("]");
199
200 // JSON does not allow trailing commas
201 if (poiIndex + 1 < data.fPointsOfInterest.count())
202 {
203 stream.writeText(",");
204 }
205 stream.writeText("\n");
206 }
207 stream.writeText(" ]\n");
208 stream.writeText(" }");
209
210 // JSON does not allow trailing commas
211 if (diffIndex + 1 < currentRecord->fDiffs.count())
212 {
213 stream.writeText(",");
214 }
215 stream.writeText(" \n");
216 }
217 stream.writeText(" ]\n");
218
219 stream.writeText(" }");
220
221 // JSON does not allow trailing commas
222 if (NULL != currentRecord->fNext)
223 {
224 stream.writeText(",");
225 }
226 stream.writeText("\n");
227 currentRecord = currentRecord->fNext;
228 }
229 stream.writeText(" ]\n");
zachr@google.coma95959c2013-07-08 15:04:45 +0000230 if (useJSONP) {
231 stream.writeText("};\n");
232 }
233 else
234 {
235 stream.writeText("}\n");
236 }
zachr@google.com945708a2013-07-02 19:55:32 +0000237}
edisonn@google.comc93c8ac2013-07-22 15:24:26 +0000238
239void SkDiffContext::outputCsv(SkWStream& stream) {
240 SkTDict<int> columns(2);
241 int cntColumns = 0;
242
243 stream.writeText("key");
244
245 DiffRecord* currentRecord = fRecords;
246
247 // Write CSV header and create a dictionary of all columns.
248 while (NULL != currentRecord) {
249 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffIndex++) {
250 DiffData& data = currentRecord->fDiffs[diffIndex];
251 if (!columns.find(data.fDiffName)) {
252 columns.set(data.fDiffName, cntColumns);
253 stream.writeText(", ");
254 stream.writeText(data.fDiffName);
255 cntColumns++;
256 }
257 }
258 currentRecord = currentRecord->fNext;
259 }
260 stream.writeText("\n");
261
262 double values[100];
263 SkASSERT(cntColumns < 100); // Make the array larger, if we ever have so many diff types.
264
265 currentRecord = fRecords;
266 while (NULL != currentRecord) {
267 for (int i = 0; i < cntColumns; i++) {
268 values[i] = -1;
269 }
270
271 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffIndex++) {
272 DiffData& data = currentRecord->fDiffs[diffIndex];
273 int index = -1;
274 SkAssertResult(columns.find(data.fDiffName, &index));
275 SkASSERT(index >= 0 && index < cntColumns);
276 values[index] = data.fResult;
277 }
278
279 const char* filename = currentRecord->fBaselinePath.c_str() +
280 strlen(currentRecord->fBaselinePath.c_str()) - 1;
281 while (filename > currentRecord->fBaselinePath.c_str() && *(filename - 1) != '/') {
282 filename--;
283 }
284
285 stream.writeText(filename);
286
287 for (int i = 0; i < cntColumns; i++) {
288 SkString str;
289 str.printf(", %f", values[i]);
290 stream.writeText(str.c_str());
291 }
292 stream.writeText("\n");
293
294 currentRecord = currentRecord->fNext;
295 }
296}