zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 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" |
edisonn@google.com | c93c8ac | 2013-07-22 15:24:26 +0000 | [diff] [blame] | 12 | #include "SkTDict.h" |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 13 | |
| 14 | #include "SkDiffContext.h" |
| 15 | #include "SkImageDiffer.h" |
| 16 | #include "skpdiff_util.h" |
| 17 | |
zachr@google.com | ee0f46d | 2013-07-23 12:57:52 +0000 | [diff] [blame] | 18 | // Truncates the number of points of interests in JSON output to not freeze the parser |
| 19 | static const int kMaxPOI = 100; |
| 20 | |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 21 | SkDiffContext::SkDiffContext() { |
| 22 | fRecords = NULL; |
| 23 | fDiffers = NULL; |
| 24 | fDifferCount = 0; |
| 25 | } |
| 26 | |
| 27 | SkDiffContext::~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 | |
| 41 | void 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 | |
| 55 | void 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 | |
| 99 | void 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.com | 92fe073 | 2013-07-16 15:47:07 +0000 | [diff] [blame] | 108 | SkDebugf("[%i/%i] ", baselineIndex, baselineEntries.count()); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 109 | 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 | |
| 126 | void 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.com | 92fe073 | 2013-07-16 15:47:07 +0000 | [diff] [blame] | 148 | SkDebugf("[%i/%i] ", entryIndex, baselineEntries.count()); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 149 | 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.com | a95959c | 2013-07-08 15:04:45 +0000 | [diff] [blame] | 156 | void SkDiffContext::outputRecords(SkWStream& stream, bool useJSONP) { |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 157 | DiffRecord* currentRecord = fRecords; |
zachr@google.com | a95959c | 2013-07-08 15:04:45 +0000 | [diff] [blame] | 158 | if (useJSONP) { |
| 159 | stream.writeText("var SkPDiffRecords = {\n"); |
zachr@google.com | 55173f2 | 2013-07-25 17:22:58 +0000 | [diff] [blame] | 160 | } else { |
zachr@google.com | a95959c | 2013-07-08 15:04:45 +0000 | [diff] [blame] | 161 | stream.writeText("{\n"); |
| 162 | } |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 163 | stream.writeText(" \"records\": [\n"); |
| 164 | while (NULL != currentRecord) { |
| 165 | stream.writeText(" {\n"); |
| 166 | |
| 167 | stream.writeText(" \"baselinePath\": \""); |
| 168 | stream.writeText(currentRecord->fBaselinePath.c_str()); |
| 169 | stream.writeText("\",\n"); |
| 170 | |
| 171 | stream.writeText(" \"testPath\": \""); |
| 172 | stream.writeText(currentRecord->fTestPath.c_str()); |
| 173 | stream.writeText("\",\n"); |
| 174 | |
| 175 | stream.writeText(" \"diffs\": [\n"); |
| 176 | for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffIndex++) { |
| 177 | DiffData& data = currentRecord->fDiffs[diffIndex]; |
| 178 | stream.writeText(" {\n"); |
| 179 | |
| 180 | stream.writeText(" \"differName\": \""); |
| 181 | stream.writeText(data.fDiffName); |
| 182 | stream.writeText("\",\n"); |
| 183 | |
| 184 | stream.writeText(" \"result\": "); |
zachr@google.com | 35f02fb | 2013-07-22 17:05:24 +0000 | [diff] [blame] | 185 | stream.writeScalarAsText((SkScalar)data.fResult); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 186 | stream.writeText(",\n"); |
| 187 | |
| 188 | stream.writeText(" \"pointsOfInterest\": [\n"); |
zachr@google.com | ee0f46d | 2013-07-23 12:57:52 +0000 | [diff] [blame] | 189 | for (int poiIndex = 0; poiIndex < data.fPointsOfInterest.count() && |
| 190 | poiIndex < kMaxPOI; poiIndex++) { |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 191 | SkIPoint poi = data.fPointsOfInterest[poiIndex]; |
| 192 | stream.writeText(" ["); |
| 193 | stream.writeDecAsText(poi.x()); |
| 194 | stream.writeText(","); |
| 195 | stream.writeDecAsText(poi.y()); |
| 196 | stream.writeText("]"); |
| 197 | |
| 198 | // JSON does not allow trailing commas |
zachr@google.com | 55173f2 | 2013-07-25 17:22:58 +0000 | [diff] [blame] | 199 | if (poiIndex + 1 < data.fPointsOfInterest.count() && |
| 200 | poiIndex + 1 < kMaxPOI) { |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 201 | stream.writeText(","); |
| 202 | } |
| 203 | stream.writeText("\n"); |
| 204 | } |
| 205 | stream.writeText(" ]\n"); |
| 206 | stream.writeText(" }"); |
| 207 | |
| 208 | // JSON does not allow trailing commas |
zachr@google.com | 55173f2 | 2013-07-25 17:22:58 +0000 | [diff] [blame] | 209 | if (diffIndex + 1 < currentRecord->fDiffs.count()) { |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 210 | stream.writeText(","); |
| 211 | } |
| 212 | stream.writeText(" \n"); |
| 213 | } |
| 214 | stream.writeText(" ]\n"); |
| 215 | |
| 216 | stream.writeText(" }"); |
| 217 | |
| 218 | // JSON does not allow trailing commas |
zachr@google.com | 55173f2 | 2013-07-25 17:22:58 +0000 | [diff] [blame] | 219 | if (NULL != currentRecord->fNext) { |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 220 | stream.writeText(","); |
| 221 | } |
| 222 | stream.writeText("\n"); |
| 223 | currentRecord = currentRecord->fNext; |
| 224 | } |
| 225 | stream.writeText(" ]\n"); |
zachr@google.com | a95959c | 2013-07-08 15:04:45 +0000 | [diff] [blame] | 226 | if (useJSONP) { |
| 227 | stream.writeText("};\n"); |
zachr@google.com | 55173f2 | 2013-07-25 17:22:58 +0000 | [diff] [blame] | 228 | } else { |
zachr@google.com | a95959c | 2013-07-08 15:04:45 +0000 | [diff] [blame] | 229 | stream.writeText("}\n"); |
| 230 | } |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 231 | } |
edisonn@google.com | c93c8ac | 2013-07-22 15:24:26 +0000 | [diff] [blame] | 232 | |
| 233 | void SkDiffContext::outputCsv(SkWStream& stream) { |
| 234 | SkTDict<int> columns(2); |
| 235 | int cntColumns = 0; |
| 236 | |
| 237 | stream.writeText("key"); |
| 238 | |
| 239 | DiffRecord* currentRecord = fRecords; |
| 240 | |
| 241 | // Write CSV header and create a dictionary of all columns. |
| 242 | while (NULL != currentRecord) { |
| 243 | for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffIndex++) { |
| 244 | DiffData& data = currentRecord->fDiffs[diffIndex]; |
| 245 | if (!columns.find(data.fDiffName)) { |
| 246 | columns.set(data.fDiffName, cntColumns); |
| 247 | stream.writeText(", "); |
| 248 | stream.writeText(data.fDiffName); |
| 249 | cntColumns++; |
| 250 | } |
| 251 | } |
| 252 | currentRecord = currentRecord->fNext; |
| 253 | } |
| 254 | stream.writeText("\n"); |
| 255 | |
| 256 | double values[100]; |
| 257 | SkASSERT(cntColumns < 100); // Make the array larger, if we ever have so many diff types. |
| 258 | |
| 259 | currentRecord = fRecords; |
| 260 | while (NULL != currentRecord) { |
| 261 | for (int i = 0; i < cntColumns; i++) { |
| 262 | values[i] = -1; |
| 263 | } |
| 264 | |
| 265 | for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffIndex++) { |
| 266 | DiffData& data = currentRecord->fDiffs[diffIndex]; |
| 267 | int index = -1; |
| 268 | SkAssertResult(columns.find(data.fDiffName, &index)); |
| 269 | SkASSERT(index >= 0 && index < cntColumns); |
| 270 | values[index] = data.fResult; |
| 271 | } |
| 272 | |
| 273 | const char* filename = currentRecord->fBaselinePath.c_str() + |
| 274 | strlen(currentRecord->fBaselinePath.c_str()) - 1; |
| 275 | while (filename > currentRecord->fBaselinePath.c_str() && *(filename - 1) != '/') { |
| 276 | filename--; |
| 277 | } |
| 278 | |
| 279 | stream.writeText(filename); |
| 280 | |
| 281 | for (int i = 0; i < cntColumns; i++) { |
| 282 | SkString str; |
| 283 | str.printf(", %f", values[i]); |
| 284 | stream.writeText(str.c_str()); |
| 285 | } |
| 286 | stream.writeText("\n"); |
| 287 | |
| 288 | currentRecord = currentRecord->fNext; |
| 289 | } |
| 290 | } |