aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/filtermain.cpp
blob: c1b2040fd08633171d2e472efd65505fc56c5c12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkDevice.h"
#include "SkGraphics.h"
#include "SkImageDecoder.h"
#include "SkImageEncoder.h"
#include "SkOSFile.h"
#include "SkPicture.h"
#include "SkPicturePlayback.h"
#include "SkPictureRecord.h"
#include "SkStream.h"
#include "picture_utils.h"
#include "path_utils.h"

static void usage() {
    SkDebugf("Usage: filter -i inFile [-o outFile] [--input-dir path] [--output-dir path]\n");
    SkDebugf("                        [-p pathFile] [-t textureDir] [-h|--help]\n\n");
    SkDebugf("    -i inFile  : file to file.\n");
    SkDebugf("    -o outFile : result of filtering.\n");
    SkDebugf("    --input-dir : process all files in dir with .skp extension.\n");
    SkDebugf("    --output-dir : results of filtering the input dir.\n");
    SkDebugf("    -p pathFile : file in which to place compileable path data.\n");
    SkDebugf("    -t textureDir : directory in which to place textures. (only available w/ single file)\n");
    SkDebugf("    -h|--help  : Show this help message.\n");
}

// SkFilterRecord allows the filter to manipulate the read in SkPicture
class SkFilterRecord : public SkPictureRecord {
public:
    SkFilterRecord(uint32_t recordFlags, SkDevice* device, SkFILEWStream* pathStream)
        : INHERITED(recordFlags, device)
        , fTransSkipped(0)
        , fTransTot(0)
        , fScalesSkipped(0)
        , fScalesTot(0)
        , fPathStream(pathStream) {
    }

    virtual ~SkFilterRecord() {
    }

    virtual bool clipPath(const SkPath& path, SkRegion::Op op, bool doAntiAlias) SK_OVERRIDE {
        if (!path.isRect(NULL) && 4 < path.countPoints()) {
            sk_tools::dump_path(fPathStream, path);
        }
        return INHERITED::clipPath(path, op, doAntiAlias);
    }

    virtual void drawPath(const SkPath& path, const SkPaint& p) SK_OVERRIDE {
        if (!path.isRect(NULL) && 4 < path.countPoints()) {
            sk_tools::dump_path(fPathStream, path);
        }
        INHERITED::drawPath(path, p);
    }

    virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE {
        ++fTransTot;

#if 0
        if (0 == dx && 0 == dy) {
            ++fTransSkipped;
            return true;
        }
#endif

        return INHERITED::translate(dx, dy);
    }

    virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE {
        ++fScalesTot;

#if 0
        if (SK_Scalar1 == sx && SK_Scalar1 == sy) {
            ++fScalesSkipped;
            return true;
        }
#endif

        return INHERITED::scale(sx, sy);
    }

    void saveImages(const SkString& path) {
        SkTRefArray<SkBitmap>* bitmaps = fBitmapHeap->extractBitmaps();

        if (NULL != bitmaps) {
            for (int i = 0; i < bitmaps->count(); ++i) {
                SkString filename(path);
                if (!path.endsWith("\\")) {
                    filename.append("\\");
                }
                filename.append("image");
                filename.appendS32(i);
                filename.append(".png");

                SkImageEncoder::EncodeFile(filename.c_str(), (*bitmaps)[i],
                                           SkImageEncoder::kPNG_Type, 0);
            }
        }

        bitmaps->unref();
    }

    void report() {
        SkDebugf("%d Trans skipped (out of %d)\n", fTransSkipped, fTransTot);
        SkDebugf("%d Scales skipped (out of %d)\n", fScalesSkipped, fScalesTot);
    }

protected:
    int fTransSkipped;
    int fTransTot;

    int fScalesSkipped;
    int fScalesTot;

    SkFILEWStream* fPathStream;
private:
    typedef SkPictureRecord INHERITED;
};

// Wrap SkPicture to allow installation of a SkFilterRecord object
class SkFilterPicture : public SkPicture {
public:
    SkFilterPicture(int width, int height, SkPictureRecord* record) {
        fWidth = width;
        fHeight = height;
        fRecord = record;
        SkSafeRef(fRecord);
    }

private:
    typedef SkPicture INHERITED;
};

static bool PNGEncodeBitmapToStream(SkWStream* stream, const SkBitmap& bitmap) {
    return SkImageEncoder::EncodeStream(stream, bitmap, SkImageEncoder::kPNG_Type, 100);
}

