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" |
| 10 | #include "SkCommandLineFlags.h" |
| 11 | #include "SkData.h" |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 12 | #include "SkJSONCPP.h" |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 13 | #include "SkMD5.h" |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 14 | #include "SkOSFile.h" |
| 15 | #include "SkPicture.h" |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 16 | #include "SkPixelSerializer.h" |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 17 | #include "SkStream.h" |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 18 | #include "SkTHash.h" |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 19 | |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 20 | |
| 21 | #include <map> |
| 22 | |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 23 | DEFINE_string2(skps, s, "skps", "A path to a directory of skps."); |
| 24 | DEFINE_string2(out, o, "img-out", "A path to an output directory."); |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 25 | DEFINE_bool(testDecode, false, "Indicates if we want to test that the images decode successfully."); |
| 26 | DEFINE_bool(writeImages, true, "Indicates if we want to write out images."); |
| 27 | DEFINE_string2(failuresJsonPath, j, "", |
| 28 | "Dump SKP and count of unknown images to the specified JSON file. Will not be " |
| 29 | "written anywhere if empty."); |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 30 | |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 31 | static int gKnown; |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 32 | static const char* gOutputDir; |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 33 | static std::map<std::string, unsigned int> gSkpToUnknownCount = {}; |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 34 | |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 35 | static SkTHashSet<SkMD5::Digest> gSeen; |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 36 | |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 37 | struct Sniffer : public SkPixelSerializer { |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 38 | |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 39 | std::string skpName; |
| 40 | |
| 41 | Sniffer(std::string name) { |
| 42 | skpName = name; |
| 43 | } |
| 44 | |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 45 | 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; |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 53 | } |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 54 | gSeen.add(digest); |
| 55 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 56 | sk_sp<SkData> data(SkData::MakeWithoutCopy(ptr, len)); |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame^] | 57 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data)); |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 58 | if (!codec) { |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 59 | // 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]++; |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 63 | 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; |
yujieqin | 7a307df | 2016-03-11 07:32:33 -0800 | [diff] [blame] | 72 | case SkEncodedFormat::kDNG_SkEncodedFormat: ext = "dng"; break; |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 73 | case SkEncodedFormat::kWBMP_SkEncodedFormat: ext = "wbmp"; break; |
| 74 | case SkEncodedFormat::kWEBP_SkEncodedFormat: ext = "webp"; break; |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 75 | default: |
| 76 | // This should be unreachable because we cannot create a codec if we do not know |
| 77 | // the image type. |
| 78 | SkASSERT(false); |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 79 | } |
| 80 | |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 81 | if (FLAGS_testDecode) { |
| 82 | SkBitmap bitmap; |
| 83 | SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType); |
| 84 | bitmap.allocPixels(info); |
rmistry | 6f47dba | 2016-04-27 07:31:23 -0700 | [diff] [blame] | 85 | const SkCodec::Result result = codec->getPixels( |
| 86 | info, bitmap.getPixels(), bitmap.rowBytes()); |
| 87 | if (SkCodec::kIncompleteInput != result && SkCodec::kSuccess != result) |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 88 | { |
| 89 | SkDebugf("Decoding failed for %s\n", skpName.c_str()); |
| 90 | gSkpToUnknownCount[skpName]++; |
| 91 | return; |
| 92 | } |
| 93 | } |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 94 | |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 95 | if (FLAGS_writeImages) { |
| 96 | SkString path; |
| 97 | path.appendf("%s/%d.%s", gOutputDir, gKnown, ext.c_str()); |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 98 | |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 99 | SkFILEWStream file(path.c_str()); |
| 100 | file.write(ptr, len); |
| 101 | |
| 102 | SkDebugf("%s\n", path.c_str()); |
| 103 | } |
| 104 | gKnown++; |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 105 | } |
| 106 | |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 107 | bool onUseEncodedData(const void* ptr, size_t len) override { |
| 108 | this->sniff(ptr, len); |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 109 | return true; |
| 110 | } |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 111 | SkData* onEncode(const SkPixmap&) override { return nullptr; } |
| 112 | }; |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 113 | |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 114 | |
| 115 | int main(int argc, char** argv) { |
| 116 | SkCommandLineFlags::SetUsage( |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 117 | "Usage: get_images_from_skps -s <dir of skps> -o <dir for output images> --testDecode " |
| 118 | "-j <output JSON path>\n"); |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 119 | |
| 120 | SkCommandLineFlags::Parse(argc, argv); |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 121 | const char* inputs = FLAGS_skps[0]; |
| 122 | gOutputDir = FLAGS_out[0]; |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 123 | |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 124 | if (!sk_isdir(inputs) || !sk_isdir(gOutputDir)) { |
| 125 | SkCommandLineFlags::PrintUsage(); |
| 126 | return 1; |
| 127 | } |
| 128 | |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 129 | SkOSFile::Iter iter(inputs, "skp"); |
| 130 | for (SkString file; iter.next(&file); ) { |
| 131 | SkAutoTDelete<SkStream> stream = |
| 132 | SkStream::NewFromFile(SkOSPath::Join(inputs, file.c_str()).c_str()); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 133 | sk_sp<SkPicture> picture(SkPicture::MakeFromStream(stream)); |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 134 | |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 135 | SkDynamicMemoryWStream scratch; |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 136 | Sniffer sniff(file.c_str()); |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 137 | picture->serialize(&scratch, &sniff); |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 138 | } |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 139 | 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; |
rmistry | cda08f8 | 2016-04-26 08:27:49 -0700 | [diff] [blame] | 162 | 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; |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 170 | } |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 171 | return 0; |
| 172 | } |