summaryrefslogtreecommitdiff
path: root/samples/pdfium_test.cc
diff options
context:
space:
mode:
authorTom Sepez <tsepez@chromium.org>2015-02-23 15:59:57 -0800
committerTom Sepez <tsepez@chromium.org>2015-02-23 15:59:57 -0800
commitef1e85caf492e1b9bf9803647d3c5db1abea77c7 (patch)
tree862726a78c398a617968e4dfde2ef105b52acf77 /samples/pdfium_test.cc
parent28ddd48bfd84c55cc51d0b16fa533c51affdeb5b (diff)
downloadpdfium-ef1e85caf492e1b9bf9803647d3c5db1abea77c7.tar.xz
Backport PNG output format to origin/master branch.
The pdfium library itself does not support the format, but the test utility can convert to this output format. GN build can't be tested standalone, so push this out to the next CL. R=jam@chromium.org Review URL: https://codereview.chromium.org/950113002
Diffstat (limited to 'samples/pdfium_test.cc')
-rw-r--r--samples/pdfium_test.cc63
1 files changed, 60 insertions, 3 deletions
diff --git a/samples/pdfium_test.cc b/samples/pdfium_test.cc
index 8f9c832fb2..e3ad47ac15 100644
--- a/samples/pdfium_test.cc
+++ b/samples/pdfium_test.cc
@@ -19,6 +19,7 @@
#include "../fpdfsdk/include/fpdftext.h"
#include "../fpdfsdk/include/fpdfview.h"
#include "../core/include/fxcrt/fx_system.h"
+#include "image_diff_png.h"
#include "v8/include/v8.h"
#include "v8/include/libplatform/libplatform.h"
@@ -32,6 +33,7 @@
enum OutputFormat {
OUTPUT_NONE,
OUTPUT_PPM,
+ OUTPUT_PNG,
#ifdef _WIN32
OUTPUT_BMP,
OUTPUT_EMF,
@@ -113,14 +115,21 @@ static bool GetExternalData(const Options& options,
}
#endif // V8_USE_EXTERNAL_STARTUP_DATA
+static bool CheckDimensions(int stride, int width, int height) {
+ if (stride < 0 || width < 0 || height < 0)
+ return false;
+ if (height > 0 && width > INT_MAX / height)
+ return false;
+ return true;
+}
+
static void WritePpm(const char* pdf_name, int num, const void* buffer_void,
int stride, int width, int height) {
const char* buffer = reinterpret_cast<const char*>(buffer_void);
- if (stride < 0 || width < 0 || height < 0)
- return;
- if (height > 0 && width > INT_MAX / height)
+ if (!CheckDimensions(stride, width, height))
return;
+
int out_len = width * height;
if (out_len > INT_MAX / 3)
return;
@@ -154,6 +163,42 @@ 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 (!CheckDimensions(stride, width, 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];
+ int chars_formatted = snprintf(
+ filename, sizeof(filename), "%s.%d.png", pdf_name, num);
+ if (chars_formatted < 0 ||
+ static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
+ fprintf(stderr, "Filname %s is too long\n", filename);
+ return;
+ }
+
+ 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);
+ 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) {
@@ -297,6 +342,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") {
@@ -475,9 +526,14 @@ void RenderPdf(const std::string& name, const char* pBuf, size_t len,
WriteEmf(page, name.c_str(), i);
break;
#endif
+ case OUTPUT_PNG:
+ WritePng(name.c_str(), i, buffer, stride, width, height);
+ break;
+
case OUTPUT_PPM:
WritePpm(name.c_str(), i, buffer, stride, width, height);
break;
+
default:
break;
}
@@ -507,6 +563,7 @@ static const char usage_string[] =
" --bmp - write page images <pdf-name>.<page-number>.bmp\n"
" --emf - write page meta files <pdf-name>.<page-number>.emf\n"
#endif
+ " --png - write page images <pdf-name>.<page-number>.png\n"
" --ppm - write page images <pdf-name>.<page-number>.ppm\n";
int main(int argc, const char* argv[]) {