blob: c256adde3127b8392ac3548734ab59a24f7848c1 [file] [log] [blame]
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +00001/*
2 * Copyright 2012 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 "SkGraphics.h"
9#include "SkPicture.h"
10#include "SkStream.h"
11
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +000012static void usage() {
13 SkDebugf("Usage: filter -i inFile -o outFile [-h|--help]");
14 SkDebugf("\n\n");
15 SkDebugf(" -i inFile : file to file.\n");
16 SkDebugf(" -o outFile : result of filtering.\n");
17 SkDebugf(" -h|--help : Show this help message.\n");
18}
19
tfarina@chromium.orga5b7cc02012-10-08 14:41:10 +000020// This function is not marked as 'static' so it can be referenced externally
21// in the iOS build.
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +000022int filter_main(int argc, char** argv) {
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +000023 SkGraphics::Init();
24
25 SkString inFile, outFile;
26
27 if (argc < 5) {
28 usage();
29 return -1;
30 }
31
32 char* const* stop = argv + argc;
33 for (++argv; argv < stop; ++argv) {
34 if (strcmp(*argv, "-i") == 0) {
35 argv++;
36 if (argv < stop && **argv) {
37 inFile.set(*argv);
38 } else {
39 SkDebugf("missing arg for --i\n");
40 usage();
41 return -1;
42 }
43 } else if (strcmp(*argv, "-o") == 0) {
44 argv++;
45 if (argv < stop && **argv) {
46 outFile.set(*argv);
47 } else {
48 SkDebugf("missing arg for --o\n");
49 usage();
50 return -1;
51 }
52 } else if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
53 usage();
54 return 0;
55 } else {
56 SkDebugf("unknown arg %s\n", *argv);
57 usage();
58 return -1;
59 }
60 }
61
62 SkPicture* inPicture = NULL;
63
64 SkFILEStream inStream(inFile.c_str());
65 if (inStream.isValid()) {
66 inPicture = SkNEW_ARGS(SkPicture, (&inStream));
67 }
68
69 if (NULL == inPicture) {
70 SkDebugf("Could not read file %s\n", inFile.c_str());
71 return -1;
72 }
73
74 SkPicture outPicture;
75 inPicture->draw(outPicture.beginRecording(inPicture->width(), inPicture->height()));
76 outPicture.endRecording();
77
78 SkFILEWStream outStream(outFile.c_str());
79 outPicture.serialize(&outStream);
80
81 SkGraphics::Term();
82
83 return 0;
84}
85
86#if !defined SK_BUILD_FOR_IOS
87int main(int argc, char * const argv[]) {
88 return filter_main(argc, (char**) argv);
89}
90#endif