blob: d02e3deffbbfc3e1319bff04277f0625606b4fb3 [file] [log] [blame]
halcanary7d825f82016-03-09 11:26:50 -08001/*
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
15class StdOutWStream : public SkWStream {
16public:
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
24private:
25 size_t fBytesWritten;
26};
27
28static 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
35static 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
42static 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.
48int 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));
mtklein18300a32016-03-16 13:53:35 -070052 if (SkPDFDocumentToBitmap(in.release(), &bm) && to_png(out, bm)) {
halcanary7d825f82016-03-09 11:26:50 -080053 return 0;
54 } else {
55 return 1;
56 }
57}