| 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 // 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 don
e 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 testPa
th[]) { | |
| 96 // Get the files in the baseline, we will then look for those inside the tes
t 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(); baselin
eIndex++) { | |
| 104 SkDebugf("[%i/%i] ", baselineIndex, baselineEntries.count()); | |
| 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 SkDebugf("[%i/%i] ", entryIndex, baselineEntries.count()); | |
| 145 const char* baselineFilename = baselineEntries[entryIndex].c_str(); | |
| 146 const char* testFilename = testEntries [entryIndex].c_str(); | |
| 147 | |
| 148 this->addDiff(baselineFilename, testFilename); | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 void SkDiffContext::outputRecords(SkWStream& stream, bool useJSONP) { | |
| 153 DiffRecord* currentRecord = fRecords; | |
| 154 if (useJSONP) { | |
| 155 stream.writeText("var SkPDiffRecords = {\n"); | |
| 156 } | |
| 157 else | |
| 158 { | |
| 159 stream.writeText("{\n"); | |
| 160 } | |
| 161 stream.writeText(" \"records\": [\n"); | |
| 162 while (NULL != currentRecord) { | |
| 163 stream.writeText(" {\n"); | |
| 164 | |
| 165 stream.writeText(" \"baselinePath\": \""); | |
| 166 stream.writeText(currentRecord->fBaselinePath.c_str()); | |
| 167 stream.writeText("\",\n"); | |
| 168 | |
| 169 stream.writeText(" \"testPath\": \""); | |
| 170 stream.writeText(currentRecord->fTestPath.c_str()); | |
| 171 stream.writeText("\",\n"); | |
| 172 | |
| 173 stream.writeText(" \"diffs\": [\n"); | |
| 174 for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); d
iffIndex++) { | |
| 175 DiffData& data = currentRecord->fDiffs[diffIndex]; | |
| 176 stream.writeText(" {\n"); | |
| 177 | |
| 178 stream.writeText(" \"differName\": \""); | |
| 179 stream.writeText(data.fDiffName); | |
| 180 stream.writeText("\",\n"); | |
| 181 | |
| 182 stream.writeText(" \"result\": "); | |
| 183 stream.writeScalarAsText(data.fResult); | |
| 184 stream.writeText(",\n"); | |
| 185 | |
| 186 stream.writeText(" \"pointsOfInterest\":
[\n"); | |
| 187 for (int poiIndex = 0; poiIndex < data.fPointsOfInterest.cou
nt(); poiIndex++) { | |
| 188 SkIPoint poi = data.fPointsOfInterest[poiIndex]; | |
| 189 stream.writeText(" ["); | |
| 190 stream.writeDecAsText(poi.x()); | |
| 191 stream.writeText(","); | |
| 192 stream.writeDecAsText(poi.y()); | |
| 193 stream.writeText("]"); | |
| 194 | |
| 195 // JSON does not allow trailing commas | |
| 196 if (poiIndex + 1 < data.fPointsOfInterest.count()) | |
| 197 { | |
| 198 stream.writeText(","); | |
| 199 } | |
| 200 stream.writeText("\n"); | |
| 201 } | |
| 202 stream.writeText(" ]\n"); | |
| 203 stream.writeText(" }"); | |
| 204 | |
| 205 // JSON does not allow trailing commas | |
| 206 if (diffIndex + 1 < currentRecord->fDiffs.count()) | |
| 207 { | |
| 208 stream.writeText(","); | |
| 209 } | |
| 210 stream.writeText(" \n"); | |
| 211 } | |
| 212 stream.writeText(" ]\n"); | |
| 213 | |
| 214 stream.writeText(" }"); | |
| 215 | |
| 216 // JSON does not allow trailing commas | |
| 217 if (NULL != currentRecord->fNext) | |
| 218 { | |
| 219 stream.writeText(","); | |
| 220 } | |
| 221 stream.writeText("\n"); | |
| 222 currentRecord = currentRecord->fNext; | |
| 223 } | |
| 224 stream.writeText(" ]\n"); | |
| 225 if (useJSONP) { | |
| 226 stream.writeText("};\n"); | |
| 227 } | |
| 228 else | |
| 229 { | |
| 230 stream.writeText("}\n"); | |
| 231 } | |
| 232 } | |
| OLD | NEW |