render_pictures_main.cpp revision d1dc920097194e3342355185528c84408a9929c8
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" 17#include "picture_utils.h" 18 19 20static void usage(const char* argv0) { 21 SkDebugf("SkPicture rendering tool\n"); 22 SkDebugf("\n" 23"Usage: \n" 24" %s <input>... <outputDir> \n\n" 25, argv0); 26 SkDebugf( 27" input: A list of directories and files to use as input.\n" 28" Files are expected to have the .skp extension.\n"); 29 SkDebugf( 30" outputDir: directory to write the rendered images.\n"); 31} 32 33static void make_output_filepath(SkString* path, const char* dir, 34 const SkString& name) { 35 sk_tools::make_filepath(path, dir, name); 36 path->remove(path->size() - 3, 3); 37 path->append("png"); 38} 39 40static void generate_image_from_picture(SkPicture& pict, SkBitmap* bitmap) { 41 sk_tools::setup_bitmap(bitmap, pict.width(), pict.height()); 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 */ 50static 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 59static 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 67static 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 77static void render_picture(const SkString& inputPath, const char* outputDir) { 78 SkString inputFilename; 79 sk_tools::get_basename(&inputFilename, inputPath); 80 81 SkFILEStream inputStream; 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 88 SkPicture picture(&inputStream); 89 SkBitmap bitmap; 90 generate_image_from_picture(picture, &bitmap); 91 write_output(outputDir, inputFilename, bitmap); 92} 93 94static void process_input(const char* input, const char* outputDir) { 95 SkOSFile::Iter iter(input, "skp"); 96 SkString inputFilename; 97 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 110int 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); 121 } 122} 123