summaryrefslogtreecommitdiff
path: root/samples
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
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')
-rw-r--r--samples/BUILD.gn12
-rw-r--r--samples/image_diff.cc398
-rw-r--r--samples/image_diff_png.cc644
-rw-r--r--samples/image_diff_png.h34
-rw-r--r--samples/pdfium_test.cc74
-rw-r--r--samples/samples.gyp18
6 files changed, 1171 insertions, 9 deletions
diff --git a/samples/BUILD.gn b/samples/BUILD.gn
index e690c23622..7f2f41fa82 100644
--- a/samples/BUILD.gn
+++ b/samples/BUILD.gn
@@ -10,9 +10,21 @@ group("samples") {
executable("pdfium_test") {
sources = [
+ "image_diff_png.cc",
"pdfium_test.cc",
]
deps = [
"//third_party/pdfium",
]
}
+
+executable("pdfium_diff") {
+ sources = [
+ "image_diff.cc",
+ "image_diff_png.cc",
+ "image_diff_png.h",
+ ]
+ deps = [
+ "//third_party/pdfium",
+ ]
+} \ No newline at end of file
diff --git a/samples/image_diff.cc b/samples/image_diff.cc
new file mode 100644
index 0000000000..3f82b66527
--- /dev/null
+++ b/samples/image_diff.cc
@@ -0,0 +1,398 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// This file input format is based loosely on
+// Tools/DumpRenderTree/ImageDiff.m
+
+// The exact format of this tool's output to stdout is important, to match
+// what the run-webkit-tests script expects.
+
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <algorithm>
+#include <cstdint>
+#include <iostream>
+#include <map>
+#include <string>
+#include <vector>
+
+#include "../third_party/base/logging.h"
+#include "../third_party/base/numerics/safe_conversions.h"
+#include "image_diff_png.h"
+
+#if defined(OS_WIN)
+#include "windows.h"
+#endif
+
+// Return codes used by this utility.
+static const int kStatusSame = 0;
+static const int kStatusDifferent = 1;
+static const int kStatusError = 2;
+
+// Color codes.
+static const uint32_t RGBA_RED = 0x000000ff;
+static const uint32_t RGBA_ALPHA = 0xff000000;
+
+class Image {
+ public:
+ Image() : w_(0), h_(0) {
+ }
+
+ Image(const Image& image)
+ : w_(image.w_),
+ h_(image.h_),
+ data_(image.data_) {
+ }
+
+ bool has_image() const {
+ return w_ > 0 && h_ > 0;
+ }
+
+ int w() const {
+ return w_;
+ }
+
+ int h() const {
+ return h_;
+ }
+
+ const unsigned char* data() const {
+ return &data_.front();
+ }
+
+ // Creates the image from the given filename on disk, and returns true on
+ // success.
+ bool CreateFromFilename(const std::string& path) {
+ FILE* f = fopen(path.c_str(), "rb");
+ if (!f)
+ return false;
+
+ std::vector<unsigned char> compressed;
+ const int buf_size = 1024;
+ unsigned char buf[buf_size];
+ size_t num_read = 0;
+ while ((num_read = fread(buf, 1, buf_size, f)) > 0) {
+ compressed.insert(compressed.end(), buf, buf + num_read);
+ }
+
+ fclose(f);
+
+ if (!image_diff_png::DecodePNG(&compressed[0], compressed.size(),
+ &data_, &w_, &h_)) {
+ Clear();
+ return false;
+ }
+ return true;
+ }
+
+ void Clear() {
+ w_ = h_ = 0;
+ data_.clear();
+ }
+
+ // Returns the RGBA value of the pixel at the given location
+ uint32_t pixel_at(int x, int y) const {
+ if (x >= 0 && x < w_ && y >= 0 && y < h_)
+ return *reinterpret_cast<const uint32_t*>(&(data_[(y * w_ + x) * 4]));
+ return 0;
+ }
+
+ void set_pixel_at(int x, int y, uint32_t color) const {
+ if (x >= 0 && x < w_ && y >= 0 && y < h_) {
+ void* addr = &const_cast<unsigned char*>(&data_.front())[(y * w_ + x) * 4];
+ *reinterpret_cast<uint32_t*>(addr) = color;
+ }
+ }
+
+ private:
+ // pixel dimensions of the image
+ int w_, h_;
+
+ std::vector<unsigned char> data_;
+};
+
+float PercentageDifferent(const Image& baseline, const Image& actual) {
+ int w = std::min(baseline.w(), actual.w());
+ int h = std::min(baseline.h(), actual.h());
+
+ // Compute pixels different in the overlap.
+ int pixels_different = 0;
+ for (int y = 0; y < h; y++) {
+ for (int x = 0; x < w; x++) {
+ if (baseline.pixel_at(x, y) != actual.pixel_at(x, y))
+ pixels_different++;
+ }
+ }
+
+ // Count pixels that are a difference in size as also being different.
+ int max_w = std::max(baseline.w(), actual.w());
+ int max_h = std::max(baseline.h(), actual.h());
+ // These pixels are off the right side, not including the lower right corner.
+ pixels_different += (max_w - w) * h;
+ // These pixels are along the bottom, including the lower right corner.
+ pixels_different += (max_h - h) * max_w;
+
+ // Like the WebKit ImageDiff tool, we define percentage different in terms
+ // of the size of the 'actual' bitmap.
+ float total_pixels = static_cast<float>(actual.w()) *
+ static_cast<float>(actual.h());
+ if (total_pixels == 0) {
+ // When the bitmap is empty, they are 100% different.
+ return 100.0f;
+ }
+ return 100.0f * pixels_different / total_pixels;
+}
+
+// FIXME: Replace with unordered_map when available.
+typedef std::map<uint32_t, int32_t> RgbaToCountMap;
+
+float HistogramPercentageDifferent(const Image& baseline, const Image& actual) {
+ // TODO(johnme): Consider using a joint histogram instead, as described in
+ // "Comparing Images Using Joint Histograms" by Pass & Zabih
+ // http://www.cs.cornell.edu/~rdz/papers/pz-jms99.pdf
+
+ int w = std::min(baseline.w(), actual.w());
+ int h = std::min(baseline.h(), actual.h());
+
+ // Count occurences of each RGBA pixel value of baseline in the overlap.
+ RgbaToCountMap baseline_histogram;
+ for (int y = 0; y < h; y++) {
+ for (int x = 0; x < w; x++) {
+ // hash_map operator[] inserts a 0 (default constructor) if key not found.
+ baseline_histogram[baseline.pixel_at(x, y)]++;
+ }
+ }
+
+ // Compute pixels different in the histogram of the overlap.
+ int pixels_different = 0;
+ for (int y = 0; y < h; y++) {
+ for (int x = 0; x < w; x++) {
+ uint32_t actual_rgba = actual.pixel_at(x, y);
+ RgbaToCountMap::iterator it = baseline_histogram.find(actual_rgba);
+ if (it != baseline_histogram.end() && it->second > 0)
+ it->second--;
+ else
+ pixels_different++;
+ }
+ }
+
+ // Count pixels that are a difference in size as also being different.
+ int max_w = std::max(baseline.w(), actual.w());
+ int max_h = std::max(baseline.h(), actual.h());
+ // These pixels are off the right side, not including the lower right corner.
+ pixels_different += (max_w - w) * h;
+ // These pixels are along the bottom, including the lower right corner.
+ pixels_different += (max_h - h) * max_w;
+
+ // Like the WebKit ImageDiff tool, we define percentage different in terms
+ // of the size of the 'actual' bitmap.
+ float total_pixels = static_cast<float>(actual.w()) *
+ static_cast<float>(actual.h());
+ if (total_pixels == 0) {
+ // When the bitmap is empty, they are 100% different.
+ return 100.0f;
+ }
+ return 100.0f * pixels_different / total_pixels;
+}
+
+void PrintHelp() {
+ fprintf(stderr,
+ "Usage:\n"
+ " image_diff [--histogram] <compare file> <reference file>\n"
+ " Compares two files on disk, returning 0 when they are the same;\n"
+ " passing \"--histogram\" additionally calculates a diff of the\n"
+ " RGBA value histograms (which is resistant to shifts in layout)\n"
+ " image_diff --diff <compare file> <reference file> <output file>\n"
+ " Compares two files on disk, outputs an image that visualizes the\n"
+ " difference to <output file>\n");
+}
+
+int CompareImages(const std::string& file1,
+ const std::string& file2,
+ bool compare_histograms) {
+ Image actual_image;
+ Image baseline_image;
+
+ if (!actual_image.CreateFromFilename(file1)) {
+ fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file1.c_str());
+ return kStatusError;
+ }
+ if (!baseline_image.CreateFromFilename(file2)) {
+ fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file2.c_str());
+ return kStatusError;
+ }
+
+ if (compare_histograms) {
+ float percent = HistogramPercentageDifferent(actual_image, baseline_image);
+ const char* passed = percent > 0.0 ? "failed" : "passed";
+ printf("histogram diff: %01.2f%% %s\n", percent, passed);
+ }
+
+ const char* diff_name = compare_histograms ? "exact diff" : "diff";
+ float percent = PercentageDifferent(actual_image, baseline_image);
+ const char* passed = percent > 0.0 ? "failed" : "passed";
+ printf("%s: %01.2f%% %s\n", diff_name, percent, passed);
+ if (percent > 0.0) {
+ // failure: The WebKit version also writes the difference image to
+ // stdout, which seems excessive for our needs.
+ return kStatusDifferent;
+ }
+ // success
+ return kStatusSame;
+
+/* Untested mode that acts like WebKit's image comparator. I wrote this but
+ decided it's too complicated. We may use it in the future if it looks useful
+
+ char buffer[2048];
+ while (fgets(buffer, sizeof(buffer), stdin)) {
+
+ if (strncmp("Content-length: ", buffer, 16) == 0) {
+ char* context;
+ strtok_s(buffer, " ", &context);
+ int image_size = strtol(strtok_s(NULL, " ", &context), NULL, 10);
+
+ bool success = false;
+ if (image_size > 0 && actual_image.has_image() == 0) {
+ if (!actual_image.CreateFromStdin(image_size)) {
+ fputs("Error, input image can't be decoded.\n", stderr);
+ return 1;
+ }
+ } else if (image_size > 0 && baseline_image.has_image() == 0) {
+ if (!baseline_image.CreateFromStdin(image_size)) {
+ fputs("Error, baseline image can't be decoded.\n", stderr);
+ return 1;
+ }
+ } else {
+ fputs("Error, image size must be specified.\n", stderr);
+ return 1;
+ }
+ }
+
+ if (actual_image.has_image() && baseline_image.has_image()) {
+ float percent = PercentageDifferent(actual_image, baseline_image);
+ if (percent > 0.0) {
+ // failure: The WebKit version also writes the difference image to
+ // stdout, which seems excessive for our needs.
+ printf("diff: %01.2f%% failed\n", percent);
+ } else {
+ // success
+ printf("diff: %01.2f%% passed\n", percent);
+ }
+ actual_image.Clear();
+ baseline_image.Clear();
+ }
+
+ fflush(stdout);
+ }
+*/
+}
+
+bool CreateImageDiff(const Image& image1, const Image& image2, Image* out) {
+ int w = std::min(image1.w(), image2.w());
+ int h = std::min(image1.h(), image2.h());
+ *out = Image(image1);
+ bool same = (image1.w() == image2.w()) && (image1.h() == image2.h());
+
+ // TODO(estade): do something with the extra pixels if the image sizes
+ // are different.
+ for (int y = 0; y < h; y++) {
+ for (int x = 0; x < w; x++) {
+ uint32_t base_pixel = image1.pixel_at(x, y);
+ if (base_pixel != image2.pixel_at(x, y)) {
+ // Set differing pixels red.
+ out->set_pixel_at(x, y, RGBA_RED | RGBA_ALPHA);
+ same = false;
+ } else {
+ // Set same pixels as faded.
+ uint32_t alpha = base_pixel & RGBA_ALPHA;
+ uint32_t new_pixel = base_pixel - ((alpha / 2) & RGBA_ALPHA);
+ out->set_pixel_at(x, y, new_pixel);
+ }
+ }
+ }
+
+ return same;
+}
+
+int DiffImages(const std::string& file1,
+ const std::string& file2,
+ const std::string& out_file) {
+ Image actual_image;
+ Image baseline_image;
+
+ if (!actual_image.CreateFromFilename(file1)) {
+ fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file1.c_str());
+ return kStatusError;
+ }
+ if (!baseline_image.CreateFromFilename(file2)) {
+ fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file2.c_str());
+ return kStatusError;
+ }
+
+ Image diff_image;
+ bool same = CreateImageDiff(baseline_image, actual_image, &diff_image);
+ if (same)
+ return kStatusSame;
+
+ std::vector<unsigned char> png_encoding;
+ image_diff_png::EncodeRGBAPNG(
+ diff_image.data(), diff_image.w(), diff_image.h(),
+ diff_image.w() * 4, &png_encoding);
+
+ FILE *f = fopen(out_file.c_str(), "wb");
+ if (!f)
+ return kStatusError;
+
+ size_t size = png_encoding.size();
+ char *ptr = reinterpret_cast<char*>(&png_encoding.front());
+ if (fwrite(ptr, 1, size, f) != size)
+ return kStatusError;
+
+ return kStatusDifferent;
+}
+
+int main(int argc, const char* argv[]) {
+ bool histograms = false;
+ bool produce_diff_image = false;
+ std::string filename1;
+ std::string filename2;
+ std::string diff_filename;
+
+ int i;
+ for (i = 1; i < argc; ++i) {
+ const char* arg = argv[i];
+ if (strstr(arg, "--") != arg)
+ break;
+ if (strcmp(arg, "--histogram") == 0) {
+ histograms = true;
+ } else if (strcmp(arg, "--diff") == 0) {
+ produce_diff_image = true;
+ }
+ }
+ if (i < argc) {
+ filename1 = argv[i];
+ ++i;
+ }
+ if (i < argc) {
+ filename2 = argv[i];
+ ++i;
+ }
+ if (i < argc) {
+ diff_filename = argv[i];
+ ++i;
+ }
+
+ if (produce_diff_image) {
+ if (!diff_filename.empty()) {
+ return DiffImages(filename1, filename2, diff_filename);
+ }
+ } else if (!filename2.empty()) {
+ return CompareImages(filename1, filename2, histograms);
+ }
+
+ PrintHelp();
+ return kStatusError;
+}
diff --git a/samples/image_diff_png.cc b/samples/image_diff_png.cc
new file mode 100644
index 0000000000..2ccc3e5c91
--- /dev/null
+++ b/samples/image_diff_png.cc
@@ -0,0 +1,644 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// This is a duplicate of chromium's src/tools/imagediff/image_diff_png.cc
+// that has been modified to build in a pdfium environment, which itself
+// was duplicated as follows:
+
+// This is a duplicate of ui/gfx/codec/png_codec.cc, after removing code related
+// to Skia, that we can use when running layout tests with minimal dependencies.
+
+#include "image_diff_png.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <string>
+
+#include "../third_party/base/logging.h"
+#include "../core/src/fxcodec/fx_lpng/include/fx_png.h"
+#include "../core/src/fxcodec/fx_zlib/include/fx_zlib.h"
+
+namespace image_diff_png {
+
+namespace {
+
+enum ColorFormat {
+ // 3 bytes per pixel (packed), in RGB order regardless of endianness.
+ // This is the native JPEG format.
+ FORMAT_RGB,
+
+ // 4 bytes per pixel, in RGBA order in memory regardless of endianness.
+ FORMAT_RGBA,
+
+ // 4 bytes per pixel, in BGRA order in memory regardless of endianness.
+ // This is the default Windows DIB order.
+ FORMAT_BGRA,
+};
+
+// Represents a comment in the tEXt ancillary chunk of the png.
+struct Comment {
+ std::string key;
+ std::string text;
+};
+
+// Converts BGRA->RGBA and RGBA->BGRA.
+void ConvertBetweenBGRAandRGBA(const unsigned char* input, int pixel_width,
+ unsigned char* output, bool* is_opaque) {
+ for (int x = 0; x < pixel_width; x++) {
+ const unsigned char* pixel_in = &input[x * 4];
+ unsigned char* pixel_out = &output[x * 4];
+ pixel_out[0] = pixel_in[2];
+ pixel_out[1] = pixel_in[1];
+ pixel_out[2] = pixel_in[0];
+ pixel_out[3] = pixel_in[3];
+ }
+}
+
+void ConvertRGBAtoRGB(const unsigned char* rgba, int pixel_width,
+ unsigned char* rgb, bool* is_opaque) {
+ for (int x = 0; x < pixel_width; x++) {
+ const unsigned char* pixel_in = &rgba[x * 4];
+ unsigned char* pixel_out = &rgb[x * 3];
+ pixel_out[0] = pixel_in[0];
+ pixel_out[1] = pixel_in[1];
+ pixel_out[2] = pixel_in[2];
+ }
+}
+
+} // namespace
+
+// Decoder --------------------------------------------------------------------
+//
+// This code is based on WebKit libpng interface (PNGImageDecoder), which is
+// in turn based on the Mozilla png decoder.
+
+namespace {
+
+// Gamma constants: We assume we're on Windows which uses a gamma of 2.2.
+const double kMaxGamma = 21474.83; // Maximum gamma accepted by png library.
+const double kDefaultGamma = 2.2;
+const double kInverseGamma = 1.0 / kDefaultGamma;
+
+class PngDecoderState {
+ public:
+ // Output is a vector<unsigned char>.
+ PngDecoderState(ColorFormat ofmt, std::vector<unsigned char>* o)
+ : output_format(ofmt),
+ output_channels(0),
+ is_opaque(true),
+ output(o),
+ row_converter(NULL),
+ width(0),
+ height(0),
+ done(false) {
+ }
+
+ ColorFormat output_format;
+ int output_channels;
+
+ // Used during the reading of an SkBitmap. Defaults to true until we see a
+ // pixel with anything other than an alpha of 255.
+ bool is_opaque;
+
+ // An intermediary buffer for decode output.
+ std::vector<unsigned char>* output;
+
+ // Called to convert a row from the library to the correct output format.
+ // When NULL, no conversion is necessary.
+ void (*row_converter)(const unsigned char* in, int w, unsigned char* out,
+ bool* is_opaque);
+
+ // Size of the image, set in the info callback.
+ int width;
+ int height;
+
+ // Set to true when we've found the end of the data.
+ bool done;
+};
+
+void ConvertRGBtoRGBA(const unsigned char* rgb, int pixel_width,
+ unsigned char* rgba, bool* is_opaque) {
+ for (int x = 0; x < pixel_width; x++) {
+ const unsigned char* pixel_in = &rgb[x * 3];
+ unsigned char* pixel_out = &rgba[x * 4];
+ pixel_out[0] = pixel_in[0];
+ pixel_out[1] = pixel_in[1];
+ pixel_out[2] = pixel_in[2];
+ pixel_out[3] = 0xff;
+ }
+}
+
+void ConvertRGBtoBGRA(const unsigned char* rgb, int pixel_width,
+ unsigned char* bgra, bool* is_opaque) {
+ for (int x = 0; x < pixel_width; x++) {
+ const unsigned char* pixel_in = &rgb[x * 3];
+ unsigned char* pixel_out = &bgra[x * 4];
+ pixel_out[0] = pixel_in[2];
+ pixel_out[1] = pixel_in[1];
+ pixel_out[2] = pixel_in[0];
+ pixel_out[3] = 0xff;
+ }
+}
+
+// Called when the png header has been read. This code is based on the WebKit
+// PNGImageDecoder
+void DecodeInfoCallback(png_struct* png_ptr, png_info* info_ptr) {
+ PngDecoderState* state = static_cast<PngDecoderState*>(
+ png_get_progressive_ptr(png_ptr));
+
+ int bit_depth, color_type, interlace_type, compression_type;
+ int filter_type, channels;
+ png_uint_32 w, h;
+ png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type,
+ &interlace_type, &compression_type, &filter_type);
+
+ // Bounds check. When the image is unreasonably big, we'll error out and
+ // end up back at the setjmp call when we set up decoding. "Unreasonably big"
+ // means "big enough that w * h * 32bpp might overflow an int"; we choose this
+ // threshold to match WebKit and because a number of places in code assume
+ // that an image's size (in bytes) fits in a (signed) int.
+ unsigned long long total_size =
+ static_cast<unsigned long long>(w) * static_cast<unsigned long long>(h);
+ if (total_size > ((1 << 29) - 1))
+ longjmp(png_jmpbuf(png_ptr), 1);
+ state->width = static_cast<int>(w);
+ state->height = static_cast<int>(h);
+
+ // Expand to ensure we use 24-bit for RGB and 32-bit for RGBA.
+ if (color_type == PNG_COLOR_TYPE_PALETTE ||
+ (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8))
+ png_set_expand(png_ptr);
+
+ // Transparency for paletted images.
+ if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
+ png_set_expand(png_ptr);
+
+ // Convert 16-bit to 8-bit.
+ if (bit_depth == 16)
+ png_set_strip_16(png_ptr);
+
+ // Expand grayscale to RGB.
+ if (color_type == PNG_COLOR_TYPE_GRAY ||
+ color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
+ png_set_gray_to_rgb(png_ptr);
+
+ // Deal with gamma and keep it under our control.
+ double gamma;
+ if (png_get_gAMA(png_ptr, info_ptr, &gamma)) {
+ if (gamma <= 0.0 || gamma > kMaxGamma) {
+ gamma = kInverseGamma;
+ png_set_gAMA(png_ptr, info_ptr, gamma);
+ }
+ png_set_gamma(png_ptr, kDefaultGamma, gamma);
+ } else {
+ png_set_gamma(png_ptr, kDefaultGamma, kInverseGamma);
+ }
+
+ // Tell libpng to send us rows for interlaced pngs.
+ if (interlace_type == PNG_INTERLACE_ADAM7)
+ png_set_interlace_handling(png_ptr);
+
+ // Update our info now
+ png_read_update_info(png_ptr, info_ptr);
+ channels = png_get_channels(png_ptr, info_ptr);
+
+ // Pick our row format converter necessary for this data.
+ if (channels == 3) {
+ switch (state->output_format) {
+ case FORMAT_RGB:
+ state->row_converter = NULL; // no conversion necessary
+ state->output_channels = 3;
+ break;
+ case FORMAT_RGBA:
+ state->row_converter = &ConvertRGBtoRGBA;
+ state->output_channels = 4;
+ break;
+ case FORMAT_BGRA:
+ state->row_converter = &ConvertRGBtoBGRA;
+ state->output_channels = 4;
+ break;
+ default:
+ NOTREACHED();
+ break;
+ }
+ } else if (channels == 4) {
+ switch (state->output_format) {
+ case FORMAT_RGB:
+ state->row_converter = &ConvertRGBAtoRGB;
+ state->output_channels = 3;
+ break;
+ case FORMAT_RGBA:
+ state->row_converter = NULL; // no conversion necessary
+ state->output_channels = 4;
+ break;
+ case FORMAT_BGRA:
+ state->row_converter = &ConvertBetweenBGRAandRGBA;
+ state->output_channels = 4;
+ break;
+ default:
+ NOTREACHED();
+ break;
+ }
+ } else {
+ NOTREACHED();
+ longjmp(png_jmpbuf(png_ptr), 1);
+ }
+
+ state->output->resize(
+ state->width * state->output_channels * state->height);
+}
+
+void DecodeRowCallback(png_struct* png_ptr, png_byte* new_row,
+ png_uint_32 row_num, int pass) {
+ PngDecoderState* state = static_cast<PngDecoderState*>(
+ png_get_progressive_ptr(png_ptr));
+
+ if (static_cast<int>(row_num) > state->height) {
+ NOTREACHED();
+ return;
+ }
+
+ unsigned char* base = NULL;
+ base = &state->output->front();
+
+ unsigned char* dest = &base[state->width * state->output_channels * row_num];
+ if (state->row_converter)
+ state->row_converter(new_row, state->width, dest, &state->is_opaque);
+ else
+ memcpy(dest, new_row, state->width * state->output_channels);
+}
+
+void DecodeEndCallback(png_struct* png_ptr, png_info* info) {
+ PngDecoderState* state = static_cast<PngDecoderState*>(
+ png_get_progressive_ptr(png_ptr));
+
+ // Mark the image as complete, this will tell the Decode function that we
+ // have successfully found the end of the data.
+ state->done = true;
+}
+
+// Automatically destroys the given read structs on destruction to make
+// cleanup and error handling code cleaner.
+class PngReadStructDestroyer {
+ public:
+ PngReadStructDestroyer(png_struct** ps, png_info** pi) : ps_(ps), pi_(pi) {
+ }
+ ~PngReadStructDestroyer() {
+ png_destroy_read_struct(ps_, pi_, NULL);
+ }
+ private:
+ png_struct** ps_;
+ png_info** pi_;
+};
+
+bool BuildPNGStruct(const unsigned char* input, size_t input_size,
+ png_struct** png_ptr, png_info** info_ptr) {
+ if (input_size < 8)
+ return false; // Input data too small to be a png
+
+ // Have libpng check the signature, it likes the first 8 bytes.
+ if (png_sig_cmp(const_cast<unsigned char*>(input), 0, 8) != 0)
+ return false;
+
+ *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ if (!*png_ptr)
+ return false;
+
+ *info_ptr = png_create_info_struct(*png_ptr);
+ if (!*info_ptr) {
+ png_destroy_read_struct(png_ptr, NULL, NULL);
+ return false;
+ }
+
+ return true;
+}
+
+} // namespace
+
+// static
+bool Decode(const unsigned char* input, size_t input_size,
+ ColorFormat format, std::vector<unsigned char>* output,
+ int* w, int* h) {
+ png_struct* png_ptr = NULL;
+ png_info* info_ptr = NULL;
+ if (!BuildPNGStruct(input, input_size, &png_ptr, &info_ptr))
+ return false;
+
+ PngReadStructDestroyer destroyer(&png_ptr, &info_ptr);
+ if (setjmp(png_jmpbuf(png_ptr))) {
+ // The destroyer will ensure that the structures are cleaned up in this
+ // case, even though we may get here as a jump from random parts of the
+ // PNG library called below.
+ return false;
+ }
+
+ PngDecoderState state(format, output);
+
+ png_set_progressive_read_fn(png_ptr, &state, &DecodeInfoCallback,
+ &DecodeRowCallback, &DecodeEndCallback);
+ png_process_data(png_ptr,
+ info_ptr,
+ const_cast<unsigned char*>(input),
+ input_size);
+
+ if (!state.done) {
+ // Fed it all the data but the library didn't think we got all the data, so
+ // this file must be truncated.
+ output->clear();
+ return false;
+ }
+
+ *w = state.width;
+ *h = state.height;
+ return true;
+}
+
+// Encoder --------------------------------------------------------------------
+//
+// This section of the code is based on nsPNGEncoder.cpp in Mozilla
+// (Copyright 2005 Google Inc.)
+
+namespace {
+
+// Passed around as the io_ptr in the png structs so our callbacks know where
+// to write data.
+struct PngEncoderState {
+ explicit PngEncoderState(std::vector<unsigned char>* o) : out(o) {}
+ std::vector<unsigned char>* out;
+};
+
+// Called by libpng to flush its internal buffer to ours.
+void EncoderWriteCallback(png_structp png, png_bytep data, png_size_t size) {
+ PngEncoderState* state = static_cast<PngEncoderState*>(png_get_io_ptr(png));
+ size_t old_size = state->out->size();
+ state->out->resize(old_size + size);
+ memcpy(&(*state->out)[old_size], data, size);
+}
+
+void FakeFlushCallback(png_structp png) {
+ // We don't need to perform any flushing since we aren't doing real IO, but
+ // we're required to provide this function by libpng.
+}
+
+void ConvertBGRAtoRGB(const unsigned char* bgra, int pixel_width,
+ unsigned char* rgb, bool* is_opaque) {
+ for (int x = 0; x < pixel_width; x++) {
+ const unsigned char* pixel_in = &bgra[x * 4];
+ unsigned char* pixel_out = &rgb[x * 3];
+ pixel_out[0] = pixel_in[2];
+ pixel_out[1] = pixel_in[1];
+ pixel_out[2] = pixel_in[0];
+ }
+}
+
+#ifdef PNG_TEXT_SUPPORTED
+
+inline char* strdup(const char* str) {
+#if defined(OS_WIN)
+ return _strdup(str);
+#else
+ return ::strdup(str);
+#endif
+}
+
+class CommentWriter {
+ public:
+ explicit CommentWriter(const std::vector<Comment>& comments)
+ : comments_(comments),
+ png_text_(new png_text[comments.size()]) {
+ for (size_t i = 0; i < comments.size(); ++i)
+ AddComment(i, comments[i]);
+ }
+
+ ~CommentWriter() {
+ for (size_t i = 0; i < comments_.size(); ++i) {
+ free(png_text_[i].key);
+ free(png_text_[i].text);
+ }
+ delete [] png_text_;
+ }
+
+ bool HasComments() {
+ return !comments_.empty();
+ }
+
+ png_text* get_png_text() {
+ return png_text_;
+ }
+
+ int size() {
+ return static_cast<int>(comments_.size());
+ }
+
+ private:
+ void AddComment(size_t pos, const Comment& comment) {
+ png_text_[pos].compression = PNG_TEXT_COMPRESSION_NONE;
+ // A PNG comment's key can only be 79 characters long.
+ if (comment.key.length() > 79)
+ return;
+ png_text_[pos].key = strdup(comment.key.substr(0, 78).c_str());
+ png_text_[pos].text = strdup(comment.text.c_str());
+ png_text_[pos].text_length = comment.text.length();
+#ifdef PNG_iTXt_SUPPORTED
+ png_text_[pos].itxt_length = 0;
+ png_text_[pos].lang = 0;
+ png_text_[pos].lang_key = 0;
+#endif
+ }
+
+ const std::vector<Comment> comments_;
+ png_text* png_text_;
+};
+#endif // PNG_TEXT_SUPPORTED
+
+// The type of functions usable for converting between pixel formats.
+typedef void (*FormatConverter)(const unsigned char* in, int w,
+ unsigned char* out, bool* is_opaque);
+
+// libpng uses a wacky setjmp-based API, which makes the compiler nervous.
+// We constrain all of the calls we make to libpng where the setjmp() is in
+// place to this function.
+// Returns true on success.
+bool DoLibpngWrite(png_struct* png_ptr, png_info* info_ptr,
+ PngEncoderState* state,
+ int width, int height, int row_byte_width,
+ const unsigned char* input, int compression_level,
+ int png_output_color_type, int output_color_components,
+ FormatConverter converter,
+ const std::vector<Comment>& comments) {
+#ifdef PNG_TEXT_SUPPORTED
+ CommentWriter comment_writer(comments);
+#endif
+ unsigned char* row_buffer = NULL;
+
+ // Make sure to not declare any locals here -- locals in the presence
+ // of setjmp() in C++ code makes gcc complain.
+
+ if (setjmp(png_jmpbuf(png_ptr))) {
+ delete[] row_buffer;
+ return false;
+ }
+
+ png_set_compression_level(png_ptr, compression_level);
+
+ // Set our callback for libpng to give us the data.
+ png_set_write_fn(png_ptr, state, EncoderWriteCallback, FakeFlushCallback);
+
+ png_set_IHDR(png_ptr, info_ptr, width, height, 8, png_output_color_type,
+ PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
+ PNG_FILTER_TYPE_DEFAULT);
+
+#ifdef PNG_TEXT_SUPPORTED
+ if (comment_writer.HasComments()) {
+ png_set_text(png_ptr, info_ptr, comment_writer.get_png_text(),
+ comment_writer.size());
+ }
+#endif
+
+ png_write_info(png_ptr, info_ptr);
+
+ if (!converter) {
+ // No conversion needed, give the data directly to libpng.
+ for (int y = 0; y < height; y ++) {
+ png_write_row(png_ptr,
+ const_cast<unsigned char*>(&input[y * row_byte_width]));
+ }
+ } else {
+ // Needs conversion using a separate buffer.
+ row_buffer = new unsigned char[width * output_color_components];
+ for (int y = 0; y < height; y ++) {
+ converter(&input[y * row_byte_width], width, row_buffer, NULL);
+ png_write_row(png_ptr, row_buffer);
+ }
+ delete[] row_buffer;
+ }
+
+ png_write_end(png_ptr, info_ptr);
+ return true;
+}
+
+} // namespace
+
+// static
+bool EncodeWithCompressionLevel(const unsigned char* input, ColorFormat format,
+ const int width, const int height,
+ int row_byte_width,
+ bool discard_transparency,
+ const std::vector<Comment>& comments,
+ int compression_level,
+ std::vector<unsigned char>* output) {
+ // Run to convert an input row into the output row format, NULL means no
+ // conversion is necessary.
+ FormatConverter converter = NULL;
+
+ int input_color_components, output_color_components;
+ int png_output_color_type;
+ switch (format) {
+ case FORMAT_RGB:
+ input_color_components = 3;
+ output_color_components = 3;
+ png_output_color_type = PNG_COLOR_TYPE_RGB;
+ discard_transparency = false;
+ break;
+
+ case FORMAT_RGBA:
+ input_color_components = 4;
+ if (discard_transparency) {
+ output_color_components = 3;
+ png_output_color_type = PNG_COLOR_TYPE_RGB;
+ converter = ConvertRGBAtoRGB;
+ } else {
+ output_color_components = 4;
+ png_output_color_type = PNG_COLOR_TYPE_RGB_ALPHA;
+ converter = NULL;
+ }
+ break;
+
+ case FORMAT_BGRA:
+ input_color_components = 4;
+ if (discard_transparency) {
+ output_color_components = 3;
+ png_output_color_type = PNG_COLOR_TYPE_RGB;
+ converter = ConvertBGRAtoRGB;
+ } else {
+ output_color_components = 4;
+ png_output_color_type = PNG_COLOR_TYPE_RGB_ALPHA;
+ converter = ConvertBetweenBGRAandRGBA;
+ }
+ break;
+
+ default:
+ NOTREACHED();
+ return false;
+ }
+
+ // Row stride should be at least as long as the length of the data.
+ if (input_color_components * width < row_byte_width)
+ return false;
+
+ png_struct* png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
+ NULL, NULL, NULL);
+ if (!png_ptr)
+ return false;
+ png_info* info_ptr = png_create_info_struct(png_ptr);
+ if (!info_ptr) {
+ png_destroy_write_struct(&png_ptr, NULL);
+ return false;
+ }
+
+ PngEncoderState state(output);
+ bool success = DoLibpngWrite(png_ptr, info_ptr, &state,
+ width, height, row_byte_width,
+ input, compression_level, png_output_color_type,
+ output_color_components, converter, comments);
+ png_destroy_write_struct(&png_ptr, &info_ptr);
+
+ return success;
+}
+
+// static
+bool Encode(const unsigned char* input, ColorFormat format,
+ const int width, const int height, int row_byte_width,
+ bool discard_transparency,
+ const std::vector<Comment>& comments,
+ std::vector<unsigned char>* output) {
+ return EncodeWithCompressionLevel(input, format, width, height,
+ row_byte_width,
+ discard_transparency,
+ comments, Z_DEFAULT_COMPRESSION,
+ output);
+}
+
+// Decode a PNG into an RGBA pixel array.
+bool DecodePNG(const unsigned char* input, size_t input_size,
+ std::vector<unsigned char>* output,
+ int* width, int* height) {
+ return Decode(input, input_size, FORMAT_RGBA, output, width, height);
+}
+
+// Encode an RGBA pixel array into a PNG.
+bool EncodeRGBAPNG(const unsigned char* input,
+ int width,
+ int height,
+ int row_byte_width,
+ std::vector<unsigned char>* output) {
+ return Encode(input, FORMAT_RGBA,
+ width, height, row_byte_width, false,
+ std::vector<Comment>(), output);
+}
+
+// Encode an BGRA pixel array into a PNG.
+bool EncodeBGRAPNG(const unsigned char* input,
+ int width,
+ int height,
+ int row_byte_width,
+ bool discard_transparency,
+ std::vector<unsigned char>* output) {
+ return Encode(input, FORMAT_BGRA,
+ width, height, row_byte_width, discard_transparency,
+ std::vector<Comment>(), output);
+}
+
+} // image_diff_png
diff --git a/samples/image_diff_png.h b/samples/image_diff_png.h
new file mode 100644
index 0000000000..ea77e9a98f
--- /dev/null
+++ b/samples/image_diff_png.h
@@ -0,0 +1,34 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef TOOLS_IMAGEDIFF_IMAGE_DIFF_PNG_H_
+#define TOOLS_IMAGEDIFF_IMAGE_DIFF_PNG_H_
+
+#include <vector>
+
+namespace image_diff_png {
+
+// Decode a PNG into an RGBA pixel array.
+bool DecodePNG(const unsigned char* input, size_t input_size,
+ std::vector<unsigned char>* output,
+ int* width, int* height);
+
+// Encode an RGBA pixel array into a PNG.
+bool EncodeRGBAPNG(const unsigned char* input,
+ int width,
+ int height,
+ int row_byte_width,
+ std::vector<unsigned char>* output);
+
+// Encode an BGRA pixel array into a PNG.
+bool EncodeBGRAPNG(const unsigned char* input,
+ int width,
+ int height,
+ int row_byte_width,
+ bool discard_transparency,
+ std::vector<unsigned char>* output);
+
+} // namespace image_diff_png
+
+#endif // TOOLS_IMAGEDIFF_IMAGE_DIFF_PNG_H_
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");
diff --git a/samples/samples.gyp b/samples/samples.gyp
index 0c8ee93661..9d316ec005 100644
--- a/samples/samples.gyp
+++ b/samples/samples.gyp
@@ -15,6 +15,24 @@
'target_name': 'pdfium_test',
'sources': [
'pdfium_test.cc',
+ 'image_diff_png.cc',
+ ],
+ },
+ {
+ 'target_name': 'pdfium_diff',
+ 'type': 'executable',
+ 'variables': { 'enable_wexit_time_destructors': 1, },
+ 'dependencies': [
+ '../pdfium.gyp:fxcodec',
+ '../third_party/third_party.gyp:safemath',
+ ],
+ 'include_dirs': [
+ '../../',
+ ],
+ 'sources': [
+ 'image_diff.cc',
+ 'image_diff_png.h',
+ 'image_diff_png.cc',
],
},
],