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"
|
2016-12-08 15:53:24 +00:00
|
|
|
#include "SkColorSpace.h"
|
2016-02-12 22:47:09 +00:00
|
|
|
#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"
|
2017-12-16 13:41:28 +00:00
|
|
|
#include "SkSerialProcs.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
|
|
|
|
2016-12-08 15:53:24 +00:00
|
|
|
#include <iostream>
|
2016-04-25 17:35:03 +00:00
|
|
|
#include <map>
|
|
|
|
|
2017-06-05 14:22:53 +00:00
|
|
|
DEFINE_string2(skps, s, "skps", "A path to a directory of skps or a single skp.");
|
2016-02-29 17:05:32 +00:00
|
|
|
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.");
|
2016-12-08 15:53:24 +00:00
|
|
|
DEFINE_bool(writeImages, true,
|
|
|
|
"Indicates if we want to write out supported/decoded images.");
|
|
|
|
DEFINE_bool(writeFailedImages, false,
|
|
|
|
"Indicates if we want to write out unsupported/failed to decode images.");
|
2016-04-25 17:35:03 +00:00
|
|
|
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-12-08 15:53:24 +00:00
|
|
|
static std::map<std::string, unsigned int> gSkpToUnsupportedCount;
|
2016-02-12 22:47:09 +00:00
|
|
|
|
2016-02-29 17:05:32 +00:00
|
|
|
static SkTHashSet<SkMD5::Digest> gSeen;
|
|
|
|
|
2017-12-16 13:41:28 +00:00
|
|
|
struct Sniffer {
|
2016-02-29 17:05:32 +00:00
|
|
|
|
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));
|
2017-07-23 19:30:02 +00:00
|
|
|
std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(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
|
|
|
|
2017-07-10 15:23:07 +00:00
|
|
|
auto writeImage = [&] (const char* name, int num) {
|
2016-12-08 15:53:24 +00:00
|
|
|
SkString path;
|
2017-07-10 15:23:07 +00:00
|
|
|
path.appendf("%s/%s%d.%s", gOutputDir, name, num, ext.c_str());
|
2016-12-08 15:53:24 +00:00
|
|
|
|
|
|
|
SkFILEWStream file(path.c_str());
|
|
|
|
file.write(ptr, len);
|
|
|
|
|
|
|
|
SkDebugf("%s\n", path.c_str());
|
|
|
|
};
|
|
|
|
|
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());
|
2017-07-10 15:23:07 +00:00
|
|
|
switch (result) {
|
|
|
|
case SkCodec::kSuccess:
|
|
|
|
case SkCodec::kIncompleteInput:
|
|
|
|
case SkCodec::kErrorInInput:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SkDebugf("Decoding failed for %s\n", skpName.c_str());
|
|
|
|
if (FLAGS_writeFailedImages) {
|
|
|
|
writeImage("unknown", gSkpToUnknownCount[skpName]);
|
|
|
|
}
|
|
|
|
gSkpToUnknownCount[skpName]++;
|
|
|
|
return;
|
2016-04-25 17:35:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FLAGS_writeImages) {
|
2017-07-10 15:23:07 +00:00
|
|
|
writeImage("", gKnown);
|
2016-12-08 15:53:24 +00:00
|
|
|
}
|
2016-02-29 17:05:32 +00:00
|
|
|
|
2016-04-25 17:35:03 +00:00
|
|
|
gKnown++;
|
2016-02-12 22:47:09 +00:00
|
|
|
}
|
2016-02-29 17:05:32 +00:00
|
|
|
};
|
2016-02-12 22:47:09 +00:00
|
|
|
|
2017-07-10 15:23:07 +00:00
|
|
|
static bool get_images_from_file(const SkString& file) {
|
2018-03-09 18:08:38 +00:00
|
|
|
Sniffer sniff(file.c_str());
|
2017-06-05 14:22:53 +00:00
|
|
|
auto stream = SkStream::MakeFromFile(file.c_str());
|
|
|
|
|
2018-03-09 18:08:38 +00:00
|
|
|
SkDeserialProcs procs;
|
|
|
|
procs.fImageProc = [](const void* data, size_t size, void* ctx) -> sk_sp<SkImage> {
|
|
|
|
((Sniffer*)ctx)->sniff(data, size);
|
|
|
|
return nullptr;
|
2017-12-16 13:41:28 +00:00
|
|
|
};
|
|
|
|
procs.fImageCtx = &sniff;
|
2018-03-09 18:08:38 +00:00
|
|
|
return SkPicture::MakeFromStream(stream.get(), &procs) != nullptr;
|
2017-06-05 14:22:53 +00:00
|
|
|
}
|
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 "
|
2017-06-09 14:18:34 +00:00
|
|
|
"-j <output JSON path> --writeImages, --writeFailedImages\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
|
|
|
|
2017-06-05 14:22:53 +00:00
|
|
|
if (!sk_isdir(gOutputDir)) {
|
2016-02-12 22:47:09 +00:00
|
|
|
SkCommandLineFlags::PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-06-05 14:22:53 +00:00
|
|
|
if (sk_isdir(inputs)) {
|
|
|
|
SkOSFile::Iter iter(inputs, "skp");
|
|
|
|
for (SkString file; iter.next(&file); ) {
|
2017-07-10 15:23:07 +00:00
|
|
|
if (!get_images_from_file(SkOSPath::Join(inputs, file.c_str()))) {
|
|
|
|
return 2;
|
|
|
|
}
|
2017-06-05 14:22:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-07-10 15:23:07 +00:00
|
|
|
if (!get_images_from_file(SkString(inputs))) {
|
|
|
|
return 2;
|
|
|
|
}
|
2016-02-12 22:47:09 +00:00
|
|
|
}
|
2016-04-25 17:35:03 +00:00
|
|
|
/**
|
|
|
|
JSON results are written out in the following format:
|
|
|
|
{
|
|
|
|
"failures": {
|
|
|
|
"skp1": 12,
|
|
|
|
"skp4": 2,
|
|
|
|
...
|
|
|
|
},
|
2016-12-08 15:53:24 +00:00
|
|
|
"unsupported": {
|
|
|
|
"skp9": 13,
|
|
|
|
"skp17": 3,
|
|
|
|
...
|
|
|
|
}
|
2016-04-25 17:35:03 +00:00
|
|
|
"totalFailures": 32,
|
2016-12-08 15:53:24 +00:00
|
|
|
"totalUnsupported": 9,
|
2016-04-25 17:35:03 +00:00
|
|
|
"totalSuccesses": 21,
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
Json::Value fRoot;
|
2016-12-08 15:53:24 +00:00
|
|
|
int totalFailures = 0;
|
2016-04-25 17:35:03 +00:00
|
|
|
for(auto it = gSkpToUnknownCount.cbegin(); it != gSkpToUnknownCount.cend(); ++it)
|
|
|
|
{
|
|
|
|
SkDebugf("%s %d\n", it->first.c_str(), it->second);
|
2016-12-08 15:53:24 +00:00
|
|
|
totalFailures += it->second;
|
2016-04-25 17:35:03 +00:00
|
|
|
fRoot["failures"][it->first.c_str()] = it->second;
|
|
|
|
}
|
2016-12-08 15:53:24 +00:00
|
|
|
fRoot["totalFailures"] = totalFailures;
|
|
|
|
int totalUnsupported = 0;
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
for (const auto& unsupported : gSkpToUnsupportedCount) {
|
|
|
|
SkDebugf("%s %d\n", unsupported.first.c_str(), unsupported.second);
|
|
|
|
totalUnsupported += unsupported.second;
|
|
|
|
fRoot["unsupported"][unsupported.first] = unsupported.second;
|
|
|
|
}
|
|
|
|
fRoot["totalUnsupported"] = totalUnsupported;
|
|
|
|
#endif
|
2016-04-25 17:35:03 +00:00
|
|
|
fRoot["totalSuccesses"] = gKnown;
|
2016-12-08 15:53:24 +00:00
|
|
|
SkDebugf("%d known, %d failures, %d unsupported\n", gKnown, totalFailures, totalUnsupported);
|
|
|
|
if (totalFailures > 0 || totalUnsupported > 0) {
|
2016-04-26 15:27:49 +00:00
|
|
|
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();
|
|
|
|
}
|
2016-04-25 17:35:03 +00:00
|
|
|
}
|
2016-02-12 22:47:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|