int filter_picture(const SkString& inFile, const SkString& outFile,
                   const SkString& textureDir, SkFILEWStream *pathStream) {
    SkPicture* inPicture = NULL;

    SkFILEStream inStream(inFile.c_str());
    if (inStream.isValid()) {
        inPicture = SkNEW_ARGS(SkPicture, (&inStream, NULL, &SkImageDecoder::DecodeStream));
    }

    if (NULL == inPicture) {
        SkDebugf("Could not read file %s\n", inFile.c_str());
        return -1;
    }

    SkBitmap bm;
    bm.setConfig(SkBitmap::kNo_Config, inPicture->width(), inPicture->height());
    SkAutoTUnref<SkDevice> dev(SkNEW_ARGS(SkDevice, (bm)));

    SkAutoTUnref<SkFilterRecord> filterRecord(SkNEW_ARGS(SkFilterRecord, (0, dev, pathStream)));

    // Playback the read in picture to the SkFilterRecorder to allow filtering
    filterRecord->beginRecording();
    inPicture->draw(filterRecord);
    filterRecord->endRecording();

    filterRecord->report();

    if (!outFile.isEmpty()) {
        SkFilterPicture outPicture(inPicture->width(), inPicture->height(), filterRecord);
        SkFILEWStream outStream(outFile.c_str());

        outPicture.serialize(&outStream);
    }

    if (!textureDir.isEmpty()) {
        filterRecord->saveImages(textureDir);
    }

    return 0;
}

// This function is not marked as 'static' so it can be referenced externally
// in the iOS build.
int tool_main(int argc, char** argv) {
    SkGraphics::Init();

    if (argc < 3) {
        usage();
        return -1;
    }

    SkString inFile, outFile, inDir, outDir, textureDir, pathFile;

    char* const* stop = argv + argc;
    for (++argv; argv < stop; ++argv) {
        if (strcmp(*argv, "-i") == 0) {
            argv++;
            if (argv < stop && **argv) {
                inFile.set(*argv);
            } else {
                SkDebugf("missing arg for -i\n");
                usage();
                return -1;
            }
        } else if (strcmp(*argv, "--input-dir") == 0) {
            argv++;
            if (argv < stop && **argv) {
                inDir.set(*argv);
            } else {
                SkDebugf("missing arg for --input-dir\n");
                usage();
                return -1;
            }
        } else if (strcmp(*argv, "--output-dir") == 0) {
            argv++;
            if (argv < stop && **argv) {
                outDir.set(*argv);
            } else {
                SkDebugf("missing arg for --output-dir\n");
                usage();
                return -1;
            }
        } else if (strcmp(*argv, "-o") == 0) {
            argv++;
            if (argv < stop && **argv) {
                outFile.set(*argv);
            } else {
                SkDebugf("missing arg for -o\n");
                usage();
                return -1;
            }
        } else if (strcmp(*argv, "-p") == 0) {
            argv++;
            if (argv < stop && **argv) {
                pathFile.set(*argv);
            } else {
                SkDebugf("missing arg for -p\n");
                usage();
                return -1;
            }
        } else if (strcmp(*argv, "-t") == 0) {
            argv++;
            if (argv < stop && **argv) {
                textureDir.set(*argv);
            } else {
                SkDebugf("missing arg for -t\n");
                usage();
                return -1;
            }
        } else if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
            usage();
            return 0;
        } else {
            SkDebugf("unknown arg %s\n", *argv);
            usage();
            return -1;
        }
    }

    if(!inDir.isEmpty() && !textureDir.isEmpty()) {
        SkDebugf("ERROR: The textureDir option is not permitted when passing an input directory.\n");
        usage();
        return -1;
    }

    SkFILEWStream *pathStream = NULL;

    if (!pathFile.isEmpty()) {
        pathStream = new SkFILEWStream(pathFile.c_str());
        if (!pathStream->isValid()) {
            SkDebugf("Could open path file %s\n", pathFile.c_str());
            delete pathStream;
            return -1;
        }

        sk_tools::dump_path_prefix(pathStream);
    }

    SkOSFile::Iter iter(inDir.c_str(), "skp");
    int failures = 0;
    SkString inputFilename, outputFilename;
    if (iter.next(&inputFilename)) {

        do {
            sk_tools::make_filepath(&inFile, inDir, inputFilename);
            if (!outDir.isEmpty()) {
                sk_tools::make_filepath(&outFile, outDir, inputFilename);
            }
            SkDebugf("Executing %s\n", inputFilename.c_str());
            filter_picture(inFile, outFile, textureDir, pathStream);
        } while(iter.next(&inputFilename));

    } else if (!inFile.isEmpty()) {
        filter_picture(inFile, outFile, textureDir, pathStream);
    } else {
        usage();
        if (NULL != pathStream) {
            delete pathStream;
            pathStream = NULL;
        }
        return -1;
    }

    if (NULL != pathStream) {
        sk_tools::dump_path_suffix(pathStream);
        delete pathStream;
        pathStream = NULL;
    }

    SkGraphics::Term();
    return 0;
}

#if !defined SK_BUILD_FOR_IOS
int main(int argc, char * const argv[]) {
    return tool_main(argc, (char**) argv);
}
#endif