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" |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame] | 11 | #include "SkRunnable.h" |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame] | 12 | #include "SkSize.h" |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 13 | #include "SkStream.h" |
edisonn@google.com | c93c8ac | 2013-07-22 15:24:26 +0000 | [diff] [blame] | 14 | #include "SkTDict.h" |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame^] | 15 | #include "SkTaskGroup.h" |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 16 | |
stephana | 21b342d | 2014-08-13 10:36:06 -0700 | [diff] [blame] | 17 | // from the tools directory for replace_char(...) |
| 18 | #include "picture_utils.h" |
| 19 | |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 20 | #include "SkDiffContext.h" |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame] | 21 | #include "SkImageDiffer.h" |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 22 | #include "skpdiff_util.h" |
| 23 | |
| 24 | SkDiffContext::SkDiffContext() { |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 25 | fDiffers = NULL; |
| 26 | fDifferCount = 0; |
| 27 | } |
| 28 | |
| 29 | SkDiffContext::~SkDiffContext() { |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 30 | if (NULL != fDiffers) { |
| 31 | SkDELETE_ARRAY(fDiffers); |
| 32 | } |
| 33 | } |
| 34 | |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame] | 35 | void SkDiffContext::setAlphaMaskDir(const SkString& path) { |
djsollen@google.com | 513a7bf | 2013-11-07 19:24:06 +0000 | [diff] [blame] | 36 | if (!path.isEmpty() && sk_mkdir(path.c_str())) { |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame] | 37 | fAlphaMaskDir = path; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | void SkDiffContext::setRgbDiffDir(const SkString& path) { |
| 42 | if (!path.isEmpty() && sk_mkdir(path.c_str())) { |
| 43 | fRgbDiffDir = path; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void SkDiffContext::setWhiteDiffDir(const SkString& path) { |
| 48 | if (!path.isEmpty() && sk_mkdir(path.c_str())) { |
| 49 | fWhiteDiffDir = path; |
djsollen@google.com | 513a7bf | 2013-11-07 19:24:06 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
stephana | 21b342d | 2014-08-13 10:36:06 -0700 | [diff] [blame] | 53 | void SkDiffContext::setLongNames(const bool useLongNames) { |
| 54 | longNames = useLongNames; |
| 55 | } |
| 56 | |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 57 | void SkDiffContext::setDiffers(const SkTDArray<SkImageDiffer*>& differs) { |
| 58 | // Delete whatever the last array of differs was |
| 59 | if (NULL != fDiffers) { |
| 60 | SkDELETE_ARRAY(fDiffers); |
| 61 | fDiffers = NULL; |
| 62 | fDifferCount = 0; |
| 63 | } |
| 64 | |
| 65 | // Copy over the new differs |
| 66 | fDifferCount = differs.count(); |
| 67 | fDiffers = SkNEW_ARRAY(SkImageDiffer*, fDifferCount); |
| 68 | differs.copy(fDiffers); |
| 69 | } |
| 70 | |
djsollen@google.com | 513a7bf | 2013-11-07 19:24:06 +0000 | [diff] [blame] | 71 | static SkString get_common_prefix(const SkString& a, const SkString& b) { |
| 72 | const size_t maxPrefixLength = SkTMin(a.size(), b.size()); |
| 73 | SkASSERT(maxPrefixLength > 0); |
| 74 | for (size_t x = 0; x < maxPrefixLength; ++x) { |
| 75 | if (a[x] != b[x]) { |
| 76 | SkString result; |
| 77 | result.set(a.c_str(), x); |
| 78 | return result; |
| 79 | } |
| 80 | } |
| 81 | if (a.size() > b.size()) { |
| 82 | return b; |
| 83 | } else { |
| 84 | return a; |
| 85 | } |
| 86 | } |
| 87 | |
stephana | 21b342d | 2014-08-13 10:36:06 -0700 | [diff] [blame] | 88 | static SkString get_combined_name(const SkString& a, const SkString& b) { |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame^] | 89 | // Note (stephana): We must keep this function in sync with |
stephana | 21b342d | 2014-08-13 10:36:06 -0700 | [diff] [blame] | 90 | // getImageDiffRelativeUrl() in static/loader.js (under rebaseline_server). |
| 91 | SkString result = a; |
| 92 | result.append("-vs-"); |
| 93 | result.append(b); |
| 94 | sk_tools::replace_char(&result, '.', '_'); |
| 95 | return result; |
| 96 | } |
| 97 | |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 98 | void SkDiffContext::addDiff(const char* baselinePath, const char* testPath) { |
| 99 | // Load the images at the paths |
| 100 | SkBitmap baselineBitmap; |
| 101 | SkBitmap testBitmap; |
| 102 | if (!SkImageDecoder::DecodeFile(baselinePath, &baselineBitmap)) { |
| 103 | SkDebugf("Failed to load bitmap \"%s\"\n", baselinePath); |
| 104 | return; |
| 105 | } |
| 106 | if (!SkImageDecoder::DecodeFile(testPath, &testBitmap)) { |
| 107 | SkDebugf("Failed to load bitmap \"%s\"\n", testPath); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | // Setup a record for this diff |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 112 | fRecordMutex.acquire(); |
| 113 | DiffRecord* newRecord = fRecords.addToHead(DiffRecord()); |
| 114 | fRecordMutex.release(); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 115 | |
djsollen@google.com | 513a7bf | 2013-11-07 19:24:06 +0000 | [diff] [blame] | 116 | // compute the common name |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 117 | SkString baseName = SkOSPath::Basename(baselinePath); |
| 118 | SkString testName = SkOSPath::Basename(testPath); |
stephana | 21b342d | 2014-08-13 10:36:06 -0700 | [diff] [blame] | 119 | |
| 120 | if (longNames) { |
| 121 | newRecord->fCommonName = get_combined_name(baseName, testName); |
| 122 | } else { |
| 123 | newRecord->fCommonName = get_common_prefix(baseName, testName); |
| 124 | } |
| 125 | newRecord->fCommonName.append(".png"); |
djsollen@google.com | 513a7bf | 2013-11-07 19:24:06 +0000 | [diff] [blame] | 126 | |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 127 | newRecord->fBaselinePath = baselinePath; |
| 128 | newRecord->fTestPath = testPath; |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame] | 129 | newRecord->fSize = SkISize::Make(baselineBitmap.width(), baselineBitmap.height()); |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 130 | |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame] | 131 | // only generate diff images if we have a place to store them |
| 132 | SkImageDiffer::BitmapsToCreate bitmapsToCreate; |
| 133 | bitmapsToCreate.alphaMask = !fAlphaMaskDir.isEmpty(); |
| 134 | bitmapsToCreate.rgbDiff = !fRgbDiffDir.isEmpty(); |
| 135 | bitmapsToCreate.whiteDiff = !fWhiteDiffDir.isEmpty(); |
djsollen@google.com | 513a7bf | 2013-11-07 19:24:06 +0000 | [diff] [blame] | 136 | |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 137 | // Perform each diff |
| 138 | for (int differIndex = 0; differIndex < fDifferCount; differIndex++) { |
| 139 | SkImageDiffer* differ = fDiffers[differIndex]; |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 140 | |
| 141 | // Copy the results into data for this record |
| 142 | DiffData& diffData = newRecord->fDiffs.push_back(); |
| 143 | diffData.fDiffName = differ->getName(); |
| 144 | |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame] | 145 | if (!differ->diff(&baselineBitmap, &testBitmap, bitmapsToCreate, &diffData.fResult)) { |
| 146 | // if the diff failed, record -1 as the result |
| 147 | // TODO(djsollen): Record more detailed information about exactly what failed. |
| 148 | // (Image dimension mismatch? etc.) See http://skbug.com/2710 ('make skpdiff |
| 149 | // report more detail when it fails to compare two images') |
djsollen@google.com | 27d7ede | 2014-02-11 16:29:39 +0000 | [diff] [blame] | 150 | diffData.fResult.result = -1; |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 151 | continue; |
djsollen@google.com | 513a7bf | 2013-11-07 19:24:06 +0000 | [diff] [blame] | 152 | } |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 153 | |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame] | 154 | if (bitmapsToCreate.alphaMask |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 155 | && SkImageDiffer::RESULT_CORRECT != diffData.fResult.result |
| 156 | && !diffData.fResult.poiAlphaMask.empty() |
| 157 | && !newRecord->fCommonName.isEmpty()) { |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 158 | |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 159 | newRecord->fAlphaMaskPath = SkOSPath::Join(fAlphaMaskDir.c_str(), |
| 160 | newRecord->fCommonName.c_str()); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 161 | |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 162 | // compute the image diff and output it |
| 163 | SkBitmap copy; |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 164 | diffData.fResult.poiAlphaMask.copyTo(©, kN32_SkColorType); |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame] | 165 | SkImageEncoder::EncodeFile(newRecord->fAlphaMaskPath.c_str(), copy, |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 166 | SkImageEncoder::kPNG_Type, 100); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 167 | |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 168 | // cleanup the existing bitmap to free up resources; |
| 169 | diffData.fResult.poiAlphaMask.reset(); |
djsollen@google.com | 513a7bf | 2013-11-07 19:24:06 +0000 | [diff] [blame] | 170 | |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame] | 171 | bitmapsToCreate.alphaMask = false; |
| 172 | } |
| 173 | |
| 174 | if (bitmapsToCreate.rgbDiff |
| 175 | && SkImageDiffer::RESULT_CORRECT != diffData.fResult.result |
| 176 | && !diffData.fResult.rgbDiffBitmap.empty() |
| 177 | && !newRecord->fCommonName.isEmpty()) { |
| 178 | // TODO(djsollen): Rather than taking the max r/g/b diffs that come back from |
| 179 | // a particular differ and storing them as toplevel fields within |
| 180 | // newRecord, we should extend outputRecords() to report optional |
| 181 | // fields for each differ (not just "result" and "pointsOfInterest"). |
| 182 | // See http://skbug.com/2712 ('allow skpdiff to report different sets |
| 183 | // of result fields for different comparison algorithms') |
| 184 | newRecord->fMaxRedDiff = diffData.fResult.maxRedDiff; |
| 185 | newRecord->fMaxGreenDiff = diffData.fResult.maxGreenDiff; |
| 186 | newRecord->fMaxBlueDiff = diffData.fResult.maxBlueDiff; |
| 187 | |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 188 | newRecord->fRgbDiffPath = SkOSPath::Join(fRgbDiffDir.c_str(), |
| 189 | newRecord->fCommonName.c_str()); |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame] | 190 | SkImageEncoder::EncodeFile(newRecord->fRgbDiffPath.c_str(), |
| 191 | diffData.fResult.rgbDiffBitmap, |
| 192 | SkImageEncoder::kPNG_Type, 100); |
| 193 | diffData.fResult.rgbDiffBitmap.reset(); |
| 194 | bitmapsToCreate.rgbDiff = false; |
| 195 | } |
| 196 | |
| 197 | if (bitmapsToCreate.whiteDiff |
| 198 | && SkImageDiffer::RESULT_CORRECT != diffData.fResult.result |
| 199 | && !diffData.fResult.whiteDiffBitmap.empty() |
| 200 | && !newRecord->fCommonName.isEmpty()) { |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 201 | newRecord->fWhiteDiffPath = SkOSPath::Join(fWhiteDiffDir.c_str(), |
| 202 | newRecord->fCommonName.c_str()); |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame] | 203 | SkImageEncoder::EncodeFile(newRecord->fWhiteDiffPath.c_str(), |
| 204 | diffData.fResult.whiteDiffBitmap, |
| 205 | SkImageEncoder::kPNG_Type, 100); |
| 206 | diffData.fResult.whiteDiffBitmap.reset(); |
| 207 | bitmapsToCreate.whiteDiff = false; |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame] | 212 | class SkThreadedDiff : public SkRunnable { |
| 213 | public: |
| 214 | SkThreadedDiff() : fDiffContext(NULL) { } |
| 215 | |
| 216 | void setup(SkDiffContext* diffContext, const SkString& baselinePath, const SkString& testPath) { |
| 217 | fDiffContext = diffContext; |
| 218 | fBaselinePath = baselinePath; |
| 219 | fTestPath = testPath; |
| 220 | } |
| 221 | |
| 222 | virtual void run() SK_OVERRIDE { |
| 223 | fDiffContext->addDiff(fBaselinePath.c_str(), fTestPath.c_str()); |
| 224 | } |
| 225 | |
| 226 | private: |
| 227 | SkDiffContext* fDiffContext; |
| 228 | SkString fBaselinePath; |
| 229 | SkString fTestPath; |
| 230 | }; |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 231 | |
| 232 | void SkDiffContext::diffDirectories(const char baselinePath[], const char testPath[]) { |
| 233 | // Get the files in the baseline, we will then look for those inside the test path |
| 234 | SkTArray<SkString> baselineEntries; |
| 235 | if (!get_directory(baselinePath, &baselineEntries)) { |
| 236 | SkDebugf("Unable to open path \"%s\"\n", baselinePath); |
| 237 | return; |
| 238 | } |
| 239 | |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame^] | 240 | SkTaskGroup tg; |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame] | 241 | SkTArray<SkThreadedDiff> runnableDiffs; |
| 242 | runnableDiffs.reset(baselineEntries.count()); |
| 243 | |
| 244 | for (int x = 0; x < baselineEntries.count(); x++) { |
| 245 | const char* baseFilename = baselineEntries[x].c_str(); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 246 | |
| 247 | // Find the real location of each file to compare |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 248 | SkString baselineFile = SkOSPath::Join(baselinePath, baseFilename); |
| 249 | SkString testFile = SkOSPath::Join(testPath, baseFilename); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 250 | |
| 251 | // Check that the test file exists and is a file |
| 252 | if (sk_exists(testFile.c_str()) && !sk_isdir(testFile.c_str())) { |
| 253 | // Queue up the comparison with the differ |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame] | 254 | runnableDiffs[x].setup(this, baselineFile, testFile); |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame^] | 255 | tg.add(&runnableDiffs[x]); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 256 | } else { |
| 257 | SkDebugf("Baseline file \"%s\" has no corresponding test file\n", baselineFile.c_str()); |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | |
| 263 | void SkDiffContext::diffPatterns(const char baselinePattern[], const char testPattern[]) { |
| 264 | // Get the files in the baseline and test patterns. Because they are in sorted order, it's easy |
| 265 | // to find corresponding images by matching entry indices. |
| 266 | |
| 267 | SkTArray<SkString> baselineEntries; |
| 268 | if (!glob_files(baselinePattern, &baselineEntries)) { |
| 269 | SkDebugf("Unable to get pattern \"%s\"\n", baselinePattern); |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | SkTArray<SkString> testEntries; |
| 274 | if (!glob_files(testPattern, &testEntries)) { |
| 275 | SkDebugf("Unable to get pattern \"%s\"\n", testPattern); |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | if (baselineEntries.count() != testEntries.count()) { |
| 280 | SkDebugf("Baseline and test patterns do not yield corresponding number of files\n"); |
| 281 | return; |
| 282 | } |
| 283 | |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame^] | 284 | SkTaskGroup tg; |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame] | 285 | SkTArray<SkThreadedDiff> runnableDiffs; |
| 286 | runnableDiffs.reset(baselineEntries.count()); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 287 | |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame] | 288 | for (int x = 0; x < baselineEntries.count(); x++) { |
| 289 | runnableDiffs[x].setup(this, baselineEntries[x], testEntries[x]); |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame^] | 290 | tg.add(&runnableDiffs[x]); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
zachr@google.com | a95959c | 2013-07-08 15:04:45 +0000 | [diff] [blame] | 294 | void SkDiffContext::outputRecords(SkWStream& stream, bool useJSONP) { |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 295 | SkTLList<DiffRecord>::Iter iter(fRecords, SkTLList<DiffRecord>::Iter::kHead_IterStart); |
| 296 | DiffRecord* currentRecord = iter.get(); |
| 297 | |
zachr@google.com | a95959c | 2013-07-08 15:04:45 +0000 | [diff] [blame] | 298 | if (useJSONP) { |
| 299 | stream.writeText("var SkPDiffRecords = {\n"); |
zachr@google.com | 55173f2 | 2013-07-25 17:22:58 +0000 | [diff] [blame] | 300 | } else { |
zachr@google.com | a95959c | 2013-07-08 15:04:45 +0000 | [diff] [blame] | 301 | stream.writeText("{\n"); |
| 302 | } |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame] | 303 | |
| 304 | // TODO(djsollen): Would it be better to use the jsoncpp library to write out the JSON? |
| 305 | // This manual approach is probably more efficient, but it sure is ugly. |
| 306 | // See http://skbug.com/2713 ('make skpdiff use jsoncpp library to write out |
| 307 | // JSON output, instead of manual writeText() calls?') |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 308 | stream.writeText(" \"records\": [\n"); |
| 309 | while (NULL != currentRecord) { |
| 310 | stream.writeText(" {\n"); |
| 311 | |
zachr@google.com | a479aa1 | 2013-08-02 15:54:30 +0000 | [diff] [blame] | 312 | SkString baselineAbsPath = get_absolute_path(currentRecord->fBaselinePath); |
| 313 | SkString testAbsPath = get_absolute_path(currentRecord->fTestPath); |
| 314 | |
djsollen@google.com | 1e391b5 | 2013-10-16 15:00:11 +0000 | [diff] [blame] | 315 | stream.writeText(" \"commonName\": \""); |
djsollen@google.com | 513a7bf | 2013-11-07 19:24:06 +0000 | [diff] [blame] | 316 | stream.writeText(currentRecord->fCommonName.c_str()); |
djsollen@google.com | 1e391b5 | 2013-10-16 15:00:11 +0000 | [diff] [blame] | 317 | stream.writeText("\",\n"); |
| 318 | |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame] | 319 | stream.writeText(" \"differencePath\": \""); |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame] | 320 | stream.writeText(get_absolute_path(currentRecord->fAlphaMaskPath).c_str()); |
| 321 | stream.writeText("\",\n"); |
| 322 | |
| 323 | stream.writeText(" \"rgbDiffPath\": \""); |
| 324 | stream.writeText(get_absolute_path(currentRecord->fRgbDiffPath).c_str()); |
| 325 | stream.writeText("\",\n"); |
| 326 | |
| 327 | stream.writeText(" \"whiteDiffPath\": \""); |
| 328 | stream.writeText(get_absolute_path(currentRecord->fWhiteDiffPath).c_str()); |
djsollen@google.com | cbbf1ca | 2013-10-16 18:36:49 +0000 | [diff] [blame] | 329 | stream.writeText("\",\n"); |
| 330 | |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 331 | stream.writeText(" \"baselinePath\": \""); |
zachr@google.com | a479aa1 | 2013-08-02 15:54:30 +0000 | [diff] [blame] | 332 | stream.writeText(baselineAbsPath.c_str()); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 333 | stream.writeText("\",\n"); |
| 334 | |
| 335 | stream.writeText(" \"testPath\": \""); |
zachr@google.com | a479aa1 | 2013-08-02 15:54:30 +0000 | [diff] [blame] | 336 | stream.writeText(testAbsPath.c_str()); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 337 | stream.writeText("\",\n"); |
| 338 | |
epoger | 54f1ad8 | 2014-07-02 07:43:04 -0700 | [diff] [blame] | 339 | stream.writeText(" \"width\": "); |
| 340 | stream.writeDecAsText(currentRecord->fSize.width()); |
| 341 | stream.writeText(",\n"); |
| 342 | stream.writeText(" \"height\": "); |
| 343 | stream.writeDecAsText(currentRecord->fSize.height()); |
| 344 | stream.writeText(",\n"); |
| 345 | |
| 346 | stream.writeText(" \"maxRedDiff\": "); |
| 347 | stream.writeDecAsText(currentRecord->fMaxRedDiff); |
| 348 | stream.writeText(",\n"); |
| 349 | stream.writeText(" \"maxGreenDiff\": "); |
| 350 | stream.writeDecAsText(currentRecord->fMaxGreenDiff); |
| 351 | stream.writeText(",\n"); |
| 352 | stream.writeText(" \"maxBlueDiff\": "); |
| 353 | stream.writeDecAsText(currentRecord->fMaxBlueDiff); |
| 354 | stream.writeText(",\n"); |
| 355 | |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 356 | stream.writeText(" \"diffs\": [\n"); |
| 357 | for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffIndex++) { |
| 358 | DiffData& data = currentRecord->fDiffs[diffIndex]; |
| 359 | stream.writeText(" {\n"); |
| 360 | |
| 361 | stream.writeText(" \"differName\": \""); |
| 362 | stream.writeText(data.fDiffName); |
| 363 | stream.writeText("\",\n"); |
| 364 | |
| 365 | stream.writeText(" \"result\": "); |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 366 | stream.writeScalarAsText((SkScalar)data.fResult.result); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 367 | stream.writeText(",\n"); |
| 368 | |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 369 | stream.writeText(" \"pointsOfInterest\": "); |
| 370 | stream.writeDecAsText(data.fResult.poiCount); |
| 371 | stream.writeText("\n"); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 372 | |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 373 | stream.writeText(" }"); |
| 374 | |
| 375 | // JSON does not allow trailing commas |
zachr@google.com | 55173f2 | 2013-07-25 17:22:58 +0000 | [diff] [blame] | 376 | if (diffIndex + 1 < currentRecord->fDiffs.count()) { |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 377 | stream.writeText(","); |
| 378 | } |
| 379 | stream.writeText(" \n"); |
| 380 | } |
| 381 | stream.writeText(" ]\n"); |
| 382 | |
| 383 | stream.writeText(" }"); |
| 384 | |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 385 | currentRecord = iter.next(); |
| 386 | |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 387 | // JSON does not allow trailing commas |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 388 | if (NULL != currentRecord) { |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 389 | stream.writeText(","); |
| 390 | } |
| 391 | stream.writeText("\n"); |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 392 | } |
| 393 | stream.writeText(" ]\n"); |
zachr@google.com | a95959c | 2013-07-08 15:04:45 +0000 | [diff] [blame] | 394 | if (useJSONP) { |
| 395 | stream.writeText("};\n"); |
zachr@google.com | 55173f2 | 2013-07-25 17:22:58 +0000 | [diff] [blame] | 396 | } else { |
zachr@google.com | a95959c | 2013-07-08 15:04:45 +0000 | [diff] [blame] | 397 | stream.writeText("}\n"); |
| 398 | } |
zachr@google.com | 945708a | 2013-07-02 19:55:32 +0000 | [diff] [blame] | 399 | } |
edisonn@google.com | c93c8ac | 2013-07-22 15:24:26 +0000 | [diff] [blame] | 400 | |
| 401 | void SkDiffContext::outputCsv(SkWStream& stream) { |
| 402 | SkTDict<int> columns(2); |
| 403 | int cntColumns = 0; |
| 404 | |
| 405 | stream.writeText("key"); |
| 406 | |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 407 | SkTLList<DiffRecord>::Iter iter(fRecords, SkTLList<DiffRecord>::Iter::kHead_IterStart); |
| 408 | DiffRecord* currentRecord = iter.get(); |
edisonn@google.com | c93c8ac | 2013-07-22 15:24:26 +0000 | [diff] [blame] | 409 | |
| 410 | // Write CSV header and create a dictionary of all columns. |
| 411 | while (NULL != currentRecord) { |
| 412 | for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffIndex++) { |
| 413 | DiffData& data = currentRecord->fDiffs[diffIndex]; |
| 414 | if (!columns.find(data.fDiffName)) { |
| 415 | columns.set(data.fDiffName, cntColumns); |
| 416 | stream.writeText(", "); |
| 417 | stream.writeText(data.fDiffName); |
| 418 | cntColumns++; |
| 419 | } |
| 420 | } |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 421 | currentRecord = iter.next(); |
edisonn@google.com | c93c8ac | 2013-07-22 15:24:26 +0000 | [diff] [blame] | 422 | } |
| 423 | stream.writeText("\n"); |
| 424 | |
| 425 | double values[100]; |
| 426 | SkASSERT(cntColumns < 100); // Make the array larger, if we ever have so many diff types. |
| 427 | |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 428 | SkTLList<DiffRecord>::Iter iter2(fRecords, SkTLList<DiffRecord>::Iter::kHead_IterStart); |
| 429 | currentRecord = iter2.get(); |
edisonn@google.com | c93c8ac | 2013-07-22 15:24:26 +0000 | [diff] [blame] | 430 | while (NULL != currentRecord) { |
| 431 | for (int i = 0; i < cntColumns; i++) { |
| 432 | values[i] = -1; |
| 433 | } |
| 434 | |
| 435 | for (int diffIndex = 0; diffIndex < currentRecord->fDiffs.count(); diffIndex++) { |
| 436 | DiffData& data = currentRecord->fDiffs[diffIndex]; |
| 437 | int index = -1; |
| 438 | SkAssertResult(columns.find(data.fDiffName, &index)); |
| 439 | SkASSERT(index >= 0 && index < cntColumns); |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 440 | values[index] = data.fResult.result; |
edisonn@google.com | c93c8ac | 2013-07-22 15:24:26 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | const char* filename = currentRecord->fBaselinePath.c_str() + |
| 444 | strlen(currentRecord->fBaselinePath.c_str()) - 1; |
| 445 | while (filename > currentRecord->fBaselinePath.c_str() && *(filename - 1) != '/') { |
| 446 | filename--; |
| 447 | } |
| 448 | |
| 449 | stream.writeText(filename); |
| 450 | |
| 451 | for (int i = 0; i < cntColumns; i++) { |
| 452 | SkString str; |
| 453 | str.printf(", %f", values[i]); |
| 454 | stream.writeText(str.c_str()); |
| 455 | } |
| 456 | stream.writeText("\n"); |
| 457 | |
djsollen@google.com | efc51b7 | 2013-11-12 18:29:17 +0000 | [diff] [blame] | 458 | currentRecord = iter2.next(); |
edisonn@google.com | c93c8ac | 2013-07-22 15:24:26 +0000 | [diff] [blame] | 459 | } |
| 460 | } |