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" |
| 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 | // 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 | |
| 37 | void 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 | |
| 51 | void 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 | |
| 95 | void 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 | |
| 121 | void 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 | |
zachr@google.com | a95959c | 2013-07-08 15:04:45 +0000 | [diff] [blame^] | 150 | void SkDiffContext::outputRecords(SkWStream& stream, bool useJSONP) { |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 151 | DiffRecord* currentRecord = fRecords; |
zachr@google.com | a95959c | 2013-07-08 15:04:45 +0000 | [diff] [blame^] | 152 | if (useJSONP) { |
| 153 | stream.writeText("var SkPDiffRecords = {\n"); |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | stream.writeText("{\n"); |
| 158 | } |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 159 | stream.writeText(" \"records\": [\n"); |
| 160 | while (NULL != currentRecord) { |
| 161 | stream.writeText(" {\n"); |
| 162 | |
| 163 | stream.writeText(" \"baselinePath\": \""); |
| 164 | stream.writeText(currentRecord->fBaselinePath.c_str()); |
| 165 | stream.writeText("\",\n"); |
| 166 | |
| 167 | stream.writeText(" \"testPath\": \""); |
| 168 | stream.writeText(currentRecord->fTestPath.c_str()); |
| 169 | stream.writeText("\",\n"); |
| 170 | |
| 171 | stream.writeText(" \"diffs\": [\n"); |
| 172 | for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffIndex++) { |
| 173 | DiffData& data = currentRecord->fDiffs[diffIndex]; |
| 174 | stream.writeText(" {\n"); |
| 175 | |
| 176 | stream.writeText(" \"differName\": \""); |
| 177 | stream.writeText(data.fDiffName); |
| 178 | stream.writeText("\",\n"); |
| 179 | |
| 180 | stream.writeText(" \"result\": "); |
| 181 | stream.writeScalarAsText(data.fResult); |
| 182 | stream.writeText(",\n"); |
| 183 | |
| 184 | stream.writeText(" \"pointsOfInterest\": [\n"); |
| 185 | for (int poiIndex = 0; poiIndex < data.fPointsOfInterest.count(); poiIndex++) { |
| 186 | SkIPoint poi = data.fPointsOfInterest[poiIndex]; |
| 187 | stream.writeText(" ["); |
| 188 | stream.writeDecAsText(poi.x()); |
| 189 | stream.writeText(","); |
| 190 | stream.writeDecAsText(poi.y()); |
| 191 | stream.writeText("]"); |
| 192 | |
| 193 | // JSON does not allow trailing commas |
| 194 | if (poiIndex + 1 < data.fPointsOfInterest.count()) |
| 195 | { |
| 196 | stream.writeText(","); |
| 197 | } |
| 198 | stream.writeText("\n"); |
| 199 | } |
| 200 | stream.writeText(" ]\n"); |
| 201 | stream.writeText(" }"); |
| 202 | |
| 203 | // JSON does not allow trailing commas |
| 204 | if (diffIndex + 1 < currentRecord->fDiffs.count()) |
| 205 | { |
| 206 | stream.writeText(","); |
| 207 | } |
| 208 | stream.writeText(" \n"); |
| 209 | } |
| 210 | stream.writeText(" ]\n"); |
| 211 | |
| 212 | stream.writeText(" }"); |
| 213 | |
| 214 | // JSON does not allow trailing commas |
| 215 | if (NULL != currentRecord->fNext) |
| 216 | { |
| 217 | stream.writeText(","); |
| 218 | } |
| 219 | stream.writeText("\n"); |
| 220 | currentRecord = currentRecord->fNext; |
| 221 | } |
| 222 | stream.writeText(" ]\n"); |
zachr@google.com | a95959c | 2013-07-08 15:04:45 +0000 | [diff] [blame^] | 223 | if (useJSONP) { |
| 224 | stream.writeText("};\n"); |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | stream.writeText("}\n"); |
| 229 | } |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 230 | } |