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