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"
|
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
|
|
|
|
2016-12-08 15:53:24 +00:00
|
|
|
#include <iostream>
|
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.");
|
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.");
|
|
|
|
DEFINE_bool(testICCSupport, false,
|
|
|
|
"Indicates if we want to test that the images with ICC profiles are supported");
|
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;
|
|
|
|
|
|
|
|
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-12-08 15:53:24 +00:00
|
|
|
auto writeImage = [&] {
|
|
|
|
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-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());
|
2016-12-08 15:53:24 +00:00
|
|
|
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]++;
|
2016-12-08 15:53:24 +00:00
|
|
|
if (FLAGS_writeFailedImages) {
|
|
|
|
writeImage();
|
|
|
|
}
|
2016-04-25 17:35:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-08 15:53:24 +00:00
|
|
|
#ifdef SK_DEBUG
|
|
|
|
if (FLAGS_testICCSupport) {
|
|
|
|
if (codec->fUnsupportedICC) {
|
|
|
|
SkDebugf("Color correction failed for %s\n", skpName.c_str());
|
|
|
|
gSkpToUnsupportedCount[skpName]++;
|
|
|
|
if (FLAGS_writeFailedImages) {
|
|
|
|
writeImage();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-04-25 17:35:03 +00:00
|
|
|
if (FLAGS_writeImages) {
|
2016-12-08 15:53:24 +00:00
|
|
|
writeImage();
|
|
|
|
}
|
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
|
|
|
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 "
|
2016-12-08 15:53:24 +00:00
|
|
|
"-j <output JSON path> --testICCSupport --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
|
|
|
|
2016-02-12 22:47:09 +00:00
|
|
|
if (!sk_isdir(inputs) || !sk_isdir(gOutputDir)) {
|
|
|
|
SkCommandLineFlags::PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
2016-12-08 15:53:24 +00:00
|
|
|
#ifndef SK_DEBUG
|
|
|
|
if (FLAGS_testICCSupport) {
|
|
|
|
std::cerr << "--testICCSupport unavailable outside of SK_DEBUG builds" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
2016-02-12 22:47:09 +00:00
|
|
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
return -1;
|
2016-04-25 17:35:03 +00:00
|
|
|
}
|
2016-02-12 22:47:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|