blob: 951bba0ee82bd66ce23789476faad354fa22c730 [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"
12
13#include "SkDiffContext.h"
14#include "SkImageDiffer.h"
15#include "skpdiff_util.h"
16
17SkDiffContext::SkDiffContext() {
18 fRecords = NULL;
19 fDiffers = NULL;
20 fDifferCount = 0;
21}
22
23SkDiffContext::~SkDiffContext() {
24 // Delete the record linked list
25 DiffRecord* currentRecord = fRecords;
26 while (NULL != currentRecord) {
27 DiffRecord* nextRecord = currentRecord->fNext;
28 SkDELETE(currentRecord);
29 currentRecord = nextRecord;
30 }
31
32 if (NULL != fDiffers) {
33 SkDELETE_ARRAY(fDiffers);
34 }
35}
36
37void SkDiffContext::setDiffers(const SkTDArray<SkImageDiffer*>& differs) {
38 // Delete whatever the last array of differs was
39 if (NULL != fDiffers) {
40 SkDELETE_ARRAY(fDiffers);
41 fDiffers = NULL;
42 fDifferCount = 0;
43 }
44
45 // Copy over the new differs
46 fDifferCount = differs.count();
47 fDiffers = SkNEW_ARRAY(SkImageDiffer*, fDifferCount);
48 differs.copy(fDiffers);
49}
50
51void SkDiffContext::addDiff(const char* baselinePath, const char* testPath) {
52 // Load the images at the paths
53 SkBitmap baselineBitmap;
54 SkBitmap testBitmap;
55 if (!SkImageDecoder::DecodeFile(baselinePath, &baselineBitmap)) {
56 SkDebugf("Failed to load bitmap \"%s\"\n", baselinePath);
57 return;
58 }
59 if (!SkImageDecoder::DecodeFile(testPath, &testBitmap)) {
60 SkDebugf("Failed to load bitmap \"%s\"\n", testPath);
61 return;
62 }
63
64 // Setup a record for this diff
65 DiffRecord* newRecord = SkNEW(DiffRecord);
66 newRecord->fBaselinePath = baselinePath;
67 newRecord->fTestPath = testPath;
68 newRecord->fNext = fRecords;
69 fRecords = newRecord;
70
71 // Perform each diff
72 for (int differIndex = 0; differIndex < fDifferCount; differIndex++) {
73 SkImageDiffer* differ = fDiffers[differIndex];
74 int diffID = differ->queueDiff(&baselineBitmap, &testBitmap);
75 if (diffID >= 0) {
76
77 // Copy the results into data for this record
78 DiffData& diffData = newRecord->fDiffs.push_back();
79
80 diffData.fDiffName = differ->getName();
81 diffData.fResult = differ->getResult(diffID);
82
83 int poiCount = differ->getPointsOfInterestCount(diffID);
84 SkIPoint* poi = differ->getPointsOfInterest(diffID);
85 diffData.fPointsOfInterest.append(poiCount, poi);
86
87 // Because we are doing everything synchronously for now, we are done with the diff
88 // after reading it.
89 differ->deleteDiff(diffID);
90 }
91 }
92}
93
94
95void SkDiffContext::diffDirectories(const char baselinePath[], const char testPath[]) {
96 // Get the files in the baseline, we will then look for those inside the test path
97 SkTArray<SkString> baselineEntries;
98 if (!get_directory(baselinePath, &baselineEntries)) {
99 SkDebugf("Unable to open path \"%s\"\n", baselinePath);
100 return;
101 }
102
103 for (int baselineIndex = 0; baselineIndex < baselineEntries.count(); baselineIndex++) {
104 const char* baseFilename = baselineEntries[baselineIndex].c_str();
105
106 // Find the real location of each file to compare
107 SkString baselineFile = SkOSPath::SkPathJoin(baselinePath, baseFilename);
108 SkString testFile = SkOSPath::SkPathJoin(testPath, baseFilename);
109
110 // Check that the test file exists and is a file
111 if (sk_exists(testFile.c_str()) && !sk_isdir(testFile.c_str())) {
112 // Queue up the comparison with the differ
113 this->addDiff(baselineFile.c_str(), testFile.c_str());
114 } else {
115 SkDebugf("Baseline file \"%s\" has no corresponding test file\n", baselineFile.c_str());
116 }
117 }
118}
119
120
121void SkDiffContext::diffPatterns(const char baselinePattern[], const char testPattern[]) {
122 // Get the files in the baseline and test patterns. Because they are in sorted order, it's easy
123 // to find corresponding images by matching entry indices.
124
125 SkTArray<SkString> baselineEntries;
126 if (!glob_files(baselinePattern, &baselineEntries)) {
127 SkDebugf("Unable to get pattern \"%s\"\n", baselinePattern);
128 return;
129 }
130
131 SkTArray<SkString> testEntries;
132 if (!glob_files(testPattern, &testEntries)) {
133 SkDebugf("Unable to get pattern \"%s\"\n", testPattern);
134 return;
135 }
136
137 if (baselineEntries.count() != testEntries.count()) {
138 SkDebugf("Baseline and test patterns do not yield corresponding number of files\n");
139 return;
140 }
141
142 for (int entryIndex = 0; entryIndex < baselineEntries.count(); entryIndex++) {
143 const char* baselineFilename = baselineEntries[entryIndex].c_str();
144 const char* testFilename = testEntries [entryIndex].c_str();
145
146 this->addDiff(baselineFilename, testFilename);
147 }
148}
149
150void SkDiffContext::outputRecords(SkWStream& stream) {
151 DiffRecord* currentRecord = fRecords;
152 stream.writeText("{\n");
153 stream.writeText(" \"records\": [\n");
154 while (NULL != currentRecord) {
155 stream.writeText(" {\n");
156
157 stream.writeText(" \"baselinePath\": \"");
158 stream.writeText(currentRecord->fBaselinePath.c_str());
159 stream.writeText("\",\n");
160
161 stream.writeText(" \"testPath\": \"");
162 stream.writeText(currentRecord->fTestPath.c_str());
163 stream.writeText("\",\n");
164
165 stream.writeText(" \"diffs\": [\n");
166 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffIndex++) {
167 DiffData& data = currentRecord->fDiffs[diffIndex];
168 stream.writeText(" {\n");
169
170 stream.writeText(" \"differName\": \"");
171 stream.writeText(data.fDiffName);
172 stream.writeText("\",\n");
173
174 stream.writeText(" \"result\": ");
175 stream.writeScalarAsText(data.fResult);
176 stream.writeText(",\n");
177
178 stream.writeText(" \"pointsOfInterest\": [\n");
179 for (int poiIndex = 0; poiIndex < data.fPointsOfInterest.count(); poiIndex++) {
180 SkIPoint poi = data.fPointsOfInterest[poiIndex];
181 stream.writeText(" [");
182 stream.writeDecAsText(poi.x());
183 stream.writeText(",");
184 stream.writeDecAsText(poi.y());
185 stream.writeText("]");
186
187 // JSON does not allow trailing commas
188 if (poiIndex + 1 < data.fPointsOfInterest.count())
189 {
190 stream.writeText(",");
191 }
192 stream.writeText("\n");
193 }
194 stream.writeText(" ]\n");
195 stream.writeText(" }");
196
197 // JSON does not allow trailing commas
198 if (diffIndex + 1 < currentRecord->fDiffs.count())
199 {
200 stream.writeText(",");
201 }
202 stream.writeText(" \n");
203 }
204 stream.writeText(" ]\n");
205
206 stream.writeText(" }");
207
208 // JSON does not allow trailing commas
209 if (NULL != currentRecord->fNext)
210 {
211 stream.writeText(",");
212 }
213 stream.writeText("\n");
214 currentRecord = currentRecord->fNext;
215 }
216 stream.writeText(" ]\n");
217 stream.writeText("}\n");
218}