OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <limits.h> | 5 #include <limits.h> |
6 #include <stdio.h> | 6 #include <stdio.h> |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 #include <string.h> | 8 #include <string.h> |
9 #include <wchar.h> | 9 #include <wchar.h> |
10 | 10 |
11 #include <list> | 11 #include <list> |
12 #include <sstream> | 12 #include <sstream> |
13 #include <string> | 13 #include <string> |
14 #include <utility> | 14 #include <utility> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "../fpdfsdk/include/fpdf_dataavail.h" | 17 #include "../fpdfsdk/include/fpdf_dataavail.h" |
18 #include "../fpdfsdk/include/fpdf_ext.h" | 18 #include "../fpdfsdk/include/fpdf_ext.h" |
19 #include "../fpdfsdk/include/fpdfformfill.h" | 19 #include "../fpdfsdk/include/fpdfformfill.h" |
20 #include "../fpdfsdk/include/fpdftext.h" | 20 #include "../fpdfsdk/include/fpdftext.h" |
21 #include "../fpdfsdk/include/fpdfview.h" | 21 #include "../fpdfsdk/include/fpdfview.h" |
22 #include "../core/include/fxcrt/fx_system.h" | 22 #include "../core/include/fxcrt/fx_system.h" |
23 #include "v8/include/v8.h" | 23 #include "v8/include/v8.h" |
24 #include "image_diff_png.h" | |
24 | 25 |
25 #ifdef _WIN32 | 26 #ifdef _WIN32 |
26 #define snprintf _snprintf | 27 #define snprintf _snprintf |
27 #define PATH_SEPARATOR '\\' | 28 #define PATH_SEPARATOR '\\' |
28 #else | 29 #else |
29 #define PATH_SEPARATOR '/' | 30 #define PATH_SEPARATOR '/' |
30 #endif | 31 #endif |
31 | 32 |
32 enum OutputFormat { | 33 enum OutputFormat { |
33 OUTPUT_NONE, | 34 OUTPUT_NONE, |
34 OUTPUT_PPM, | 35 OUTPUT_PPM, |
36 OUTPUT_PNG, | |
35 #ifdef _WIN32 | 37 #ifdef _WIN32 |
36 OUTPUT_BMP, | 38 OUTPUT_BMP, |
37 OUTPUT_EMF, | 39 OUTPUT_EMF, |
38 #endif | 40 #endif |
39 }; | 41 }; |
40 | 42 |
41 struct Options { | 43 struct Options { |
42 Options() : output_format(OUTPUT_NONE) { } | 44 Options() : output_format(OUTPUT_NONE) { } |
43 | 45 |
44 OutputFormat output_format; | 46 OutputFormat output_format; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
147 // B | 149 // B |
148 dest_line[(w * 3) + 2] = src_line[w * 4]; | 150 dest_line[(w * 3) + 2] = src_line[w * 4]; |
149 } | 151 } |
150 } | 152 } |
151 fwrite(result, out_len, 1, fp); | 153 fwrite(result, out_len, 1, fp); |
152 delete [] result; | 154 delete [] result; |
153 } | 155 } |
154 fclose(fp); | 156 fclose(fp); |
155 } | 157 } |
156 | 158 |
159 static void WritePng(const char* pdf_name, int num, const void* buffer_void, | |
160 int stride, int width, int height) { | |
161 if (stride < 0 || width < 0 || height < 0) | |
Lei Zhang
2015/02/05 22:11:08
We do the same checks in every version of WriteFoo
| |
162 return; | |
163 if (height > 0 && width > INT_MAX / height) | |
164 return; | |
165 | |
166 std::vector<unsigned char> png_encoding; | |
167 const unsigned char* buffer = static_cast<const unsigned char*>(buffer_void); | |
168 if (!image_diff_png::EncodeBGRAPNG( | |
169 buffer, width, height, stride, false, &png_encoding)) { | |
170 fprintf(stderr, "Failed to convert bitmap to PNG\n"); | |
171 return; | |
172 } | |
173 | |
174 char filename[256]; | |
175 snprintf(filename, sizeof(filename), "%s.%d.png", pdf_name, num); | |
Lei Zhang
2015/02/05 22:11:08
Check the return value?
Tom Sepez
2015/02/05 22:58:21
Done.
| |
176 FILE* fp = fopen(filename, "wb"); | |
177 if (!fp) { | |
178 fprintf(stderr, "Failed to open %s for output\n", filename); | |
179 return; | |
180 } | |
181 | |
182 | |
183 size_t bytes_written = fwrite(&png_encoding.front(), 1, png_encoding.size(), f p); | |
Lei Zhang
2015/02/05 22:11:08
over 80 chars / line
Tom Sepez
2015/02/05 22:58:21
Done.
| |
184 if (bytes_written != png_encoding.size()) | |
185 fprintf(stderr, "Failed to write to %s\n", filename); | |
186 | |
187 (void) fclose(fp); | |
188 } | |
189 | |
157 #ifdef _WIN32 | 190 #ifdef _WIN32 |
158 static void WriteBmp(const char* pdf_name, int num, const void* buffer, | 191 static void WriteBmp(const char* pdf_name, int num, const void* buffer, |
159 int stride, int width, int height) { | 192 int stride, int width, int height) { |
160 if (stride < 0 || width < 0 || height < 0) | 193 if (stride < 0 || width < 0 || height < 0) |
161 return; | 194 return; |
162 if (height > 0 && width > INT_MAX / height) | 195 if (height > 0 && width > INT_MAX / height) |
163 return; | 196 return; |
164 int out_len = stride * height; | 197 int out_len = stride * height; |
165 if (out_len > INT_MAX / 3) | 198 if (out_len > INT_MAX / 3) |
166 return; | 199 return; |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
286 options->exe_path = args[0]; | 319 options->exe_path = args[0]; |
287 size_t cur_idx = 1; | 320 size_t cur_idx = 1; |
288 for (; cur_idx < args.size(); ++cur_idx) { | 321 for (; cur_idx < args.size(); ++cur_idx) { |
289 const std::string& cur_arg = args[cur_idx]; | 322 const std::string& cur_arg = args[cur_idx]; |
290 if (cur_arg == "--ppm") { | 323 if (cur_arg == "--ppm") { |
291 if (options->output_format != OUTPUT_NONE) { | 324 if (options->output_format != OUTPUT_NONE) { |
292 fprintf(stderr, "Duplicate or conflicting --ppm argument\n"); | 325 fprintf(stderr, "Duplicate or conflicting --ppm argument\n"); |
293 return false; | 326 return false; |
294 } | 327 } |
295 options->output_format = OUTPUT_PPM; | 328 options->output_format = OUTPUT_PPM; |
329 } else if (cur_arg == "--png") { | |
330 if (options->output_format != OUTPUT_NONE) { | |
331 fprintf(stderr, "Duplicate or conflicting --png argument\n"); | |
332 return false; | |
333 } | |
334 options->output_format = OUTPUT_PNG; | |
296 } | 335 } |
297 #ifdef _WIN32 | 336 #ifdef _WIN32 |
298 else if (cur_arg == "--emf") { | 337 else if (cur_arg == "--emf") { |
299 if (options->output_format != OUTPUT_NONE) { | 338 if (options->output_format != OUTPUT_NONE) { |
300 fprintf(stderr, "Duplicate or conflicting --emf argument\n"); | 339 fprintf(stderr, "Duplicate or conflicting --emf argument\n"); |
301 return false; | 340 return false; |
302 } | 341 } |
303 options->output_format = OUTPUT_EMF; | 342 options->output_format = OUTPUT_EMF; |
304 } | 343 } |
305 else if (cur_arg == "--bmp") { | 344 else if (cur_arg == "--bmp") { |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
469 WriteBmp(name.c_str(), i, buffer, stride, width, height); | 508 WriteBmp(name.c_str(), i, buffer, stride, width, height); |
470 break; | 509 break; |
471 | 510 |
472 case OUTPUT_EMF: | 511 case OUTPUT_EMF: |
473 WriteEmf(page, name.c_str(), i); | 512 WriteEmf(page, name.c_str(), i); |
474 break; | 513 break; |
475 #endif | 514 #endif |
476 case OUTPUT_PPM: | 515 case OUTPUT_PPM: |
477 WritePpm(name.c_str(), i, buffer, stride, width, height); | 516 WritePpm(name.c_str(), i, buffer, stride, width, height); |
478 break; | 517 break; |
518 | |
519 case OUTPUT_PNG: | |
520 WritePng(name.c_str(), i, buffer, stride, width, height); | |
521 break; | |
522 | |
479 default: | 523 default: |
480 break; | 524 break; |
481 } | 525 } |
482 | 526 |
483 FPDFBitmap_Destroy(bitmap); | 527 FPDFBitmap_Destroy(bitmap); |
484 | 528 |
485 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); | 529 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); |
486 FORM_OnBeforeClosePage(page, form); | 530 FORM_OnBeforeClosePage(page, form); |
487 FPDFText_ClosePage(text_page); | 531 FPDFText_ClosePage(text_page); |
488 FPDF_ClosePage(page); | 532 FPDF_ClosePage(page); |
489 } | 533 } |
490 | 534 |
491 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); | 535 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); |
492 FPDF_CloseDocument(doc); | 536 FPDF_CloseDocument(doc); |
493 FPDFDOC_ExitFormFillEnvironment(form); | 537 FPDFDOC_ExitFormFillEnvironment(form); |
494 FPDFAvail_Destroy(pdf_avail); | 538 FPDFAvail_Destroy(pdf_avail); |
495 | 539 |
496 printf("Loaded, parsed and rendered %" PRIuS " pages.\n", rendered_pages); | 540 printf("Loaded, parsed and rendered %" PRIuS " pages.\n", rendered_pages); |
497 printf("Skipped %" PRIuS " bad pages.\n", bad_pages); | 541 printf("Skipped %" PRIuS " bad pages.\n", bad_pages); |
498 } | 542 } |
499 | 543 |
500 int main(int argc, const char* argv[]) { | 544 int main(int argc, const char* argv[]) { |
501 std::vector<std::string> args(argv, argv + argc); | 545 std::vector<std::string> args(argv, argv + argc); |
502 Options options; | 546 Options options; |
503 std::list<std::string> files; | 547 std::list<std::string> files; |
504 if (!ParseCommandLine(args, &options, &files)) { | 548 if (!ParseCommandLine(args, &options, &files)) { |
505 printf("Usage: pdfium_test [OPTION] [FILE]...\n"); | 549 printf("Usage: pdfium_test [OPTION] [FILE]...\n"); |
506 printf("--bin-dir=<path> - override path to v8 external data\n"); | 550 printf("--bin-dir=<path> - override path to v8 external data\n"); |
507 printf("--scale=<number> - scale output size by number (e.g. 0.5)\n"); | 551 printf("--scale=<number> - scale output size by number (e.g. 0.5)\n"); |
552 printf("--png - write page images <pdf-name>.<page-number>.png\n"); | |
508 printf("--ppm - write page images <pdf-name>.<page-number>.ppm\n"); | 553 printf("--ppm - write page images <pdf-name>.<page-number>.ppm\n"); |
509 #ifdef _WIN32 | 554 #ifdef _WIN32 |
510 printf("--bmp - write page images <pdf-name>.<page-number>.bmp\n"); | 555 printf("--bmp - write page images <pdf-name>.<page-number>.bmp\n"); |
511 printf("--emf - write page meta files <pdf-name>.<page-number>.emf\n"); | 556 printf("--emf - write page meta files <pdf-name>.<page-number>.emf\n"); |
512 #endif | 557 #endif |
513 return 1; | 558 return 1; |
514 } | 559 } |
515 | 560 |
516 v8::V8::InitializeICU(); | 561 v8::V8::InitializeICU(); |
517 | 562 |
(...skipping 25 matching lines...) Expand all Loading... | |
543 if (!file_contents) | 588 if (!file_contents) |
544 continue; | 589 continue; |
545 RenderPdf(filename, file_contents, file_length, options); | 590 RenderPdf(filename, file_contents, file_length, options); |
546 free(file_contents); | 591 free(file_contents); |
547 } | 592 } |
548 | 593 |
549 FPDF_DestroyLibrary(); | 594 FPDF_DestroyLibrary(); |
550 | 595 |
551 return 0; | 596 return 0; |
552 } | 597 } |
OLD | NEW |