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 | |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame^] | 8 | #include "SkDevice.h" |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 9 | #include "SkGraphics.h" |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame^] | 10 | #include "SkImageEncoder.h" |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 11 | #include "SkPicture.h" |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame^] | 12 | #include "SkPicturePlayback.h" |
| 13 | #include "SkPictureRecord.h" |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 14 | #include "SkStream.h" |
| 15 | |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 16 | static void usage() { |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame^] | 17 | SkDebugf("Usage: filter -i inFile [-o outFile] [-t textureDir] [-h|--help]"); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 18 | SkDebugf("\n\n"); |
| 19 | SkDebugf(" -i inFile : file to file.\n"); |
| 20 | SkDebugf(" -o outFile : result of filtering.\n"); |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame^] | 21 | SkDebugf(" -t textureDir : directory in which to place textures.\n"); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 22 | SkDebugf(" -h|--help : Show this help message.\n"); |
| 23 | } |
| 24 | |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame^] | 25 | // SkFilterRecord allows the filter to manipulate the read in SkPicture |
| 26 | class SkFilterRecord : public SkPictureRecord { |
| 27 | public: |
| 28 | SkFilterRecord(uint32_t recordFlags, SkDevice* device) |
| 29 | : INHERITED(recordFlags, device) { |
| 30 | } |
| 31 | |
| 32 | void saveImages(const SkString& path) { |
| 33 | SkTRefArray<SkBitmap>* bitmaps = fBitmapHeap->extractBitmaps(); |
| 34 | |
| 35 | if (NULL != bitmaps) { |
| 36 | for (int i = 0; i < bitmaps->count(); ++i) { |
| 37 | SkString filename(path); |
| 38 | if (!path.endsWith("\\")) { |
| 39 | filename.append("\\"); |
| 40 | } |
| 41 | filename.append("image"); |
| 42 | filename.appendS32(i); |
| 43 | filename.append(".png"); |
| 44 | |
| 45 | SkImageEncoder::EncodeFile(filename.c_str(), (*bitmaps)[i], |
| 46 | SkImageEncoder::kPNG_Type, 0); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | bitmaps->unref(); |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | typedef SkPictureRecord INHERITED; |
| 55 | }; |
| 56 | |
tfarina@chromium.org | a5b7cc0 | 2012-10-08 14:41:10 +0000 | [diff] [blame] | 57 | // This function is not marked as 'static' so it can be referenced externally |
| 58 | // in the iOS build. |
caryclark@google.com | 9598f42 | 2012-10-09 12:32:37 +0000 | [diff] [blame] | 59 | int tool_main(int argc, char** argv) { |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 60 | SkGraphics::Init(); |
| 61 | |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame^] | 62 | if (argc < 3) { |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 63 | usage(); |
| 64 | return -1; |
| 65 | } |
| 66 | |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame^] | 67 | SkString inFile, outFile, textureDir; |
| 68 | |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 69 | char* const* stop = argv + argc; |
| 70 | for (++argv; argv < stop; ++argv) { |
| 71 | if (strcmp(*argv, "-i") == 0) { |
| 72 | argv++; |
| 73 | if (argv < stop && **argv) { |
| 74 | inFile.set(*argv); |
| 75 | } else { |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame^] | 76 | SkDebugf("missing arg for -i\n"); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 77 | usage(); |
| 78 | return -1; |
| 79 | } |
| 80 | } else if (strcmp(*argv, "-o") == 0) { |
| 81 | argv++; |
| 82 | if (argv < stop && **argv) { |
| 83 | outFile.set(*argv); |
| 84 | } else { |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame^] | 85 | SkDebugf("missing arg for -o\n"); |
| 86 | usage(); |
| 87 | return -1; |
| 88 | } |
| 89 | } else if (strcmp(*argv, "-t") == 0) { |
| 90 | argv++; |
| 91 | if (argv < stop && **argv) { |
| 92 | textureDir.set(*argv); |
| 93 | } else { |
| 94 | SkDebugf("missing arg for -t\n"); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 95 | usage(); |
| 96 | return -1; |
| 97 | } |
| 98 | } else if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) { |
| 99 | usage(); |
| 100 | return 0; |
| 101 | } else { |
| 102 | SkDebugf("unknown arg %s\n", *argv); |
| 103 | usage(); |
| 104 | return -1; |
| 105 | } |
| 106 | } |
| 107 | |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame^] | 108 | if (inFile.isEmpty()) { |
| 109 | usage(); |
| 110 | return -1; |
| 111 | } |
| 112 | |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 113 | SkPicture* inPicture = NULL; |
| 114 | |
| 115 | SkFILEStream inStream(inFile.c_str()); |
| 116 | if (inStream.isValid()) { |
| 117 | inPicture = SkNEW_ARGS(SkPicture, (&inStream)); |
| 118 | } |
| 119 | |
| 120 | if (NULL == inPicture) { |
| 121 | SkDebugf("Could not read file %s\n", inFile.c_str()); |
| 122 | return -1; |
| 123 | } |
| 124 | |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame^] | 125 | if (!outFile.isEmpty()) { |
| 126 | // Playback the read in picture to another picture to allow whatever |
| 127 | // translation occurs inside Skia to occur |
| 128 | SkPicture outPicture; |
| 129 | inPicture->draw(outPicture.beginRecording(inPicture->width(), inPicture->height())); |
| 130 | outPicture.endRecording(); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 131 | |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame^] | 132 | SkFILEWStream outStream(outFile.c_str()); |
| 133 | outPicture.serialize(&outStream); |
| 134 | } |
| 135 | |
| 136 | if (!textureDir.isEmpty()) { |
| 137 | SkBitmap bm; |
| 138 | bm.setConfig(SkBitmap::kNo_Config, inPicture->width(), inPicture->height()); |
| 139 | SkAutoTUnref<SkDevice> dev(SkNEW_ARGS(SkDevice, (bm))); |
| 140 | |
| 141 | SkFilterRecord filterRecord(0, dev); |
| 142 | |
| 143 | filterRecord.beginRecording(); |
| 144 | inPicture->draw(&filterRecord); |
| 145 | filterRecord.endRecording(); |
| 146 | |
| 147 | filterRecord.saveImages(textureDir); |
| 148 | } |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 149 | |
| 150 | SkGraphics::Term(); |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | #if !defined SK_BUILD_FOR_IOS |
| 156 | int main(int argc, char * const argv[]) { |
caryclark@google.com | 9598f42 | 2012-10-09 12:32:37 +0000 | [diff] [blame] | 157 | return tool_main(argc, (char**) argv); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 158 | } |
| 159 | #endif |