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.
11 #include "SkColorPriv.h"
12 #include "SkImageEncoder.h"
14 #include "SkPicture.h"
17 #include "picture_utils.h"
20 static void usage(const char* argv0) {
21 SkDebugf("SkPicture rendering tool\n");
24 " %s <inputDir> <outputDir> \n\n"
27 " inputDir: directory to read the serialized SkPicture files.\n");
29 " outputDir: directory to write the rendered images.\n");
32 static void make_output_filepath(SkString* path, const char* dir,
33 const SkString& name) {
34 sk_tools::make_filepath(path, dir, name);
35 path->remove(path->size() - 3, 3);
39 static void generate_image_from_picture(SkPicture& pict, SkBitmap* bitmap) {
40 sk_tools::setup_bitmap(bitmap, pict.width(), pict.height());
41 SkCanvas canvas(*bitmap);
42 canvas.drawPicture(pict);
45 /* since PNG insists on unpremultiplying our alpha, we take no precision chances
46 and force all pixels to be 100% opaque, otherwise on compare we may not get
49 static void force_all_opaque(const SkBitmap& bitmap) {
50 SkAutoLockPixels lock(bitmap);
51 for (int y = 0; y < bitmap.height(); y++) {
52 for (int x = 0; x < bitmap.width(); x++) {
53 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
58 static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
60 bitmap.copyTo(©, SkBitmap::kARGB_8888_Config);
61 force_all_opaque(copy);
62 return SkImageEncoder::EncodeFile(path.c_str(), copy,
63 SkImageEncoder::kPNG_Type, 100);
66 static void write_output(const char* outputDir, const SkString& inputFilename,
67 const SkBitmap& bitmap) {
69 make_output_filepath(&outputPath, outputDir, inputFilename);
70 bool isWritten = write_bitmap(outputPath, bitmap);
72 SkDebugf("Could not write to file %s\n", outputPath.c_str());
76 static void render_picture(const char* inputDir, const char* outputDir,
77 const SkString& inputFilename) {
78 SkFILEStream inputStream;
81 sk_tools::make_filepath(&inputPath, inputDir, inputFilename);
82 inputStream.setPath(inputPath.c_str());
83 if (!inputStream.isValid()) {
84 SkDebugf("Could not open file %s\n", inputPath.c_str());
88 SkPicture picture(&inputStream);
90 generate_image_from_picture(picture, &bitmap);
91 write_output(outputDir, inputFilename, bitmap);
94 int main(int argc, char* const argv[]) {
96 const char* outputDir;
104 SkOSFile::Iter iter(inputDir, "skp");
105 SkString inputFilename;
107 while(iter.next(&inputFilename)) {
108 render_picture(inputDir, outputDir, inputFilename);