render_pictures_main.cpp revision 451bb9f801d668275394ca5bd57f238e13cf3d17
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 "SkColorPriv.h"
11#include "SkDevice.h"
12#include "SkImageEncoder.h"
13#include "SkOSFile.h"
14#include "SkPicture.h"
15#include "SkStream.h"
16#include "SkString.h"
17#include "SkTArray.h"
18#include "PictureRenderer.h"
19#include "picture_utils.h"
20
21static void usage(const char* argv0) {
22    SkDebugf("SkPicture rendering tool\n");
23    SkDebugf("\n"
24"Usage: \n"
25"     %s <input>... <outputDir> \n"
26"     [--pipe | --tile]"
27, argv0);
28    SkDebugf("\n\n");
29    SkDebugf(
30"     input:     A list of directories and files to use as input.\n"
31"                    Files are expected to have the .skp extension.\n");
32    SkDebugf(
33"     outputDir: directory to write the rendered images.\n");
34    SkDebugf(
35"     --pipe : Render using a SkGPipe\n");
36    SkDebugf(
37"     --tile : Render using tiles.\n");
38}
39
40static void make_output_filepath(SkString* path, const SkString& dir,
41                                 const SkString& name) {
42    sk_tools::make_filepath(path, dir, name);
43    path->remove(path->size() - 3, 3);
44    path->append("png");
45}
46
47/* since PNG insists on unpremultiplying our alpha, we take no precision chances
48    and force all pixels to be 100% opaque, otherwise on compare we may not get
49    a perfect match.
50 */
51static void force_all_opaque(const SkBitmap& bitmap) {
52    SkAutoLockPixels lock(bitmap);
53    for (int y = 0; y < bitmap.height(); y++) {
54        for (int x = 0; x < bitmap.width(); x++) {
55            *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
56        }
57    }
58}
59
60static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
61    SkBitmap copy;
62    bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
63    force_all_opaque(copy);
64    return SkImageEncoder::EncodeFile(path.c_str(), copy,
65                                      SkImageEncoder::kPNG_Type, 100);
66}
67
68static void write_output(const SkString& outputDir, const SkString& inputFilename,
69                         const SkBitmap& bitmap) {
70    SkString outputPath;
71    make_output_filepath(&outputPath, outputDir, inputFilename);
72    bool isWritten = write_bitmap(outputPath, bitmap);
73    if (!isWritten) {
74        SkDebugf("Could not write to file %s\n", outputPath.c_str());
75    }
76}
77
78static void render_picture(const SkString& inputPath, const SkString& outputDir,
79                           sk_tools::PictureRenderer& renderer) {
80    SkString inputFilename;
81    sk_tools::get_basename(&inputFilename, inputPath);
82
83    SkFILEStream inputStream;
84    inputStream.setPath(inputPath.c_str());
85    if (!inputStream.isValid()) {
86        SkDebugf("Could not open file %s\n", inputPath.c_str());
87        return;
88    }
89
90    SkPicture picture(&inputStream);
91    SkBitmap bitmap;
92    sk_tools::setup_bitmap(&bitmap, picture.width(), picture.height());
93    SkCanvas canvas(bitmap);
94
95    renderer.init(picture);
96    renderer.render(&picture, &canvas);
97    write_output(outputDir, inputFilename, bitmap);
98}
99
100static void process_input(const SkString& input, const SkString& outputDir,
101                          sk_tools::PictureRenderer& renderer) {
102    SkOSFile::Iter iter(input.c_str(), "skp");
103    SkString inputFilename;
104
105    if (iter.next(&inputFilename)) {
106        do {
107            SkString inputPath;
108            sk_tools::make_filepath(&inputPath, input, inputFilename);
109            render_picture(inputPath, outputDir, renderer);
110        } while(iter.next(&inputFilename));
111    } else {
112        SkString inputPath(input);
113        render_picture(inputPath, outputDir, renderer);
114    }
115}
116
117static void parse_commandline(int argc, char* const argv[], SkTArray<SkString>* inputs,
118                              sk_tools::PictureRenderer*& renderer){
119    const char* argv0 = argv[0];
120    char* const* stop = argv + argc;
121
122    for (++argv; argv < stop; ++argv) {
123        if (0 == strcmp(*argv, "--pipe")) {
124            renderer = SkNEW(sk_tools::PipePictureRenderer);
125        } else if (0 == strcmp(*argv, "--tile")) {
126            renderer = SkNEW(sk_tools::TiledPictureRenderer);
127        } else if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) {
128            SkDELETE(renderer);
129            usage(argv0);
130            exit(-1);
131        } else {
132            inputs->push_back(SkString(*argv));
133        }
134    }
135
136    if (inputs->count() < 2) {
137        SkDELETE(renderer);
138        usage(argv0);
139        exit(-1);
140    }
141
142    if (NULL == renderer) {
143        renderer = SkNEW(sk_tools::SimplePictureRenderer);
144    }
145}
146
147int main(int argc, char* const argv[]) {
148    SkTArray<SkString> inputs;
149    sk_tools::PictureRenderer* renderer = NULL;
150
151    parse_commandline(argc, argv, &inputs, renderer);
152    SkString outputDir = inputs[inputs.count() - 1];
153    SkASSERT(renderer);
154
155    for (int i = 0; i < inputs.count() - 1; i ++) {
156        process_input(inputs[i], outputDir, *renderer);
157    }
158
159    SkDELETE(renderer);
160}
161