blob: da7df634b34f16d7672bd6566ef7a6a70aa5098d [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
19
20static void usage(const char* argv0) {
21 SkDebugf("SkPicture rendering tool\n");
22 SkDebugf("\n"
23"Usage: \n"
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000024" %s <input>... <outputDir> \n\n"
junov@chromium.org777442d2012-06-12 14:56:36 +000025, argv0);
26 SkDebugf(
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000027" input: A list of directories and files to use as input.\n"
28" Files are expected to have the .skp extension.\n");
junov@chromium.org777442d2012-06-12 14:56:36 +000029 SkDebugf(
30" outputDir: directory to write the rendered images.\n");
31}
32
junov@chromium.org777442d2012-06-12 14:56:36 +000033static void make_output_filepath(SkString* path, const char* dir,
34 const SkString& name) {
twiz@google.coma31b8bb2012-06-22 18:24:56 +000035 sk_tools::make_filepath(path, dir, name);
junov@chromium.org777442d2012-06-12 14:56:36 +000036 path->remove(path->size() - 3, 3);
37 path->append("png");
38}
39
junov@chromium.org777442d2012-06-12 14:56:36 +000040static void generate_image_from_picture(SkPicture& pict, SkBitmap* bitmap) {
twiz@google.coma31b8bb2012-06-22 18:24:56 +000041 sk_tools::setup_bitmap(bitmap, pict.width(), pict.height());
junov@chromium.org777442d2012-06-12 14:56:36 +000042 SkCanvas canvas(*bitmap);
43 canvas.drawPicture(pict);
44}
45
46/* since PNG insists on unpremultiplying our alpha, we take no precision chances
47 and force all pixels to be 100% opaque, otherwise on compare we may not get
48 a perfect match.
49 */
50static void force_all_opaque(const SkBitmap& bitmap) {
51 SkAutoLockPixels lock(bitmap);
52 for (int y = 0; y < bitmap.height(); y++) {
53 for (int x = 0; x < bitmap.width(); x++) {
54 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
55 }
56 }
57}
58
59static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
60 SkBitmap copy;
61 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
62 force_all_opaque(copy);
63 return SkImageEncoder::EncodeFile(path.c_str(), copy,
64 SkImageEncoder::kPNG_Type, 100);
65}
66
67static void write_output(const char* outputDir, const SkString& inputFilename,
68 const SkBitmap& bitmap) {
69 SkString outputPath;
70 make_output_filepath(&outputPath, outputDir, inputFilename);
71 bool isWritten = write_bitmap(outputPath, bitmap);
72 if (!isWritten) {
73 SkDebugf("Could not write to file %s\n", outputPath.c_str());
74 }
75}
76
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000077static void render_picture(const SkString& inputPath, const char* outputDir) {
78 SkString inputFilename;
79 sk_tools::get_basename(&inputFilename, inputPath);
twiz@google.coma31b8bb2012-06-22 18:24:56 +000080
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000081 SkFILEStream inputStream;
twiz@google.coma31b8bb2012-06-22 18:24:56 +000082 inputStream.setPath(inputPath.c_str());
83 if (!inputStream.isValid()) {
84 SkDebugf("Could not open file %s\n", inputPath.c_str());
85 return;
86 }
87
junov@chromium.org777442d2012-06-12 14:56:36 +000088 SkPicture picture(&inputStream);
89 SkBitmap bitmap;
90 generate_image_from_picture(picture, &bitmap);
91 write_output(outputDir, inputFilename, bitmap);
92}
93
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000094static void process_input(const char* input, const char* outputDir) {
95 SkOSFile::Iter iter(input, "skp");
junov@chromium.org777442d2012-06-12 14:56:36 +000096 SkString inputFilename;
97
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000098 if (iter.next(&inputFilename)) {
99 do {
100 SkString inputPath;
101 sk_tools::make_filepath(&inputPath, input, inputFilename);
102 render_picture(inputPath, outputDir);
103 } while(iter.next(&inputFilename));
104 } else {
105 SkString inputPath(input);
106 render_picture(inputPath, outputDir);
107 }
108}
109
110int main(int argc, char* const argv[]) {
111 const char* outputDir;
112 if (argc < 3) {
113 usage(argv[0]);
114 return -1;
115 }
116
117 outputDir = argv[argc - 1];
118
119 for (int i = 1; i < argc - 1; i ++) {
120 process_input(argv[i], outputDir);
junov@chromium.org777442d2012-06-12 14:56:36 +0000121 }
122}