2016-02-12 22:47:09 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SkCodec.h"
|
|
|
|
#include "SkCommandLineFlags.h"
|
|
|
|
#include "SkData.h"
|
2016-02-29 17:05:32 +00:00
|
|
|
#include "SkMD5.h"
|
2016-02-12 22:47:09 +00:00
|
|
|
#include "SkOSFile.h"
|
|
|
|
#include "SkPicture.h"
|
2016-02-29 17:05:32 +00:00
|
|
|
#include "SkPixelSerializer.h"
|
2016-02-12 22:47:09 +00:00
|
|
|
#include "SkStream.h"
|
2016-02-29 17:05:32 +00:00
|
|
|
#include "SkTHash.h"
|
2016-02-12 22:47:09 +00:00
|
|
|
|
2016-02-29 17:05:32 +00:00
|
|
|
DEFINE_string2(skps, s, "skps", "A path to a directory of skps.");
|
|
|
|
DEFINE_string2(out, o, "img-out", "A path to an output directory.");
|
2016-02-12 22:47:09 +00:00
|
|
|
|
2016-02-29 17:05:32 +00:00
|
|
|
static int gKnown;
|
|
|
|
static int gUnknown;
|
2016-02-12 22:47:09 +00:00
|
|
|
static const char* gOutputDir;
|
|
|
|
|
2016-02-29 17:05:32 +00:00
|
|
|
static SkTHashSet<SkMD5::Digest> gSeen;
|
|
|
|
|
|
|
|
struct Sniffer : public SkPixelSerializer {
|
|
|
|
|
|
|
|
void sniff(const void* ptr, size_t len) {
|
|
|
|
SkMD5 md5;
|
|
|
|
md5.write(ptr, len);
|
|
|
|
SkMD5::Digest digest;
|
|
|
|
md5.finish(digest);
|
2016-02-12 22:47:09 +00:00
|
|
|
|
2016-02-29 17:05:32 +00:00
|
|
|
if (gSeen.contains(digest)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gSeen.add(digest);
|
2016-02-12 22:47:09 +00:00
|
|
|
|
2016-02-29 17:05:32 +00:00
|
|
|
SkAutoTUnref<SkData> data(SkData::NewWithoutCopy(ptr, len));
|
|
|
|
SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data));
|
|
|
|
if (!codec) {
|
|
|
|
gUnknown++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SkString ext;
|
2016-02-12 22:47:09 +00:00
|
|
|
switch (codec->getEncodedFormat()) {
|
2016-02-29 17:05:32 +00:00
|
|
|
case SkEncodedFormat::kBMP_SkEncodedFormat: ext = "bmp"; break;
|
|
|
|
case SkEncodedFormat::kGIF_SkEncodedFormat: ext = "gif"; break;
|
|
|
|
case SkEncodedFormat::kICO_SkEncodedFormat: ext = "ico"; break;
|
|
|
|
case SkEncodedFormat::kJPEG_SkEncodedFormat: ext = "jpg"; break;
|
|
|
|
case SkEncodedFormat::kPNG_SkEncodedFormat: ext = "png"; break;
|
2016-03-11 15:32:33 +00:00
|
|
|
case SkEncodedFormat::kDNG_SkEncodedFormat: ext = "dng"; break;
|
2016-02-29 17:05:32 +00:00
|
|
|
case SkEncodedFormat::kWBMP_SkEncodedFormat: ext = "wbmp"; break;
|
|
|
|
case SkEncodedFormat::kWEBP_SkEncodedFormat: ext = "webp"; break;
|
|
|
|
default: gUnknown++; return;
|
2016-02-12 22:47:09 +00:00
|
|
|
}
|
2016-02-29 17:05:32 +00:00
|
|
|
|
|
|
|
SkString path;
|
|
|
|
path.appendf("%s/%d.%s", gOutputDir, gKnown++, ext.c_str());
|
|
|
|
|
|
|
|
SkFILEWStream file(path.c_str());
|
|
|
|
file.write(ptr, len);
|
|
|
|
|
|
|
|
SkDebugf("%s\n", path.c_str());
|
2016-02-12 22:47:09 +00:00
|
|
|
}
|
|
|
|
|
2016-02-29 17:05:32 +00:00
|
|
|
bool onUseEncodedData(const void* ptr, size_t len) override {
|
|
|
|
this->sniff(ptr, len);
|
2016-02-12 22:47:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-02-29 17:05:32 +00:00
|
|
|
SkData* onEncode(const SkPixmap&) override { return nullptr; }
|
|
|
|
};
|
2016-02-12 22:47:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
SkCommandLineFlags::SetUsage(
|
|
|
|
"Usage: get_images_from_skps -s <dir of skps> -o <dir for output images>\n");
|
|
|
|
|
|
|
|
SkCommandLineFlags::Parse(argc, argv);
|
|
|
|
const char* inputs = FLAGS_skps[0];
|
|
|
|
gOutputDir = FLAGS_out[0];
|
2016-02-29 17:05:32 +00:00
|
|
|
|
2016-02-12 22:47:09 +00:00
|
|
|
if (!sk_isdir(inputs) || !sk_isdir(gOutputDir)) {
|
|
|
|
SkCommandLineFlags::PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkOSFile::Iter iter(inputs, "skp");
|
|
|
|
for (SkString file; iter.next(&file); ) {
|
|
|
|
SkAutoTDelete<SkStream> stream =
|
|
|
|
SkStream::NewFromFile(SkOSPath::Join(inputs, file.c_str()).c_str());
|
2016-03-18 14:25:55 +00:00
|
|
|
sk_sp<SkPicture> picture(SkPicture::MakeFromStream(stream));
|
2016-02-12 22:47:09 +00:00
|
|
|
|
2016-02-29 17:05:32 +00:00
|
|
|
SkDynamicMemoryWStream scratch;
|
|
|
|
Sniffer sniff;
|
|
|
|
picture->serialize(&scratch, &sniff);
|
2016-02-12 22:47:09 +00:00
|
|
|
}
|
2016-02-29 17:05:32 +00:00
|
|
|
SkDebugf("%d known, %d unknown\n", gKnown, gUnknown);
|
2016-02-12 22:47:09 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|