2012-10-04 13:00:33 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2012-10-19 19:06:11 +00:00
|
|
|
#include "SkDevice.h"
|
2012-10-04 13:00:33 +00:00
|
|
|
#include "SkGraphics.h"
|
2012-11-12 18:03:19 +00:00
|
|
|
#include "SkImageDecoder.h"
|
2012-10-19 19:06:11 +00:00
|
|
|
#include "SkImageEncoder.h"
|
2012-11-13 18:50:33 +00:00
|
|
|
#include "SkOSFile.h"
|
2012-10-04 13:00:33 +00:00
|
|
|
#include "SkPicture.h"
|
2012-10-19 19:06:11 +00:00
|
|
|
#include "SkPicturePlayback.h"
|
|
|
|
#include "SkPictureRecord.h"
|
2012-10-04 13:00:33 +00:00
|
|
|
#include "SkStream.h"
|
2012-11-13 18:50:33 +00:00
|
|
|
#include "picture_utils.h"
|
2012-12-07 20:56:13 +00:00
|
|
|
#include "path_utils.h"
|
2012-10-04 13:00:33 +00:00
|
|
|
|
|
|
|
static void usage() {
|
2012-12-07 20:56:13 +00:00
|
|
|
SkDebugf("Usage: filter -i inFile [-o outFile] [--input-dir path] [--output-dir path]\n");
|
|
|
|
SkDebugf(" [-p pathFile] [-t textureDir] [-h|--help]\n\n");
|
2012-10-04 13:00:33 +00:00
|
|
|
SkDebugf(" -i inFile : file to file.\n");
|
|
|
|
SkDebugf(" -o outFile : result of filtering.\n");
|
2012-11-13 18:50:33 +00:00
|
|
|
SkDebugf(" --input-dir : process all files in dir with .skp extension.\n");
|
|
|
|
SkDebugf(" --output-dir : results of filtering the input dir.\n");
|
2012-12-07 20:56:13 +00:00
|
|
|
SkDebugf(" -p pathFile : file in which to place compileable path data.\n");
|
2012-11-13 18:50:33 +00:00
|
|
|
SkDebugf(" -t textureDir : directory in which to place textures. (only available w/ single file)\n");
|
2012-10-04 13:00:33 +00:00
|
|
|
SkDebugf(" -h|--help : Show this help message.\n");
|
|
|
|
}
|
|
|
|
|
2012-10-19 19:06:11 +00:00
|
|
|
// SkFilterRecord allows the filter to manipulate the read in SkPicture
|
|
|
|
class SkFilterRecord : public SkPictureRecord {
|
|
|
|
public:
|
2012-12-07 20:56:13 +00:00
|
|
|
SkFilterRecord(uint32_t recordFlags, SkDevice* device, SkFILEWStream* pathStream)
|
2012-10-24 19:30:41 +00:00
|
|
|
: INHERITED(recordFlags, device)
|
|
|
|
, fTransSkipped(0)
|
|
|
|
, fTransTot(0)
|
|
|
|
, fScalesSkipped(0)
|
2012-12-07 20:56:13 +00:00
|
|
|
, 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);
|
2012-10-24 19:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE {
|
|
|
|
++fTransTot;
|
|
|
|
|
2012-11-12 18:03:19 +00:00
|
|
|
#if 0
|
2012-10-24 19:30:41 +00:00
|
|
|
if (0 == dx && 0 == dy) {
|
|
|
|
++fTransSkipped;
|
|
|
|
return true;
|
|
|
|
}
|
2012-11-12 18:03:19 +00:00
|
|
|
#endif
|
2012-10-24 19:30:41 +00:00
|
|
|
|
|
|
|
return INHERITED::translate(dx, dy);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE {
|
|
|
|
++fScalesTot;
|
|
|
|
|
2012-11-12 18:03:19 +00:00
|
|
|
#if 0
|
2012-10-24 19:30:41 +00:00
|
|
|
if (SK_Scalar1 == sx && SK_Scalar1 == sy) {
|
|
|
|
++fScalesSkipped;
|
|
|
|
return true;
|
|
|
|
}
|
2012-11-12 18:03:19 +00:00
|
|
|
#endif
|
2012-10-24 19:30:41 +00:00
|
|
|
|
|
|
|
return INHERITED::scale(sx, sy);
|
2012-10-19 19:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2012-10-24 19:30:41 +00:00
|
|
|
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;
|
|
|
|
|
2012-12-07 20:56:13 +00:00
|
|
|
SkFILEWStream* fPathStream;
|
2012-10-19 19:06:11 +00:00
|
|
|
private:
|
|
|
|
typedef SkPictureRecord INHERITED;
|
|
|
|
};
|
|
|
|
|
2012-10-24 19:30:41 +00:00
|
|
|
// Wrap SkPicture to allow installation of a SkFilterRecord object
|
|
|
|
class SkFilterPicture : public SkPicture {
|
|
|
|
public:
|
2012-10-25 14:45:08 +00:00
|
|
|
SkFilterPicture(int width, int height, SkPictureRecord* record) {
|
|
|
|
fWidth = width;
|
|
|
|
fHeight = height;
|
2012-10-24 19:30:41 +00:00
|
|
|
fRecord = record;
|
|
|
|
SkSafeRef(fRecord);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SkPicture INHERITED;
|
|
|
|
};
|
|
|
|
|
2012-11-12 18:03:19 +00:00
|
|
|
static bool PNGEncodeBitmapToStream(SkWStream* stream, const SkBitmap& bitmap) {
|
|
|
|
return SkImageEncoder::EncodeStream(stream, bitmap, SkImageEncoder::kPNG_Type, 100);
|
|
|
|
}
|
2012-10-24 19:30:41 +00:00
|
|
|
|
2012-12-08 02:01:38 +00:00
|
|
|
int filter_picture(const SkString& inFile, const SkString& outFile,
|
2012-12-07 20:56:13 +00:00
|
|
|
const SkString& textureDir, SkFILEWStream *pathStream) {
|
2012-11-13 18:50:33 +00:00
|
|
|
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)));
|
|
|
|
|
2012-12-07 20:56:13 +00:00
|
|
|
SkAutoTUnref<SkFilterRecord> filterRecord(SkNEW_ARGS(SkFilterRecord, (0, dev, pathStream)));
|
2012-11-13 18:50:33 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2012-10-08 14:41:10 +00:00
|
|
|
// This function is not marked as 'static' so it can be referenced externally
|
|
|
|
// in the iOS build.
|
2012-10-09 12:32:37 +00:00
|
|
|
int tool_main(int argc, char** argv) {
|
2012-10-04 13:00:33 +00:00
|
|
|
SkGraphics::Init();
|
|
|
|
|
2012-10-19 19:06:11 +00:00
|
|
|
if (argc < 3) {
|
2012-10-04 13:00:33 +00:00
|
|
|
usage();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-12-07 20:56:13 +00:00
|
|
|
SkString inFile, outFile, inDir, outDir, textureDir, pathFile;
|
2012-10-19 19:06:11 +00:00
|
|
|
|
2012-10-04 13:00:33 +00:00
|
|
|
char* const* stop = argv + argc;
|
|
|
|
for (++argv; argv < stop; ++argv) {
|
|
|
|
if (strcmp(*argv, "-i") == 0) {
|
|
|
|
argv++;
|
|
|
|
if (argv < stop && **argv) {
|
|
|
|
inFile.set(*argv);
|
|
|
|
} else {
|
2012-10-19 19:06:11 +00:00
|
|
|
SkDebugf("missing arg for -i\n");
|
2012-10-04 13:00:33 +00:00
|
|
|
usage();
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-13 18:50:33 +00:00
|
|
|
} 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;
|
|
|
|
}
|
2012-10-04 13:00:33 +00:00
|
|
|
} else if (strcmp(*argv, "-o") == 0) {
|
|
|
|
argv++;
|
|
|
|
if (argv < stop && **argv) {
|
|
|
|
outFile.set(*argv);
|
|
|
|
} else {
|
2012-10-19 19:06:11 +00:00
|
|
|
SkDebugf("missing arg for -o\n");
|
|
|
|
usage();
|
|
|
|
return -1;
|
|
|
|
}
|
2012-12-07 20:56:13 +00:00
|
|
|
} else if (strcmp(*argv, "-p") == 0) {
|
|
|
|
argv++;
|
|
|
|
if (argv < stop && **argv) {
|
|
|
|
pathFile.set(*argv);
|
|
|
|
} else {
|
|
|
|
SkDebugf("missing arg for -p\n");
|
|
|
|
usage();
|
|
|
|
return -1;
|
|
|
|
}
|
2012-10-19 19:06:11 +00:00
|
|
|
} else if (strcmp(*argv, "-t") == 0) {
|
|
|
|
argv++;
|
|
|
|
if (argv < stop && **argv) {
|
|
|
|
textureDir.set(*argv);
|
|
|
|
} else {
|
|
|
|
SkDebugf("missing arg for -t\n");
|
2012-10-04 13:00:33 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-13 18:50:33 +00:00
|
|
|
if(!inDir.isEmpty() && !textureDir.isEmpty()) {
|
|
|
|
SkDebugf("ERROR: The textureDir option is not permitted when passing an input directory.\n");
|
2012-10-19 19:06:11 +00:00
|
|
|
usage();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-12-07 20:56:13 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-11-13 18:50:33 +00:00
|
|
|
SkOSFile::Iter iter(inDir.c_str(), "skp");
|
|
|
|
int failures = 0;
|
|
|
|
SkString inputFilename, outputFilename;
|
|
|
|
if (iter.next(&inputFilename)) {
|
2012-10-04 13:00:33 +00:00
|
|
|
|
2012-11-13 18:50:33 +00:00
|
|
|
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());
|
2012-12-07 20:56:13 +00:00
|
|
|
filter_picture(inFile, outFile, textureDir, pathStream);
|
2012-11-13 18:50:33 +00:00
|
|
|
} while(iter.next(&inputFilename));
|
2012-10-04 13:00:33 +00:00
|
|
|
|
2012-11-13 18:50:33 +00:00
|
|
|
} else if (!inFile.isEmpty()) {
|
2012-12-07 20:56:13 +00:00
|
|
|
filter_picture(inFile, outFile, textureDir, pathStream);
|
2012-11-13 18:50:33 +00:00
|
|
|
} else {
|
|
|
|
usage();
|
2012-12-07 20:56:13 +00:00
|
|
|
if (NULL != pathStream) {
|
|
|
|
delete pathStream;
|
|
|
|
pathStream = NULL;
|
|
|
|
}
|
2012-10-04 13:00:33 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-12-07 20:56:13 +00:00
|
|
|
if (NULL != pathStream) {
|
|
|
|
sk_tools::dump_path_suffix(pathStream);
|
|
|
|
delete pathStream;
|
|
|
|
pathStream = NULL;
|
|
|
|
}
|
|
|
|
|
2012-10-04 13:00:33 +00:00
|
|
|
SkGraphics::Term();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined SK_BUILD_FOR_IOS
|
|
|
|
int main(int argc, char * const argv[]) {
|
2012-10-09 12:32:37 +00:00
|
|
|
return tool_main(argc, (char**) argv);
|
2012-10-04 13:00:33 +00:00
|
|
|
}
|
|
|
|
#endif
|