Chromium Code Reviews| Index: samples/pdfium_test.cc |
| diff --git a/samples/pdfium_test.cc b/samples/pdfium_test.cc |
| index f7911c36b93ca3186722f1cb46c6914b201d1b99..f07b6ec6ef19eb1217559d26a8a955a97bd0ea4b 100644 |
| --- a/samples/pdfium_test.cc |
| +++ b/samples/pdfium_test.cc |
| @@ -21,6 +21,7 @@ |
| #include "../fpdfsdk/include/fpdfview.h" |
| #include "../core/include/fxcrt/fx_system.h" |
| #include "v8/include/v8.h" |
| +#include "image_diff_png.h" |
| #ifdef _WIN32 |
| #define snprintf _snprintf |
| @@ -32,6 +33,7 @@ |
| enum OutputFormat { |
| OUTPUT_NONE, |
| OUTPUT_PPM, |
| + OUTPUT_PNG, |
| #ifdef _WIN32 |
| OUTPUT_BMP, |
| OUTPUT_EMF, |
| @@ -154,6 +156,37 @@ static void WritePpm(const char* pdf_name, int num, const void* buffer_void, |
| fclose(fp); |
| } |
| +static void WritePng(const char* pdf_name, int num, const void* buffer_void, |
| + int stride, int width, int height) { |
| + 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
|
| + return; |
| + if (height > 0 && width > INT_MAX / height) |
| + return; |
| + |
| + std::vector<unsigned char> png_encoding; |
| + const unsigned char* buffer = static_cast<const unsigned char*>(buffer_void); |
| + if (!image_diff_png::EncodeBGRAPNG( |
| + buffer, width, height, stride, false, &png_encoding)) { |
| + fprintf(stderr, "Failed to convert bitmap to PNG\n"); |
| + return; |
| + } |
| + |
| + char filename[256]; |
| + 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.
|
| + FILE* fp = fopen(filename, "wb"); |
| + if (!fp) { |
| + fprintf(stderr, "Failed to open %s for output\n", filename); |
| + return; |
| + } |
| + |
| + |
| + size_t bytes_written = fwrite(&png_encoding.front(), 1, png_encoding.size(), fp); |
|
Lei Zhang
2015/02/05 22:11:08
over 80 chars / line
Tom Sepez
2015/02/05 22:58:21
Done.
|
| + if (bytes_written != png_encoding.size()) |
| + fprintf(stderr, "Failed to write to %s\n", filename); |
| + |
| + (void) fclose(fp); |
| +} |
| + |
| #ifdef _WIN32 |
| static void WriteBmp(const char* pdf_name, int num, const void* buffer, |
| int stride, int width, int height) { |
| @@ -293,6 +326,12 @@ bool ParseCommandLine(const std::vector<std::string>& args, |
| return false; |
| } |
| options->output_format = OUTPUT_PPM; |
| + } else if (cur_arg == "--png") { |
| + if (options->output_format != OUTPUT_NONE) { |
| + fprintf(stderr, "Duplicate or conflicting --png argument\n"); |
| + return false; |
| + } |
| + options->output_format = OUTPUT_PNG; |
| } |
| #ifdef _WIN32 |
| else if (cur_arg == "--emf") { |
| @@ -476,6 +515,11 @@ void RenderPdf(const std::string& name, const char* pBuf, size_t len, |
| case OUTPUT_PPM: |
| WritePpm(name.c_str(), i, buffer, stride, width, height); |
| break; |
| + |
| + case OUTPUT_PNG: |
| + WritePng(name.c_str(), i, buffer, stride, width, height); |
| + break; |
| + |
| default: |
| break; |
| } |
| @@ -505,6 +549,7 @@ int main(int argc, const char* argv[]) { |
| printf("Usage: pdfium_test [OPTION] [FILE]...\n"); |
| printf("--bin-dir=<path> - override path to v8 external data\n"); |
| printf("--scale=<number> - scale output size by number (e.g. 0.5)\n"); |
| + printf("--png - write page images <pdf-name>.<page-number>.png\n"); |
| printf("--ppm - write page images <pdf-name>.<page-number>.ppm\n"); |
| #ifdef _WIN32 |
| printf("--bmp - write page images <pdf-name>.<page-number>.bmp\n"); |