edisonn@google.com | c319abe | 2012-11-01 19:52:38 +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 | #include "SkCanvas.h" |
| 9 | #include "SkDevice.h" |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 10 | #include "SkForceLinking.h" |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 11 | #include "SkGraphics.h" |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 12 | #include "SkImageEncoder.h" |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 13 | #include "SkOSFile.h" |
| 14 | #include "SkPicture.h" |
commit-bot@chromium.org | 608ea65 | 2013-10-03 19:29:21 +0000 | [diff] [blame] | 15 | #include "SkPixelRef.h" |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 16 | #include "SkStream.h" |
| 17 | #include "SkTArray.h" |
| 18 | #include "PdfRenderer.h" |
| 19 | #include "picture_utils.h" |
| 20 | |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 21 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 22 | |
edisonn@google.com | 184487c | 2013-03-08 18:00:16 +0000 | [diff] [blame] | 23 | #ifdef SK_USE_CDB |
| 24 | #include "win_dbghelp.h" |
| 25 | #endif |
| 26 | |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 27 | /** |
| 28 | * render_pdfs |
| 29 | * |
| 30 | * Given list of directories and files to use as input, expects to find .skp |
| 31 | * files and it will convert them to .pdf files writing them in the output |
| 32 | * directory. |
| 33 | * |
| 34 | * Returns zero exit code if all .skp files were converted successfully, |
| 35 | * otherwise returns error code 1. |
| 36 | */ |
| 37 | |
| 38 | static const char PDF_FILE_EXTENSION[] = "pdf"; |
| 39 | static const char SKP_FILE_EXTENSION[] = "skp"; |
| 40 | |
| 41 | static void usage(const char* argv0) { |
| 42 | SkDebugf("SKP to PDF rendering tool\n"); |
| 43 | SkDebugf("\n" |
| 44 | "Usage: \n" |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 45 | " %s <input>... [-w <outputDir>] [--jpegQuality N] \n" |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 46 | , argv0); |
| 47 | SkDebugf("\n\n"); |
| 48 | SkDebugf( |
| 49 | " input: A list of directories and files to use as input. Files are\n" |
| 50 | " expected to have the .skp extension.\n\n"); |
| 51 | SkDebugf( |
| 52 | " outputDir: directory to write the rendered pdfs.\n\n"); |
| 53 | SkDebugf("\n"); |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 54 | SkDebugf( |
| 55 | " jpegQuality N: encodes images in JPEG at quality level N, which can\n" |
| 56 | " be in range 0-100).\n" |
| 57 | " N = -1 will disable JPEG compression.\n" |
| 58 | " Default is N = 100, maximum quality.\n\n"); |
| 59 | SkDebugf("\n"); |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** Replaces the extension of a file. |
| 63 | * @param path File name whose extension will be changed. |
| 64 | * @param old_extension The old extension. |
| 65 | * @param new_extension The new extension. |
| 66 | * @returns false if the file did not has the expected extension. |
| 67 | * if false is returned, contents of path are undefined. |
| 68 | */ |
| 69 | static bool replace_filename_extension(SkString* path, |
| 70 | const char old_extension[], |
| 71 | const char new_extension[]) { |
| 72 | if (path->endsWith(old_extension)) { |
| 73 | path->remove(path->size() - strlen(old_extension), |
| 74 | strlen(old_extension)); |
| 75 | if (!path->endsWith(".")) { |
| 76 | return false; |
| 77 | } |
| 78 | path->append(new_extension); |
| 79 | return true; |
| 80 | } |
| 81 | return false; |
| 82 | } |
| 83 | |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 84 | int gJpegQuality = 100; |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 85 | // the size_t* parameter is deprecated, so we ignore it |
| 86 | static SkData* encode_to_dct_data(size_t*, const SkBitmap& bitmap) { |
commit-bot@chromium.org | 608ea65 | 2013-10-03 19:29:21 +0000 | [diff] [blame] | 87 | if (gJpegQuality == -1) { |
| 88 | return NULL; |
| 89 | } |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 90 | |
commit-bot@chromium.org | 608ea65 | 2013-10-03 19:29:21 +0000 | [diff] [blame] | 91 | SkBitmap bm = bitmap; |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 92 | #if defined(SK_BUILD_FOR_MAC) |
commit-bot@chromium.org | 608ea65 | 2013-10-03 19:29:21 +0000 | [diff] [blame] | 93 | // Workaround bug #1043 where bitmaps with referenced pixels cause |
| 94 | // CGImageDestinationFinalize to crash |
| 95 | SkBitmap copy; |
commit-bot@chromium.org | d5f032d | 2014-02-24 18:51:43 +0000 | [diff] [blame] | 96 | bitmap.deepCopyTo(©); |
commit-bot@chromium.org | 608ea65 | 2013-10-03 19:29:21 +0000 | [diff] [blame] | 97 | bm = copy; |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 98 | #endif |
| 99 | |
commit-bot@chromium.org | 608ea65 | 2013-10-03 19:29:21 +0000 | [diff] [blame] | 100 | return SkImageEncoder::EncodeData(bm, |
| 101 | SkImageEncoder::kJPEG_Type, |
| 102 | gJpegQuality); |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 103 | } |
| 104 | |
skia.committer@gmail.com | 760f2d9 | 2012-11-02 02:01:24 +0000 | [diff] [blame] | 105 | /** Builds the output filename. path = dir/name, and it replaces expected |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 106 | * .skp extension with .pdf extention. |
| 107 | * @param path Output filename. |
| 108 | * @param name The name of the file. |
| 109 | * @returns false if the file did not has the expected extension. |
| 110 | * if false is returned, contents of path are undefined. |
| 111 | */ |
| 112 | static bool make_output_filepath(SkString* path, const SkString& dir, |
| 113 | const SkString& name) { |
| 114 | sk_tools::make_filepath(path, dir, name); |
| 115 | return replace_filename_extension(path, |
| 116 | SKP_FILE_EXTENSION, |
| 117 | PDF_FILE_EXTENSION); |
| 118 | } |
| 119 | |
| 120 | /** Write the output of pdf renderer to a file. |
| 121 | * @param outputDir Output dir. |
| 122 | * @param inputFilename The skp file that was read. |
| 123 | * @param renderer The object responsible to write the pdf file. |
| 124 | */ |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 125 | static SkWStream* open_stream(const SkString& outputDir, |
| 126 | const SkString& inputFilename) { |
edisonn@google.com | 4fa566b | 2013-01-11 20:30:41 +0000 | [diff] [blame] | 127 | if (outputDir.isEmpty()) { |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 128 | return SkNEW(SkDynamicMemoryWStream); |
edisonn@google.com | 4fa566b | 2013-01-11 20:30:41 +0000 | [diff] [blame] | 129 | } |
| 130 | |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 131 | SkString outputPath; |
| 132 | if (!make_output_filepath(&outputPath, outputDir, inputFilename)) { |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 133 | return NULL; |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 134 | } |
edisonn@google.com | 4fa566b | 2013-01-11 20:30:41 +0000 | [diff] [blame] | 135 | |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 136 | SkFILEWStream* stream = SkNEW_ARGS(SkFILEWStream, (outputPath.c_str())); |
| 137 | if (!stream->isValid()) { |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 138 | SkDebugf("Could not write to file %s\n", outputPath.c_str()); |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 139 | return NULL; |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 140 | } |
edisonn@google.com | 4fa566b | 2013-01-11 20:30:41 +0000 | [diff] [blame] | 141 | |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 142 | return stream; |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /** Reads an skp file, renders it to pdf and writes the output to a pdf file |
| 146 | * @param inputPath The skp file to be read. |
| 147 | * @param outputDir Output dir. |
| 148 | * @param renderer The object responsible to render the skp object into pdf. |
| 149 | */ |
| 150 | static bool render_pdf(const SkString& inputPath, const SkString& outputDir, |
| 151 | sk_tools::PdfRenderer& renderer) { |
| 152 | SkString inputFilename; |
| 153 | sk_tools::get_basename(&inputFilename, inputPath); |
| 154 | |
| 155 | SkFILEStream inputStream; |
| 156 | inputStream.setPath(inputPath.c_str()); |
| 157 | if (!inputStream.isValid()) { |
| 158 | SkDebugf("Could not open file %s\n", inputPath.c_str()); |
| 159 | return false; |
| 160 | } |
| 161 | |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 162 | SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(&inputStream)); |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 163 | |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 164 | if (NULL == picture.get()) { |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 165 | SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str()); |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | SkDebugf("exporting... [%i %i] %s\n", picture->width(), picture->height(), |
| 170 | inputPath.c_str()); |
| 171 | |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 172 | SkWStream* stream(open_stream(outputDir, inputFilename)); |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 173 | |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 174 | if (!stream) { |
| 175 | return false; |
| 176 | } |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 177 | |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 178 | renderer.init(picture, stream); |
| 179 | |
| 180 | bool success = renderer.render(); |
| 181 | SkDELETE(stream); |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 182 | |
| 183 | renderer.end(); |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 184 | |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 185 | return success; |
| 186 | } |
| 187 | |
| 188 | /** For each file in the directory or for the file passed in input, call |
| 189 | * render_pdf. |
| 190 | * @param input A directory or an skp file. |
| 191 | * @param outputDir Output dir. |
| 192 | * @param renderer The object responsible to render the skp object into pdf. |
| 193 | */ |
| 194 | static int process_input(const SkString& input, const SkString& outputDir, |
| 195 | sk_tools::PdfRenderer& renderer) { |
| 196 | int failures = 0; |
| 197 | if (sk_isdir(input.c_str())) { |
| 198 | SkOSFile::Iter iter(input.c_str(), SKP_FILE_EXTENSION); |
| 199 | SkString inputFilename; |
| 200 | while (iter.next(&inputFilename)) { |
| 201 | SkString inputPath; |
| 202 | sk_tools::make_filepath(&inputPath, input, inputFilename); |
| 203 | if (!render_pdf(inputPath, outputDir, renderer)) { |
| 204 | ++failures; |
| 205 | } |
| 206 | } |
| 207 | } else { |
| 208 | SkString inputPath(input); |
| 209 | if (!render_pdf(inputPath, outputDir, renderer)) { |
| 210 | ++failures; |
| 211 | } |
| 212 | } |
| 213 | return failures; |
| 214 | } |
| 215 | |
| 216 | static void parse_commandline(int argc, char* const argv[], |
edisonn@google.com | 4fa566b | 2013-01-11 20:30:41 +0000 | [diff] [blame] | 217 | SkTArray<SkString>* inputs, |
| 218 | SkString* outputDir) { |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 219 | const char* argv0 = argv[0]; |
| 220 | char* const* stop = argv + argc; |
| 221 | |
| 222 | for (++argv; argv < stop; ++argv) { |
| 223 | if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) { |
| 224 | usage(argv0); |
| 225 | exit(-1); |
edisonn@google.com | 4fa566b | 2013-01-11 20:30:41 +0000 | [diff] [blame] | 226 | } else if (0 == strcmp(*argv, "-w")) { |
| 227 | ++argv; |
| 228 | if (argv >= stop) { |
| 229 | SkDebugf("Missing outputDir for -w\n"); |
| 230 | usage(argv0); |
| 231 | exit(-1); |
| 232 | } |
| 233 | *outputDir = SkString(*argv); |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 234 | } else if (0 == strcmp(*argv, "--jpegQuality")) { |
| 235 | ++argv; |
| 236 | if (argv >= stop) { |
| 237 | SkDebugf("Missing argument for --jpegQuality\n"); |
| 238 | usage(argv0); |
| 239 | exit(-1); |
| 240 | } |
| 241 | gJpegQuality = atoi(*argv); |
| 242 | if (gJpegQuality < -1 || gJpegQuality > 100) { |
| 243 | SkDebugf("Invalid argument for --jpegQuality\n"); |
| 244 | usage(argv0); |
skia.committer@gmail.com | 83f0d30 | 2013-04-25 07:01:04 +0000 | [diff] [blame] | 245 | exit(-1); |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 246 | } |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 247 | } else { |
| 248 | inputs->push_back(SkString(*argv)); |
| 249 | } |
| 250 | } |
| 251 | |
edisonn@google.com | 4fa566b | 2013-01-11 20:30:41 +0000 | [diff] [blame] | 252 | if (inputs->count() < 1) { |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 253 | usage(argv0); |
| 254 | exit(-1); |
| 255 | } |
| 256 | } |
| 257 | |
edisonn@google.com | 184487c | 2013-03-08 18:00:16 +0000 | [diff] [blame] | 258 | int tool_main_core(int argc, char** argv); |
| 259 | int tool_main_core(int argc, char** argv) { |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 260 | SkAutoGraphics ag; |
| 261 | SkTArray<SkString> inputs; |
| 262 | |
| 263 | SkAutoTUnref<sk_tools::PdfRenderer> |
commit-bot@chromium.org | 608ea65 | 2013-10-03 19:29:21 +0000 | [diff] [blame] | 264 | renderer(SkNEW_ARGS(sk_tools::SimplePdfRenderer, (encode_to_dct_data))); |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 265 | SkASSERT(renderer.get()); |
| 266 | |
edisonn@google.com | 4fa566b | 2013-01-11 20:30:41 +0000 | [diff] [blame] | 267 | SkString outputDir; |
| 268 | parse_commandline(argc, argv, &inputs, &outputDir); |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 269 | |
| 270 | int failures = 0; |
edisonn@google.com | 4fa566b | 2013-01-11 20:30:41 +0000 | [diff] [blame] | 271 | for (int i = 0; i < inputs.count(); i ++) { |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 272 | failures += process_input(inputs[i], outputDir, *renderer); |
| 273 | } |
| 274 | |
| 275 | if (failures != 0) { |
| 276 | SkDebugf("Failed to render %i PDFs.\n", failures); |
| 277 | return 1; |
| 278 | } |
edisonn@google.com | 184487c | 2013-03-08 18:00:16 +0000 | [diff] [blame] | 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | int tool_main(int argc, char** argv); |
| 284 | int tool_main(int argc, char** argv) { |
| 285 | #ifdef SK_USE_CDB |
| 286 | setUpDebuggingFromArgs(argv[0]); |
| 287 | __try { |
| 288 | #endif |
| 289 | return tool_main_core(argc, argv); |
| 290 | #ifdef SK_USE_CDB |
| 291 | } |
| 292 | __except(GenerateDumpAndPrintCallstack(GetExceptionInformation())) |
| 293 | { |
| 294 | return -1; |
| 295 | } |
| 296 | #endif |
humper@google.com | f286329 | 2013-01-07 20:08:56 +0000 | [diff] [blame] | 297 | return 0; |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 298 | } |
| 299 | |
edisonn@google.com | 9cf5b28 | 2012-11-01 20:07:33 +0000 | [diff] [blame] | 300 | #if !defined SK_BUILD_FOR_IOS |
| 301 | int main(int argc, char * const argv[]) { |
| 302 | return tool_main(argc, (char**) argv); |
| 303 | } |
| 304 | #endif |