junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 8 | #include "SkBitmap.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkColorPriv.h" |
keyar@chromium.org | 472b379 | 2012-07-20 22:34:27 +0000 | [diff] [blame] | 11 | #include "SkDevice.h" |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 12 | #include "SkImageEncoder.h" |
| 13 | #include "SkOSFile.h" |
| 14 | #include "SkPicture.h" |
| 15 | #include "SkStream.h" |
| 16 | #include "SkString.h" |
senorblanco@chromium.org | 3cbbb54 | 2012-07-13 18:55:53 +0000 | [diff] [blame] | 17 | #include "SkTArray.h" |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 18 | #include "PictureRenderer.h" |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 19 | #include "picture_utils.h" |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 20 | |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 21 | static void usage(const char* argv0) { |
| 22 | SkDebugf("SkPicture rendering tool\n"); |
| 23 | SkDebugf("\n" |
| 24 | "Usage: \n" |
keyar@chromium.org | 472b379 | 2012-07-20 22:34:27 +0000 | [diff] [blame] | 25 | " %s <input>... <outputDir> \n" |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 26 | " [--mode pipe | simple | tile width[%] height[%]]" |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 27 | , argv0); |
keyar@chromium.org | 472b379 | 2012-07-20 22:34:27 +0000 | [diff] [blame] | 28 | SkDebugf("\n\n"); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 29 | SkDebugf( |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 30 | " input: A list of directories and files to use as input. Files are\n" |
| 31 | " expected to have the .skp extension.\n\n"); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 32 | SkDebugf( |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 33 | " outputDir: directory to write the rendered images.\n\n"); |
keyar@chromium.org | 472b379 | 2012-07-20 22:34:27 +0000 | [diff] [blame] | 34 | SkDebugf( |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 35 | " --mode pipe | simple | tile width[%] height[%]: Run in the\n" |
| 36 | " corresponding mode. Default is simple.\n"); |
keyar@chromium.org | 472b379 | 2012-07-20 22:34:27 +0000 | [diff] [blame] | 37 | SkDebugf( |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 38 | " pipe, Render using a SkGPipe.\n"); |
| 39 | SkDebugf( |
| 40 | " simple, Render using the default rendering method.\n"); |
| 41 | SkDebugf( |
| 42 | " tile width[%] height[%], Do a simple render using tiles\n" |
| 43 | " with the given dimensions.\n"); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 44 | } |
| 45 | |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 46 | static void make_output_filepath(SkString* path, const SkString& dir, |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 47 | const SkString& name) { |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 48 | sk_tools::make_filepath(path, dir, name); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 49 | path->remove(path->size() - 3, 3); |
| 50 | path->append("png"); |
| 51 | } |
| 52 | |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 53 | /* since PNG insists on unpremultiplying our alpha, we take no precision chances |
| 54 | and force all pixels to be 100% opaque, otherwise on compare we may not get |
| 55 | a perfect match. |
| 56 | */ |
| 57 | static void force_all_opaque(const SkBitmap& bitmap) { |
| 58 | SkAutoLockPixels lock(bitmap); |
| 59 | for (int y = 0; y < bitmap.height(); y++) { |
| 60 | for (int x = 0; x < bitmap.width(); x++) { |
| 61 | *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) { |
| 67 | SkBitmap copy; |
| 68 | bitmap.copyTo(©, SkBitmap::kARGB_8888_Config); |
| 69 | force_all_opaque(copy); |
| 70 | return SkImageEncoder::EncodeFile(path.c_str(), copy, |
| 71 | SkImageEncoder::kPNG_Type, 100); |
| 72 | } |
| 73 | |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 74 | static void write_output(const SkString& outputDir, const SkString& inputFilename, |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 75 | const SkBitmap& bitmap) { |
| 76 | SkString outputPath; |
| 77 | make_output_filepath(&outputPath, outputDir, inputFilename); |
| 78 | bool isWritten = write_bitmap(outputPath, bitmap); |
| 79 | if (!isWritten) { |
| 80 | SkDebugf("Could not write to file %s\n", outputPath.c_str()); |
| 81 | } |
| 82 | } |
| 83 | |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 84 | static void render_picture(const SkString& inputPath, const SkString& outputDir, |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 85 | sk_tools::PictureRenderer& renderer) { |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 86 | SkString inputFilename; |
| 87 | sk_tools::get_basename(&inputFilename, inputPath); |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 88 | |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 89 | SkFILEStream inputStream; |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 90 | inputStream.setPath(inputPath.c_str()); |
| 91 | if (!inputStream.isValid()) { |
| 92 | SkDebugf("Could not open file %s\n", inputPath.c_str()); |
| 93 | return; |
| 94 | } |
| 95 | |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 96 | SkPicture picture(&inputStream); |
| 97 | SkBitmap bitmap; |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 98 | sk_tools::setup_bitmap(&bitmap, picture.width(), picture.height()); |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 99 | |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 100 | renderer.init(&picture); |
| 101 | |
| 102 | renderer.render(); |
| 103 | |
| 104 | renderer.getCanvas()->readPixels(&bitmap, 0, 0); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 105 | write_output(outputDir, inputFilename, bitmap); |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 106 | |
| 107 | renderer.end(); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 108 | } |
| 109 | |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 110 | static void process_input(const SkString& input, const SkString& outputDir, |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 111 | sk_tools::PictureRenderer& renderer) { |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 112 | SkOSFile::Iter iter(input.c_str(), "skp"); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 113 | SkString inputFilename; |
| 114 | |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 115 | if (iter.next(&inputFilename)) { |
| 116 | do { |
| 117 | SkString inputPath; |
| 118 | sk_tools::make_filepath(&inputPath, input, inputFilename); |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 119 | render_picture(inputPath, outputDir, renderer); |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 120 | } while(iter.next(&inputFilename)); |
| 121 | } else { |
| 122 | SkString inputPath(input); |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 123 | render_picture(inputPath, outputDir, renderer); |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | static void parse_commandline(int argc, char* const argv[], SkTArray<SkString>* inputs, |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 128 | sk_tools::PictureRenderer*& renderer){ |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 129 | const char* argv0 = argv[0]; |
| 130 | char* const* stop = argv + argc; |
| 131 | |
| 132 | for (++argv; argv < stop; ++argv) { |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 133 | if (0 == strcmp(*argv, "--mode")) { |
| 134 | SkDELETE(renderer); |
| 135 | |
keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 136 | ++argv; |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 137 | if (argv >= stop) { |
| 138 | SkDebugf("Missing mode for --mode\n"); |
| 139 | usage(argv0); |
| 140 | exit(-1); |
| 141 | } |
| 142 | |
| 143 | if (0 == strcmp(*argv, "pipe")) { |
| 144 | renderer = SkNEW(sk_tools::PipePictureRenderer); |
| 145 | } else if (0 == strcmp(*argv, "simple")) { |
| 146 | renderer = SkNEW(sk_tools::SimplePictureRenderer); |
| 147 | } else if (0 == strcmp(*argv, "tile")) { |
| 148 | sk_tools::TiledPictureRenderer* tileRenderer = |
| 149 | SkNEW(sk_tools::TiledPictureRenderer); |
| 150 | ++argv; |
| 151 | if (argv >= stop) { |
| 152 | SkDELETE(tileRenderer); |
| 153 | SkDebugf("Missing width for --mode tile\n"); |
| 154 | usage(argv0); |
| 155 | exit(-1); |
| 156 | } |
| 157 | |
keyar@chromium.org | 163b567 | 2012-08-01 17:53:29 +0000 | [diff] [blame] | 158 | if (sk_tools::is_percentage(*argv)) { |
keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 159 | tileRenderer->setTileWidthPercentage(atof(*argv)); |
| 160 | if (!(tileRenderer->getTileWidthPercentage() > 0)) { |
| 161 | SkDELETE(tileRenderer); |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 162 | SkDebugf("--mode tile must be given a width percentage > 0\n"); |
keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 163 | exit(-1); |
| 164 | } |
| 165 | } else { |
| 166 | tileRenderer->setTileWidth(atoi(*argv)); |
| 167 | if (!(tileRenderer->getTileWidth() > 0)) { |
| 168 | SkDELETE(tileRenderer); |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 169 | SkDebugf("--mode tile must be given a width > 0\n"); |
keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 170 | exit(-1); |
| 171 | } |
| 172 | } |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 173 | |
| 174 | ++argv; |
| 175 | if (argv >= stop) { |
| 176 | SkDELETE(tileRenderer); |
| 177 | SkDebugf("Missing height for --mode tile\n"); |
| 178 | usage(argv0); |
| 179 | exit(-1); |
| 180 | } |
| 181 | |
keyar@chromium.org | 163b567 | 2012-08-01 17:53:29 +0000 | [diff] [blame] | 182 | if (sk_tools::is_percentage(*argv)) { |
keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 183 | tileRenderer->setTileHeightPercentage(atof(*argv)); |
| 184 | if (!(tileRenderer->getTileHeightPercentage() > 0)) { |
| 185 | SkDELETE(tileRenderer); |
| 186 | SkDebugf( |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 187 | "--mode tile must be given a height percentage > 0\n"); |
keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 188 | exit(-1); |
| 189 | } |
| 190 | } else { |
| 191 | tileRenderer->setTileHeight(atoi(*argv)); |
| 192 | if (!(tileRenderer->getTileHeight() > 0)) { |
| 193 | SkDELETE(tileRenderer); |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 194 | SkDebugf("--mode tile must be given a height > 0\n"); |
keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 195 | exit(-1); |
| 196 | } |
| 197 | } |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 198 | |
| 199 | renderer = tileRenderer; |
keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 200 | } else { |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 201 | SkDebugf("%s is not a valid mode for --mode\n", *argv); |
keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 202 | usage(argv0); |
| 203 | exit(-1); |
| 204 | } |
keyar@chromium.org | 472b379 | 2012-07-20 22:34:27 +0000 | [diff] [blame] | 205 | } else if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) { |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 206 | SkDELETE(renderer); |
keyar@chromium.org | 472b379 | 2012-07-20 22:34:27 +0000 | [diff] [blame] | 207 | usage(argv0); |
| 208 | exit(-1); |
keyar@chromium.org | a2333d9 | 2012-07-16 17:29:16 +0000 | [diff] [blame] | 209 | } else { |
| 210 | inputs->push_back(SkString(*argv)); |
| 211 | } |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 212 | } |
keyar@chromium.org | 472b379 | 2012-07-20 22:34:27 +0000 | [diff] [blame] | 213 | |
| 214 | if (inputs->count() < 2) { |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 215 | SkDELETE(renderer); |
keyar@chromium.org | 472b379 | 2012-07-20 22:34:27 +0000 | [diff] [blame] | 216 | usage(argv0); |
| 217 | exit(-1); |
| 218 | } |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 219 | |
| 220 | if (NULL == renderer) { |
| 221 | renderer = SkNEW(sk_tools::SimplePictureRenderer); |
| 222 | } |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | int main(int argc, char* const argv[]) { |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 226 | SkTArray<SkString> inputs; |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 227 | sk_tools::PictureRenderer* renderer = NULL; |
keyar@chromium.org | 472b379 | 2012-07-20 22:34:27 +0000 | [diff] [blame] | 228 | |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 229 | parse_commandline(argc, argv, &inputs, renderer); |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 230 | SkString outputDir = inputs[inputs.count() - 1]; |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 231 | SkASSERT(renderer); |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 232 | |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 233 | for (int i = 0; i < inputs.count() - 1; i ++) { |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 234 | process_input(inputs[i], outputDir, *renderer); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 235 | } |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 236 | |
| 237 | SkDELETE(renderer); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 238 | } |