diff options
author | msarett <msarett@google.com> | 2016-02-12 14:47:09 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-02-12 14:47:09 -0800 |
commit | 3478f753ffc28a6f0f0877cc06be7373f960c527 (patch) | |
tree | f5d388ff692dd0d3e1c7a189c2802a9ac737f312 | |
parent | 5ab17ed12ec05b9a11b4cfbe901f6cdfa26169ca (diff) |
Adding a tool to get images from skps
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1696763002
Review URL: https://codereview.chromium.org/1696763002
-rw-r--r-- | gyp/tools.gyp | 12 | ||||
-rw-r--r-- | tools/flags/SkCommandLineFlags.cpp | 4 | ||||
-rw-r--r-- | tools/flags/SkCommandLineFlags.h | 5 | ||||
-rw-r--r-- | tools/get_images_from_skps.cpp | 139 |
4 files changed, 160 insertions, 0 deletions
diff --git a/gyp/tools.gyp b/gyp/tools.gyp index e428076bb9..4b89427545 100644 --- a/gyp/tools.gyp +++ b/gyp/tools.gyp @@ -19,6 +19,7 @@ 'dependencies': [ 'chrome_fuzz', 'dump_record', + 'get_images_from_skps', 'gpuveto', 'imgblur', 'imgconv', @@ -280,6 +281,17 @@ ], }, { + 'target_name': 'get_images_from_skps', + 'type': 'executable', + 'sources': [ + '../tools/get_images_from_skps.cpp', + ], + 'dependencies': [ + 'flags.gyp:flags', + 'skia_lib.gyp:skia_lib', + ], + }, + { 'target_name': 'gpuveto', 'type': 'executable', 'sources': [ diff --git a/tools/flags/SkCommandLineFlags.cpp b/tools/flags/SkCommandLineFlags.cpp index cddf9fb0b3..3dd746c8d2 100644 --- a/tools/flags/SkCommandLineFlags.cpp +++ b/tools/flags/SkCommandLineFlags.cpp @@ -150,6 +150,10 @@ void SkCommandLineFlags::SetUsage(const char* usage) { gUsage.set(usage); } +void SkCommandLineFlags::PrintUsage() { + SkDebugf("%s", gUsage.c_str()); +} + // Maximum line length for the help message. #define LINE_LENGTH 72 diff --git a/tools/flags/SkCommandLineFlags.h b/tools/flags/SkCommandLineFlags.h index 909718d3e7..2c77cbf35e 100644 --- a/tools/flags/SkCommandLineFlags.h +++ b/tools/flags/SkCommandLineFlags.h @@ -108,6 +108,11 @@ public: static void SetUsage(const char* usage); /** + * Call this to display the help message. Should be called after SetUsage. + */ + static void PrintUsage(); + + /** * Call at the beginning of main to parse flags created by DEFINE_x, above. * Must only be called once. */ diff --git a/tools/get_images_from_skps.cpp b/tools/get_images_from_skps.cpp new file mode 100644 index 0000000000..02fbc457a4 --- /dev/null +++ b/tools/get_images_from_skps.cpp @@ -0,0 +1,139 @@ +/* + * 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 int gUnknownCtr = 0; +static int gFailureCtr = 0; +static const char* gOutputDir; + +void setup_output_dirs() { + const char* exts[] = { "jpg", "png", "gif", "webp", "bmp", "wbmp", "ico", "dng", "unknown" }; + for (const char* ext : exts) { + sk_mkdir(SkOSPath::Join(gOutputDir, ext).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; + SkAutoTUnref<SkData> data(SkData::NewWithoutCopy(encoded, length)); + SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data)); + if (codec) { + switch (codec->getEncodedFormat()) { + case SkEncodedFormat::kJPEG_SkEncodedFormat: + path = SkOSPath::Join(SkOSPath::Join(gOutputDir, "jpg").c_str(), ""); + path.appendS32(gCtr++); + path.append(".jpg"); + break; + case SkEncodedFormat::kPNG_SkEncodedFormat: + path = SkOSPath::Join(SkOSPath::Join(gOutputDir, "png").c_str(), ""); + path.appendS32(gCtr++); + path.append(".png"); + break; + case SkEncodedFormat::kGIF_SkEncodedFormat: + path = SkOSPath::Join(SkOSPath::Join(gOutputDir, "gif").c_str(), ""); + path.appendS32(gCtr++); + path.append(".gif"); + break; + case SkEncodedFormat::kWEBP_SkEncodedFormat: + path = SkOSPath::Join(SkOSPath::Join(gOutputDir, "webp").c_str(), ""); + path.appendS32(gCtr++); + path.append(".webp"); + break; + case SkEncodedFormat::kBMP_SkEncodedFormat: + path = SkOSPath::Join(SkOSPath::Join(gOutputDir, "bmp").c_str(), ""); + path.appendS32(gCtr++); + path.append(".bmp"); + break; + case SkEncodedFormat::kWBMP_SkEncodedFormat: + path = SkOSPath::Join(SkOSPath::Join(gOutputDir, "wbmp").c_str(), ""); + path.appendS32(gCtr++); + path.append(".wbmp"); + break; + case SkEncodedFormat::kICO_SkEncodedFormat: + path = SkOSPath::Join(SkOSPath::Join(gOutputDir, "ico").c_str(), ""); + path.appendS32(gCtr++); + path.append(".ico"); + break; + case SkEncodedFormat::kRAW_SkEncodedFormat: + path = SkOSPath::Join(SkOSPath::Join(gOutputDir, "dng").c_str(), ""); + path.appendS32(gCtr++); + path.append(".dng"); + break; + default: + path = SkOSPath::Join(gOutputDir, "unknown"); + path.appendS32(gUnknownCtr++); + break; + } + } else { + path = SkOSPath::Join(gOutputDir, "unknown"); + path.appendS32(gUnknownCtr++); + } + + FILE* file = sk_fopen(path.c_str(), kWrite_SkFILE_Flag); + if (file) { + sk_fwrite(encoded, length, file); + sk_fclose(file); + gSuccessCtr++; + return true; + } + + gFailureCtr++; + SkDebugf("Could not open %s\n", path.c_str()); + return false; +} + +int main(int argc, char** argv) { + SkCommandLineFlags::SetUsage( + "Usage: get_images_from_skps -s <dir of skps> -o <dir for output images>\n"); + + SkCommandLineFlags::Parse(argc, argv); + if (FLAGS_skps.isEmpty() || FLAGS_out.isEmpty()) { + SkCommandLineFlags::PrintUsage(); + return 1; + } + + const char* inputs = FLAGS_skps[0]; + gOutputDir = FLAGS_out[0]; + if (!sk_isdir(inputs) || !sk_isdir(gOutputDir)) { + SkCommandLineFlags::PrintUsage(); + return 1; + } + + setup_output_dirs(); + 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)); + + SkCanvas canvas; + canvas.drawPicture(picture); + } + + SkDebugf("Successfully saved %d recognized images and %d unrecognized images\n", gSuccessCtr, + gUnknownCtr); + SkDebugf("Failed to write %d images\n", gFailureCtr); + return 0; +} |