commit-bot@chromium.org | ee18f2a | 2014-05-08 14:48:44 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 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 "SkBitmapHasher.h" |
| 10 | #include "SkData.h" |
| 11 | #include "SkDataUtils.h" |
| 12 | #include "SkJSONCPP.h" |
| 13 | #include "SkOSFile.h" |
| 14 | #include "SkStream.h" |
| 15 | #include "SkTypes.h" |
| 16 | |
| 17 | #include "image_expectations.h" |
| 18 | |
| 19 | /* |
| 20 | * TODO(epoger): Make constant strings consistent instead of mixing hypenated and camel-caps. |
| 21 | * |
| 22 | * TODO(epoger): Similar constants are already maintained in 2 other places: |
| 23 | * gm/gm_json.py and gm/gm_expectations.cpp. We shouldn't add yet a third place. |
| 24 | * Figure out a way to share the definitions instead. |
| 25 | * |
| 26 | * Note that, as of https://codereview.chromium.org/226293002 , the JSON |
| 27 | * schema used here has started to differ from the one in gm_expectations.cpp . |
| 28 | * TODO(epoger): Consider getting GM and render_pictures to use the same JSON |
| 29 | * output module. |
| 30 | */ |
| 31 | const static char kJsonKey_ActualResults[] = "actual-results"; |
| 32 | const static char kJsonKey_Header[] = "header"; |
| 33 | const static char kJsonKey_Header_Type[] = "type"; |
| 34 | const static char kJsonKey_Header_Revision[] = "revision"; // unique within Type |
| 35 | const static char kJsonKey_Image_ChecksumAlgorithm[] = "checksumAlgorithm"; |
| 36 | const static char kJsonKey_Image_ChecksumValue[] = "checksumValue"; |
| 37 | const static char kJsonKey_Image_ComparisonResult[] = "comparisonResult"; |
| 38 | const static char kJsonKey_Image_Filepath[] = "filepath"; |
| 39 | const static char kJsonKey_Source_TiledImages[] = "tiled-images"; |
| 40 | const static char kJsonKey_Source_WholeImage[] = "whole-image"; |
| 41 | // Values (not keys) that are written out by this JSON generator |
| 42 | const static char kJsonValue_Header_Type[] = "ChecksummedImages"; |
| 43 | const static int kJsonValue_Header_Revision = 1; |
| 44 | const static char kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5[] = "bitmap-64bitMD5"; |
| 45 | const static char kJsonValue_Image_ComparisonResult_NoComparison[] = "no-comparison"; |
| 46 | |
| 47 | namespace sk_tools { |
| 48 | |
| 49 | void ImageResultsSummary::add(const char *sourceName, const char *fileName, uint64_t hash, |
| 50 | const int *tileNumber) { |
| 51 | Json::Value image; |
| 52 | image[kJsonKey_Image_ChecksumAlgorithm] = kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5; |
| 53 | image[kJsonKey_Image_ChecksumValue] = Json::UInt64(hash); |
| 54 | image[kJsonKey_Image_ComparisonResult] = kJsonValue_Image_ComparisonResult_NoComparison; |
| 55 | image[kJsonKey_Image_Filepath] = fileName; |
| 56 | if (NULL == tileNumber) { |
| 57 | fActualResults[sourceName][kJsonKey_Source_WholeImage] = image; |
| 58 | } else { |
| 59 | fActualResults[sourceName][kJsonKey_Source_TiledImages][*tileNumber] = image; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void ImageResultsSummary::add(const char *sourceName, const char *fileName, const SkBitmap& bitmap, |
| 64 | const int *tileNumber) { |
| 65 | uint64_t hash; |
| 66 | SkAssertResult(SkBitmapHasher::ComputeDigest(bitmap, &hash)); |
| 67 | this->add(sourceName, fileName, hash, tileNumber); |
| 68 | } |
| 69 | |
| 70 | void ImageResultsSummary::writeToFile(const char *filename) { |
| 71 | Json::Value header; |
| 72 | header[kJsonKey_Header_Type] = kJsonValue_Header_Type; |
| 73 | header[kJsonKey_Header_Revision] = kJsonValue_Header_Revision; |
| 74 | Json::Value root; |
| 75 | root[kJsonKey_Header] = header; |
| 76 | root[kJsonKey_ActualResults] = fActualResults; |
| 77 | std::string jsonStdString = root.toStyledString(); |
| 78 | SkFILEWStream stream(filename); |
| 79 | stream.write(jsonStdString.c_str(), jsonStdString.length()); |
| 80 | } |
| 81 | |
| 82 | } // namespace sk_tools |