blob: 0e7232e2dc4c8cd8c5a452ae14123f8c7397edaf [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"
17
18
19static void usage(const char* argv0) {
20 SkDebugf("SkPicture rendering tool\n");
21 SkDebugf("\n"
22"Usage: \n"
23" %s <inputDir> <outputDir> \n\n"
24, argv0);
25 SkDebugf(
26" inputDir: directory to read the serialized SkPicture files.\n");
27 SkDebugf(
28" outputDir: directory to write the rendered images.\n");
29}
30
31static void make_filepath(SkString* path, const char* dir,
32 const SkString& name) {
33 size_t len = strlen(dir);
34 path->set(dir);
35 if (0 < len && '/' != dir[len - 1]) {
36 path->append("/");
37 }
38 path->append(name);
39}
40
41static void open_picture_stream(const char* inputDir,
42 const SkString& inputFilename,
43 SkFILEStream* inputStream) {
44 SkString inputPath;
45 make_filepath(&inputPath, inputDir, inputFilename);
46 inputStream->setPath(inputPath.c_str());
47 if (!inputStream->isValid()) {
48 SkDebugf("Could not open file %s\n", inputPath.c_str());
49 }
50}
51
52static void make_output_filepath(SkString* path, const char* dir,
53 const SkString& name) {
54 make_filepath(path, dir, name);
55 path->remove(path->size() - 3, 3);
56 path->append("png");
57}
58
59static void setup_bitmap(SkPicture& picture, SkBitmap* bitmap) {
60 bitmap->setConfig(SkBitmap::kARGB_8888_Config, picture.width(),
61 picture.height());
62 bitmap->allocPixels();
63 bitmap->eraseColor(0);
64}
65
66static void generate_image_from_picture(SkPicture& pict, SkBitmap* bitmap) {
67 setup_bitmap(pict, bitmap);
68 SkCanvas canvas(*bitmap);
69 canvas.drawPicture(pict);
70}
71
72/* since PNG insists on unpremultiplying our alpha, we take no precision chances
73 and force all pixels to be 100% opaque, otherwise on compare we may not get
74 a perfect match.
75 */
76static void force_all_opaque(const SkBitmap& bitmap) {
77 SkAutoLockPixels lock(bitmap);
78 for (int y = 0; y < bitmap.height(); y++) {
79 for (int x = 0; x < bitmap.width(); x++) {
80 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
81 }
82 }
83}
84
85static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
86 SkBitmap copy;
87 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
88 force_all_opaque(copy);
89 return SkImageEncoder::EncodeFile(path.c_str(), copy,
90 SkImageEncoder::kPNG_Type, 100);
91}
92
93static void write_output(const char* outputDir, const SkString& inputFilename,
94 const SkBitmap& bitmap) {
95 SkString outputPath;
96 make_output_filepath(&outputPath, outputDir, inputFilename);
97 bool isWritten = write_bitmap(outputPath, bitmap);
98 if (!isWritten) {
99 SkDebugf("Could not write to file %s\n", outputPath.c_str());
100 }
101}
102
103static void render_picture(const char* inputDir, const char* outputDir,
104 const SkString& inputFilename) {
105 SkFILEStream inputStream;
106 open_picture_stream(inputDir, inputFilename, &inputStream);
107 SkPicture picture(&inputStream);
108 SkBitmap bitmap;
109 generate_image_from_picture(picture, &bitmap);
110 write_output(outputDir, inputFilename, bitmap);
111}
112
113int main(int argc, char* const argv[]) {
114 const char* inputDir;
115 const char* outputDir;
116 if (argc != 3) {
117 usage(argv[0]);
118 }
119
120 inputDir = argv[1];
121 outputDir = argv[2];
122
123 SkOSFile::Iter iter(inputDir, "skp");
124 SkString inputFilename;
125
126 while(iter.next(&inputFilename)) {
127 render_picture(inputDir, outputDir, inputFilename);
128 }
129}