get_images_from_skps.cpp revision 7a307df926da2afbdadfb36250925c7f76e3493a
1/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkCodec.h"
9#include "SkCommandLineFlags.h"
10#include "SkData.h"
11#include "SkMD5.h"
12#include "SkOSFile.h"
13#include "SkPicture.h"
14#include "SkPixelSerializer.h"
15#include "SkStream.h"
16#include "SkTHash.h"
17
18DEFINE_string2(skps, s, "skps", "A path to a directory of skps.");
19DEFINE_string2(out, o, "img-out", "A path to an output directory.");
20
21static int gKnown;
22static int gUnknown;
23static const char* gOutputDir;
24
25static SkTHashSet<SkMD5::Digest> gSeen;
26
27struct Sniffer : public SkPixelSerializer {
28
29    void sniff(const void* ptr, size_t len) {
30        SkMD5 md5;
31        md5.write(ptr, len);
32        SkMD5::Digest digest;
33        md5.finish(digest);
34
35        if (gSeen.contains(digest)) {
36            return;
37        }
38        gSeen.add(digest);
39
40        SkAutoTUnref<SkData> data(SkData::NewWithoutCopy(ptr, len));
41        SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data));
42        if (!codec) {
43            gUnknown++;
44            return;
45        }
46        SkString ext;
47        switch (codec->getEncodedFormat()) {
48            case SkEncodedFormat::kBMP_SkEncodedFormat:  ext =  "bmp"; break;
49            case SkEncodedFormat::kGIF_SkEncodedFormat:  ext =  "gif"; break;
50            case SkEncodedFormat::kICO_SkEncodedFormat:  ext =  "ico"; break;
51            case SkEncodedFormat::kJPEG_SkEncodedFormat: ext =  "jpg"; break;
52            case SkEncodedFormat::kPNG_SkEncodedFormat:  ext =  "png"; break;
53            case SkEncodedFormat::kDNG_SkEncodedFormat:  ext =  "dng"; break;
54            case SkEncodedFormat::kWBMP_SkEncodedFormat: ext = "wbmp"; break;
55            case SkEncodedFormat::kWEBP_SkEncodedFormat: ext = "webp"; break;
56            default: gUnknown++; return;
57        }
58
59        SkString path;
60        path.appendf("%s/%d.%s", gOutputDir, gKnown++, ext.c_str());
61
62        SkFILEWStream file(path.c_str());
63        file.write(ptr, len);
64
65        SkDebugf("%s\n", path.c_str());
66    }
67
68    bool onUseEncodedData(const void* ptr, size_t len) override {
69        this->sniff(ptr, len);
70        return true;
71    }
72    SkData* onEncode(const SkPixmap&) override { return nullptr; }
73};
74
75
76int main(int argc, char** argv) {
77    SkCommandLineFlags::SetUsage(
78            "Usage: get_images_from_skps -s <dir of skps> -o <dir for output images>\n");
79
80    SkCommandLineFlags::Parse(argc, argv);
81    const char* inputs = FLAGS_skps[0];
82    gOutputDir = FLAGS_out[0];
83
84    if (!sk_isdir(inputs) || !sk_isdir(gOutputDir)) {
85        SkCommandLineFlags::PrintUsage();
86        return 1;
87    }
88
89    SkOSFile::Iter iter(inputs, "skp");
90    for (SkString file; iter.next(&file); ) {
91        SkAutoTDelete<SkStream> stream =
92                SkStream::NewFromFile(SkOSPath::Join(inputs, file.c_str()).c_str());
93        SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(stream));
94
95        SkDynamicMemoryWStream scratch;
96        Sniffer sniff;
97        picture->serialize(&scratch, &sniff);
98    }
99    SkDebugf("%d known, %d unknown\n", gKnown, gUnknown);
100
101    return 0;
102}
103