blob: 09e1572f1765ebb75828dd27e189664cbb3da7ed [file] [log] [blame]
junov@chromium.org777442d2012-06-12 14:56:36 +00001/*
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
9#include "SkBitmap.h"
10#include "SkCanvas.h"
11#include "SkColorPriv.h"
12#include "SkImageEncoder.h"
13#include "SkOSFile.h"
14#include "SkPicture.h"
15#include "SkStream.h"
16#include "SkString.h"
twiz@google.coma31b8bb2012-06-22 18:24:56 +000017#include "picture_utils.h"
junov@chromium.org777442d2012-06-12 14:56:36 +000018
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +000019typedef void (*RenderFunc) (SkPicture*, SkBitmap*);
junov@chromium.org777442d2012-06-12 14:56:36 +000020
21static void usage(const char* argv0) {
22 SkDebugf("SkPicture rendering tool\n");
23 SkDebugf("\n"
24"Usage: \n"
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000025" %s <input>... <outputDir> \n\n"
junov@chromium.org777442d2012-06-12 14:56:36 +000026, argv0);
27 SkDebugf(
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000028" input: A list of directories and files to use as input.\n"
29" Files are expected to have the .skp extension.\n");
junov@chromium.org777442d2012-06-12 14:56:36 +000030 SkDebugf(
31" outputDir: directory to write the rendered images.\n");
32}
33
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +000034static void make_output_filepath(SkString* path, const SkString& dir,
junov@chromium.org777442d2012-06-12 14:56:36 +000035 const SkString& name) {
twiz@google.coma31b8bb2012-06-22 18:24:56 +000036 sk_tools::make_filepath(path, dir, name);
junov@chromium.org777442d2012-06-12 14:56:36 +000037 path->remove(path->size() - 3, 3);
38 path->append("png");
39}
40
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +000041static void simple_render(SkPicture* pict, SkBitmap* bitmap) {
42 sk_tools::setup_bitmap(bitmap, pict->width(), pict->height());
junov@chromium.org777442d2012-06-12 14:56:36 +000043 SkCanvas canvas(*bitmap);
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +000044 canvas.drawPicture(*pict);
junov@chromium.org777442d2012-06-12 14:56:36 +000045}
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
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +000068static void write_output(const SkString& outputDir, const SkString& inputFilename,
junov@chromium.org777442d2012-06-12 14:56:36 +000069 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
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +000078static void render_picture(const SkString& inputPath, const SkString& outputDir,
79 RenderFunc renderFunc) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000080 SkString inputFilename;
81 sk_tools::get_basename(&inputFilename, inputPath);
twiz@google.coma31b8bb2012-06-22 18:24:56 +000082
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000083 SkFILEStream inputStream;
twiz@google.coma31b8bb2012-06-22 18:24:56 +000084 inputStream.setPath(inputPath.c_str());
85 if (!inputStream.isValid()) {
86 SkDebugf("Could not open file %s\n", inputPath.c_str());
87 return;
88 }
89
junov@chromium.org777442d2012-06-12 14:56:36 +000090 SkPicture picture(&inputStream);
91 SkBitmap bitmap;
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +000092 renderFunc(&picture, &bitmap);
junov@chromium.org777442d2012-06-12 14:56:36 +000093 write_output(outputDir, inputFilename, bitmap);
94}
95
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +000096static void process_input(const SkString& input, const SkString& outputDir,
97 RenderFunc renderFunc) {
98 SkOSFile::Iter iter(input.c_str(), "skp");
junov@chromium.org777442d2012-06-12 14:56:36 +000099 SkString inputFilename;
100
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000101 if (iter.next(&inputFilename)) {
102 do {
103 SkString inputPath;
104 sk_tools::make_filepath(&inputPath, input, inputFilename);
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000105 render_picture(inputPath, outputDir, renderFunc);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000106 } while(iter.next(&inputFilename));
107 } else {
108 SkString inputPath(input);
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000109 render_picture(inputPath, outputDir, renderFunc);
110 }
111}
112
113static void parse_commandline(int argc, char* const argv[], SkTArray<SkString>* inputs,
114 RenderFunc* renderFunc){
115 const char* argv0 = argv[0];
116 char* const* stop = argv + argc;
117
118 for (++argv; argv < stop; ++argv) {
119 inputs->push_back(SkString(*argv));
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000120 }
121}
122
123int main(int argc, char* const argv[]) {
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000124 SkTArray<SkString> inputs;
125 RenderFunc renderFunc = simple_render;
126 parse_commandline(argc, argv, &inputs, &renderFunc);
127 SkString outputDir = inputs[inputs.count() - 1];
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000128
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000129 for (int i = 0; i < inputs.count() - 1; i ++) {
130 process_input(inputs[i], outputDir, renderFunc);
junov@chromium.org777442d2012-06-12 14:56:36 +0000131 }
132}