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