blob: c81254f2c7a5519376164141a71dc90b0434abc4 [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"
Mike Reedef038482017-12-16 08:41:28 -050018#include "SkSerialProcs.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
Mike Reedef038482017-12-16 08:41:28 -050044struct Sniffer {
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 }
mtklein2d225e32016-02-29 09:05:32 -0800125};
msarett3478f752016-02-12 14:47:09 -0800126
Leon Scroggins IIIda1893f2017-07-10 11:23:07 -0400127static bool get_images_from_file(const SkString& file) {
Leon Scroggins III4b0b00e2017-06-05 10:22:53 -0400128 auto stream = SkStream::MakeFromFile(file.c_str());
129 sk_sp<SkPicture> picture(SkPicture::MakeFromStream(stream.get()));
Leon Scroggins IIIda1893f2017-07-10 11:23:07 -0400130 if (!picture) {
131 return false;
132 }
Leon Scroggins III4b0b00e2017-06-05 10:22:53 -0400133
134 SkDynamicMemoryWStream scratch;
135 Sniffer sniff(file.c_str());
Mike Reedef038482017-12-16 08:41:28 -0500136 SkSerialProcs procs;
137 procs.fImageProc = [](SkImage* image, void* ctx) {
138 if (auto data = image->refEncodedData()) {
139 ((Sniffer*)ctx)->sniff(data->data(), data->size());
140 }
141 return SkData::MakeEmpty();
142 };
143 procs.fImageCtx = &sniff;
144 picture->serialize(procs);
Leon Scroggins IIIda1893f2017-07-10 11:23:07 -0400145 return true;
Leon Scroggins III4b0b00e2017-06-05 10:22:53 -0400146}
msarett3478f752016-02-12 14:47:09 -0800147
148int main(int argc, char** argv) {
149 SkCommandLineFlags::SetUsage(
rmistry8d965a62016-04-25 10:35:03 -0700150 "Usage: get_images_from_skps -s <dir of skps> -o <dir for output images> --testDecode "
Matt Sarettee7c8202017-06-09 10:18:34 -0400151 "-j <output JSON path> --writeImages, --writeFailedImages\n");
msarett3478f752016-02-12 14:47:09 -0800152
153 SkCommandLineFlags::Parse(argc, argv);
msarett3478f752016-02-12 14:47:09 -0800154 const char* inputs = FLAGS_skps[0];
155 gOutputDir = FLAGS_out[0];
mtklein2d225e32016-02-29 09:05:32 -0800156
Leon Scroggins III4b0b00e2017-06-05 10:22:53 -0400157 if (!sk_isdir(gOutputDir)) {
msarett3478f752016-02-12 14:47:09 -0800158 SkCommandLineFlags::PrintUsage();
159 return 1;
160 }
161
Leon Scroggins III4b0b00e2017-06-05 10:22:53 -0400162 if (sk_isdir(inputs)) {
163 SkOSFile::Iter iter(inputs, "skp");
164 for (SkString file; iter.next(&file); ) {
Leon Scroggins IIIda1893f2017-07-10 11:23:07 -0400165 if (!get_images_from_file(SkOSPath::Join(inputs, file.c_str()))) {
166 return 2;
167 }
Leon Scroggins III4b0b00e2017-06-05 10:22:53 -0400168 }
169 } else {
Leon Scroggins IIIda1893f2017-07-10 11:23:07 -0400170 if (!get_images_from_file(SkString(inputs))) {
171 return 2;
172 }
msarett3478f752016-02-12 14:47:09 -0800173 }
rmistry8d965a62016-04-25 10:35:03 -0700174 /**
175 JSON results are written out in the following format:
176 {
177 "failures": {
178 "skp1": 12,
179 "skp4": 2,
180 ...
181 },
raftiasd737bee2016-12-08 10:53:24 -0500182 "unsupported": {
183 "skp9": 13,
184 "skp17": 3,
185 ...
186 }
rmistry8d965a62016-04-25 10:35:03 -0700187 "totalFailures": 32,
raftiasd737bee2016-12-08 10:53:24 -0500188 "totalUnsupported": 9,
rmistry8d965a62016-04-25 10:35:03 -0700189 "totalSuccesses": 21,
190 }
191 */
192 Json::Value fRoot;
raftiasd737bee2016-12-08 10:53:24 -0500193 int totalFailures = 0;
rmistry8d965a62016-04-25 10:35:03 -0700194 for(auto it = gSkpToUnknownCount.cbegin(); it != gSkpToUnknownCount.cend(); ++it)
195 {
196 SkDebugf("%s %d\n", it->first.c_str(), it->second);
raftiasd737bee2016-12-08 10:53:24 -0500197 totalFailures += it->second;
rmistry8d965a62016-04-25 10:35:03 -0700198 fRoot["failures"][it->first.c_str()] = it->second;
199 }
raftiasd737bee2016-12-08 10:53:24 -0500200 fRoot["totalFailures"] = totalFailures;
201 int totalUnsupported = 0;
202#ifdef SK_DEBUG
203 for (const auto& unsupported : gSkpToUnsupportedCount) {
204 SkDebugf("%s %d\n", unsupported.first.c_str(), unsupported.second);
205 totalUnsupported += unsupported.second;
206 fRoot["unsupported"][unsupported.first] = unsupported.second;
207 }
208 fRoot["totalUnsupported"] = totalUnsupported;
209#endif
rmistry8d965a62016-04-25 10:35:03 -0700210 fRoot["totalSuccesses"] = gKnown;
raftiasd737bee2016-12-08 10:53:24 -0500211 SkDebugf("%d known, %d failures, %d unsupported\n", gKnown, totalFailures, totalUnsupported);
212 if (totalFailures > 0 || totalUnsupported > 0) {
rmistrycda08f82016-04-26 08:27:49 -0700213 if (!FLAGS_failuresJsonPath.isEmpty()) {
214 SkDebugf("Writing failures to %s\n", FLAGS_failuresJsonPath[0]);
215 SkFILEWStream stream(FLAGS_failuresJsonPath[0]);
216 stream.writeText(Json::StyledWriter().write(fRoot).c_str());
217 stream.flush();
218 }
219 return -1;
rmistry8d965a62016-04-25 10:35:03 -0700220 }
msarett3478f752016-02-12 14:47:09 -0800221 return 0;
222}