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 const char* gOutputDir; | |
22 | |
23 static void printUsage() { | |
24 SkDebugf("Usage: get_images_from_skps -s <dir of skps> -o <dir for output im ages>\n"); | |
scroggo
2016/02/12 17:18:38
SkCommandLineFlags has a method to specify usage (
msarett
2016/02/12 21:30:29
Done.
| |
25 } | |
26 | |
27 void setup_output_dirs() { | |
28 const char* exts[] = { "jpg", "png", "gif", "webp", "bmp", "wbmp", "ico", "d ng", "unknown" }; | |
29 for (const char* ext : exts) { | |
30 SkString path(gOutputDir); | |
31 path.append("/"); | |
scroggo
2016/02/12 17:18:38
Should these use SkOSPath::Join so that it will wo
msarett
2016/02/12 21:30:29
Done.
| |
32 path.append(ext); | |
33 sk_mkdir(path.c_str()); | |
34 } | |
35 } | |
36 | |
37 bool store_encoded_to_file(const void* encoded, size_t length, SkBitmap* bitmap) { | |
38 // Silence warnings about empty bitmaps. | |
39 bitmap->allocN32Pixels(1, 1, true); | |
40 | |
41 SkString path(gOutputDir); | |
42 SkAutoTUnref<SkData> data(SkData::NewWithoutCopy(encoded, length)); | |
43 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data)); | |
44 if (codec) { | |
45 switch (codec->getEncodedFormat()) { | |
46 case SkEncodedFormat::kJPEG_SkEncodedFormat: | |
47 path.append("/jpg/"); | |
48 path.appendS32(gCtr++); | |
49 path.append(".jpg"); | |
50 break; | |
51 case SkEncodedFormat::kPNG_SkEncodedFormat: | |
52 path.append("/png/"); | |
53 path.appendS32(gCtr++); | |
54 path.append(".png"); | |
55 break; | |
56 case SkEncodedFormat::kGIF_SkEncodedFormat: | |
57 path.append("/gif/"); | |
58 path.appendS32(gCtr++); | |
59 path.append(".gif"); | |
60 break; | |
61 case SkEncodedFormat::kWEBP_SkEncodedFormat: | |
62 path.append("/webp/"); | |
63 path.appendS32(gCtr++); | |
64 path.append(".webp"); | |
65 break; | |
66 case SkEncodedFormat::kBMP_SkEncodedFormat: | |
67 path.append("/bmp/"); | |
68 path.appendS32(gCtr++); | |
69 path.append(".bmp"); | |
70 break; | |
71 case SkEncodedFormat::kWBMP_SkEncodedFormat: | |
72 path.append("/wbmp/"); | |
73 path.appendS32(gCtr++); | |
74 path.append(".wbmp"); | |
75 break; | |
76 case SkEncodedFormat::kICO_SkEncodedFormat: | |
77 path.append("/ico/"); | |
78 path.appendS32(gCtr++); | |
79 path.append(".ico"); | |
80 break; | |
81 case SkEncodedFormat::kRAW_SkEncodedFormat: | |
82 path.append("/dng/"); | |
83 path.appendS32(gCtr++); | |
84 path.append(".dng"); | |
85 break; | |
86 default: | |
87 path.append("/unknown/"); | |
88 path.appendS32(gCtr++); | |
89 break; | |
90 } | |
91 } else { | |
92 path.append("/unknown/"); | |
93 path.appendS32(gCtr++); | |
94 } | |
95 | |
96 FILE* file = sk_fopen(path.c_str(), kWrite_SkFILE_Flag); | |
97 if (file) { | |
98 sk_fwrite(encoded, length, file); | |
99 sk_fclose(file); | |
100 gSuccessCtr++; | |
101 return true; | |
102 } | |
103 | |
104 SkDebugf("Could not open %s\n", path.c_str()); | |
105 return false; | |
106 } | |
107 | |
108 int main(int argc, char** argv) { | |
109 SkCommandLineFlags::Parse(argc, argv); | |
110 if (FLAGS_skps.isEmpty() || FLAGS_out.isEmpty()) { | |
111 printUsage(); | |
112 return 0; | |
113 } | |
114 | |
115 const char* inputs = FLAGS_skps[0]; | |
116 gOutputDir = FLAGS_out[0]; | |
117 setup_output_dirs(); | |
118 if (!sk_isdir(inputs) || !sk_isdir(gOutputDir)) { | |
119 printUsage(); | |
120 return 0; | |
121 } | |
122 | |
123 SkOSFile::Iter iter(inputs, "skp"); | |
124 for (SkString file; iter.next(&file); ) { | |
125 SkAutoTDelete<SkStream> stream = | |
126 SkStream::NewFromFile(SkOSPath::Join(inputs, file.c_str()).c_str ()); | |
127 | |
128 // Rather than passing in a function that actually decodes the encoded d ata, | |
129 // we pass in a function that saves the encoded data to a file. | |
130 SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(stream, stor e_encoded_to_file)); | |
msarett
2016/02/12 16:33:49
Originally, I wanted to write code that actually p
scroggo
2016/02/12 17:18:38
Yep! This is similar to a feature we used to have
mtklein
2016/02/12 17:21:41
Unlike render_pictures, this program has multiple
scroggo
2016/02/12 18:50:27
sgtm
| |
131 | |
132 SkCanvas canvas; | |
133 canvas.drawPicture(picture); | |
134 } | |
135 | |
136 SkDebugf("Saved %d images to %s\n", gSuccessCtr, gOutputDir); | |
scroggo
2016/02/12 17:18:38
Should we also report the number of images that fa
msarett
2016/02/12 21:30:29
Yes that sounds good.
| |
137 return 1; | |
scroggo
2016/02/12 17:18:38
FWIW, I think our other testing tools return 0 and
msarett
2016/02/12 21:30:29
Haha yes. Fixed.
| |
138 } | |
OLD | NEW |