Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(581)

Side by Side Diff: experimental/PdfViewer/pdf_viewer_main.cpp

Issue 18562010: pdfviewer: measure mem usage, command line flags library. (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 #include "SkCanvas.h" 1 #include "SkCanvas.h"
2 #include "SkCommandLineFlags.h"
2 #include "SkDevice.h" 3 #include "SkDevice.h"
3 #include "SkForceLinking.h"
4 #include "SkGraphics.h" 4 #include "SkGraphics.h"
5 #include "SkImageDecoder.h" 5 #include "SkImageDecoder.h"
6 #include "SkImageEncoder.h" 6 #include "SkImageEncoder.h"
7 #include "SkOSFile.h" 7 #include "SkOSFile.h"
8 #include "SkPicture.h" 8 #include "SkPicture.h"
9 #include "SkStream.h" 9 #include "SkStream.h"
10 #include "SkTypeface.h" 10 #include "SkTypeface.h"
11 #include "SkTArray.h" 11 #include "SkTArray.h"
12 #include "picture_utils.h" 12 #include "picture_utils.h"
13 13
14 #include "SkPdfRenderer.h" 14 #include "SkPdfRenderer.h"
15 15
16 DEFINE_string2(readPath, r, "", "pdf files or directories of pdf files to proces s.");
17 DEFINE_string2(writePath, w, "", "Directory to write the rendered pages.");
18 DEFINE_bool2(noExtensionForOnePagePdf, n, false, "No page extension if only one page.");
19 DEFINE_bool2(showMemoryUsage, m, false, "Show Memory usage.");
20
16 /** 21 /**
17 * Given list of directories and files to use as input, expects to find .pdf 22 * Given list of directories and files to use as input, expects to find .pdf
18 * files and it will convert them to .png files writing them in the same directo ry 23 * files and it will convert them to .png files writing them in the same directo ry
19 * one file for each page. 24 * one file for each page.
20 * 25 *
21 * Returns zero exit code if all .pdf files were converted successfully, 26 * Returns zero exit code if all .pdf files were converted successfully,
22 * otherwise returns error code 1. 27 * otherwise returns error code 1.
23 */ 28 */
24 29
25 static const char PDF_FILE_EXTENSION[] = "pdf"; 30 static const char PDF_FILE_EXTENSION[] = "pdf";
26 static const char PNG_FILE_EXTENSION[] = "png"; 31 static const char PNG_FILE_EXTENSION[] = "png";
27 32
28 // TODO(edisonn): add ability to write to a new directory.
29 static void usage(const char* argv0) {
30 SkDebugf("PDF to PNG rendering tool\n");
31 SkDebugf("\n"
32 "Usage: \n"
33 " %s <input>... [-w <outputDir>] [-n | --no-page-ext] \n"
34 , argv0);
35 SkDebugf("\n\n");
36 SkDebugf(
37 " input: A list of directories and files to use as input. Files are\n"
38 " expected to have the .skp extension.\n\n");
39 SkDebugf(
40 " outputDir: directory to write the rendered pdfs.\n\n");
41 SkDebugf(
42 " -n: no page extension if only one page.\n\n");
43 SkDebugf("\n");
44 }
45
46 /** Replaces the extension of a file. 33 /** Replaces the extension of a file.
47 * @param path File name whose extension will be changed. 34 * @param path File name whose extension will be changed.
48 * @param old_extension The old extension. 35 * @param old_extension The old extension.
49 * @param new_extension The new extension. 36 * @param new_extension The new extension.
50 * @returns false if the file did not has the expected extension. 37 * @returns false if the file did not has the expected extension.
51 * if false is returned, contents of path are undefined. 38 * if false is returned, contents of path are undefined.
52 */ 39 */
53 static bool add_page_and_replace_filename_extension(SkString* path, int page, 40 static bool add_page_and_replace_filename_extension(SkString* path, int page,
54 const char old_extension[], 41 const char old_extension[],
55 const char new_extension[]) { 42 const char new_extension[]) {
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 124
138 return true; 125 return true;
139 } 126 }
140 127
141 /** Reads an skp file, renders it to pdf and writes the output to a pdf file 128 /** Reads an skp file, renders it to pdf and writes the output to a pdf file
142 * @param inputPath The skp file to be read. 129 * @param inputPath The skp file to be read.
143 * @param outputDir Output dir. 130 * @param outputDir Output dir.
144 * @param renderer The object responsible to render the skp object into pdf. 131 * @param renderer The object responsible to render the skp object into pdf.
145 */ 132 */
146 static bool process_pdf(const SkString& inputPath, const SkString& outputDir, 133 static bool process_pdf(const SkString& inputPath, const SkString& outputDir,
147 SkPdfRenderer& renderer, bool noPageExt) { 134 SkPdfRenderer& renderer, bool noPageExt, bool showMemory Usage) {
148 SkDebugf("Loading PDF: %s\n", inputPath.c_str()); 135 SkDebugf("Loading PDF: %s\n", inputPath.c_str());
149 136
150 SkString inputFilename; 137 SkString inputFilename;
151 sk_tools::get_basename(&inputFilename, inputPath); 138 sk_tools::get_basename(&inputFilename, inputPath);
152 139
153 SkFILEStream inputStream; 140 SkFILEStream inputStream;
154 inputStream.setPath(inputPath.c_str()); 141 inputStream.setPath(inputPath.c_str());
155 if (!inputStream.isValid()) { 142 if (!inputStream.isValid()) {
156 SkDebugf("Could not open file %s\n", inputPath.c_str()); 143 SkDebugf("Could not open file %s\n", inputPath.c_str());
157 return false; 144 return false;
158 } 145 }
159 146
160 bool success = false; 147 bool success = false;
161 148
162 success = renderer.load(inputPath); 149 success = renderer.load(inputPath);
163 150
164 if (success) { 151 if (success) {
152 if (showMemoryUsage) {
153 SkDebugf("Memory usage after load: %u\n", (unsigned int)renderer.byt esUsed());
154 }
165 if (!renderer.pages()) 155 if (!renderer.pages())
166 { 156 {
167 SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str()); 157 SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str());
168 return false; 158 return false;
169 } else { 159 } else {
170 for (int pn = 0; pn < renderer.pages(); ++pn) { 160 for (int pn = 0; pn < renderer.pages(); ++pn) {
171 success = render_page(outputDir, inputFilename, renderer, noPage Ext && renderer.pages() == 1 ? -1 : pn) && success; 161 success = render_page(outputDir, inputFilename, renderer, noPage Ext && renderer.pages() == 1 ? -1 : pn) && success;
162 SkDebugf("Memory usage after page %i rendered: %u\n", pn, (unsig ned int)renderer.bytesUsed());
172 } 163 }
173 } 164 }
174 } 165 }
175 166
176 return success; 167 return success;
177 } 168 }
178 169
179 /** For each file in the directory or for the file passed in input, call 170 /** For each file in the directory or for the file passed in input, call
180 * parse_pdf. 171 * parse_pdf.
181 * @param input A directory or an pdf file. 172 * @param input A directory or an pdf file.
182 * @param outputDir Output dir. 173 * @param outputDir Output dir.
183 * @param renderer The object responsible to render the skp object into pdf. 174 * @param renderer The object responsible to render the skp object into pdf.
184 */ 175 */
185 static int process_input(const SkString& input, const SkString& outputDir, 176 static int process_input(const char* input, const SkString& outputDir,
186 SkPdfRenderer& renderer, bool noPageExt) { 177 SkPdfRenderer& renderer, bool noPageExt, bool showMemor yUsage) {
187 int failures = 0; 178 int failures = 0;
188 if (sk_isdir(input.c_str())) { 179 if (sk_isdir(input)) {
189 SkOSFile::Iter iter(input.c_str(), PDF_FILE_EXTENSION); 180 SkOSFile::Iter iter(input, PDF_FILE_EXTENSION);
190 SkString inputFilename; 181 SkString inputFilename;
191 while (iter.next(&inputFilename)) { 182 while (iter.next(&inputFilename)) {
192 SkString inputPath; 183 SkString inputPath;
193 sk_tools::make_filepath(&inputPath, input, inputFilename); 184 SkString _input;
194 if (!process_pdf(inputPath, outputDir, renderer, noPageExt)) { 185 _input.append(input);
186 sk_tools::make_filepath(&inputPath, _input, inputFilename);
187 if (!process_pdf(inputPath, outputDir, renderer, noPageExt, showMemo ryUsage)) {
195 ++failures; 188 ++failures;
196 } 189 }
197 } 190 }
198 } else { 191 } else {
199 SkString inputPath(input); 192 SkString inputPath(input);
200 if (!process_pdf(inputPath, outputDir, renderer, noPageExt)) { 193 if (!process_pdf(inputPath, outputDir, renderer, noPageExt, showMemoryUs age)) {
201 ++failures; 194 ++failures;
202 } 195 }
203 } 196 }
204 return failures; 197 return failures;
205 } 198 }
206 199
207 static void parse_commandline(int argc, char* const argv[], 200 int tool_main(int argc, char** argv);
208 SkTArray<SkString>* inputs, 201 int tool_main(int argc, char** argv) {
209 SkString* outputDir, bool* noPageExt) { 202 SkCommandLineFlags::SetUsage("Parse and Render .pdf files (pdf viewer).");
210 const char* argv0 = argv[0]; 203 SkCommandLineFlags::Parse(argc, argv);
211 char* const* stop = argv + argc;
212 204
213 for (++argv; argv < stop; ++argv) { 205 if (FLAGS_readPath.isEmpty()) {
214 if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) { 206 SkDebugf(".pdf files or directories are required.\n");
215 usage(argv0);
216 exit(-1);
217 } else if ((0 == strcmp(*argv, "-n")) || (0 == strcmp(*argv, "--no-page- ext"))) {
218 *noPageExt = true;
219 } else if (0 == strcmp(*argv, "-w")) {
220 ++argv;
221 if (argv >= stop) {
222 SkDebugf("Missing outputDir for -w\n");
223 usage(argv0);
224 exit(-1);
225 }
226 *outputDir = SkString(*argv);
227 } else {
228 inputs->push_back(SkString(*argv));
229 }
230 }
231
232 if (inputs->count() < 1) {
233 usage(argv0);
234 exit(-1); 207 exit(-1);
235 } 208 }
236 }
237
238 int tool_main(int argc, char** argv);
239 int tool_main(int argc, char** argv) {
240 SkAutoGraphics ag;
241 SkTArray<SkString> inputs;
242 209
243 SkPdfRenderer renderer; 210 SkPdfRenderer renderer;
244 211
245 SkString outputDir; 212 SkString outputDir;
246 bool noPageExt = false; 213 if (FLAGS_writePath.count() == 1) {
247 parse_commandline(argc, argv, &inputs, &outputDir, &noPageExt); 214 outputDir.set(FLAGS_writePath[0]);
215 }
248 216
249 int failures = 0; 217 int failures = 0;
250 for (int i = 0; i < inputs.count(); i ++) { 218 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
251 failures += process_input(inputs[i], outputDir, renderer, noPageExt); 219 failures += process_input(FLAGS_readPath[i], outputDir, renderer,
220 FLAGS_noExtensionForOnePagePdf,
221 FLAGS_showMemoryUsage);
252 renderer.unload(); 222 renderer.unload();
253 } 223 }
254 224
255 reportPdfRenderStats(); 225 reportPdfRenderStats();
256 226
257 if (failures != 0) { 227 if (failures != 0) {
258 SkDebugf("Failed to render %i PDFs.\n", failures); 228 SkDebugf("Failed to render %i PDFs.\n", failures);
259 return 1; 229 return 1;
260 } 230 }
261 231
262 return 0; 232 return 0;
263 } 233 }
264 234
265 #if !defined SK_BUILD_FOR_IOS 235 #if !defined SK_BUILD_FOR_IOS
266 int main(int argc, char * const argv[]) { 236 int main(int argc, char * const argv[]) {
267 return tool_main(argc, (char**) argv); 237 return tool_main(argc, (char**) argv);
268 } 238 }
269 #endif 239 #endif
OLDNEW
« no previous file with comments | « experimental/PdfViewer/SkPdfRenderer.cpp ('k') | experimental/PdfViewer/pdfparser/native/SkNativeParsedPDF.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698