Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1040)

Side by Side Diff: experimental/skpdiff/SkDiffContext.cpp

Issue 19608005: move skpdiff into tools (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « experimental/skpdiff/SkDiffContext.h ('k') | experimental/skpdiff/SkDifferentPixelsMetric.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
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 #include "SkTDict.h"
13
14 #include "SkDiffContext.h"
15 #include "SkImageDiffer.h"
16 #include "skpdiff_util.h"
17
18 SkDiffContext::SkDiffContext() {
19 fRecords = NULL;
20 fDiffers = NULL;
21 fDifferCount = 0;
22 }
23
24 SkDiffContext::~SkDiffContext() {
25 // Delete the record linked list
26 DiffRecord* currentRecord = fRecords;
27 while (NULL != currentRecord) {
28 DiffRecord* nextRecord = currentRecord->fNext;
29 SkDELETE(currentRecord);
30 currentRecord = nextRecord;
31 }
32
33 if (NULL != fDiffers) {
34 SkDELETE_ARRAY(fDiffers);
35 }
36 }
37
38 void SkDiffContext::setDiffers(const SkTDArray<SkImageDiffer*>& differs) {
39 // Delete whatever the last array of differs was
40 if (NULL != fDiffers) {
41 SkDELETE_ARRAY(fDiffers);
42 fDiffers = NULL;
43 fDifferCount = 0;
44 }
45
46 // Copy over the new differs
47 fDifferCount = differs.count();
48 fDiffers = SkNEW_ARRAY(SkImageDiffer*, fDifferCount);
49 differs.copy(fDiffers);
50 }
51
52 void SkDiffContext::addDiff(const char* baselinePath, const char* testPath) {
53 // Load the images at the paths
54 SkBitmap baselineBitmap;
55 SkBitmap testBitmap;
56 if (!SkImageDecoder::DecodeFile(baselinePath, &baselineBitmap)) {
57 SkDebugf("Failed to load bitmap \"%s\"\n", baselinePath);
58 return;
59 }
60 if (!SkImageDecoder::DecodeFile(testPath, &testBitmap)) {
61 SkDebugf("Failed to load bitmap \"%s\"\n", testPath);
62 return;
63 }
64
65 // Setup a record for this diff
66 DiffRecord* newRecord = SkNEW(DiffRecord);
67 newRecord->fBaselinePath = baselinePath;
68 newRecord->fTestPath = testPath;
69 newRecord->fNext = fRecords;
70 fRecords = newRecord;
71
72 // Perform each diff
73 for (int differIndex = 0; differIndex < fDifferCount; differIndex++) {
74 SkImageDiffer* differ = fDiffers[differIndex];
75 int diffID = differ->queueDiff(&baselineBitmap, &testBitmap);
76 if (diffID >= 0) {
77
78 // Copy the results into data for this record
79 DiffData& diffData = newRecord->fDiffs.push_back();
80
81 diffData.fDiffName = differ->getName();
82 diffData.fResult = differ->getResult(diffID);
83
84 int poiCount = differ->getPointsOfInterestCount(diffID);
85 SkIPoint* poi = differ->getPointsOfInterest(diffID);
86 diffData.fPointsOfInterest.append(poiCount, poi);
87
88 // Because we are doing everything synchronously for now, we are don e with the diff
89 // after reading it.
90 differ->deleteDiff(diffID);
91 }
92 }
93 }
94
95
96 void SkDiffContext::diffDirectories(const char baselinePath[], const char testPa th[]) {
97 // Get the files in the baseline, we will then look for those inside the tes t path
98 SkTArray<SkString> baselineEntries;
99 if (!get_directory(baselinePath, &baselineEntries)) {
100 SkDebugf("Unable to open path \"%s\"\n", baselinePath);
101 return;
102 }
103
104 for (int baselineIndex = 0; baselineIndex < baselineEntries.count(); baselin eIndex++) {
105 SkDebugf("[%i/%i] ", baselineIndex, baselineEntries.count());
106 const char* baseFilename = baselineEntries[baselineIndex].c_str();
107
108 // Find the real location of each file to compare
109 SkString baselineFile = SkOSPath::SkPathJoin(baselinePath, baseFilename) ;
110 SkString testFile = SkOSPath::SkPathJoin(testPath, baseFilename);
111
112 // Check that the test file exists and is a file
113 if (sk_exists(testFile.c_str()) && !sk_isdir(testFile.c_str())) {
114 // Queue up the comparison with the differ
115 this->addDiff(baselineFile.c_str(), testFile.c_str());
116 } else {
117 SkDebugf("Baseline file \"%s\" has no corresponding test file\n", ba selineFile.c_str());
118 }
119 }
120 }
121
122
123 void SkDiffContext::diffPatterns(const char baselinePattern[], const char testPa ttern[]) {
124 // Get the files in the baseline and test patterns. Because they are in sort ed order, it's easy
125 // to find corresponding images by matching entry indices.
126
127 SkTArray<SkString> baselineEntries;
128 if (!glob_files(baselinePattern, &baselineEntries)) {
129 SkDebugf("Unable to get pattern \"%s\"\n", baselinePattern);
130 return;
131 }
132
133 SkTArray<SkString> testEntries;
134 if (!glob_files(testPattern, &testEntries)) {
135 SkDebugf("Unable to get pattern \"%s\"\n", testPattern);
136 return;
137 }
138
139 if (baselineEntries.count() != testEntries.count()) {
140 SkDebugf("Baseline and test patterns do not yield corresponding number o f files\n");
141 return;
142 }
143
144 for (int entryIndex = 0; entryIndex < baselineEntries.count(); entryIndex++) {
145 SkDebugf("[%i/%i] ", entryIndex, baselineEntries.count());
146 const char* baselineFilename = baselineEntries[entryIndex].c_str();
147 const char* testFilename = testEntries [entryIndex].c_str();
148
149 this->addDiff(baselineFilename, testFilename);
150 }
151 }
152
153 void SkDiffContext::outputRecords(SkWStream& stream, bool useJSONP) {
154 DiffRecord* currentRecord = fRecords;
155 if (useJSONP) {
156 stream.writeText("var SkPDiffRecords = {\n");
157 }
158 else
159 {
160 stream.writeText("{\n");
161 }
162 stream.writeText(" \"records\": [\n");
163 while (NULL != currentRecord) {
164 stream.writeText(" {\n");
165
166 stream.writeText(" \"baselinePath\": \"");
167 stream.writeText(currentRecord->fBaselinePath.c_str());
168 stream.writeText("\",\n");
169
170 stream.writeText(" \"testPath\": \"");
171 stream.writeText(currentRecord->fTestPath.c_str());
172 stream.writeText("\",\n");
173
174 stream.writeText(" \"diffs\": [\n");
175 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); d iffIndex++) {
176 DiffData& data = currentRecord->fDiffs[diffIndex];
177 stream.writeText(" {\n");
178
179 stream.writeText(" \"differName\": \"");
180 stream.writeText(data.fDiffName);
181 stream.writeText("\",\n");
182
183 stream.writeText(" \"result\": ");
184 stream.writeScalarAsText((SkScalar)data.fResult);
185 stream.writeText(",\n");
186
187 stream.writeText(" \"pointsOfInterest\": [\n");
188 for (int poiIndex = 0; poiIndex < data.fPointsOfInterest.cou nt(); poiIndex++) {
189 SkIPoint poi = data.fPointsOfInterest[poiIndex];
190 stream.writeText(" [");
191 stream.writeDecAsText(poi.x());
192 stream.writeText(",");
193 stream.writeDecAsText(poi.y());
194 stream.writeText("]");
195
196 // JSON does not allow trailing commas
197 if (poiIndex + 1 < data.fPointsOfInterest.count())
198 {
199 stream.writeText(",");
200 }
201 stream.writeText("\n");
202 }
203 stream.writeText(" ]\n");
204 stream.writeText(" }");
205
206 // JSON does not allow trailing commas
207 if (diffIndex + 1 < currentRecord->fDiffs.count())
208 {
209 stream.writeText(",");
210 }
211 stream.writeText(" \n");
212 }
213 stream.writeText(" ]\n");
214
215 stream.writeText(" }");
216
217 // JSON does not allow trailing commas
218 if (NULL != currentRecord->fNext)
219 {
220 stream.writeText(",");
221 }
222 stream.writeText("\n");
223 currentRecord = currentRecord->fNext;
224 }
225 stream.writeText(" ]\n");
226 if (useJSONP) {
227 stream.writeText("};\n");
228 }
229 else
230 {
231 stream.writeText("}\n");
232 }
233 }
234
235 void SkDiffContext::outputCsv(SkWStream& stream) {
236 SkTDict<int> columns(2);
237 int cntColumns = 0;
238
239 stream.writeText("key");
240
241 DiffRecord* currentRecord = fRecords;
242
243 // Write CSV header and create a dictionary of all columns.
244 while (NULL != currentRecord) {
245 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffI ndex++) {
246 DiffData& data = currentRecord->fDiffs[diffIndex];
247 if (!columns.find(data.fDiffName)) {
248 columns.set(data.fDiffName, cntColumns);
249 stream.writeText(", ");
250 stream.writeText(data.fDiffName);
251 cntColumns++;
252 }
253 }
254 currentRecord = currentRecord->fNext;
255 }
256 stream.writeText("\n");
257
258 double values[100];
259 SkASSERT(cntColumns < 100); // Make the array larger, if we ever have so ma ny diff types.
260
261 currentRecord = fRecords;
262 while (NULL != currentRecord) {
263 for (int i = 0; i < cntColumns; i++) {
264 values[i] = -1;
265 }
266
267 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffI ndex++) {
268 DiffData& data = currentRecord->fDiffs[diffIndex];
269 int index = -1;
270 SkAssertResult(columns.find(data.fDiffName, &index));
271 SkASSERT(index >= 0 && index < cntColumns);
272 values[index] = data.fResult;
273 }
274
275 const char* filename = currentRecord->fBaselinePath.c_str() +
276 strlen(currentRecord->fBaselinePath.c_str()) - 1;
277 while (filename > currentRecord->fBaselinePath.c_str() && *(filename - 1 ) != '/') {
278 filename--;
279 }
280
281 stream.writeText(filename);
282
283 for (int i = 0; i < cntColumns; i++) {
284 SkString str;
285 str.printf(", %f", values[i]);
286 stream.writeText(str.c_str());
287 }
288 stream.writeText("\n");
289
290 currentRecord = currentRecord->fNext;
291 }
292 }
OLDNEW
« no previous file with comments | « experimental/skpdiff/SkDiffContext.h ('k') | experimental/skpdiff/SkDifferentPixelsMetric.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698