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 | |
| 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.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 17 | #include "picture_utils.h" |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 18 | |
| 19 | |
| 20 | static void usage(const char* argv0) { |
| 21 | SkDebugf("SkPicture rendering tool\n"); |
| 22 | SkDebugf("\n" |
| 23 | "Usage: \n" |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 24 | " %s <input>... <outputDir> \n\n" |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 25 | , argv0); |
| 26 | SkDebugf( |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 27 | " input: A list of directories and files to use as input.\n" |
| 28 | " Files are expected to have the .skp extension.\n"); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 29 | SkDebugf( |
| 30 | " outputDir: directory to write the rendered images.\n"); |
| 31 | } |
| 32 | |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 33 | static void make_output_filepath(SkString* path, const char* dir, |
| 34 | const SkString& name) { |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 35 | sk_tools::make_filepath(path, dir, name); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 36 | path->remove(path->size() - 3, 3); |
| 37 | path->append("png"); |
| 38 | } |
| 39 | |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 40 | static void generate_image_from_picture(SkPicture& pict, SkBitmap* bitmap) { |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 41 | sk_tools::setup_bitmap(bitmap, pict.width(), pict.height()); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 42 | 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 | */ |
| 50 | static 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 | |
| 59 | static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) { |
| 60 | SkBitmap copy; |
| 61 | bitmap.copyTo(©, SkBitmap::kARGB_8888_Config); |
| 62 | force_all_opaque(copy); |
| 63 | return SkImageEncoder::EncodeFile(path.c_str(), copy, |
| 64 | SkImageEncoder::kPNG_Type, 100); |
| 65 | } |
| 66 | |
| 67 | static 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.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 77 | static void render_picture(const SkString& inputPath, const char* outputDir) { |
| 78 | SkString inputFilename; |
| 79 | sk_tools::get_basename(&inputFilename, inputPath); |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 80 | |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 81 | SkFILEStream inputStream; |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 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.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 88 | SkPicture picture(&inputStream); |
| 89 | SkBitmap bitmap; |
| 90 | generate_image_from_picture(picture, &bitmap); |
| 91 | write_output(outputDir, inputFilename, bitmap); |
| 92 | } |
| 93 | |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 94 | static void process_input(const char* input, const char* outputDir) { |
| 95 | SkOSFile::Iter iter(input, "skp"); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 96 | SkString inputFilename; |
| 97 | |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 98 | 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 | |
| 110 | int 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.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 121 | } |
| 122 | } |