blob: 930089bdff7f3edede2b77d053fd4b62e52c50c1 [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"
24" %s <inputDir> <outputDir> \n\n"
25, argv0);
26 SkDebugf(
27" inputDir: directory to read the serialized SkPicture files.\n");
28 SkDebugf(
29" outputDir: directory to write the rendered images.\n");
30}
31
junov@chromium.org777442d2012-06-12 14:56:36 +000032static void make_output_filepath(SkString* path, const char* dir,
33 const SkString& name) {
twiz@google.coma31b8bb2012-06-22 18:24:56 +000034 sk_tools::make_filepath(path, dir, name);
junov@chromium.org777442d2012-06-12 14:56:36 +000035 path->remove(path->size() - 3, 3);
36 path->append("png");
37}
38
junov@chromium.org777442d2012-06-12 14:56:36 +000039static void generate_image_from_picture(SkPicture& pict, SkBitmap* bitmap) {
twiz@google.coma31b8bb2012-06-22 18:24:56 +000040 sk_tools::setup_bitmap(bitmap, pict.width(), pict.height());
junov@chromium.org777442d2012-06-12 14:56:36 +000041 SkCanvas canvas(*bitmap);
42 canvas.drawPicture(pict);
43}
44
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
47 a perfect match.
48 */
49static 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);
54 }
55 }
56}
57
58static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
59 SkBitmap copy;
60 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
61 force_all_opaque(copy);
62 return SkImageEncoder::EncodeFile(path.c_str(), copy,
63 SkImageEncoder::kPNG_Type, 100);
64}
65
66static void write_output(const char* outputDir, const SkString& inputFilename,
67 const SkBitmap& bitmap) {
68 SkString outputPath;
69 make_output_filepath(&outputPath, outputDir, inputFilename);
70 bool isWritten = write_bitmap(outputPath, bitmap);
71 if (!isWritten) {
72 SkDebugf("Could not write to file %s\n", outputPath.c_str());
73 }
74}
75
76static void render_picture(const char* inputDir, const char* outputDir,
77 const SkString& inputFilename) {
78 SkFILEStream inputStream;
twiz@google.coma31b8bb2012-06-22 18:24:56 +000079
80 SkString inputPath;
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());
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
94int main(int argc, char* const argv[]) {
95 const char* inputDir;
96 const char* outputDir;
97 if (argc != 3) {
98 usage(argv[0]);
99 }
100
101 inputDir = argv[1];
102 outputDir = argv[2];
103
104 SkOSFile::Iter iter(inputDir, "skp");
105 SkString inputFilename;
106
107 while(iter.next(&inputFilename)) {
108 render_picture(inputDir, outputDir, inputFilename);
109 }
110}