2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "SkColorPriv.h"
12 #include "SkImageEncoder.h"
14 #include "SkPicture.h"
18 #include "PictureRenderer.h"
19 #include "picture_utils.h"
21 static void usage(const char* argv0) {
22 SkDebugf("SkPicture rendering tool\n");
25 " %s <input>... <outputDir> \n"
30 " input: A list of directories and files to use as input.\n"
31 " Files are expected to have the .skp extension.\n");
33 " outputDir: directory to write the rendered images.\n");
35 " --pipe : Render using a SkGPipe\n");
37 " --tile : Render using tiles.\n");
40 static 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);
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
51 static 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);
60 static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
62 bitmap.copyTo(©, SkBitmap::kARGB_8888_Config);
63 force_all_opaque(copy);
64 return SkImageEncoder::EncodeFile(path.c_str(), copy,
65 SkImageEncoder::kPNG_Type, 100);
68 static void write_output(const SkString& outputDir, const SkString& inputFilename,
69 const SkBitmap& bitmap) {
71 make_output_filepath(&outputPath, outputDir, inputFilename);
72 bool isWritten = write_bitmap(outputPath, bitmap);
74 SkDebugf("Could not write to file %s\n", outputPath.c_str());
78 static void render_picture(const SkString& inputPath, const SkString& outputDir,
79 sk_tools::PictureRenderer& renderer) {
80 SkString inputFilename;
81 sk_tools::get_basename(&inputFilename, inputPath);
83 SkFILEStream inputStream;
84 inputStream.setPath(inputPath.c_str());
85 if (!inputStream.isValid()) {
86 SkDebugf("Could not open file %s\n", inputPath.c_str());
90 SkPicture picture(&inputStream);
92 sk_tools::setup_bitmap(&bitmap, picture.width(), picture.height());
93 SkCanvas canvas(bitmap);
95 renderer.init(picture);
96 renderer.render(&picture, &canvas);
97 write_output(outputDir, inputFilename, bitmap);
100 static 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;
105 if (iter.next(&inputFilename)) {
108 sk_tools::make_filepath(&inputPath, input, inputFilename);
109 render_picture(inputPath, outputDir, renderer);
110 } while(iter.next(&inputFilename));
112 SkString inputPath(input);
113 render_picture(inputPath, outputDir, renderer);
117 static 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;
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"))) {
132 inputs->push_back(SkString(*argv));
136 if (inputs->count() < 2) {
142 if (NULL == renderer) {
143 renderer = SkNEW(sk_tools::SimplePictureRenderer);
147 int main(int argc, char* const argv[]) {
148 SkTArray<SkString> inputs;
149 sk_tools::PictureRenderer* renderer = NULL;
151 parse_commandline(argc, argv, &inputs, renderer);
152 SkString outputDir = inputs[inputs.count() - 1];
155 for (int i = 0; i < inputs.count() - 1; i ++) {
156 process_input(inputs[i], outputDir, *renderer);