blob: 0a2164cf39810ccc493dc5c2651270e2e445c303 [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
Leon Scroggins III4b0b00e2017-06-05 10:22:53 -040026DEFINE_string2(skps, s, "skps", "A path to a directory of skps or a single skp.");
mtklein2d225e32016-02-29 09:05:32 -080027DEFINE_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.");
rmistry8d965a62016-04-25 10:35:03 -070033DEFINE_string2(failuresJsonPath, j, "",
34 "Dump SKP and count of unknown images to the specified JSON file. Will not be "
35 "written anywhere if empty.");
msarett3478f752016-02-12 14:47:09 -080036
mtklein2d225e32016-02-29 09:05:32 -080037static int gKnown;
msarett3478f752016-02-12 14:47:09 -080038static const char* gOutputDir;
rmistry8d965a62016-04-25 10:35:03 -070039static std::map<std::string, unsigned int> gSkpToUnknownCount = {};
raftiasd737bee2016-12-08 10:53:24 -050040static std::map<std::string, unsigned int> gSkpToUnsupportedCount;
msarett3478f752016-02-12 14:47:09 -080041
mtklein2d225e32016-02-29 09:05:32 -080042static SkTHashSet<SkMD5::Digest> gSeen;
msarett3478f752016-02-12 14:47:09 -080043
mtklein2d225e32016-02-29 09:05:32 -080044struct Sniffer : public SkPixelSerializer {
msarett3478f752016-02-12 14:47:09 -080045
rmistry8d965a62016-04-25 10:35:03 -070046 std::string skpName;
47
48 Sniffer(std::string name) {
49 skpName = name;
50 }
51
mtklein2d225e32016-02-29 09:05:32 -080052 void sniff(const void* ptr, size_t len) {
53 SkMD5 md5;
54 md5.write(ptr, len);
55 SkMD5::Digest digest;
56 md5.finish(digest);
57
58 if (gSeen.contains(digest)) {
59 return;
msarett3478f752016-02-12 14:47:09 -080060 }
mtklein2d225e32016-02-29 09:05:32 -080061 gSeen.add(digest);
62
bungeman38d909e2016-08-02 14:40:46 -070063 sk_sp<SkData> data(SkData::MakeWithoutCopy(ptr, len));
Mike Reedede7bac2017-07-23 15:30:02 -040064 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(data);
mtklein2d225e32016-02-29 09:05:32 -080065 if (!codec) {
rmistry8d965a62016-04-25 10:35:03 -070066 // FIXME: This code is currently unreachable because we create an empty generator when
67 // we fail to create a codec.
68 SkDebugf("Codec could not be created for %s\n", skpName.c_str());
69 gSkpToUnknownCount[skpName]++;
mtklein2d225e32016-02-29 09:05:32 -080070 return;
71 }
72 SkString ext;
73 switch (codec->getEncodedFormat()) {
Hal Canarydb683012016-11-23 08:55:18 -070074 case SkEncodedImageFormat::kBMP: ext = "bmp"; break;
75 case SkEncodedImageFormat::kGIF: ext = "gif"; break;
76 case SkEncodedImageFormat::kICO: ext = "ico"; break;
77 case SkEncodedImageFormat::kJPEG: ext = "jpg"; break;
78 case SkEncodedImageFormat::kPNG: ext = "png"; break;
79 case SkEncodedImageFormat::kDNG: ext = "dng"; break;
80 case SkEncodedImageFormat::kWBMP: ext = "wbmp"; break;
81 case SkEncodedImageFormat::kWEBP: ext = "webp"; break;
rmistry8d965a62016-04-25 10:35:03 -070082 default:
83 // This should be unreachable because we cannot create a codec if we do not know
84 // the image type.
85 SkASSERT(false);
mtklein2d225e32016-02-29 09:05:32 -080086 }
87
Leon Scroggins IIIda1893f2017-07-10 11:23:07 -040088 auto writeImage = [&] (const char* name, int num) {
rmistry8d965a62016-04-25 10:35:03 -070089 SkString path;
Leon Scroggins IIIda1893f2017-07-10 11:23:07 -040090 path.appendf("%s/%s%d.%s", gOutputDir, name, num, ext.c_str());
mtklein2d225e32016-02-29 09:05:32 -080091
rmistry8d965a62016-04-25 10:35:03 -070092 SkFILEWStream file(path.c_str());
93 file.write(ptr, len);
94
95 SkDebugf("%s\n", path.c_str());
raftiasd737bee2016-12-08 10:53:24 -050096 };
97
raftiasd737bee2016-12-08 10:53:24 -050098 if (FLAGS_testDecode) {
99 SkBitmap bitmap;
100 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
101 bitmap.allocPixels(info);
102 const SkCodec::Result result = codec->getPixels(
103 info, bitmap.getPixels(), bitmap.rowBytes());
Leon Scroggins IIIda1893f2017-07-10 11:23:07 -0400104 switch (result) {
105 case SkCodec::kSuccess:
106 case SkCodec::kIncompleteInput:
107 case SkCodec::kErrorInInput:
108 break;
109 default:
110 SkDebugf("Decoding failed for %s\n", skpName.c_str());
111 if (FLAGS_writeFailedImages) {
112 writeImage("unknown", gSkpToUnknownCount[skpName]);
113 }
114 gSkpToUnknownCount[skpName]++;
115 return;
raftiasd737bee2016-12-08 10:53:24 -0500116 }
rmistry8d965a62016-04-25 10:35:03 -0700117 }
raftiasd737bee2016-12-08 10:53:24 -0500118
raftiasd737bee2016-12-08 10:53:24 -0500119 if (FLAGS_writeImages) {
Leon Scroggins IIIda1893f2017-07-10 11:23:07 -0400120 writeImage("", gKnown);
raftiasd737bee2016-12-08 10:53:24 -0500121 }
122
rmistry8d965a62016-04-25 10:35:03 -0700123 gKnown++;
msarett3478f752016-02-12 14:47:09 -0800124 }
125
mtklein2d225e32016-02-29 09:05:32 -0800126 bool onUseEncodedData(const void* ptr, size_t len) override {
127 this->sniff(ptr, len);
msarett3478f752016-02-12 14:47:09 -0800128 return true;
129 }
mtklein2d225e32016-02-29 09:05:32 -0800130 SkData* onEncode(const SkPixmap&) override { return nullptr; }
131};
msarett3478f752016-02-12 14:47:09 -0800132
Leon Scroggins IIIda1893f2017-07-10 11:23:07 -0400133static bool get_images_from_file(const SkString& file) {
Leon Scroggins III4b0b00e2017-06-05 10:22:53 -0400134 auto stream = SkStream::MakeFromFile(file.c_str());
135 sk_sp<SkPicture> picture(SkPicture::MakeFromStream(stream.get()));
Leon Scroggins IIIda1893f2017-07-10 11:23:07 -0400136 if (!picture) {
137 return false;
138 }
Leon Scroggins III4b0b00e2017-06-05 10:22:53 -0400139
140 SkDynamicMemoryWStream scratch;
141 Sniffer sniff(file.c_str());
142 picture->serialize(&scratch, &sniff);
Leon Scroggins IIIda1893f2017-07-10 11:23:07 -0400143 return true;
Leon Scroggins III4b0b00e2017-06-05 10:22:53 -0400144}
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 "
Matt Sarettee7c8202017-06-09 10:18:34 -0400149 "-j <output JSON path> --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
Leon Scroggins III4b0b00e2017-06-05 10:22:53 -0400155 if (!sk_isdir(gOutputDir)) {
msarett3478f752016-02-12 14:47:09 -0800156 SkCommandLineFlags::PrintUsage();
157 return 1;
158 }
159
Leon Scroggins III4b0b00e2017-06-05 10:22:53 -0400160 if (sk_isdir(inputs)) {
161 SkOSFile::Iter iter(inputs, "skp");
162 for (SkString file; iter.next(&file); ) {
Leon Scroggins IIIda1893f2017-07-10 11:23:07 -0400163 if (!get_images_from_file(SkOSPath::Join(inputs, file.c_str()))) {
164 return 2;
165 }
Leon Scroggins III4b0b00e2017-06-05 10:22:53 -0400166 }
167 } else {
Leon Scroggins IIIda1893f2017-07-10 11:23:07 -0400168 if (!get_images_from_file(SkString(inputs))) {
169 return 2;
170 }
msarett3478f752016-02-12 14:47:09 -0800171 }
rmistry8d965a62016-04-25 10:35:03 -0700172 /**
173 JSON results are written out in the following format:
174 {
175 "failures": {
176 "skp1": 12,
177 "skp4": 2,
178 ...
179 },
raftiasd737bee2016-12-08 10:53:24 -0500180 "unsupported": {
181 "skp9": 13,
182 "skp17": 3,
183 ...
184 }
rmistry8d965a62016-04-25 10:35:03 -0700185 "totalFailures": 32,
raftiasd737bee2016-12-08 10:53:24 -0500186 "totalUnsupported": 9,
rmistry8d965a62016-04-25 10:35:03 -0700187 "totalSuccesses": 21,
188 }
189 */
190 Json::Value fRoot;
raftiasd737bee2016-12-08 10:53:24 -0500191 int totalFailures = 0;
rmistry8d965a62016-04-25 10:35:03 -0700192 for(auto it = gSkpToUnknownCount.cbegin(); it != gSkpToUnknownCount.cend(); ++it)
193 {
194 SkDebugf("%s %d\n", it->first.c_str(), it->second);
raftiasd737bee2016-12-08 10:53:24 -0500195 totalFailures += it->second;
rmistry8d965a62016-04-25 10:35:03 -0700196 fRoot["failures"][it->first.c_str()] = it->second;
197 }
raftiasd737bee2016-12-08 10:53:24 -0500198 fRoot["totalFailures"] = totalFailures;
199 int totalUnsupported = 0;
200#ifdef SK_DEBUG
201 for (const auto& unsupported : gSkpToUnsupportedCount) {
202 SkDebugf("%s %d\n", unsupported.first.c_str(), unsupported.second);
203 totalUnsupported += unsupported.second;
204 fRoot["unsupported"][unsupported.first] = unsupported.second;
205 }
206 fRoot["totalUnsupported"] = totalUnsupported;
207#endif
rmistry8d965a62016-04-25 10:35:03 -0700208 fRoot["totalSuccesses"] = gKnown;
raftiasd737bee2016-12-08 10:53:24 -0500209 SkDebugf("%d known, %d failures, %d unsupported\n", gKnown, totalFailures, totalUnsupported);
210 if (totalFailures > 0 || totalUnsupported > 0) {
rmistrycda08f82016-04-26 08:27:49 -0700211 if (!FLAGS_failuresJsonPath.isEmpty()) {
212 SkDebugf("Writing failures to %s\n", FLAGS_failuresJsonPath[0]);
213 SkFILEWStream stream(FLAGS_failuresJsonPath[0]);
214 stream.writeText(Json::StyledWriter().write(fRoot).c_str());
215 stream.flush();
216 }
217 return -1;
rmistry8d965a62016-04-25 10:35:03 -0700218 }
msarett3478f752016-02-12 14:47:09 -0800219 return 0;
220}