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.
|
|
|
|
*/
|
|
|
|
|
2016-04-25 17:35:03 +00:00
|
|
|
#include "SkBitmap.h"
|
2016-02-12 22:47:09 +00:00
|
|
|
#include "SkCodec.h"
|
|
|
|
#include "SkCommandLineFlags.h"
|
|
|
|
#include "SkData.h"
|
2016-04-25 17:35:03 +00:00
|
|
|
#include "SkJSONCPP.h"
|
2016-02-29 17:05:32 +00:00
|
|
|
#include "SkMD5.h"
|
2016-02-12 22:47:09 +00:00
|
|
|
#include "SkOSFile.h"
|
2016-11-07 23:05:29 +00:00
|
|
|
#include "SkOSPath.h"
|
2016-02-12 22:47:09 +00:00
|
|
|
#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-04-25 17:35:03 +00:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
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-04-25 17:35:03 +00:00
|
|
|
DEFINE_bool(testDecode, false, "Indicates if we want to test that the images decode successfully.");
|
|
|
|
DEFINE_bool(writeImages, true, "Indicates if we want to write out images.");
|
|
|
|
DEFINE_string2(failuresJsonPath, j, "",
|
|
|
|
"Dump SKP and count of unknown images to the specified JSON file. Will not be "
|
|
|
|
"written anywhere if empty.");
|
2016-02-12 22:47:09 +00:00
|
|
|
|
2016-02-29 17:05:32 +00:00
|
|
|
static int gKnown;
|
2016-02-12 22:47:09 +00:00
|
|
|
static const char* gOutputDir;
|
2016-04-25 17:35:03 +00:00
|
|
|
static std::map<std::string, unsigned int> gSkpToUnknownCount = {};
|
2016-02-12 22:47:09 +00:00
|
|
|
|
2016-02-29 17:05:32 +00:00
|
|
|
static SkTHashSet<SkMD5::Digest> gSeen;
|
|
|
|
|
|
|
|
struct Sniffer : public SkPixelSerializer {
|
|
|
|
|
2016-04-25 17:35:03 +00:00
|
|
|
std::string skpName;
|
|
|
|
|
|
|
|
Sniffer(std::string name) {
|
|
|
|
skpName = name;
|
|
|
|
}
|
|
|
|
|
2016-02-29 17:05:32 +00:00
|
|
|
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-08-02 21:40:46 +00:00
|
|
|
sk_sp<SkData> data(SkData::MakeWithoutCopy(ptr, len));
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
|
2016-02-29 17:05:32 +00:00
|
|
|
if (!codec) {
|
2016-04-25 17:35:03 +00:00
|
|
|
// FIXME: This code is currently unreachable because we create an empty generator when
|
|
|
|
// we fail to create a codec.
|
|
|
|
SkDebugf("Codec could not be created for %s\n", skpName.c_str());
|
|
|
|
gSkpToUnknownCount[skpName]++;
|
2016-02-29 17:05:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
SkString ext;
|
2016-02-12 22:47:09 +00:00
|
|
|
switch (codec->getEncodedFormat()) {
|
2016-11-23 15:55:18 +00:00
|
|
|
case SkEncodedImageFormat::kBMP: ext = "bmp"; break;
|
|
|
|
case SkEncodedImageFormat::kGIF: ext = "gif"; break;
|
|
|
|
case SkEncodedImageFormat::kICO: ext = "ico"; break;
|
|
|
|
case SkEncodedImageFormat::kJPEG: ext = "jpg"; break;
|
|
|
|
case SkEncodedImageFormat::kPNG: ext = "png"; break;
|
|
|
|
case SkEncodedImageFormat::kDNG: ext = "dng"; break;
|
|
|
|
case SkEncodedImageFormat::kWBMP: ext = "wbmp"; break;
|
|
|
|
case SkEncodedImageFormat::kWEBP: ext = "webp"; break;
|
2016-04-25 17:35:03 +00:00
|
|
|
default:
|
|
|
|
// This should be unreachable because we cannot create a codec if we do not know
|
|
|
|
// the image type.
|
|
|
|
SkASSERT(false);
|
2016-02-12 22:47:09 +00:00
|
|
|
}
|
2016-02-29 17:05:32 +00:00
|
|
|
|
2016-04-25 17:35:03 +00:00
|
|
|
if (FLAGS_testDecode) {
|
|
|
|
SkBitmap bitmap;
|
|
|
|
SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
|
|
|
|
bitmap.allocPixels(info);
|
2016-04-27 14:31:23 +00:00
|
|
|
const SkCodec::Result result = codec->getPixels(
|
|
|
|
info, bitmap.getPixels(), bitmap.rowBytes());
|
|
|
|
if (SkCodec::kIncompleteInput != result && SkCodec::kSuccess != result)
|
2016-04-25 17:35:03 +00:00
|
|
|
{
|
|
|
|
SkDebugf("Decoding failed for %s\n", skpName.c_str());
|
|
|
|
gSkpToUnknownCount[skpName]++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FLAGS_writeImages) {
|
|
|
|
SkString path;
|
|
|
|
path.appendf("%s/%d.%s", gOutputDir, gKnown, ext.c_str());
|
2016-02-29 17:05:32 +00:00
|
|
|
|
2016-04-25 17:35:03 +00:00
|
|
|
SkFILEWStream file(path.c_str());
|
|
|
|
file.write(ptr, len);
|
2016-02-29 17:05:32 +00:00
|
|
|
|
2016-04-25 17:35:03 +00:00
|
|
|
SkDebugf("%s\n", path.c_str());
|
|
|
|
}
|
|
|
|
gKnown++;
|
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(
|
2016-04-25 17:35:03 +00:00
|
|
|
"Usage: get_images_from_skps -s <dir of skps> -o <dir for output images> --testDecode "
|
|
|
|
"-j <output JSON path>\n");
|
2016-02-12 22:47:09 +00:00
|
|
|
|
|
|
|
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); ) {
|
2016-09-16 13:24:20 +00:00
|
|
|
std::unique_ptr<SkStream> stream =
|
|
|
|
SkStream::MakeFromFile(SkOSPath::Join(inputs, file.c_str()).c_str());
|
|
|
|
sk_sp<SkPicture> picture(SkPicture::MakeFromStream(stream.get()));
|
2016-02-12 22:47:09 +00:00
|
|
|
|
2016-02-29 17:05:32 +00:00
|
|
|
SkDynamicMemoryWStream scratch;
|
2016-04-25 17:35:03 +00:00
|
|
|
Sniffer sniff(file.c_str());
|
2016-02-29 17:05:32 +00:00
|
|
|
picture->serialize(&scratch, &sniff);
|
2016-02-12 22:47:09 +00:00
|
|
|
}
|
2016-04-25 17:35:03 +00:00
|
|
|
int totalUnknowns = 0;
|
|
|
|
/**
|
|
|
|
JSON results are written out in the following format:
|
|
|
|
{
|
|
|
|
"failures": {
|
|
|
|
"skp1": 12,
|
|
|
|
"skp4": 2,
|
|
|
|
...
|
|
|
|
},
|
|
|
|
"totalFailures": 32,
|
|
|
|
"totalSuccesses": 21,
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
Json::Value fRoot;
|
|
|
|
for(auto it = gSkpToUnknownCount.cbegin(); it != gSkpToUnknownCount.cend(); ++it)
|
|
|
|
{
|
|
|
|
SkDebugf("%s %d\n", it->first.c_str(), it->second);
|
|
|
|
totalUnknowns += it->second;
|
|
|
|
fRoot["failures"][it->first.c_str()] = it->second;
|
|
|
|
}
|
|
|
|
SkDebugf("%d known, %d unknown\n", gKnown, totalUnknowns);
|
|
|
|
fRoot["totalFailures"] = totalUnknowns;
|
|
|
|
fRoot["totalSuccesses"] = gKnown;
|
2016-04-26 15:27:49 +00:00
|
|
|
if (totalUnknowns > 0) {
|
|
|
|
if (!FLAGS_failuresJsonPath.isEmpty()) {
|
|
|
|
SkDebugf("Writing failures to %s\n", FLAGS_failuresJsonPath[0]);
|
|
|
|
SkFILEWStream stream(FLAGS_failuresJsonPath[0]);
|
|
|
|
stream.writeText(Json::StyledWriter().write(fRoot).c_str());
|
|
|
|
stream.flush();
|
|
|
|
}
|
|
|
|
return -1;
|
2016-04-25 17:35:03 +00:00
|
|
|
}
|
2016-02-12 22:47:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|