blob: c413c6d96120fd8182042b111b978d796e7f6617 [file] [log] [blame]
msarett3478f752016-02-12 14:47:09 -08001/*
2 * Copyright 2016 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
rmistry8d965a62016-04-25 10:35:03 -07008#include "SkBitmap.h"
msarett3478f752016-02-12 14:47:09 -08009#include "SkCodec.h"
raftiasd737bee2016-12-08 10:53:24 -050010#include "SkColorSpace.h"
msarett3478f752016-02-12 14:47:09 -080011#include "SkCommandLineFlags.h"
12#include "SkData.h"
rmistry8d965a62016-04-25 10:35:03 -070013#include "SkJSONCPP.h"
mtklein2d225e32016-02-29 09:05:32 -080014#include "SkMD5.h"
msarett3478f752016-02-12 14:47:09 -080015#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050016#include "SkOSPath.h"
msarett3478f752016-02-12 14:47:09 -080017#include "SkPicture.h"
mtklein2d225e32016-02-29 09:05:32 -080018#include "SkPixelSerializer.h"
msarett3478f752016-02-12 14:47:09 -080019#include "SkStream.h"
mtklein2d225e32016-02-29 09:05:32 -080020#include "SkTHash.h"
msarett3478f752016-02-12 14:47:09 -080021
rmistry8d965a62016-04-25 10:35:03 -070022
raftiasd737bee2016-12-08 10:53:24 -050023#include <iostream>
rmistry8d965a62016-04-25 10:35:03 -070024#include <map>
25
mtklein2d225e32016-02-29 09:05:32 -080026DEFINE_string2(skps, s, "skps", "A path to a directory of skps.");
27DEFINE_string2(out, o, "img-out", "A path to an output directory.");
rmistry8d965a62016-04-25 10:35:03 -070028DEFINE_bool(testDecode, false, "Indicates if we want to test that the images decode successfully.");
raftiasd737bee2016-12-08 10:53:24 -050029DEFINE_bool(writeImages, true,
30 "Indicates if we want to write out supported/decoded images.");
31DEFINE_bool(writeFailedImages, false,
32 "Indicates if we want to write out unsupported/failed to decode images.");
33DEFINE_bool(testICCSupport, false,
34 "Indicates if we want to test that the images with ICC profiles are supported");
rmistry8d965a62016-04-25 10:35:03 -070035DEFINE_string2(failuresJsonPath, j, "",
36 "Dump SKP and count of unknown images to the specified JSON file. Will not be "
37 "written anywhere if empty.");
msarett3478f752016-02-12 14:47:09 -080038
mtklein2d225e32016-02-29 09:05:32 -080039static int gKnown;
msarett3478f752016-02-12 14:47:09 -080040static const char* gOutputDir;
rmistry8d965a62016-04-25 10:35:03 -070041static std::map<std::string, unsigned int> gSkpToUnknownCount = {};
raftiasd737bee2016-12-08 10:53:24 -050042static std::map<std::string, unsigned int> gSkpToUnsupportedCount;
msarett3478f752016-02-12 14:47:09 -080043
mtklein2d225e32016-02-29 09:05:32 -080044static SkTHashSet<SkMD5::Digest> gSeen;
msarett3478f752016-02-12 14:47:09 -080045
mtklein2d225e32016-02-29 09:05:32 -080046struct Sniffer : public SkPixelSerializer {
msarett3478f752016-02-12 14:47:09 -080047
rmistry8d965a62016-04-25 10:35:03 -070048 std::string skpName;
49
50 Sniffer(std::string name) {
51 skpName = name;
52 }
53
mtklein2d225e32016-02-29 09:05:32 -080054 void sniff(const void* ptr, size_t len) {
55 SkMD5 md5;
56 md5.write(ptr, len);
57 SkMD5::Digest digest;
58 md5.finish(digest);
59
60 if (gSeen.contains(digest)) {
61 return;
msarett3478f752016-02-12 14:47:09 -080062 }
mtklein2d225e32016-02-29 09:05:32 -080063 gSeen.add(digest);
64
bungeman38d909e2016-08-02 14:40:46 -070065 sk_sp<SkData> data(SkData::MakeWithoutCopy(ptr, len));
Ben Wagner145dbcd2016-11-03 14:40:50 -040066 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
mtklein2d225e32016-02-29 09:05:32 -080067 if (!codec) {
rmistry8d965a62016-04-25 10:35:03 -070068 // FIXME: This code is currently unreachable because we create an empty generator when
69 // we fail to create a codec.
70 SkDebugf("Codec could not be created for %s\n", skpName.c_str());
71 gSkpToUnknownCount[skpName]++;
mtklein2d225e32016-02-29 09:05:32 -080072 return;
73 }
74 SkString ext;
75 switch (codec->getEncodedFormat()) {
Hal Canarydb683012016-11-23 08:55:18 -070076 case SkEncodedImageFormat::kBMP: ext = "bmp"; break;
77 case SkEncodedImageFormat::kGIF: ext = "gif"; break;
78 case SkEncodedImageFormat::kICO: ext = "ico"; break;
79 case SkEncodedImageFormat::kJPEG: ext = "jpg"; break;
80 case SkEncodedImageFormat::kPNG: ext = "png"; break;
81 case SkEncodedImageFormat::kDNG: ext = "dng"; break;
82 case SkEncodedImageFormat::kWBMP: ext = "wbmp"; break;
83 case SkEncodedImageFormat::kWEBP: ext = "webp"; break;
rmistry8d965a62016-04-25 10:35:03 -070084 default:
85 // This should be unreachable because we cannot create a codec if we do not know
86 // the image type.
87 SkASSERT(false);
mtklein2d225e32016-02-29 09:05:32 -080088 }
89
raftiasd737bee2016-12-08 10:53:24 -050090 auto writeImage = [&] {
rmistry8d965a62016-04-25 10:35:03 -070091 SkString path;
92 path.appendf("%s/%d.%s", gOutputDir, gKnown, ext.c_str());
mtklein2d225e32016-02-29 09:05:32 -080093
rmistry8d965a62016-04-25 10:35:03 -070094 SkFILEWStream file(path.c_str());
95 file.write(ptr, len);
96
97 SkDebugf("%s\n", path.c_str());
raftiasd737bee2016-12-08 10:53:24 -050098 };
99
100
101 if (FLAGS_testDecode) {
102 SkBitmap bitmap;
103 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
104 bitmap.allocPixels(info);
105 const SkCodec::Result result = codec->getPixels(
106 info, bitmap.getPixels(), bitmap.rowBytes());
107 if (SkCodec::kIncompleteInput != result && SkCodec::kSuccess != result) {
108 SkDebugf("Decoding failed for %s\n", skpName.c_str());
109 gSkpToUnknownCount[skpName]++;
110 if (FLAGS_writeFailedImages) {
111 writeImage();
112 }
113 return;
114 }
rmistry8d965a62016-04-25 10:35:03 -0700115 }
raftiasd737bee2016-12-08 10:53:24 -0500116
117#ifdef SK_DEBUG
118 if (FLAGS_testICCSupport) {
119 if (codec->fUnsupportedICC) {
120 SkDebugf("Color correction failed for %s\n", skpName.c_str());
121 gSkpToUnsupportedCount[skpName]++;
122 if (FLAGS_writeFailedImages) {
123 writeImage();
124 }
125 return;
126 }
127 }
128#endif
129
130 if (FLAGS_writeImages) {
131 writeImage();
132 }
133
134
rmistry8d965a62016-04-25 10:35:03 -0700135 gKnown++;
msarett3478f752016-02-12 14:47:09 -0800136 }
137
mtklein2d225e32016-02-29 09:05:32 -0800138 bool onUseEncodedData(const void* ptr, size_t len) override {
139 this->sniff(ptr, len);
msarett3478f752016-02-12 14:47:09 -0800140 return true;
141 }
mtklein2d225e32016-02-29 09:05:32 -0800142 SkData* onEncode(const SkPixmap&) override { return nullptr; }
143};
msarett3478f752016-02-12 14:47:09 -0800144
msarett3478f752016-02-12 14:47:09 -0800145
146int main(int argc, char** argv) {
147 SkCommandLineFlags::SetUsage(
rmistry8d965a62016-04-25 10:35:03 -0700148 "Usage: get_images_from_skps -s <dir of skps> -o <dir for output images> --testDecode "
raftiasd737bee2016-12-08 10:53:24 -0500149 "-j <output JSON path> --testICCSupport --writeImages, --writeFailedImages\n");
msarett3478f752016-02-12 14:47:09 -0800150
151 SkCommandLineFlags::Parse(argc, argv);
msarett3478f752016-02-12 14:47:09 -0800152 const char* inputs = FLAGS_skps[0];
153 gOutputDir = FLAGS_out[0];
mtklein2d225e32016-02-29 09:05:32 -0800154
msarett3478f752016-02-12 14:47:09 -0800155 if (!sk_isdir(inputs) || !sk_isdir(gOutputDir)) {
156 SkCommandLineFlags::PrintUsage();
157 return 1;
158 }
raftiasd737bee2016-12-08 10:53:24 -0500159#ifndef SK_DEBUG
160 if (FLAGS_testICCSupport) {
161 std::cerr << "--testICCSupport unavailable outside of SK_DEBUG builds" << std::endl;
162 return 1;
163 }
164#endif
msarett3478f752016-02-12 14:47:09 -0800165
msarett3478f752016-02-12 14:47:09 -0800166 SkOSFile::Iter iter(inputs, "skp");
167 for (SkString file; iter.next(&file); ) {
bungemanf93d7112016-09-16 06:24:20 -0700168 std::unique_ptr<SkStream> stream =
169 SkStream::MakeFromFile(SkOSPath::Join(inputs, file.c_str()).c_str());
170 sk_sp<SkPicture> picture(SkPicture::MakeFromStream(stream.get()));
msarett3478f752016-02-12 14:47:09 -0800171
mtklein2d225e32016-02-29 09:05:32 -0800172 SkDynamicMemoryWStream scratch;
rmistry8d965a62016-04-25 10:35:03 -0700173 Sniffer sniff(file.c_str());
mtklein2d225e32016-02-29 09:05:32 -0800174 picture->serialize(&scratch, &sniff);
msarett3478f752016-02-12 14:47:09 -0800175 }
rmistry8d965a62016-04-25 10:35:03 -0700176 /**
177 JSON results are written out in the following format:
178 {
179 "failures": {
180 "skp1": 12,
181 "skp4": 2,
182 ...
183 },
raftiasd737bee2016-12-08 10:53:24 -0500184 "unsupported": {
185 "skp9": 13,
186 "skp17": 3,
187 ...
188 }
rmistry8d965a62016-04-25 10:35:03 -0700189 "totalFailures": 32,
raftiasd737bee2016-12-08 10:53:24 -0500190 "totalUnsupported": 9,
rmistry8d965a62016-04-25 10:35:03 -0700191 "totalSuccesses": 21,
192 }
193 */
194 Json::Value fRoot;
raftiasd737bee2016-12-08 10:53:24 -0500195 int totalFailures = 0;
rmistry8d965a62016-04-25 10:35:03 -0700196 for(auto it = gSkpToUnknownCount.cbegin(); it != gSkpToUnknownCount.cend(); ++it)
197 {
198 SkDebugf("%s %d\n", it->first.c_str(), it->second);
raftiasd737bee2016-12-08 10:53:24 -0500199 totalFailures += it->second;
rmistry8d965a62016-04-25 10:35:03 -0700200 fRoot["failures"][it->first.c_str()] = it->second;
201 }
raftiasd737bee2016-12-08 10:53:24 -0500202 fRoot["totalFailures"] = totalFailures;
203 int totalUnsupported = 0;
204#ifdef SK_DEBUG
205 for (const auto& unsupported : gSkpToUnsupportedCount) {
206 SkDebugf("%s %d\n", unsupported.first.c_str(), unsupported.second);
207 totalUnsupported += unsupported.second;
208 fRoot["unsupported"][unsupported.first] = unsupported.second;
209 }
210 fRoot["totalUnsupported"] = totalUnsupported;
211#endif
rmistry8d965a62016-04-25 10:35:03 -0700212 fRoot["totalSuccesses"] = gKnown;
raftiasd737bee2016-12-08 10:53:24 -0500213 SkDebugf("%d known, %d failures, %d unsupported\n", gKnown, totalFailures, totalUnsupported);
214 if (totalFailures > 0 || totalUnsupported > 0) {
rmistrycda08f82016-04-26 08:27:49 -0700215 if (!FLAGS_failuresJsonPath.isEmpty()) {
216 SkDebugf("Writing failures to %s\n", FLAGS_failuresJsonPath[0]);
217 SkFILEWStream stream(FLAGS_failuresJsonPath[0]);
218 stream.writeText(Json::StyledWriter().write(fRoot).c_str());
219 stream.flush();
220 }
221 return -1;
rmistry8d965a62016-04-25 10:35:03 -0700222 }
msarett3478f752016-02-12 14:47:09 -0800223 return 0;
224}