OLD | NEW |
---|---|
(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 | |
13 #include "SkDiffContext.h" | |
14 #include "SkImageDiffer.h" | |
15 #include "skpdiff_util.h" | |
16 | |
17 SkDiffContext::SkDiffContext() { | |
18 fRecords = NULL; | |
19 fDiffers = NULL; | |
20 fDifferCount = 0; | |
21 } | |
22 | |
23 SkDiffContext::~SkDiffContext() | |
24 { | |
bsalomon
2013/07/02 19:28:37
up
| |
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 (unsigned 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 const char* baseFilename = baselineEntries[baselineIndex].c_str(); | |
106 | |
107 // Find the real location of each file to compare | |
108 SkString baselineFile = SkOSPath::SkPathJoin(baselinePath, baseFilename) ; | |
109 SkString testFile = SkOSPath::SkPathJoin(testPath, baseFilename); | |
110 | |
111 // Check that the test file exists and is a file | |
112 if (sk_exists(testFile.c_str()) && !sk_isdir(testFile.c_str())) { | |
113 // Queue up the comparison with the differ | |
114 this->addDiff(baselineFile.c_str(), testFile.c_str()); | |
115 } else { | |
116 SkDebugf("Baseline file \"%s\" has no corresponding test file\n", ba selineFile.c_str()); | |
117 } | |
118 } | |
119 } | |
120 | |
121 | |
122 void SkDiffContext::diffPatterns(const char baselinePattern[], const char testPa ttern[]) { | |
123 // Get the files in the baseline and test patterns. Because they are in sort ed order, it's easy | |
124 // to find corresponding images by matching entry indices. | |
125 | |
126 SkTArray<SkString> baselineEntries; | |
127 if (!glob_files(baselinePattern, &baselineEntries)) { | |
128 SkDebugf("Unable to get pattern \"%s\"\n", baselinePattern); | |
129 return; | |
130 } | |
131 | |
132 SkTArray<SkString> testEntries; | |
133 if (!glob_files(testPattern, &testEntries)) { | |
134 SkDebugf("Unable to get pattern \"%s\"\n", testPattern); | |
135 return; | |
136 } | |
137 | |
138 if (baselineEntries.count() != testEntries.count()) { | |
139 SkDebugf("Baseline and test patterns do not yield corresponding number o f files\n"); | |
140 return; | |
141 } | |
142 | |
143 for (int entryIndex = 0; entryIndex < baselineEntries.count(); entryIndex++) { | |
144 const char* baselineFilename = baselineEntries[entryIndex].c_str(); | |
145 const char* testFilename = testEntries [entryIndex].c_str(); | |
146 | |
147 this->addDiff(baselineFilename, testFilename); | |
148 } | |
149 } | |
150 | |
151 void SkDiffContext::outputRecords(SkWStream& stream) { | |
152 DiffRecord* currentRecord = fRecords; | |
153 stream.writeText("{\n"); | |
154 stream.writeText(" \"records\": [\n"); | |
155 while (NULL != currentRecord) { | |
156 stream.writeText(" {\n"); | |
157 | |
158 stream.writeText(" \"baselinePath\": \""); | |
159 stream.writeText(currentRecord->fBaselinePath.c_str()); | |
160 stream.writeText("\",\n"); | |
161 | |
162 stream.writeText(" \"testPath\": \""); | |
163 stream.writeText(currentRecord->fTestPath.c_str()); | |
164 stream.writeText("\",\n"); | |
165 | |
166 stream.writeText(" \"diffs\": [\n"); | |
167 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); d iffIndex++) { | |
168 DiffData& data = currentRecord->fDiffs[diffIndex]; | |
169 stream.writeText(" {\n"); | |
170 | |
171 stream.writeText(" \"differName\": \""); | |
172 stream.writeText(data.fDiffName); | |
173 stream.writeText("\",\n"); | |
174 | |
175 stream.writeText(" \"result\": "); | |
176 stream.writeScalarAsText(data.fResult); | |
177 stream.writeText(",\n"); | |
178 | |
179 stream.writeText(" \"pointsOfInterest\": [\n"); | |
180 for (int poiIndex = 0; poiIndex < data.fPointsOfInterest.cou nt(); poiIndex++) { | |
181 SkIPoint poi = data.fPointsOfInterest[poiIndex]; | |
182 stream.writeText(" ["); | |
183 stream.writeDecAsText(poi.x()); | |
184 stream.writeText(","); | |
185 stream.writeDecAsText(poi.y()); | |
186 stream.writeText("]"); | |
187 | |
188 // JSON does not allow trailing commas | |
189 if (poiIndex + 1 < data.fPointsOfInterest.count()) | |
190 { | |
191 stream.writeText(","); | |
192 } | |
193 stream.writeText("\n"); | |
194 } | |
195 stream.writeText(" ]\n"); | |
196 stream.writeText(" }"); | |
197 | |
198 // JSON does not allow trailing commas | |
199 if (diffIndex + 1 < currentRecord->fDiffs.count()) | |
200 { | |
201 stream.writeText(","); | |
202 } | |
203 stream.writeText(" \n"); | |
204 } | |
205 stream.writeText(" ]\n"); | |
206 | |
207 stream.writeText(" }"); | |
208 | |
209 // JSON does not allow trailing commas | |
210 if (NULL != currentRecord->fNext) | |
211 { | |
212 stream.writeText(","); | |
213 } | |
214 stream.writeText("\n"); | |
215 currentRecord = currentRecord->fNext; | |
216 } | |
217 stream.writeText(" ]\n"); | |
218 stream.writeText("}\n"); | |
219 } | |
OLD | NEW |