Index: tools/get_images_from_skps.cpp |
diff --git a/tools/get_images_from_skps.cpp b/tools/get_images_from_skps.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e57c3de8e7b6a73108b38a8f9ba2be3e531c0b3e |
--- /dev/null |
+++ b/tools/get_images_from_skps.cpp |
@@ -0,0 +1,138 @@ |
+/* |
+ * Copyright 2016 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
+#include "SkCanvas.h" |
+#include "SkCodec.h" |
+#include "SkCommandLineFlags.h" |
+#include "SkData.h" |
+#include "SkOSFile.h" |
+#include "SkPicture.h" |
+#include "SkStream.h" |
+ |
+DEFINE_string2(skps, s, "", "A path to a directory of skps."); |
+DEFINE_string2(out, o, "", "A path to an output directory."); |
+ |
+static int gCtr = 0; |
+static int gSuccessCtr = 0; |
+static const char* gOutputDir; |
+ |
+static void printUsage() { |
+ SkDebugf("Usage: get_images_from_skps -s <dir of skps> -o <dir for output images>\n"); |
scroggo
2016/02/12 17:18:38
SkCommandLineFlags has a method to specify usage (
msarett
2016/02/12 21:30:29
Done.
|
+} |
+ |
+void setup_output_dirs() { |
+ const char* exts[] = { "jpg", "png", "gif", "webp", "bmp", "wbmp", "ico", "dng", "unknown" }; |
+ for (const char* ext : exts) { |
+ SkString path(gOutputDir); |
+ 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.
|
+ path.append(ext); |
+ sk_mkdir(path.c_str()); |
+ } |
+} |
+ |
+bool store_encoded_to_file(const void* encoded, size_t length, SkBitmap* bitmap) { |
+ // Silence warnings about empty bitmaps. |
+ bitmap->allocN32Pixels(1, 1, true); |
+ |
+ SkString path(gOutputDir); |
+ SkAutoTUnref<SkData> data(SkData::NewWithoutCopy(encoded, length)); |
+ SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data)); |
+ if (codec) { |
+ switch (codec->getEncodedFormat()) { |
+ case SkEncodedFormat::kJPEG_SkEncodedFormat: |
+ path.append("/jpg/"); |
+ path.appendS32(gCtr++); |
+ path.append(".jpg"); |
+ break; |
+ case SkEncodedFormat::kPNG_SkEncodedFormat: |
+ path.append("/png/"); |
+ path.appendS32(gCtr++); |
+ path.append(".png"); |
+ break; |
+ case SkEncodedFormat::kGIF_SkEncodedFormat: |
+ path.append("/gif/"); |
+ path.appendS32(gCtr++); |
+ path.append(".gif"); |
+ break; |
+ case SkEncodedFormat::kWEBP_SkEncodedFormat: |
+ path.append("/webp/"); |
+ path.appendS32(gCtr++); |
+ path.append(".webp"); |
+ break; |
+ case SkEncodedFormat::kBMP_SkEncodedFormat: |
+ path.append("/bmp/"); |
+ path.appendS32(gCtr++); |
+ path.append(".bmp"); |
+ break; |
+ case SkEncodedFormat::kWBMP_SkEncodedFormat: |
+ path.append("/wbmp/"); |
+ path.appendS32(gCtr++); |
+ path.append(".wbmp"); |
+ break; |
+ case SkEncodedFormat::kICO_SkEncodedFormat: |
+ path.append("/ico/"); |
+ path.appendS32(gCtr++); |
+ path.append(".ico"); |
+ break; |
+ case SkEncodedFormat::kRAW_SkEncodedFormat: |
+ path.append("/dng/"); |
+ path.appendS32(gCtr++); |
+ path.append(".dng"); |
+ break; |
+ default: |
+ path.append("/unknown/"); |
+ path.appendS32(gCtr++); |
+ break; |
+ } |
+ } else { |
+ path.append("/unknown/"); |
+ path.appendS32(gCtr++); |
+ } |
+ |
+ FILE* file = sk_fopen(path.c_str(), kWrite_SkFILE_Flag); |
+ if (file) { |
+ sk_fwrite(encoded, length, file); |
+ sk_fclose(file); |
+ gSuccessCtr++; |
+ return true; |
+ } |
+ |
+ SkDebugf("Could not open %s\n", path.c_str()); |
+ return false; |
+} |
+ |
+int main(int argc, char** argv) { |
+ SkCommandLineFlags::Parse(argc, argv); |
+ if (FLAGS_skps.isEmpty() || FLAGS_out.isEmpty()) { |
+ printUsage(); |
+ return 0; |
+ } |
+ |
+ const char* inputs = FLAGS_skps[0]; |
+ gOutputDir = FLAGS_out[0]; |
+ setup_output_dirs(); |
+ if (!sk_isdir(inputs) || !sk_isdir(gOutputDir)) { |
+ printUsage(); |
+ return 0; |
+ } |
+ |
+ SkOSFile::Iter iter(inputs, "skp"); |
+ for (SkString file; iter.next(&file); ) { |
+ SkAutoTDelete<SkStream> stream = |
+ SkStream::NewFromFile(SkOSPath::Join(inputs, file.c_str()).c_str()); |
+ |
+ // Rather than passing in a function that actually decodes the encoded data, |
+ // we pass in a function that saves the encoded data to a file. |
+ SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(stream, store_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
|
+ |
+ SkCanvas canvas; |
+ canvas.drawPicture(picture); |
+ } |
+ |
+ 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.
|
+ 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.
|
+} |