blob: b415219564c8383645c403ff4872217154f5552b [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
robertphillips@google.com801cee12012-10-19 19:06:11 +00008#include "SkDevice.h"
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +00009#include "SkGraphics.h"
robertphillips@google.com801cee12012-10-19 19:06:11 +000010#include "SkImageEncoder.h"
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +000011#include "SkPicture.h"
robertphillips@google.com801cee12012-10-19 19:06:11 +000012#include "SkPicturePlayback.h"
13#include "SkPictureRecord.h"
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +000014#include "SkStream.h"
15
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +000016static void usage() {
robertphillips@google.com801cee12012-10-19 19:06:11 +000017 SkDebugf("Usage: filter -i inFile [-o outFile] [-t textureDir] [-h|--help]");
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +000018 SkDebugf("\n\n");
19 SkDebugf(" -i inFile : file to file.\n");
20 SkDebugf(" -o outFile : result of filtering.\n");
robertphillips@google.com801cee12012-10-19 19:06:11 +000021 SkDebugf(" -t textureDir : directory in which to place textures.\n");
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +000022 SkDebugf(" -h|--help : Show this help message.\n");
23}
24
robertphillips@google.com801cee12012-10-19 19:06:11 +000025// SkFilterRecord allows the filter to manipulate the read in SkPicture
26class SkFilterRecord : public SkPictureRecord {
27public:
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
53private:
54 typedef SkPictureRecord INHERITED;
55};
56
tfarina@chromium.orga5b7cc02012-10-08 14:41:10 +000057// This function is not marked as 'static' so it can be referenced externally
58// in the iOS build.
caryclark@google.com9598f422012-10-09 12:32:37 +000059int tool_main(int argc, char** argv) {
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +000060 SkGraphics::Init();
61
robertphillips@google.com801cee12012-10-19 19:06:11 +000062 if (argc < 3) {
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +000063 usage();
64 return -1;
65 }
66
robertphillips@google.com801cee12012-10-19 19:06:11 +000067 SkString inFile, outFile, textureDir;
68
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +000069 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.com801cee12012-10-19 19:06:11 +000076 SkDebugf("missing arg for -i\n");
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +000077 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.com801cee12012-10-19 19:06:11 +000085 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.comc7e4a5a2012-10-04 13:00:33 +000095 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.com801cee12012-10-19 19:06:11 +0000108 if (inFile.isEmpty()) {
109 usage();
110 return -1;
111 }
112
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +0000113 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.com801cee12012-10-19 19:06:11 +0000125 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.comc7e4a5a2012-10-04 13:00:33 +0000131
robertphillips@google.com801cee12012-10-19 19:06:11 +0000132 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.comc7e4a5a2012-10-04 13:00:33 +0000149
150 SkGraphics::Term();
151
152 return 0;
153}
154
155#if !defined SK_BUILD_FOR_IOS
156int main(int argc, char * const argv[]) {
caryclark@google.com9598f422012-10-09 12:32:37 +0000157 return tool_main(argc, (char**) argv);
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +0000158}
159#endif