From f956badf3dd2c7eedba47dfcb787d6e6dfe92cf7 Mon Sep 17 00:00:00 2001 From: Henrique Nakashima Date: Thu, 16 Aug 2018 16:41:42 +0000 Subject: Add a way to save a barcode generated bitmap to a .png. This is useful for debugging and validation purposes. The hashes that are in BarcodeTest are not necessarily for valid outputs. This CL refactors the code in embedder_test.png that already does this, moving it to testing/utils where unit tests can access it too. Bug: pdfium:1135 Change-Id: I6f1d70a4e133f8f04dbe52646087f99c448e95f8 Reviewed-on: https://pdfium-review.googlesource.com/40152 Commit-Queue: Henrique Nakashima Reviewed-by: Ryan Harrison Reviewed-by: Tom Sepez --- BUILD.gn | 3 +++ testing/embedder_test.cpp | 31 ++------------------------ testing/utils/bitmap_saver.cpp | 48 ++++++++++++++++++++++++++++++++++++++++ testing/utils/bitmap_saver.h | 21 ++++++++++++++++++ xfa/fwl/cfx_barcode_unittest.cpp | 6 +++++ 5 files changed, 80 insertions(+), 29 deletions(-) create mode 100644 testing/utils/bitmap_saver.cpp create mode 100644 testing/utils/bitmap_saver.h diff --git a/BUILD.gn b/BUILD.gn index 4fbb2932ad..8e236e174d 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -248,6 +248,8 @@ jumbo_static_library("test_support") { "testing/string_write_stream.h", "testing/test_support.cpp", "testing/test_support.h", + "testing/utils/bitmap_saver.cpp", + "testing/utils/bitmap_saver.h", "testing/utils/path_service.cpp", "testing/utils/path_service.h", ] @@ -255,6 +257,7 @@ jumbo_static_library("test_support") { "testing/resources/", ] deps = [ + ":image_diff", ":pdfium", "//testing/gmock", "//testing/gtest", diff --git a/testing/embedder_test.cpp b/testing/embedder_test.cpp index 3f953d22df..215009ceac 100644 --- a/testing/embedder_test.cpp +++ b/testing/embedder_test.cpp @@ -6,13 +6,11 @@ #include -#include #include #include #include #include #include -#include #include "core/fdrm/crypto/fx_crypt.h" #include "public/cpp/fpdf_scopers.h" @@ -21,8 +19,8 @@ #include "public/fpdf_text.h" #include "public/fpdfview.h" #include "testing/gmock/include/gmock/gmock.h" -#include "testing/image_diff/image_diff_png.h" #include "testing/test_support.h" +#include "testing/utils/bitmap_saver.h" #include "testing/utils/path_service.h" #include "third_party/base/logging.h" #include "third_party/base/ptr_util.h" @@ -494,32 +492,7 @@ std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) { // static void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap, const std::string& filename) { - const int stride = FPDFBitmap_GetStride(bitmap); - const int width = FPDFBitmap_GetWidth(bitmap); - const int height = FPDFBitmap_GetHeight(bitmap); - const auto* buffer = - static_cast(FPDFBitmap_GetBuffer(bitmap)); - - std::vector png_encoding; - bool encoded; - if (FPDFBitmap_GetFormat(bitmap) == FPDFBitmap_Gray) { - encoded = image_diff_png::EncodeGrayPNG(buffer, width, height, stride, - &png_encoding); - } else { - encoded = image_diff_png::EncodeBGRAPNG(buffer, width, height, stride, - /*discard_transparency=*/false, - &png_encoding); - } - - ASSERT_TRUE(encoded); - ASSERT_LT(filename.size(), 256u); - - std::ofstream png_file; - png_file.open(filename, std::ios_base::out | std::ios_base::binary); - png_file.write(reinterpret_cast(&png_encoding.front()), - png_encoding.size()); - ASSERT_TRUE(png_file.good()); - png_file.close(); + BitmapSaver::WriteBitmapToPng(bitmap, filename); } #endif diff --git a/testing/utils/bitmap_saver.cpp b/testing/utils/bitmap_saver.cpp new file mode 100644 index 0000000000..38f2cf2738 --- /dev/null +++ b/testing/utils/bitmap_saver.cpp @@ -0,0 +1,48 @@ +// Copyright 2018 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "testing/utils/bitmap_saver.h" + +#include +#include + +#include "testing/gtest/include/gtest/gtest.h" +#include "testing/image_diff/image_diff_png.h" + +// static +void BitmapSaver::WriteBitmapToPng(FPDF_BITMAP bitmap, + const std::string& filename) { + const int stride = FPDFBitmap_GetStride(bitmap); + const int width = FPDFBitmap_GetWidth(bitmap); + const int height = FPDFBitmap_GetHeight(bitmap); + const auto* buffer = + static_cast(FPDFBitmap_GetBuffer(bitmap)); + + std::vector png_encoding; + bool encoded; + if (FPDFBitmap_GetFormat(bitmap) == FPDFBitmap_Gray) { + encoded = image_diff_png::EncodeGrayPNG(buffer, width, height, stride, + &png_encoding); + } else { + encoded = image_diff_png::EncodeBGRAPNG(buffer, width, height, stride, + /*discard_transparency=*/false, + &png_encoding); + } + + ASSERT_TRUE(encoded); + ASSERT_LT(filename.size(), 256u); + + std::ofstream png_file; + png_file.open(filename, std::ios_base::out | std::ios_base::binary); + png_file.write(reinterpret_cast(&png_encoding.front()), + png_encoding.size()); + ASSERT_TRUE(png_file.good()); + png_file.close(); +} + +// static +void BitmapSaver::WriteBitmapToPng(CFX_DIBitmap* bitmap, + const std::string& filename) { + WriteBitmapToPng(reinterpret_cast(bitmap), filename); +} diff --git a/testing/utils/bitmap_saver.h b/testing/utils/bitmap_saver.h new file mode 100644 index 0000000000..9f931fc323 --- /dev/null +++ b/testing/utils/bitmap_saver.h @@ -0,0 +1,21 @@ +// Copyright 2018 PDFium 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 TESTING_UTILS_BITMAP_SAVER_H_ +#define TESTING_UTILS_BITMAP_SAVER_H_ + +#include + +#include "public/fpdfview.h" + +class CFX_DIBitmap; + +class BitmapSaver { + public: + static void WriteBitmapToPng(FPDF_BITMAP bitmap, const std::string& filename); + static void WriteBitmapToPng(CFX_DIBitmap* bitmap, + const std::string& filename); +}; + +#endif // TESTING_UTILS_BITMAP_SAVER_H_ diff --git a/xfa/fwl/cfx_barcode_unittest.cpp b/xfa/fwl/cfx_barcode_unittest.cpp index 4b1dc059ce..0fa2352175 100644 --- a/xfa/fwl/cfx_barcode_unittest.cpp +++ b/xfa/fwl/cfx_barcode_unittest.cpp @@ -14,6 +14,7 @@ #include "core/fxge/cfx_renderdevice.h" #include "testing/gtest/include/gtest/gtest.h" #include "testing/test_support.h" +#include "testing/utils/bitmap_saver.h" #include "third_party/base/ptr_util.h" class BarcodeTest : public testing::Test { @@ -58,6 +59,11 @@ class BarcodeTest : public testing::Test { bitmap_->GetPitch() * bitmap_->GetHeight()); } + // Manually insert calls to this as needed for debugging. + void SaveBitmap(const std::string& filename) { + BitmapSaver::WriteBitmapToPng(bitmap_.Get(), filename); + } + protected: CFX_Matrix matrix_; std::unique_ptr barcode_; -- cgit v1.2.3