blob: 486781810d8ce61ed2ac705454bba58a5c155259 [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"
Florin Malita3425e222018-06-19 10:52:20 -040013#include "SkJSONWriter.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 Sniffer sniff(file.c_str());
Mike Reed43f0ba02018-03-09 13:08:38 -0500129 auto stream = SkStream::MakeFromFile(file.c_str());
130
131 SkDeserialProcs procs;
132 procs.fImageProc = [](const void* data, size_t size, void* ctx) -> sk_sp<SkImage> {
133 ((Sniffer*)ctx)->sniff(data, size);
134 return nullptr;
Mike Reedef038482017-12-16 08:41:28 -0500135 };
136 procs.fImageCtx = &sniff;
Mike Reed43f0ba02018-03-09 13:08:38 -0500137 return SkPicture::MakeFromStream(stream.get(), &procs) != nullptr;
Leon Scroggins III4b0b00e2017-06-05 10:22:53 -0400138}
msarett3478f752016-02-12 14:47:09 -0800139
140int main(int argc, char** argv) {
141 SkCommandLineFlags::SetUsage(
rmistry8d965a62016-04-25 10:35:03 -0700142 "Usage: get_images_from_skps -s <dir of skps> -o <dir for output images> --testDecode "
Matt Sarettee7c8202017-06-09 10:18:34 -0400143 "-j <output JSON path> --writeImages, --writeFailedImages\n");
msarett3478f752016-02-12 14:47:09 -0800144
145 SkCommandLineFlags::Parse(argc, argv);
msarett3478f752016-02-12 14:47:09 -0800146 const char* inputs = FLAGS_skps[0];
147 gOutputDir = FLAGS_out[0];
mtklein2d225e32016-02-29 09:05:32 -0800148
Leon Scroggins III4b0b00e2017-06-05 10:22:53 -0400149 if (!sk_isdir(gOutputDir)) {
msarett3478f752016-02-12 14:47:09 -0800150 SkCommandLineFlags::PrintUsage();
151 return 1;
152 }
153
Leon Scroggins III4b0b00e2017-06-05 10:22:53 -0400154 if (sk_isdir(inputs)) {
155 SkOSFile::Iter iter(inputs, "skp");
156 for (SkString file; iter.next(&file); ) {
Leon Scroggins IIIda1893f2017-07-10 11:23:07 -0400157 if (!get_images_from_file(SkOSPath::Join(inputs, file.c_str()))) {
158 return 2;
159 }
Leon Scroggins III4b0b00e2017-06-05 10:22:53 -0400160 }
161 } else {
Leon Scroggins IIIda1893f2017-07-10 11:23:07 -0400162 if (!get_images_from_file(SkString(inputs))) {
163 return 2;
164 }
msarett3478f752016-02-12 14:47:09 -0800165 }
rmistry8d965a62016-04-25 10:35:03 -0700166 /**
167 JSON results are written out in the following format:
168 {
169 "failures": {
170 "skp1": 12,
171 "skp4": 2,
172 ...
173 },
raftiasd737bee2016-12-08 10:53:24 -0500174 "unsupported": {
175 "skp9": 13,
176 "skp17": 3,
177 ...
178 }
rmistry8d965a62016-04-25 10:35:03 -0700179 "totalFailures": 32,
raftiasd737bee2016-12-08 10:53:24 -0500180 "totalUnsupported": 9,
rmistry8d965a62016-04-25 10:35:03 -0700181 "totalSuccesses": 21,
182 }
183 */
Florin Malita3425e222018-06-19 10:52:20 -0400184
185 unsigned int totalFailures = 0,
186 totalUnsupported = 0;
187 SkDynamicMemoryWStream memStream;
188 SkJSONWriter writer(&memStream, SkJSONWriter::Mode::kPretty);
189 writer.beginObject();
rmistry8d965a62016-04-25 10:35:03 -0700190 {
Florin Malita3425e222018-06-19 10:52:20 -0400191 writer.beginObject("failures");
192 {
193 for(const auto& failure : gSkpToUnknownCount) {
194 SkDebugf("%s %d\n", failure.first.c_str(), failure.second);
195 totalFailures += failure.second;
196 writer.appendU32(failure.first.c_str(), failure.second);
197 }
198 }
199 writer.endObject();
200 writer.appendU32("totalFailures", totalFailures);
201
raftiasd737bee2016-12-08 10:53:24 -0500202#ifdef SK_DEBUG
Florin Malita3425e222018-06-19 10:52:20 -0400203 writer.beginObject("unsupported");
204 {
205 for (const auto& unsupported : gSkpToUnsupportedCount) {
206 SkDebugf("%s %d\n", unsupported.first.c_str(), unsupported.second);
207 totalUnsupported += unsupported.second;
208 writer.appendHexU32(unsupported.first.c_str(), unsupported.second);
209 }
210
211 }
212 writer.endObject();
213 writer.appendU32("totalUnsupported", totalUnsupported);
raftiasd737bee2016-12-08 10:53:24 -0500214#endif
Florin Malita3425e222018-06-19 10:52:20 -0400215
216 writer.appendS32("totalSuccesses", gKnown);
217 SkDebugf("%d known, %d failures, %d unsupported\n",
218 gKnown, totalFailures, totalUnsupported);
219 }
220 writer.endObject();
221 writer.flush();
222
raftiasd737bee2016-12-08 10:53:24 -0500223 if (totalFailures > 0 || totalUnsupported > 0) {
rmistrycda08f82016-04-26 08:27:49 -0700224 if (!FLAGS_failuresJsonPath.isEmpty()) {
225 SkDebugf("Writing failures to %s\n", FLAGS_failuresJsonPath[0]);
226 SkFILEWStream stream(FLAGS_failuresJsonPath[0]);
Florin Malita3425e222018-06-19 10:52:20 -0400227 auto jsonStream = memStream.detachAsStream();
228 stream.writeStream(jsonStream.get(), jsonStream->getLength());
rmistrycda08f82016-04-26 08:27:49 -0700229 }
rmistry8d965a62016-04-25 10:35:03 -0700230 }
Florin Malita3425e222018-06-19 10:52:20 -0400231
msarett3478f752016-02-12 14:47:09 -0800232 return 0;
233}