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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/codec/SkCodec.h" |
| 9 | #include "include/core/SkBitmap.h" |
| 10 | #include "include/core/SkColorSpace.h" |
| 11 | #include "include/core/SkData.h" |
| 12 | #include "include/core/SkPicture.h" |
| 13 | #include "include/core/SkSerialProcs.h" |
| 14 | #include "include/core/SkStream.h" |
| 15 | #include "include/private/SkTHash.h" |
| 16 | #include "src/core/SkMD5.h" |
| 17 | #include "src/core/SkOSFile.h" |
| 18 | #include "src/utils/SkJSONWriter.h" |
| 19 | #include "src/utils/SkOSPath.h" |
| 20 | #include "tools/flags/CommandLineFlags.h" |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 21 | |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 22 | #include <iostream> |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 23 | #include <map> |
| 24 | |
Mike Klein | 84836b7 | 2019-03-21 11:31:36 -0500 | [diff] [blame] | 25 | static DEFINE_string2(skps, s, "skps", "A path to a directory of skps or a single skp."); |
| 26 | static DEFINE_string2(out, o, "img-out", "A path to an output directory."); |
| 27 | static DEFINE_bool(testDecode, false, |
| 28 | "Indicates if we want to test that the images decode successfully."); |
| 29 | static DEFINE_bool(writeImages, true, |
| 30 | "Indicates if we want to write out supported/decoded images."); |
| 31 | static DEFINE_bool(writeFailedImages, false, |
| 32 | "Indicates if we want to write out unsupported/failed to decode images."); |
| 33 | static DEFINE_string2(failuresJsonPath, j, "", |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 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); |
Hal Canary | 0f2f522 | 2019-04-03 10:13:45 -0400 | [diff] [blame] | 55 | SkMD5::Digest digest = md5.finish(); |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 56 | |
| 57 | if (gSeen.contains(digest)) { |
| 58 | return; |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 59 | } |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 60 | gSeen.add(digest); |
| 61 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 62 | sk_sp<SkData> data(SkData::MakeWithoutCopy(ptr, len)); |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 63 | std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(data); |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 64 | if (!codec) { |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 65 | // FIXME: This code is currently unreachable because we create an empty generator when |
| 66 | // we fail to create a codec. |
| 67 | SkDebugf("Codec could not be created for %s\n", skpName.c_str()); |
| 68 | gSkpToUnknownCount[skpName]++; |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 69 | return; |
| 70 | } |
| 71 | SkString ext; |
| 72 | switch (codec->getEncodedFormat()) { |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 73 | case SkEncodedImageFormat::kBMP: ext = "bmp"; break; |
| 74 | case SkEncodedImageFormat::kGIF: ext = "gif"; break; |
| 75 | case SkEncodedImageFormat::kICO: ext = "ico"; break; |
| 76 | case SkEncodedImageFormat::kJPEG: ext = "jpg"; break; |
| 77 | case SkEncodedImageFormat::kPNG: ext = "png"; break; |
| 78 | case SkEncodedImageFormat::kDNG: ext = "dng"; break; |
| 79 | case SkEncodedImageFormat::kWBMP: ext = "wbmp"; break; |
| 80 | case SkEncodedImageFormat::kWEBP: ext = "webp"; break; |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 81 | default: |
| 82 | // This should be unreachable because we cannot create a codec if we do not know |
| 83 | // the image type. |
| 84 | SkASSERT(false); |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Leon Scroggins III | da1893f | 2017-07-10 11:23:07 -0400 | [diff] [blame] | 87 | auto writeImage = [&] (const char* name, int num) { |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 88 | SkString path; |
Leon Scroggins III | da1893f | 2017-07-10 11:23:07 -0400 | [diff] [blame] | 89 | path.appendf("%s/%s%d.%s", gOutputDir, name, num, ext.c_str()); |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 90 | |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 91 | SkFILEWStream file(path.c_str()); |
| 92 | file.write(ptr, len); |
| 93 | |
| 94 | SkDebugf("%s\n", path.c_str()); |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 95 | }; |
| 96 | |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 97 | if (FLAGS_testDecode) { |
| 98 | SkBitmap bitmap; |
| 99 | SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType); |
| 100 | bitmap.allocPixels(info); |
| 101 | const SkCodec::Result result = codec->getPixels( |
| 102 | info, bitmap.getPixels(), bitmap.rowBytes()); |
Leon Scroggins III | da1893f | 2017-07-10 11:23:07 -0400 | [diff] [blame] | 103 | switch (result) { |
| 104 | case SkCodec::kSuccess: |
| 105 | case SkCodec::kIncompleteInput: |
| 106 | case SkCodec::kErrorInInput: |
| 107 | break; |
| 108 | default: |
| 109 | SkDebugf("Decoding failed for %s\n", skpName.c_str()); |
| 110 | if (FLAGS_writeFailedImages) { |
| 111 | writeImage("unknown", gSkpToUnknownCount[skpName]); |
| 112 | } |
| 113 | gSkpToUnknownCount[skpName]++; |
| 114 | return; |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 115 | } |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 116 | } |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 117 | |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 118 | if (FLAGS_writeImages) { |
Leon Scroggins III | da1893f | 2017-07-10 11:23:07 -0400 | [diff] [blame] | 119 | writeImage("", gKnown); |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 120 | } |
| 121 | |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 122 | gKnown++; |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 123 | } |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 124 | }; |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 125 | |
Leon Scroggins III | da1893f | 2017-07-10 11:23:07 -0400 | [diff] [blame] | 126 | static bool get_images_from_file(const SkString& file) { |
Leon Scroggins III | 4b0b00e | 2017-06-05 10:22:53 -0400 | [diff] [blame] | 127 | Sniffer sniff(file.c_str()); |
Mike Reed | 43f0ba0 | 2018-03-09 13:08:38 -0500 | [diff] [blame] | 128 | auto stream = SkStream::MakeFromFile(file.c_str()); |
| 129 | |
| 130 | SkDeserialProcs procs; |
| 131 | procs.fImageProc = [](const void* data, size_t size, void* ctx) -> sk_sp<SkImage> { |
| 132 | ((Sniffer*)ctx)->sniff(data, size); |
| 133 | return nullptr; |
Mike Reed | ef03848 | 2017-12-16 08:41:28 -0500 | [diff] [blame] | 134 | }; |
| 135 | procs.fImageCtx = &sniff; |
Mike Reed | 43f0ba0 | 2018-03-09 13:08:38 -0500 | [diff] [blame] | 136 | return SkPicture::MakeFromStream(stream.get(), &procs) != nullptr; |
Leon Scroggins III | 4b0b00e | 2017-06-05 10:22:53 -0400 | [diff] [blame] | 137 | } |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 138 | |
| 139 | int main(int argc, char** argv) { |
Mike Klein | 88544fb | 2019-03-20 10:50:33 -0500 | [diff] [blame] | 140 | CommandLineFlags::SetUsage( |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 141 | "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] | 142 | "-j <output JSON path> --writeImages, --writeFailedImages\n"); |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 143 | |
Mike Klein | 88544fb | 2019-03-20 10:50:33 -0500 | [diff] [blame] | 144 | CommandLineFlags::Parse(argc, argv); |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 145 | const char* inputs = FLAGS_skps[0]; |
| 146 | gOutputDir = FLAGS_out[0]; |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 147 | |
Leon Scroggins III | 4b0b00e | 2017-06-05 10:22:53 -0400 | [diff] [blame] | 148 | if (!sk_isdir(gOutputDir)) { |
Mike Klein | 88544fb | 2019-03-20 10:50:33 -0500 | [diff] [blame] | 149 | CommandLineFlags::PrintUsage(); |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 150 | return 1; |
| 151 | } |
| 152 | |
Leon Scroggins III | 4b0b00e | 2017-06-05 10:22:53 -0400 | [diff] [blame] | 153 | if (sk_isdir(inputs)) { |
| 154 | SkOSFile::Iter iter(inputs, "skp"); |
| 155 | for (SkString file; iter.next(&file); ) { |
Leon Scroggins III | da1893f | 2017-07-10 11:23:07 -0400 | [diff] [blame] | 156 | if (!get_images_from_file(SkOSPath::Join(inputs, file.c_str()))) { |
| 157 | return 2; |
| 158 | } |
Leon Scroggins III | 4b0b00e | 2017-06-05 10:22:53 -0400 | [diff] [blame] | 159 | } |
| 160 | } else { |
Leon Scroggins III | da1893f | 2017-07-10 11:23:07 -0400 | [diff] [blame] | 161 | if (!get_images_from_file(SkString(inputs))) { |
| 162 | return 2; |
| 163 | } |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 164 | } |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 165 | /** |
| 166 | JSON results are written out in the following format: |
| 167 | { |
| 168 | "failures": { |
| 169 | "skp1": 12, |
| 170 | "skp4": 2, |
| 171 | ... |
| 172 | }, |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 173 | "unsupported": { |
| 174 | "skp9": 13, |
| 175 | "skp17": 3, |
| 176 | ... |
| 177 | } |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 178 | "totalFailures": 32, |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 179 | "totalUnsupported": 9, |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 180 | "totalSuccesses": 21, |
| 181 | } |
| 182 | */ |
Florin Malita | 3425e22 | 2018-06-19 10:52:20 -0400 | [diff] [blame] | 183 | |
| 184 | unsigned int totalFailures = 0, |
| 185 | totalUnsupported = 0; |
| 186 | SkDynamicMemoryWStream memStream; |
| 187 | SkJSONWriter writer(&memStream, SkJSONWriter::Mode::kPretty); |
| 188 | writer.beginObject(); |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 189 | { |
Florin Malita | 3425e22 | 2018-06-19 10:52:20 -0400 | [diff] [blame] | 190 | writer.beginObject("failures"); |
| 191 | { |
| 192 | for(const auto& failure : gSkpToUnknownCount) { |
| 193 | SkDebugf("%s %d\n", failure.first.c_str(), failure.second); |
| 194 | totalFailures += failure.second; |
| 195 | writer.appendU32(failure.first.c_str(), failure.second); |
| 196 | } |
| 197 | } |
| 198 | writer.endObject(); |
| 199 | writer.appendU32("totalFailures", totalFailures); |
| 200 | |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 201 | #ifdef SK_DEBUG |
Florin Malita | 3425e22 | 2018-06-19 10:52:20 -0400 | [diff] [blame] | 202 | writer.beginObject("unsupported"); |
| 203 | { |
| 204 | for (const auto& unsupported : gSkpToUnsupportedCount) { |
| 205 | SkDebugf("%s %d\n", unsupported.first.c_str(), unsupported.second); |
| 206 | totalUnsupported += unsupported.second; |
| 207 | writer.appendHexU32(unsupported.first.c_str(), unsupported.second); |
| 208 | } |
| 209 | |
| 210 | } |
| 211 | writer.endObject(); |
| 212 | writer.appendU32("totalUnsupported", totalUnsupported); |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 213 | #endif |
Florin Malita | 3425e22 | 2018-06-19 10:52:20 -0400 | [diff] [blame] | 214 | |
| 215 | writer.appendS32("totalSuccesses", gKnown); |
| 216 | SkDebugf("%d known, %d failures, %d unsupported\n", |
| 217 | gKnown, totalFailures, totalUnsupported); |
| 218 | } |
| 219 | writer.endObject(); |
| 220 | writer.flush(); |
| 221 | |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 222 | if (totalFailures > 0 || totalUnsupported > 0) { |
rmistry | cda08f8 | 2016-04-26 08:27:49 -0700 | [diff] [blame] | 223 | if (!FLAGS_failuresJsonPath.isEmpty()) { |
| 224 | SkDebugf("Writing failures to %s\n", FLAGS_failuresJsonPath[0]); |
| 225 | SkFILEWStream stream(FLAGS_failuresJsonPath[0]); |
Florin Malita | 3425e22 | 2018-06-19 10:52:20 -0400 | [diff] [blame] | 226 | auto jsonStream = memStream.detachAsStream(); |
| 227 | stream.writeStream(jsonStream.get(), jsonStream->getLength()); |
rmistry | cda08f8 | 2016-04-26 08:27:49 -0700 | [diff] [blame] | 228 | } |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 229 | } |
Florin Malita | 3425e22 | 2018-06-19 10:52:20 -0400 | [diff] [blame] | 230 | |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 231 | return 0; |
| 232 | } |