1/*
2 * Copyright 2012 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 "SkBitmap.h"
9#include "SkCanvas.h"
10#include "SkGraphics.h"
11#include "SkOSFile.h"
12#include "SkImageDecoder.h"
13#include "SkPicture.h"
14#include "SkStream.h"
15#include "SkString.h"
16#include "SkDumpCanvas.h"
17
18static SkPicture* inspect(const char path[]) {
19    SkFILEStream stream(path);
20    if (!stream.isValid()) {
21        printf("-- Can't open '%s'\n", path);
22        return nullptr;
23    }
24
25    printf("Opening '%s'...\n", path);
26
27    {
28        int32_t header[3];
29        if (stream.read(header, sizeof(header)) != sizeof(header)) {
30            printf("-- Failed to read header (12 bytes)\n");
31            return nullptr;
32        }
33        printf("version:%d width:%d height:%d\n", header[0], header[1], header[2]);
34    }
35
36    stream.rewind();
37    SkPicture* pic = SkPicture::CreateFromStream(&stream);
38    if (nullptr == pic) {
39        SkDebugf("Could not create SkPicture: %s\n", path);
40        return nullptr;
41    }
42    printf("picture cullRect: [%f %f %f %f]\n",
43           pic->cullRect().fLeft, pic->cullRect().fTop,
44           pic->cullRect().fRight, pic->cullRect().fBottom);
45    return pic;
46}
47
48static void dumpOps(SkPicture* pic) {
49#ifdef SK_DEVELOPER
50    SkDebugfDumper dumper;
51    SkDumpCanvas canvas(&dumper);
52    canvas.drawPicture(pic);
53#else
54    printf("SK_DEVELOPER mode not enabled\n");
55#endif
56}
57
58int tool_main(int argc, char** argv);
59int tool_main(int argc, char** argv) {
60    SkAutoGraphics ag;
61    if (argc < 2) {
62        printf("Usage: pinspect [--dump-ops] filename [filename ...]\n");
63        return 1;
64    }
65
66    bool doDumpOps = false;
67
68    int index = 1;
69    if (!strcmp(argv[index], "--dump-ops")) {
70        index += 1;
71        doDumpOps = true;
72    }
73
74    for (; index < argc; ++index) {
75        SkAutoTUnref<SkPicture> pic(inspect(argv[index]));
76        if (doDumpOps) {
77            dumpOps(pic);
78        }
79        if (index < argc - 1) {
80            printf("\n");
81        }
82    }
83    return 0;
84}
85
86#if !defined SK_BUILD_FOR_IOS
87int main(int argc, char * const argv[]) {
88    return tool_main(argc, (char**) argv);
89}
90#endif
91