OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkCanvas.h" | |
9 #include "SkCodec.h" | 8 #include "SkCodec.h" |
10 #include "SkCommandLineFlags.h" | 9 #include "SkCommandLineFlags.h" |
11 #include "SkData.h" | 10 #include "SkData.h" |
| 11 #include "SkMD5.h" |
12 #include "SkOSFile.h" | 12 #include "SkOSFile.h" |
13 #include "SkPicture.h" | 13 #include "SkPicture.h" |
| 14 #include "SkPixelSerializer.h" |
14 #include "SkStream.h" | 15 #include "SkStream.h" |
| 16 #include "SkTHash.h" |
15 | 17 |
16 DEFINE_string2(skps, s, "", "A path to a directory of skps."); | 18 DEFINE_string2(skps, s, "skps", "A path to a directory of skps."); |
17 DEFINE_string2(out, o, "", "A path to an output directory."); | 19 DEFINE_string2(out, o, "img-out", "A path to an output directory."); |
18 | 20 |
19 static int gCtr = 0; | 21 static int gKnown; |
20 static int gSuccessCtr = 0; | 22 static int gUnknown; |
21 static int gUnknownCtr = 0; | |
22 static int gFailureCtr = 0; | |
23 static const char* gOutputDir; | 23 static const char* gOutputDir; |
24 | 24 |
25 void setup_output_dirs() { | 25 static SkTHashSet<SkMD5::Digest> gSeen; |
26 const char* exts[] = { "jpg", "png", "gif", "webp", "bmp", "wbmp", "ico", "d
ng", "unknown" }; | |
27 for (const char* ext : exts) { | |
28 sk_mkdir(SkOSPath::Join(gOutputDir, ext).c_str()); | |
29 } | |
30 } | |
31 | 26 |
32 bool store_encoded_to_file(const void* encoded, size_t length, SkBitmap* bitmap)
{ | 27 struct Sniffer : public SkPixelSerializer { |
33 // Silence warnings about empty bitmaps. | |
34 bitmap->allocN32Pixels(1, 1, true); | |
35 | 28 |
36 SkString path; | 29 void sniff(const void* ptr, size_t len) { |
37 SkAutoTUnref<SkData> data(SkData::NewWithoutCopy(encoded, length)); | 30 SkMD5 md5; |
38 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data)); | 31 md5.write(ptr, len); |
39 if (codec) { | 32 SkMD5::Digest digest; |
| 33 md5.finish(digest); |
| 34 |
| 35 if (gSeen.contains(digest)) { |
| 36 return; |
| 37 } |
| 38 gSeen.add(digest); |
| 39 |
| 40 SkAutoTUnref<SkData> data(SkData::NewWithoutCopy(ptr, len)); |
| 41 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data)); |
| 42 if (!codec) { |
| 43 gUnknown++; |
| 44 return; |
| 45 } |
| 46 SkString ext; |
40 switch (codec->getEncodedFormat()) { | 47 switch (codec->getEncodedFormat()) { |
41 case SkEncodedFormat::kJPEG_SkEncodedFormat: | 48 case SkEncodedFormat::kBMP_SkEncodedFormat: ext = "bmp"; break; |
42 path = SkOSPath::Join(SkOSPath::Join(gOutputDir, "jpg").c_str(),
""); | 49 case SkEncodedFormat::kGIF_SkEncodedFormat: ext = "gif"; break; |
43 path.appendS32(gCtr++); | 50 case SkEncodedFormat::kICO_SkEncodedFormat: ext = "ico"; break; |
44 path.append(".jpg"); | 51 case SkEncodedFormat::kJPEG_SkEncodedFormat: ext = "jpg"; break; |
45 break; | 52 case SkEncodedFormat::kPNG_SkEncodedFormat: ext = "png"; break; |
46 case SkEncodedFormat::kPNG_SkEncodedFormat: | 53 case SkEncodedFormat::kRAW_SkEncodedFormat: ext = "dng"; break; |
47 path = SkOSPath::Join(SkOSPath::Join(gOutputDir, "png").c_str(),
""); | 54 case SkEncodedFormat::kWBMP_SkEncodedFormat: ext = "wbmp"; break; |
48 path.appendS32(gCtr++); | 55 case SkEncodedFormat::kWEBP_SkEncodedFormat: ext = "webp"; break; |
49 path.append(".png"); | 56 default: gUnknown++; return; |
50 break; | |
51 case SkEncodedFormat::kGIF_SkEncodedFormat: | |
52 path = SkOSPath::Join(SkOSPath::Join(gOutputDir, "gif").c_str(),
""); | |
53 path.appendS32(gCtr++); | |
54 path.append(".gif"); | |
55 break; | |
56 case SkEncodedFormat::kWEBP_SkEncodedFormat: | |
57 path = SkOSPath::Join(SkOSPath::Join(gOutputDir, "webp").c_str()
, ""); | |
58 path.appendS32(gCtr++); | |
59 path.append(".webp"); | |
60 break; | |
61 case SkEncodedFormat::kBMP_SkEncodedFormat: | |
62 path = SkOSPath::Join(SkOSPath::Join(gOutputDir, "bmp").c_str(),
""); | |
63 path.appendS32(gCtr++); | |
64 path.append(".bmp"); | |
65 break; | |
66 case SkEncodedFormat::kWBMP_SkEncodedFormat: | |
67 path = SkOSPath::Join(SkOSPath::Join(gOutputDir, "wbmp").c_str()
, ""); | |
68 path.appendS32(gCtr++); | |
69 path.append(".wbmp"); | |
70 break; | |
71 case SkEncodedFormat::kICO_SkEncodedFormat: | |
72 path = SkOSPath::Join(SkOSPath::Join(gOutputDir, "ico").c_str(),
""); | |
73 path.appendS32(gCtr++); | |
74 path.append(".ico"); | |
75 break; | |
76 case SkEncodedFormat::kRAW_SkEncodedFormat: | |
77 path = SkOSPath::Join(SkOSPath::Join(gOutputDir, "dng").c_str(),
""); | |
78 path.appendS32(gCtr++); | |
79 path.append(".dng"); | |
80 break; | |
81 default: | |
82 path = SkOSPath::Join(gOutputDir, "unknown"); | |
83 path.appendS32(gUnknownCtr++); | |
84 break; | |
85 } | 57 } |
86 } else { | 58 |
87 path = SkOSPath::Join(gOutputDir, "unknown"); | 59 SkString path; |
88 path.appendS32(gUnknownCtr++); | 60 path.appendf("%s/%d.%s", gOutputDir, gKnown++, ext.c_str()); |
| 61 |
| 62 SkFILEWStream file(path.c_str()); |
| 63 file.write(ptr, len); |
| 64 |
| 65 SkDebugf("%s\n", path.c_str()); |
89 } | 66 } |
90 | 67 |
91 FILE* file = sk_fopen(path.c_str(), kWrite_SkFILE_Flag); | 68 bool onUseEncodedData(const void* ptr, size_t len) override { |
92 if (file) { | 69 this->sniff(ptr, len); |
93 sk_fwrite(encoded, length, file); | |
94 sk_fclose(file); | |
95 gSuccessCtr++; | |
96 return true; | 70 return true; |
97 } | 71 } |
| 72 SkData* onEncode(const SkPixmap&) override { return nullptr; } |
| 73 }; |
98 | 74 |
99 gFailureCtr++; | |
100 SkDebugf("Could not open %s\n", path.c_str()); | |
101 return false; | |
102 } | |
103 | 75 |
104 int main(int argc, char** argv) { | 76 int main(int argc, char** argv) { |
105 SkCommandLineFlags::SetUsage( | 77 SkCommandLineFlags::SetUsage( |
106 "Usage: get_images_from_skps -s <dir of skps> -o <dir for output ima
ges>\n"); | 78 "Usage: get_images_from_skps -s <dir of skps> -o <dir for output ima
ges>\n"); |
107 | 79 |
108 SkCommandLineFlags::Parse(argc, argv); | 80 SkCommandLineFlags::Parse(argc, argv); |
109 if (FLAGS_skps.isEmpty() || FLAGS_out.isEmpty()) { | |
110 SkCommandLineFlags::PrintUsage(); | |
111 return 1; | |
112 } | |
113 | |
114 const char* inputs = FLAGS_skps[0]; | 81 const char* inputs = FLAGS_skps[0]; |
115 gOutputDir = FLAGS_out[0]; | 82 gOutputDir = FLAGS_out[0]; |
| 83 |
116 if (!sk_isdir(inputs) || !sk_isdir(gOutputDir)) { | 84 if (!sk_isdir(inputs) || !sk_isdir(gOutputDir)) { |
117 SkCommandLineFlags::PrintUsage(); | 85 SkCommandLineFlags::PrintUsage(); |
118 return 1; | 86 return 1; |
119 } | 87 } |
120 | 88 |
121 setup_output_dirs(); | |
122 SkOSFile::Iter iter(inputs, "skp"); | 89 SkOSFile::Iter iter(inputs, "skp"); |
123 for (SkString file; iter.next(&file); ) { | 90 for (SkString file; iter.next(&file); ) { |
124 SkAutoTDelete<SkStream> stream = | 91 SkAutoTDelete<SkStream> stream = |
125 SkStream::NewFromFile(SkOSPath::Join(inputs, file.c_str()).c_str
()); | 92 SkStream::NewFromFile(SkOSPath::Join(inputs, file.c_str()).c_str
()); |
| 93 SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(stream)); |
126 | 94 |
127 // Rather than passing in a function that actually decodes the encoded d
ata, | 95 SkDynamicMemoryWStream scratch; |
128 // we pass in a function that saves the encoded data to a file. | 96 Sniffer sniff; |
129 SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(stream, stor
e_encoded_to_file)); | 97 picture->serialize(&scratch, &sniff); |
| 98 } |
| 99 SkDebugf("%d known, %d unknown\n", gKnown, gUnknown); |
130 | 100 |
131 SkCanvas canvas; | |
132 canvas.drawPicture(picture); | |
133 } | |
134 | |
135 SkDebugf("Successfully saved %d recognized images and %d unrecognized images
\n", gSuccessCtr, | |
136 gUnknownCtr); | |
137 SkDebugf("Failed to write %d images\n", gFailureCtr); | |
138 return 0; | 101 return 0; |
139 } | 102 } |
OLD | NEW |