msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 8 | #include "SkBitmap.h" |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 9 | #include "SkCodec.h" |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 10 | #include "SkColorSpace.h" |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 11 | #include "SkCommandLineFlags.h" |
| 12 | #include "SkData.h" |
Florin Malita | 3425e22 | 2018-06-19 10:52:20 -0400 | [diff] [blame] | 13 | #include "SkJSONWriter.h" |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 14 | #include "SkMD5.h" |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 15 | #include "SkOSFile.h" |
Ben Wagner | bf111d7 | 2016-11-07 18:05:29 -0500 | [diff] [blame] | 16 | #include "SkOSPath.h" |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 17 | #include "SkPicture.h" |
Mike Reed | ef03848 | 2017-12-16 08:41:28 -0500 | [diff] [blame] | 18 | #include "SkSerialProcs.h" |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 19 | #include "SkStream.h" |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 20 | #include "SkTHash.h" |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 21 | |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 22 | |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 23 | #include <iostream> |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 24 | #include <map> |
| 25 | |
Leon Scroggins III | 4b0b00e | 2017-06-05 10:22:53 -0400 | [diff] [blame] | 26 | DEFINE_string2(skps, s, "skps", "A path to a directory of skps or a single skp."); |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 27 | DEFINE_string2(out, o, "img-out", "A path to an output directory."); |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 28 | DEFINE_bool(testDecode, false, "Indicates if we want to test that the images decode successfully."); |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 29 | DEFINE_bool(writeImages, true, |
| 30 | "Indicates if we want to write out supported/decoded images."); |
| 31 | DEFINE_bool(writeFailedImages, false, |
| 32 | "Indicates if we want to write out unsupported/failed to decode images."); |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 33 | DEFINE_string2(failuresJsonPath, j, "", |
| 34 | "Dump SKP and count of unknown images to the specified JSON file. Will not be " |
| 35 | "written anywhere if empty."); |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 36 | |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 37 | static int gKnown; |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 38 | static const char* gOutputDir; |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 39 | static std::map<std::string, unsigned int> gSkpToUnknownCount = {}; |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 40 | static std::map<std::string, unsigned int> gSkpToUnsupportedCount; |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 41 | |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 42 | static SkTHashSet<SkMD5::Digest> gSeen; |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 43 | |
Mike Reed | ef03848 | 2017-12-16 08:41:28 -0500 | [diff] [blame] | 44 | struct Sniffer { |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 45 | |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 46 | std::string skpName; |
| 47 | |
| 48 | Sniffer(std::string name) { |
| 49 | skpName = name; |
| 50 | } |
| 51 | |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 52 | 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; |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 60 | } |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 61 | gSeen.add(digest); |
| 62 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 63 | sk_sp<SkData> data(SkData::MakeWithoutCopy(ptr, len)); |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 64 | std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(data); |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 65 | if (!codec) { |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 66 | // 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]++; |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 70 | return; |
| 71 | } |
| 72 | SkString ext; |
| 73 | switch (codec->getEncodedFormat()) { |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 74 | 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; |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 82 | default: |
| 83 | // This should be unreachable because we cannot create a codec if we do not know |
| 84 | // the image type. |
| 85 | SkASSERT(false); |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Leon Scroggins III | da1893f | 2017-07-10 11:23:07 -0400 | [diff] [blame] | 88 | auto writeImage = [&] (const char* name, int num) { |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 89 | SkString path; |
Leon Scroggins III | da1893f | 2017-07-10 11:23:07 -0400 | [diff] [blame] | 90 | path.appendf("%s/%s%d.%s", gOutputDir, name, num, ext.c_str()); |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 91 | |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 92 | SkFILEWStream file(path.c_str()); |
| 93 | file.write(ptr, len); |
| 94 | |
| 95 | SkDebugf("%s\n", path.c_str()); |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 96 | }; |
| 97 | |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 98 | 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 III | da1893f | 2017-07-10 11:23:07 -0400 | [diff] [blame] | 104 | 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; |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 116 | } |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 117 | } |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 118 | |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 119 | if (FLAGS_writeImages) { |
Leon Scroggins III | da1893f | 2017-07-10 11:23:07 -0400 | [diff] [blame] | 120 | writeImage("", gKnown); |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 121 | } |
| 122 | |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 123 | gKnown++; |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 124 | } |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 125 | }; |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 126 | |
Leon Scroggins III | da1893f | 2017-07-10 11:23:07 -0400 | [diff] [blame] | 127 | static bool get_images_from_file(const SkString& file) { |
Leon Scroggins III | 4b0b00e | 2017-06-05 10:22:53 -0400 | [diff] [blame] | 128 | Sniffer sniff(file.c_str()); |
Mike Reed | 43f0ba0 | 2018-03-09 13:08:38 -0500 | [diff] [blame] | 129 | 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 Reed | ef03848 | 2017-12-16 08:41:28 -0500 | [diff] [blame] | 135 | }; |
| 136 | procs.fImageCtx = &sniff; |
Mike Reed | 43f0ba0 | 2018-03-09 13:08:38 -0500 | [diff] [blame] | 137 | return SkPicture::MakeFromStream(stream.get(), &procs) != nullptr; |
Leon Scroggins III | 4b0b00e | 2017-06-05 10:22:53 -0400 | [diff] [blame] | 138 | } |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 139 | |
| 140 | int main(int argc, char** argv) { |
| 141 | SkCommandLineFlags::SetUsage( |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 142 | "Usage: get_images_from_skps -s <dir of skps> -o <dir for output images> --testDecode " |
Matt Sarett | ee7c820 | 2017-06-09 10:18:34 -0400 | [diff] [blame] | 143 | "-j <output JSON path> --writeImages, --writeFailedImages\n"); |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 144 | |
| 145 | SkCommandLineFlags::Parse(argc, argv); |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 146 | const char* inputs = FLAGS_skps[0]; |
| 147 | gOutputDir = FLAGS_out[0]; |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 148 | |
Leon Scroggins III | 4b0b00e | 2017-06-05 10:22:53 -0400 | [diff] [blame] | 149 | if (!sk_isdir(gOutputDir)) { |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 150 | SkCommandLineFlags::PrintUsage(); |
| 151 | return 1; |
| 152 | } |
| 153 | |
Leon Scroggins III | 4b0b00e | 2017-06-05 10:22:53 -0400 | [diff] [blame] | 154 | if (sk_isdir(inputs)) { |
| 155 | SkOSFile::Iter iter(inputs, "skp"); |
| 156 | for (SkString file; iter.next(&file); ) { |
Leon Scroggins III | da1893f | 2017-07-10 11:23:07 -0400 | [diff] [blame] | 157 | if (!get_images_from_file(SkOSPath::Join(inputs, file.c_str()))) { |
| 158 | return 2; |
| 159 | } |
Leon Scroggins III | 4b0b00e | 2017-06-05 10:22:53 -0400 | [diff] [blame] | 160 | } |
| 161 | } else { |
Leon Scroggins III | da1893f | 2017-07-10 11:23:07 -0400 | [diff] [blame] | 162 | if (!get_images_from_file(SkString(inputs))) { |
| 163 | return 2; |
| 164 | } |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 165 | } |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 166 | /** |
| 167 | JSON results are written out in the following format: |
| 168 | { |
| 169 | "failures": { |
| 170 | "skp1": 12, |
| 171 | "skp4": 2, |
| 172 | ... |
| 173 | }, |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 174 | "unsupported": { |
| 175 | "skp9": 13, |
| 176 | "skp17": 3, |
| 177 | ... |
| 178 | } |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 179 | "totalFailures": 32, |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 180 | "totalUnsupported": 9, |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 181 | "totalSuccesses": 21, |
| 182 | } |
| 183 | */ |
Florin Malita | 3425e22 | 2018-06-19 10:52:20 -0400 | [diff] [blame] | 184 | |
| 185 | unsigned int totalFailures = 0, |
| 186 | totalUnsupported = 0; |
| 187 | SkDynamicMemoryWStream memStream; |
| 188 | SkJSONWriter writer(&memStream, SkJSONWriter::Mode::kPretty); |
| 189 | writer.beginObject(); |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 190 | { |
Florin Malita | 3425e22 | 2018-06-19 10:52:20 -0400 | [diff] [blame] | 191 | 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 | |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 202 | #ifdef SK_DEBUG |
Florin Malita | 3425e22 | 2018-06-19 10:52:20 -0400 | [diff] [blame] | 203 | 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); |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 214 | #endif |
Florin Malita | 3425e22 | 2018-06-19 10:52:20 -0400 | [diff] [blame] | 215 | |
| 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 | |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 223 | if (totalFailures > 0 || totalUnsupported > 0) { |
rmistry | cda08f8 | 2016-04-26 08:27:49 -0700 | [diff] [blame] | 224 | if (!FLAGS_failuresJsonPath.isEmpty()) { |
| 225 | SkDebugf("Writing failures to %s\n", FLAGS_failuresJsonPath[0]); |
| 226 | SkFILEWStream stream(FLAGS_failuresJsonPath[0]); |
Florin Malita | 3425e22 | 2018-06-19 10:52:20 -0400 | [diff] [blame] | 227 | auto jsonStream = memStream.detachAsStream(); |
| 228 | stream.writeStream(jsonStream.get(), jsonStream->getLength()); |
rmistry | cda08f8 | 2016-04-26 08:27:49 -0700 | [diff] [blame] | 229 | } |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 230 | } |
Florin Malita | 3425e22 | 2018-06-19 10:52:20 -0400 | [diff] [blame] | 231 | |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 232 | return 0; |
| 233 | } |