blob: e38a245915b4c5335a0278c9de387e9e7330d2c4 [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"
10#include "SkCommandLineFlags.h"
11#include "SkData.h"
rmistry8d965a62016-04-25 10:35:03 -070012#include "SkJSONCPP.h"
mtklein2d225e32016-02-29 09:05:32 -080013#include "SkMD5.h"
msarett3478f752016-02-12 14:47:09 -080014#include "SkOSFile.h"
15#include "SkPicture.h"
mtklein2d225e32016-02-29 09:05:32 -080016#include "SkPixelSerializer.h"
msarett3478f752016-02-12 14:47:09 -080017#include "SkStream.h"
mtklein2d225e32016-02-29 09:05:32 -080018#include "SkTHash.h"
msarett3478f752016-02-12 14:47:09 -080019
rmistry8d965a62016-04-25 10:35:03 -070020
21#include <map>
22
mtklein2d225e32016-02-29 09:05:32 -080023DEFINE_string2(skps, s, "skps", "A path to a directory of skps.");
24DEFINE_string2(out, o, "img-out", "A path to an output directory.");
rmistry8d965a62016-04-25 10:35:03 -070025DEFINE_bool(testDecode, false, "Indicates if we want to test that the images decode successfully.");
26DEFINE_bool(writeImages, true, "Indicates if we want to write out images.");
27DEFINE_string2(failuresJsonPath, j, "",
28 "Dump SKP and count of unknown images to the specified JSON file. Will not be "
29 "written anywhere if empty.");
msarett3478f752016-02-12 14:47:09 -080030
mtklein2d225e32016-02-29 09:05:32 -080031static int gKnown;
msarett3478f752016-02-12 14:47:09 -080032static const char* gOutputDir;
rmistry8d965a62016-04-25 10:35:03 -070033static std::map<std::string, unsigned int> gSkpToUnknownCount = {};
msarett3478f752016-02-12 14:47:09 -080034
mtklein2d225e32016-02-29 09:05:32 -080035static SkTHashSet<SkMD5::Digest> gSeen;
msarett3478f752016-02-12 14:47:09 -080036
mtklein2d225e32016-02-29 09:05:32 -080037struct Sniffer : public SkPixelSerializer {
msarett3478f752016-02-12 14:47:09 -080038
rmistry8d965a62016-04-25 10:35:03 -070039 std::string skpName;
40
41 Sniffer(std::string name) {
42 skpName = name;
43 }
44
mtklein2d225e32016-02-29 09:05:32 -080045 void sniff(const void* ptr, size_t len) {
46 SkMD5 md5;
47 md5.write(ptr, len);
48 SkMD5::Digest digest;
49 md5.finish(digest);
50
51 if (gSeen.contains(digest)) {
52 return;
msarett3478f752016-02-12 14:47:09 -080053 }
mtklein2d225e32016-02-29 09:05:32 -080054 gSeen.add(digest);
55
bungeman38d909e2016-08-02 14:40:46 -070056 sk_sp<SkData> data(SkData::MakeWithoutCopy(ptr, len));
reed42943c82016-09-12 12:01:44 -070057 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data));
mtklein2d225e32016-02-29 09:05:32 -080058 if (!codec) {
rmistry8d965a62016-04-25 10:35:03 -070059 // FIXME: This code is currently unreachable because we create an empty generator when
60 // we fail to create a codec.
61 SkDebugf("Codec could not be created for %s\n", skpName.c_str());
62 gSkpToUnknownCount[skpName]++;
mtklein2d225e32016-02-29 09:05:32 -080063 return;
64 }
65 SkString ext;
66 switch (codec->getEncodedFormat()) {
67 case SkEncodedFormat::kBMP_SkEncodedFormat: ext = "bmp"; break;
68 case SkEncodedFormat::kGIF_SkEncodedFormat: ext = "gif"; break;
69 case SkEncodedFormat::kICO_SkEncodedFormat: ext = "ico"; break;
70 case SkEncodedFormat::kJPEG_SkEncodedFormat: ext = "jpg"; break;
71 case SkEncodedFormat::kPNG_SkEncodedFormat: ext = "png"; break;
yujieqin7a307df2016-03-11 07:32:33 -080072 case SkEncodedFormat::kDNG_SkEncodedFormat: ext = "dng"; break;
mtklein2d225e32016-02-29 09:05:32 -080073 case SkEncodedFormat::kWBMP_SkEncodedFormat: ext = "wbmp"; break;
74 case SkEncodedFormat::kWEBP_SkEncodedFormat: ext = "webp"; break;
rmistry8d965a62016-04-25 10:35:03 -070075 default:
76 // This should be unreachable because we cannot create a codec if we do not know
77 // the image type.
78 SkASSERT(false);
mtklein2d225e32016-02-29 09:05:32 -080079 }
80
rmistry8d965a62016-04-25 10:35:03 -070081 if (FLAGS_testDecode) {
82 SkBitmap bitmap;
83 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
84 bitmap.allocPixels(info);
rmistry6f47dba2016-04-27 07:31:23 -070085 const SkCodec::Result result = codec->getPixels(
86 info, bitmap.getPixels(), bitmap.rowBytes());
87 if (SkCodec::kIncompleteInput != result && SkCodec::kSuccess != result)
rmistry8d965a62016-04-25 10:35:03 -070088 {
89 SkDebugf("Decoding failed for %s\n", skpName.c_str());
90 gSkpToUnknownCount[skpName]++;
91 return;
92 }
93 }
mtklein2d225e32016-02-29 09:05:32 -080094
rmistry8d965a62016-04-25 10:35:03 -070095 if (FLAGS_writeImages) {
96 SkString path;
97 path.appendf("%s/%d.%s", gOutputDir, gKnown, ext.c_str());
mtklein2d225e32016-02-29 09:05:32 -080098
rmistry8d965a62016-04-25 10:35:03 -070099 SkFILEWStream file(path.c_str());
100 file.write(ptr, len);
101
102 SkDebugf("%s\n", path.c_str());
103 }
104 gKnown++;
msarett3478f752016-02-12 14:47:09 -0800105 }
106
mtklein2d225e32016-02-29 09:05:32 -0800107 bool onUseEncodedData(const void* ptr, size_t len) override {
108 this->sniff(ptr, len);
msarett3478f752016-02-12 14:47:09 -0800109 return true;
110 }
mtklein2d225e32016-02-29 09:05:32 -0800111 SkData* onEncode(const SkPixmap&) override { return nullptr; }
112};
msarett3478f752016-02-12 14:47:09 -0800113
msarett3478f752016-02-12 14:47:09 -0800114
115int main(int argc, char** argv) {
116 SkCommandLineFlags::SetUsage(
rmistry8d965a62016-04-25 10:35:03 -0700117 "Usage: get_images_from_skps -s <dir of skps> -o <dir for output images> --testDecode "
118 "-j <output JSON path>\n");
msarett3478f752016-02-12 14:47:09 -0800119
120 SkCommandLineFlags::Parse(argc, argv);
msarett3478f752016-02-12 14:47:09 -0800121 const char* inputs = FLAGS_skps[0];
122 gOutputDir = FLAGS_out[0];
mtklein2d225e32016-02-29 09:05:32 -0800123
msarett3478f752016-02-12 14:47:09 -0800124 if (!sk_isdir(inputs) || !sk_isdir(gOutputDir)) {
125 SkCommandLineFlags::PrintUsage();
126 return 1;
127 }
128
msarett3478f752016-02-12 14:47:09 -0800129 SkOSFile::Iter iter(inputs, "skp");
130 for (SkString file; iter.next(&file); ) {
bungemanf93d7112016-09-16 06:24:20 -0700131 std::unique_ptr<SkStream> stream =
132 SkStream::MakeFromFile(SkOSPath::Join(inputs, file.c_str()).c_str());
133 sk_sp<SkPicture> picture(SkPicture::MakeFromStream(stream.get()));
msarett3478f752016-02-12 14:47:09 -0800134
mtklein2d225e32016-02-29 09:05:32 -0800135 SkDynamicMemoryWStream scratch;
rmistry8d965a62016-04-25 10:35:03 -0700136 Sniffer sniff(file.c_str());
mtklein2d225e32016-02-29 09:05:32 -0800137 picture->serialize(&scratch, &sniff);
msarett3478f752016-02-12 14:47:09 -0800138 }
rmistry8d965a62016-04-25 10:35:03 -0700139 int totalUnknowns = 0;
140 /**
141 JSON results are written out in the following format:
142 {
143 "failures": {
144 "skp1": 12,
145 "skp4": 2,
146 ...
147 },
148 "totalFailures": 32,
149 "totalSuccesses": 21,
150 }
151 */
152 Json::Value fRoot;
153 for(auto it = gSkpToUnknownCount.cbegin(); it != gSkpToUnknownCount.cend(); ++it)
154 {
155 SkDebugf("%s %d\n", it->first.c_str(), it->second);
156 totalUnknowns += it->second;
157 fRoot["failures"][it->first.c_str()] = it->second;
158 }
159 SkDebugf("%d known, %d unknown\n", gKnown, totalUnknowns);
160 fRoot["totalFailures"] = totalUnknowns;
161 fRoot["totalSuccesses"] = gKnown;
rmistrycda08f82016-04-26 08:27:49 -0700162 if (totalUnknowns > 0) {
163 if (!FLAGS_failuresJsonPath.isEmpty()) {
164 SkDebugf("Writing failures to %s\n", FLAGS_failuresJsonPath[0]);
165 SkFILEWStream stream(FLAGS_failuresJsonPath[0]);
166 stream.writeText(Json::StyledWriter().write(fRoot).c_str());
167 stream.flush();
168 }
169 return -1;
rmistry8d965a62016-04-25 10:35:03 -0700170 }
msarett3478f752016-02-12 14:47:09 -0800171 return 0;
172}