halcanary | 7d825f8 | 2016-03-09 11:26:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 <stdio.h> |
| 9 | |
| 10 | #include "SkBitmap.h" |
| 11 | #include "SkCGUtils.h" |
| 12 | #include "SkImageEncoder.h" |
| 13 | #include "SkStream.h" |
| 14 | |
| 15 | class StdOutWStream : public SkWStream { |
| 16 | public: |
| 17 | StdOutWStream() : fBytesWritten(0) {} |
| 18 | bool write(const void* buffer, size_t size) final { |
| 19 | fBytesWritten += size; |
| 20 | return size == fwrite(buffer, 1, size, stdout); |
| 21 | } |
| 22 | size_t bytesWritten() const final { return fBytesWritten; } |
| 23 | |
| 24 | private: |
| 25 | size_t fBytesWritten; |
| 26 | }; |
| 27 | |
| 28 | static SkStreamAsset* open_for_reading(const char* path) { |
| 29 | if (!path || !path[0] || 0 == strcmp(path, "-")) { |
| 30 | return new SkFILEStream(stdin, SkFILEStream::kCallerRetains_Ownership); |
| 31 | } |
| 32 | return SkStream::NewFromFile(path); |
| 33 | } |
| 34 | |
| 35 | static SkWStream* open_for_writing(const char* path) { |
| 36 | if (!path || !path[0] || 0 == strcmp(path, "-")) { |
| 37 | return new StdOutWStream; |
| 38 | } |
| 39 | return new SkFILEWStream(path); |
| 40 | } |
| 41 | |
| 42 | static bool to_png(SkWStream* o, const SkBitmap& bm) { |
| 43 | return SkImageEncoder::EncodeStream(o, bm, SkImageEncoder::kPNG_Type, 100); |
| 44 | } |
| 45 | |
| 46 | // Note: I could implement this using only MacOS|CG API calls, but |
| 47 | // since most of this is already done in Skia, here it is. |
| 48 | int main(int argc, char** argv) { |
| 49 | SkBitmap bm; |
| 50 | SkAutoTDelete<SkStream> in(open_for_reading(argc > 1 ? argv[1] : NULL)); |
| 51 | SkAutoTDelete<SkWStream> out(open_for_writing(argc > 2 ? argv[2] : NULL)); |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame^] | 52 | if (SkPDFDocumentToBitmap(in.release(), &bm) && to_png(out, bm)) { |
halcanary | 7d825f8 | 2016-03-09 11:26:50 -0800 | [diff] [blame] | 53 | return 0; |
| 54 | } else { |
| 55 | return 1; |
| 56 | } |
| 57 | } |