blob: 11faa4285473d78b59f50f1037c2c0b3a56382eb [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.");
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
Leon Scroggins III4b0b00e2017-06-05 10:22:53 -0400145static void get_images_from_file(const SkString& file) {
146 auto stream = SkStream::MakeFromFile(file.c_str());
147 sk_sp<SkPicture> picture(SkPicture::MakeFromStream(stream.get()));
148
149 SkDynamicMemoryWStream scratch;
150 Sniffer sniff(file.c_str());
151 picture->serialize(&scratch, &sniff);
152}
msarett3478f752016-02-12 14:47:09 -0800153
154int main(int argc, char** argv) {
155 SkCommandLineFlags::SetUsage(
rmistry8d965a62016-04-25 10:35:03 -0700156 "Usage: get_images_from_skps -s <dir of skps> -o <dir for output images> --testDecode "
raftiasd737bee2016-12-08 10:53:24 -0500157 "-j <output JSON path> --testICCSupport --writeImages, --writeFailedImages\n");
msarett3478f752016-02-12 14:47:09 -0800158
159 SkCommandLineFlags::Parse(argc, argv);
msarett3478f752016-02-12 14:47:09 -0800160 const char* inputs = FLAGS_skps[0];
161 gOutputDir = FLAGS_out[0];
mtklein2d225e32016-02-29 09:05:32 -0800162
Leon Scroggins III4b0b00e2017-06-05 10:22:53 -0400163 if (!sk_isdir(gOutputDir)) {
msarett3478f752016-02-12 14:47:09 -0800164 SkCommandLineFlags::PrintUsage();
165 return 1;
166 }
raftiasd737bee2016-12-08 10:53:24 -0500167#ifndef SK_DEBUG
168 if (FLAGS_testICCSupport) {
169 std::cerr << "--testICCSupport unavailable outside of SK_DEBUG builds" << std::endl;
170 return 1;
171 }
172#endif
msarett3478f752016-02-12 14:47:09 -0800173
Leon Scroggins III4b0b00e2017-06-05 10:22:53 -0400174 if (sk_isdir(inputs)) {
175 SkOSFile::Iter iter(inputs, "skp");
176 for (SkString file; iter.next(&file); ) {
177 get_images_from_file(SkOSPath::Join(inputs, file.c_str()));
178 }
179 } else {
180 get_images_from_file(SkString(inputs));
msarett3478f752016-02-12 14:47:09 -0800181 }
rmistry8d965a62016-04-25 10:35:03 -0700182 /**
183 JSON results are written out in the following format:
184 {
185 "failures": {
186 "skp1": 12,
187 "skp4": 2,
188 ...
189 },
raftiasd737bee2016-12-08 10:53:24 -0500190 "unsupported": {
191 "skp9": 13,
192 "skp17": 3,
193 ...
194 }
rmistry8d965a62016-04-25 10:35:03 -0700195 "totalFailures": 32,
raftiasd737bee2016-12-08 10:53:24 -0500196 "totalUnsupported": 9,
rmistry8d965a62016-04-25 10:35:03 -0700197 "totalSuccesses": 21,
198 }
199 */
200 Json::Value fRoot;
raftiasd737bee2016-12-08 10:53:24 -0500201 int totalFailures = 0;
rmistry8d965a62016-04-25 10:35:03 -0700202 for(auto it = gSkpToUnknownCount.cbegin(); it != gSkpToUnknownCount.cend(); ++it)
203 {
204 SkDebugf("%s %d\n", it->first.c_str(), it->second);
raftiasd737bee2016-12-08 10:53:24 -0500205 totalFailures += it->second;
rmistry8d965a62016-04-25 10:35:03 -0700206 fRoot["failures"][it->first.c_str()] = it->second;
207 }
raftiasd737bee2016-12-08 10:53:24 -0500208 fRoot["totalFailures"] = totalFailures;
209 int totalUnsupported = 0;
210#ifdef SK_DEBUG
211 for (const auto& unsupported : gSkpToUnsupportedCount) {
212 SkDebugf("%s %d\n", unsupported.first.c_str(), unsupported.second);
213 totalUnsupported += unsupported.second;
214 fRoot["unsupported"][unsupported.first] = unsupported.second;
215 }
216 fRoot["totalUnsupported"] = totalUnsupported;
217#endif
rmistry8d965a62016-04-25 10:35:03 -0700218 fRoot["totalSuccesses"] = gKnown;
raftiasd737bee2016-12-08 10:53:24 -0500219 SkDebugf("%d known, %d failures, %d unsupported\n", gKnown, totalFailures, totalUnsupported);
220 if (totalFailures > 0 || totalUnsupported > 0) {
rmistrycda08f82016-04-26 08:27:49 -0700221 if (!FLAGS_failuresJsonPath.isEmpty()) {
222 SkDebugf("Writing failures to %s\n", FLAGS_failuresJsonPath[0]);
223 SkFILEWStream stream(FLAGS_failuresJsonPath[0]);
224 stream.writeText(Json::StyledWriter().write(fRoot).c_str());
225 stream.flush();
226 }
227 return -1;
rmistry8d965a62016-04-25 10:35:03 -0700228 }
msarett3478f752016-02-12 14:47:09 -0800229 return 0;
230}