blob: a1fa589a2fa24ffdc0676c33cc2cfb9375c2d1ff [file] [log] [blame]
commit-bot@chromium.orgee18f2a2014-05-08 14:48:44 +00001/*
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 */
31const static char kJsonKey_ActualResults[] = "actual-results";
32const static char kJsonKey_Header[] = "header";
33const static char kJsonKey_Header_Type[] = "type";
34const static char kJsonKey_Header_Revision[] = "revision"; // unique within Type
35const static char kJsonKey_Image_ChecksumAlgorithm[] = "checksumAlgorithm";
36const static char kJsonKey_Image_ChecksumValue[] = "checksumValue";
37const static char kJsonKey_Image_ComparisonResult[] = "comparisonResult";
38const static char kJsonKey_Image_Filepath[] = "filepath";
39const static char kJsonKey_Source_TiledImages[] = "tiled-images";
40const static char kJsonKey_Source_WholeImage[] = "whole-image";
41// Values (not keys) that are written out by this JSON generator
42const static char kJsonValue_Header_Type[] = "ChecksummedImages";
43const static int kJsonValue_Header_Revision = 1;
44const static char kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5[] = "bitmap-64bitMD5";
45const static char kJsonValue_Image_ComparisonResult_NoComparison[] = "no-comparison";
46
47namespace 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