diff options
author | halcanary <halcanary@google.com> | 2016-03-09 11:26:50 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-03-09 11:26:51 -0800 |
commit | 7d825f8d5ce2a705a345168936a48bf51f54f1b3 (patch) | |
tree | a827e1e40fd7d6b5ee1a2482e262f9271a5c7061 | |
parent | cf430139db4bf2f00114903642a884ae81baed4a (diff) |
experimental: coreGraphicsPdf2png
motivation: used for testing on my macbook.
TBR=
Review URL: https://codereview.chromium.org/1131643004
-rw-r--r-- | experimental/tools/coreGraphicsPdf2png.cpp | 57 | ||||
-rw-r--r-- | gyp/experimental.gyp | 15 |
2 files changed, 72 insertions, 0 deletions
diff --git a/experimental/tools/coreGraphicsPdf2png.cpp b/experimental/tools/coreGraphicsPdf2png.cpp new file mode 100644 index 0000000000..98c5b286c8 --- /dev/null +++ b/experimental/tools/coreGraphicsPdf2png.cpp @@ -0,0 +1,57 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <stdio.h> + +#include "SkBitmap.h" +#include "SkCGUtils.h" +#include "SkImageEncoder.h" +#include "SkStream.h" + +class StdOutWStream : public SkWStream { +public: + StdOutWStream() : fBytesWritten(0) {} + bool write(const void* buffer, size_t size) final { + fBytesWritten += size; + return size == fwrite(buffer, 1, size, stdout); + } + size_t bytesWritten() const final { return fBytesWritten; } + +private: + size_t fBytesWritten; +}; + +static SkStreamAsset* open_for_reading(const char* path) { + if (!path || !path[0] || 0 == strcmp(path, "-")) { + return new SkFILEStream(stdin, SkFILEStream::kCallerRetains_Ownership); + } + return SkStream::NewFromFile(path); +} + +static SkWStream* open_for_writing(const char* path) { + if (!path || !path[0] || 0 == strcmp(path, "-")) { + return new StdOutWStream; + } + return new SkFILEWStream(path); +} + +static bool to_png(SkWStream* o, const SkBitmap& bm) { + return SkImageEncoder::EncodeStream(o, bm, SkImageEncoder::kPNG_Type, 100); +} + +// Note: I could implement this using only MacOS|CG API calls, but +// since most of this is already done in Skia, here it is. +int main(int argc, char** argv) { + SkBitmap bm; + SkAutoTDelete<SkStream> in(open_for_reading(argc > 1 ? argv[1] : NULL)); + SkAutoTDelete<SkWStream> out(open_for_writing(argc > 2 ? argv[2] : NULL)); + if (SkPDFDocumentToBitmap(in.detach(), &bm) && to_png(out, bm)) { + return 0; + } else { + return 1; + } +} diff --git a/gyp/experimental.gyp b/gyp/experimental.gyp index 3cd5a59c24..1fc86999b6 100644 --- a/gyp/experimental.gyp +++ b/gyp/experimental.gyp @@ -24,4 +24,19 @@ }, }, ], + 'conditions': [ + ['skia_os == "mac"', + { + 'targets': [ + { + 'target_name': 'coreGraphicsPdf2png', + 'type': 'executable', + 'include_dirs': [ '../src/core', ], + 'sources': [ '../experimental/tools/coreGraphicsPdf2png.cpp', ], + 'dependencies': [ 'skia_lib.gyp:skia_lib', ] + }, + ], + }, + ], + ], } |