summaryrefslogtreecommitdiff
path: root/samples/pdfium_test.cc
diff options
context:
space:
mode:
authorTom Sepez <tsepez@chromium.org>2015-02-05 15:06:01 -0800
committerTom Sepez <tsepez@chromium.org>2015-02-05 15:06:01 -0800
commitaf18cb3e7c89d4b6b93765d0f3e5ae6e2e0d8163 (patch)
treeb60ec53ec26aa55221a81330322938eec8bc3197 /samples/pdfium_test.cc
parent93daa3c8942b837a948d4803145a07e2fd9c084b (diff)
downloadpdfium-af18cb3e7c89d4b6b93765d0f3e5ae6e2e0d8163.tar.xz
XFA: Support PNG format in pdfium_test and add image diffing
Lays the groundwork for pixel-diffing tests in pdfium. This is a port of chromium's tools/image_diff/image_diff_png.cc onto the top of the fxcodec-provided version of libpng. pdfium_test is modified to support the new format, and the tools/image_diff.cc image comparison utility is provided, stripped of its base/ dependencies. Unfortunately, this can't be back-ported to origin/main, since the underlying PNG support isn't present inside pdfium. BUG=https://code.google.com/p/pdfium/issues/detail?id=62 R=thestig@chromium.org Review URL: https://codereview.chromium.org/870203005
Diffstat (limited to 'samples/pdfium_test.cc')
-rw-r--r--samples/pdfium_test.cc74
1 files changed, 65 insertions, 9 deletions
diff --git a/samples/pdfium_test.cc b/samples/pdfium_test.cc
index f7911c36b9..fc4bbe4aa4 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,
@@ -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,13 +163,48 @@ 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) {
- if (stride < 0 || width < 0 || height < 0)
- return;
- if (height > 0 && width > INT_MAX / height)
+ if (!CheckDimensions(stride, width, height))
return;
+
int out_len = stride * height;
if (out_len > INT_MAX / 3)
return;
@@ -199,9 +243,9 @@ void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) {
snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num);
HDC dc = CreateEnhMetaFileA(NULL, filename, NULL, NULL);
-
- HRGN rgn = CreateRectRgn(0, 0, width, height);
- SelectClipRgn(dc, rgn);
+
+ HRGN rgn = CreateRectRgn(0, 0, width, height);
+ SelectClipRgn(dc, rgn);
DeleteObject(rgn);
SelectObject(dc, GetStockObject(NULL_PEN));
@@ -293,6 +337,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 +526,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 +560,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");