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" |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 13 | #include "SkJSONCPP.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" |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 18 | #include "SkPixelSerializer.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 | |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 26 | DEFINE_string2(skps, s, "skps", "A path to a directory of skps."); |
| 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."); |
| 33 | DEFINE_bool(testICCSupport, false, |
| 34 | "Indicates if we want to test that the images with ICC profiles are supported"); |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 35 | DEFINE_string2(failuresJsonPath, j, "", |
| 36 | "Dump SKP and count of unknown images to the specified JSON file. Will not be " |
| 37 | "written anywhere if empty."); |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 38 | |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 39 | static int gKnown; |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 40 | static const char* gOutputDir; |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 41 | static std::map<std::string, unsigned int> gSkpToUnknownCount = {}; |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 42 | static std::map<std::string, unsigned int> gSkpToUnsupportedCount; |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 43 | |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 44 | static SkTHashSet<SkMD5::Digest> gSeen; |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 45 | |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 46 | struct Sniffer : public SkPixelSerializer { |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 47 | |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 48 | std::string skpName; |
| 49 | |
| 50 | Sniffer(std::string name) { |
| 51 | skpName = name; |
| 52 | } |
| 53 | |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 54 | 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; |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 62 | } |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 63 | gSeen.add(digest); |
| 64 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 65 | sk_sp<SkData> data(SkData::MakeWithoutCopy(ptr, len)); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 66 | std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data)); |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 67 | if (!codec) { |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 68 | // 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]++; |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 72 | return; |
| 73 | } |
| 74 | SkString ext; |
| 75 | switch (codec->getEncodedFormat()) { |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 76 | 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; |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 84 | default: |
| 85 | // This should be unreachable because we cannot create a codec if we do not know |
| 86 | // the image type. |
| 87 | SkASSERT(false); |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 88 | } |
| 89 | |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 90 | auto writeImage = [&] { |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 91 | SkString path; |
| 92 | path.appendf("%s/%d.%s", gOutputDir, gKnown, ext.c_str()); |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 93 | |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 94 | SkFILEWStream file(path.c_str()); |
| 95 | file.write(ptr, len); |
| 96 | |
| 97 | SkDebugf("%s\n", path.c_str()); |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 98 | }; |
| 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 | } |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 115 | } |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 116 | |
| 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 | |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 135 | gKnown++; |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 136 | } |
| 137 | |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 138 | bool onUseEncodedData(const void* ptr, size_t len) override { |
| 139 | this->sniff(ptr, len); |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 140 | return true; |
| 141 | } |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 142 | SkData* onEncode(const SkPixmap&) override { return nullptr; } |
| 143 | }; |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 144 | |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 145 | |
| 146 | int main(int argc, char** argv) { |
| 147 | SkCommandLineFlags::SetUsage( |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 148 | "Usage: get_images_from_skps -s <dir of skps> -o <dir for output images> --testDecode " |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 149 | "-j <output JSON path> --testICCSupport --writeImages, --writeFailedImages\n"); |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 150 | |
| 151 | SkCommandLineFlags::Parse(argc, argv); |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 152 | const char* inputs = FLAGS_skps[0]; |
| 153 | gOutputDir = FLAGS_out[0]; |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 154 | |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 155 | if (!sk_isdir(inputs) || !sk_isdir(gOutputDir)) { |
| 156 | SkCommandLineFlags::PrintUsage(); |
| 157 | return 1; |
| 158 | } |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 159 | #ifndef SK_DEBUG |
| 160 | if (FLAGS_testICCSupport) { |
| 161 | std::cerr << "--testICCSupport unavailable outside of SK_DEBUG builds" << std::endl; |
| 162 | return 1; |
| 163 | } |
| 164 | #endif |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 165 | |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 166 | SkOSFile::Iter iter(inputs, "skp"); |
| 167 | for (SkString file; iter.next(&file); ) { |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 168 | std::unique_ptr<SkStream> stream = |
| 169 | SkStream::MakeFromFile(SkOSPath::Join(inputs, file.c_str()).c_str()); |
| 170 | sk_sp<SkPicture> picture(SkPicture::MakeFromStream(stream.get())); |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 171 | |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 172 | SkDynamicMemoryWStream scratch; |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 173 | Sniffer sniff(file.c_str()); |
mtklein | 2d225e3 | 2016-02-29 09:05:32 -0800 | [diff] [blame] | 174 | picture->serialize(&scratch, &sniff); |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 175 | } |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 176 | /** |
| 177 | JSON results are written out in the following format: |
| 178 | { |
| 179 | "failures": { |
| 180 | "skp1": 12, |
| 181 | "skp4": 2, |
| 182 | ... |
| 183 | }, |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 184 | "unsupported": { |
| 185 | "skp9": 13, |
| 186 | "skp17": 3, |
| 187 | ... |
| 188 | } |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 189 | "totalFailures": 32, |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 190 | "totalUnsupported": 9, |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 191 | "totalSuccesses": 21, |
| 192 | } |
| 193 | */ |
| 194 | Json::Value fRoot; |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 195 | int totalFailures = 0; |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 196 | for(auto it = gSkpToUnknownCount.cbegin(); it != gSkpToUnknownCount.cend(); ++it) |
| 197 | { |
| 198 | SkDebugf("%s %d\n", it->first.c_str(), it->second); |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 199 | totalFailures += it->second; |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 200 | fRoot["failures"][it->first.c_str()] = it->second; |
| 201 | } |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 202 | fRoot["totalFailures"] = totalFailures; |
| 203 | int totalUnsupported = 0; |
| 204 | #ifdef SK_DEBUG |
| 205 | for (const auto& unsupported : gSkpToUnsupportedCount) { |
| 206 | SkDebugf("%s %d\n", unsupported.first.c_str(), unsupported.second); |
| 207 | totalUnsupported += unsupported.second; |
| 208 | fRoot["unsupported"][unsupported.first] = unsupported.second; |
| 209 | } |
| 210 | fRoot["totalUnsupported"] = totalUnsupported; |
| 211 | #endif |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 212 | fRoot["totalSuccesses"] = gKnown; |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 213 | SkDebugf("%d known, %d failures, %d unsupported\n", gKnown, totalFailures, totalUnsupported); |
| 214 | if (totalFailures > 0 || totalUnsupported > 0) { |
rmistry | cda08f8 | 2016-04-26 08:27:49 -0700 | [diff] [blame] | 215 | if (!FLAGS_failuresJsonPath.isEmpty()) { |
| 216 | SkDebugf("Writing failures to %s\n", FLAGS_failuresJsonPath[0]); |
| 217 | SkFILEWStream stream(FLAGS_failuresJsonPath[0]); |
| 218 | stream.writeText(Json::StyledWriter().write(fRoot).c_str()); |
| 219 | stream.flush(); |
| 220 | } |
| 221 | return -1; |
rmistry | 8d965a6 | 2016-04-25 10:35:03 -0700 | [diff] [blame] | 222 | } |
msarett | 3478f75 | 2016-02-12 14:47:09 -0800 | [diff] [blame] | 223 | return 0; |
| 224 | } |