2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkBitmapHasher.h"
11 #include "SkJSONCPP.h"
16 #include "image_expectations.h"
19 * TODO(epoger): Make constant strings consistent instead of mixing hypenated and camel-caps.
21 * TODO(epoger): Similar constants are already maintained in 2 other places:
22 * gm/gm_json.py and gm/gm_expectations.cpp. We shouldn't add yet a third place.
23 * Figure out a way to share the definitions instead.
25 * Note that, as of https://codereview.chromium.org/226293002 , the JSON
26 * schema used here has started to differ from the one in gm_expectations.cpp .
27 * TODO(epoger): Consider getting GM and render_pictures to use the same JSON
30 const static char kJsonKey_ActualResults[] = "actual-results";
31 const static char kJsonKey_Descriptions[] = "descriptions";
32 const static char kJsonKey_ExpectedResults[] = "expected-results";
33 const static char kJsonKey_Header[] = "header";
34 const static char kJsonKey_Header_Type[] = "type";
35 const static char kJsonKey_Header_Revision[] = "revision";
36 const static char kJsonKey_Image_ChecksumAlgorithm[] = "checksumAlgorithm";
37 const static char kJsonKey_Image_ChecksumValue[] = "checksumValue";
38 const static char kJsonKey_Image_ComparisonResult[] = "comparisonResult";
39 const static char kJsonKey_Image_Filepath[] = "filepath";
40 const static char kJsonKey_Image_IgnoreFailure[] = "ignoreFailure";
41 const static char kJsonKey_Source_TiledImages[] = "tiled-images";
42 const static char kJsonKey_Source_WholeImage[] = "whole-image";
43 // Values (not keys) that are written out by this JSON generator
44 const static char kJsonValue_Header_Type[] = "ChecksummedImages";
45 const static int kJsonValue_Header_Revision = 1;
46 const static char kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5[] = "bitmap-64bitMD5";
47 const static char kJsonValue_Image_ComparisonResult_Failed[] = "failed";
48 const static char kJsonValue_Image_ComparisonResult_FailureIgnored[] = "failure-ignored";
49 const static char kJsonValue_Image_ComparisonResult_NoComparison[] = "no-comparison";
50 const static char kJsonValue_Image_ComparisonResult_Succeeded[] = "succeeded";
54 // ImageDigest class...
56 ImageDigest::ImageDigest(const SkBitmap &bitmap) {
57 if (!SkBitmapHasher::ComputeDigest(bitmap, &fHashValue)) {
58 SkFAIL("unable to compute image digest");
62 ImageDigest::ImageDigest(const SkString &hashType, uint64_t hashValue) {
63 if (!hashType.equals(kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5)) {
64 SkFAIL((SkString("unsupported hashType ")+=hashType).c_str());
66 fHashValue = hashValue;
70 SkString ImageDigest::getHashType() const {
71 // TODO(epoger): The current implementation assumes that the
72 // result digest is always of type kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5 .
73 return SkString(kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5);
76 uint64_t ImageDigest::getHashValue() const {
80 // BitmapAndDigest class...
82 BitmapAndDigest::BitmapAndDigest(const SkBitmap &bitmap) : fBitmap(bitmap) {
85 const ImageDigest *BitmapAndDigest::getImageDigestPtr() {
86 if (NULL == fImageDigestRef.get()) {
87 fImageDigestRef.reset(SkNEW_ARGS(ImageDigest, (fBitmap)));
89 return fImageDigestRef.get();
92 const SkBitmap *BitmapAndDigest::getBitmapPtr() const {
96 // ImageResultsAndExpectations class...
98 bool ImageResultsAndExpectations::readExpectationsFile(const char *jsonPath) {
99 if (NULL == jsonPath) {
100 SkDebugf("JSON expectations filename not specified\n");
103 SkFILE* filePtr = sk_fopen(jsonPath, kRead_SkFILE_Flag);
104 if (NULL == filePtr) {
105 SkDebugf("JSON expectations file '%s' does not exist\n", jsonPath);
108 size_t size = sk_fgetsize(filePtr);
110 SkDebugf("JSON expectations file '%s' is empty, so no expectations\n", jsonPath);
112 fExpectedResults.clear();
115 bool parsedJson = Parse(filePtr, &fExpectedJsonRoot);
118 SkDebugf("Failed to parse JSON expectations file '%s'\n", jsonPath);
121 Json::Value header = fExpectedJsonRoot[kJsonKey_Header];
122 Json::Value headerType = header[kJsonKey_Header_Type];
123 Json::Value headerRevision = header[kJsonKey_Header_Revision];
124 if (strcmp(headerType.asCString(), kJsonValue_Header_Type)) {
125 SkDebugf("JSON expectations file '%s': expected headerType '%s', found '%s'\n",
126 jsonPath, kJsonValue_Header_Type, headerType.asCString());
129 if (headerRevision.asInt() != kJsonValue_Header_Revision) {
130 SkDebugf("JSON expectations file '%s': expected headerRevision %d, found %d\n",
131 jsonPath, kJsonValue_Header_Revision, headerRevision.asInt());
134 fExpectedResults = fExpectedJsonRoot[kJsonKey_ExpectedResults];
138 void ImageResultsAndExpectations::add(const char *sourceName, const char *fileName,
139 const ImageDigest &digest, const int *tileNumber) {
140 // Get expectation, if any.
141 Json::Value expectedImage;
142 if (!fExpectedResults.isNull()) {
143 if (NULL == tileNumber) {
144 expectedImage = fExpectedResults[sourceName][kJsonKey_Source_WholeImage];
146 expectedImage = fExpectedResults[sourceName][kJsonKey_Source_TiledImages]
151 // Fill in info about the actual result itself.
152 Json::Value actualChecksumAlgorithm = digest.getHashType().c_str();
153 Json::Value actualChecksumValue = Json::UInt64(digest.getHashValue());
154 Json::Value actualImage;
155 actualImage[kJsonKey_Image_ChecksumAlgorithm] = actualChecksumAlgorithm;
156 actualImage[kJsonKey_Image_ChecksumValue] = actualChecksumValue;
157 actualImage[kJsonKey_Image_Filepath] = fileName;
159 // Compare against expectedImage to fill in comparisonResult.
160 Json::Value comparisonResult = kJsonValue_Image_ComparisonResult_NoComparison;
161 if (!expectedImage.isNull()) {
162 if ((actualChecksumAlgorithm == expectedImage[kJsonKey_Image_ChecksumAlgorithm]) &&
163 (actualChecksumValue == expectedImage[kJsonKey_Image_ChecksumValue])) {
164 comparisonResult = kJsonValue_Image_ComparisonResult_Succeeded;
165 } else if (expectedImage[kJsonKey_Image_IgnoreFailure] == true) {
166 comparisonResult = kJsonValue_Image_ComparisonResult_FailureIgnored;
168 comparisonResult = kJsonValue_Image_ComparisonResult_Failed;
171 actualImage[kJsonKey_Image_ComparisonResult] = comparisonResult;
173 // Add this actual result to our collection.
174 if (NULL == tileNumber) {
175 fActualResults[sourceName][kJsonKey_Source_WholeImage] = actualImage;
177 fActualResults[sourceName][kJsonKey_Source_TiledImages][*tileNumber] = actualImage;
181 void ImageResultsAndExpectations::addDescription(const char *key, const char *value) {
182 fDescriptions[key] = value;
185 bool ImageResultsAndExpectations::matchesExpectation(const char *sourceName,
186 const ImageDigest &digest,
187 const int *tileNumber) {
188 if (fExpectedResults.isNull()) {
192 Json::Value expectedImage;
193 if (NULL == tileNumber) {
194 expectedImage = fExpectedResults[sourceName][kJsonKey_Source_WholeImage];
196 expectedImage = fExpectedResults[sourceName][kJsonKey_Source_TiledImages][*tileNumber];
198 if (expectedImage.isNull()) {
202 Json::Value actualChecksumAlgorithm = digest.getHashType().c_str();
203 Json::Value actualChecksumValue = Json::UInt64(digest.getHashValue());
204 return ((actualChecksumAlgorithm == expectedImage[kJsonKey_Image_ChecksumAlgorithm]) &&
205 (actualChecksumValue == expectedImage[kJsonKey_Image_ChecksumValue]));
208 void ImageResultsAndExpectations::writeToFile(const char *filename) const {
210 header[kJsonKey_Header_Type] = kJsonValue_Header_Type;
211 header[kJsonKey_Header_Revision] = kJsonValue_Header_Revision;
213 root[kJsonKey_ActualResults] = fActualResults;
214 root[kJsonKey_Descriptions] = fDescriptions;
215 root[kJsonKey_Header] = header;
216 std::string jsonStdString = root.toStyledString();
217 SkFILEWStream stream(filename);
218 stream.write(jsonStdString.c_str(), jsonStdString.length());
221 /*static*/ bool ImageResultsAndExpectations::Parse(SkFILE *filePtr,
222 Json::Value *jsonRoot) {
223 SkAutoDataUnref dataRef(SkData::NewFromFILE(filePtr));
224 if (NULL == dataRef.get()) {
228 const char *bytes = reinterpret_cast<const char *>(dataRef.get()->data());
229 size_t size = dataRef.get()->size();
231 if (!reader.parse(bytes, bytes+size, *jsonRoot)) {
238 } // namespace sk_tools