summaryrefslogtreecommitdiff
path: root/testing/utils
diff options
context:
space:
mode:
authorHenrique Nakashima <hnakashima@chromium.org>2018-08-16 16:41:42 +0000
committerChromium commit bot <commit-bot@chromium.org>2018-08-16 16:41:42 +0000
commitf956badf3dd2c7eedba47dfcb787d6e6dfe92cf7 (patch)
treee14a2baf941514395ea29922eb9960a1a20dfe9d /testing/utils
parent048d7f7c1e0c6c42679a5646ae9db5b7d98ed755 (diff)
downloadpdfium-f956badf3dd2c7eedba47dfcb787d6e6dfe92cf7.tar.xz
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 <hnakashima@chromium.org> Reviewed-by: Ryan Harrison <rharrison@chromium.org> Reviewed-by: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'testing/utils')
-rw-r--r--testing/utils/bitmap_saver.cpp48
-rw-r--r--testing/utils/bitmap_saver.h21
2 files changed, 69 insertions, 0 deletions
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 <fstream>
+#include <vector>
+
+#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<const unsigned char*>(FPDFBitmap_GetBuffer(bitmap));
+
+ std::vector<unsigned char> 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<char*>(&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<FPDF_BITMAP>(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 <string>
+
+#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_