From dc2bb9ad21a10550fb451d7c842c63cbce98045b Mon Sep 17 00:00:00 2001 From: Henrique Nakashima Date: Tue, 21 Aug 2018 19:50:17 +0000 Subject: Optimize rendering of two dimensional barcodes: defer upscale. Defer upscaling as late as possible so that intermediary data structures are smaller. Made a couple of changes along the way to preserve the barcode correctness and fix some padding issues. For my example, this is a ~21x improvement in rendering time, down from ~190ms per barcode to ~9ms. Bug: 872907, pdfium:1135 Change-Id: If532e0f168f02fea9c31d473f34c0009da4f4612 Reviewed-on: https://pdfium-review.googlesource.com/40010 Reviewed-by: Lei Zhang Commit-Queue: Henrique Nakashima --- DEPS | 2 +- fxbarcode/BC_TwoDimWriter.cpp | 101 ++++++++++++--------- fxbarcode/BC_TwoDimWriter.h | 10 ++ fxbarcode/common/BC_CommonBitMatrix.cpp | 4 +- .../xfa_specific/barcode_test_expected.pdf.0.png | Bin 10717 -> 10669 bytes xfa/fwl/cfx_barcode_unittest.cpp | 2 +- 6 files changed, 71 insertions(+), 48 deletions(-) diff --git a/DEPS b/DEPS index 997eb197fe..eed693cba1 100644 --- a/DEPS +++ b/DEPS @@ -29,7 +29,7 @@ vars = { 'jinja2_revision': '45571de473282bd1d8b63a8dfcb1fd268d0635d2', 'jpeg_turbo_revision': '7260e4d8b8e1e40b17f03fafdf1cd83296900f76', 'markupsafe_revision': '8f45f5cfa0009d2a70589bcda0349b8cb2b72783', - 'pdfium_tests_revision': '0121d24bf42e437bea80dffe251cccf013482551', + 'pdfium_tests_revision': '5d08954b10c09828682c5c26ad8397f36966f8c1', 'skia_revision': '588f879677d4f36e16a42dd96876534f104c2e2f', 'tools_memory_revision': 'f7b00daf4df7f6c469f5fbc68d7f40f6bd15d6e6', 'trace_event_revision': '211b3ed9d0481b4caddbee1322321b86a483ca1f', diff --git a/fxbarcode/BC_TwoDimWriter.cpp b/fxbarcode/BC_TwoDimWriter.cpp index 09f39c2443..36e554c6cf 100644 --- a/fxbarcode/BC_TwoDimWriter.cpp +++ b/fxbarcode/BC_TwoDimWriter.cpp @@ -21,32 +21,41 @@ CBC_TwoDimWriter::~CBC_TwoDimWriter() {} void CBC_TwoDimWriter::RenderDeviceResult(CFX_RenderDevice* device, const CFX_Matrix* matrix) { + ASSERT(m_output); + CFX_GraphStateData stateData; CFX_PathData path; path.AppendRect(0, 0, (float)m_Width, (float)m_Height); device->DrawPath(&path, matrix, &stateData, m_backgroundColor, m_backgroundColor, FXFILL_ALTERNATE); - int32_t leftPos = 0; - int32_t topPos = 0; - if (m_bFixedSize) { - leftPos = (m_Width - m_output->GetWidth()) / 2; - topPos = (m_Height - m_output->GetHeight()) / 2; - } + int32_t leftPos = m_leftPadding; + int32_t topPos = m_topPadding; + CFX_Matrix matri = *matrix; - if (m_Width < m_output->GetWidth() && m_Height < m_output->GetHeight()) { - CFX_Matrix matriScale((float)m_Width / (float)m_output->GetWidth(), 0.0, - 0.0, (float)m_Height / (float)m_output->GetHeight(), - 0.0, 0.0); + if (m_Width < m_outputWidth && m_Height < m_outputHeight) { + CFX_Matrix matriScale(static_cast(m_Width) / m_outputWidth, 0.0, 0.0, + static_cast(m_Height) / m_outputHeight, 0.0, + 0.0); matriScale.Concat(*matrix); matri = matriScale; } - for (int32_t x = 0; x < m_output->GetWidth(); x++) { - for (int32_t y = 0; y < m_output->GetHeight(); y++) { + + CFX_GraphStateData data; + for (int32_t x = 0; x < m_inputWidth; x++) { + for (int32_t y = 0; y < m_inputHeight; y++) { if (m_output->Get(x, y)) { - CFX_GraphStateData data; + // In the output, each module is shifted by 1 due to the one module + // padding added to create quiet areas. + int start_x_output = x + 1; + int end_x_output = x + 2; + int start_y_output = y + 1; + int end_y_output = y + 2; + CFX_PathData rect; - rect.AppendRect((float)leftPos + x, (float)topPos + y, - (float)(leftPos + x + 1), (float)(topPos + y + 1)); + rect.AppendRect(leftPos + start_x_output * m_multiX, + topPos + start_y_output * m_multiY, + leftPos + end_x_output * m_multiX, + topPos + end_y_output * m_multiY); device->DrawPath(&rect, &matri, &data, m_barColor, 0, FXFILL_WINDING); } } @@ -60,10 +69,10 @@ int32_t CBC_TwoDimWriter::GetErrorCorrectionLevel() const { bool CBC_TwoDimWriter::RenderResult(uint8_t* code, int32_t codeWidth, int32_t codeHeight) { - int32_t inputWidth = codeWidth; - int32_t inputHeight = codeHeight; - int32_t tempWidth = inputWidth + 2; - int32_t tempHeight = inputHeight + 2; + m_inputWidth = codeWidth; + m_inputHeight = codeHeight; + int32_t tempWidth = m_inputWidth + 2; + int32_t tempHeight = m_inputHeight + 2; float moduleHSize = std::min(m_ModuleWidth, m_ModuleHeight); moduleHSize = std::min(moduleHSize, 8.0f); moduleHSize = std::max(moduleHSize, 1.0f); @@ -71,41 +80,45 @@ bool CBC_TwoDimWriter::RenderResult(uint8_t* code, pdfium::base::CheckedNumeric scaledHeight = tempHeight; scaledWidth *= moduleHSize; scaledHeight *= moduleHSize; + m_outputWidth = scaledWidth.ValueOrDie(); + m_outputHeight = scaledHeight.ValueOrDie(); - int32_t outputWidth = scaledWidth.ValueOrDie(); - int32_t outputHeight = scaledHeight.ValueOrDie(); if (m_bFixedSize) { - if (m_Width < outputWidth || m_Height < outputHeight) { + if (m_Width < m_outputWidth || m_Height < m_outputHeight) { return false; } } else { - if (m_Width > outputWidth || m_Height > outputHeight) { - outputWidth = - (int32_t)(outputWidth * ceil((float)m_Width / (float)outputWidth)); - outputHeight = - (int32_t)(outputHeight * ceil((float)m_Height / (float)outputHeight)); + if (m_Width > m_outputWidth || m_Height > m_outputHeight) { + int32_t width_factor = static_cast( + floor(static_cast(m_Width) / m_outputWidth)); + int32_t height_factor = static_cast( + floor(static_cast(m_Height) / m_outputHeight)); + width_factor = std::max(width_factor, 1); + height_factor = std::max(height_factor, 1); + + m_outputWidth *= width_factor; + m_outputHeight *= height_factor; } } - int32_t multiX = (int32_t)ceil((float)outputWidth / (float)tempWidth); - int32_t multiY = (int32_t)ceil((float)outputHeight / (float)tempHeight); + m_multiX = + static_cast(ceil(static_cast(m_outputWidth) / tempWidth)); + m_multiY = static_cast( + ceil(static_cast(m_outputHeight) / tempHeight)); if (m_bFixedSize) { - multiX = std::min(multiX, multiY); - multiY = multiX; + m_multiX = std::min(m_multiX, m_multiY); + m_multiY = m_multiX; } - int32_t leftPadding = std::max((outputWidth - (inputWidth * multiX)) / 2, 0); - int32_t topPadding = std::max((outputHeight - (inputHeight * multiY)) / 2, 0); + + m_leftPadding = std::max((m_Width - m_outputWidth) / 2, 0); + m_topPadding = std::max((m_Height - m_outputHeight) / 2, 0); + m_output = pdfium::MakeUnique(); - m_output->Init(outputWidth, outputHeight); - for (int32_t inputY = 0, outputY = topPadding; - (inputY < inputHeight) && (outputY < outputHeight - multiY); - inputY++, outputY += multiY) { - for (int32_t inputX = 0, outputX = leftPadding; - (inputX < inputWidth) && (outputX < outputWidth - multiX); - inputX++, outputX += multiX) { - if (code[inputX + inputY * inputWidth] == 1 && - !m_output->SetRegion(outputX, outputY, multiX, multiY)) { - return false; - } + m_output->Init(m_inputWidth, m_inputHeight); + + for (int32_t y = 0; y < m_inputHeight; ++y) { + for (int32_t x = 0; x < m_inputWidth; ++x) { + if (code[x + y * m_inputWidth] == 1) + m_output->Set(x, y); } } return true; diff --git a/fxbarcode/BC_TwoDimWriter.h b/fxbarcode/BC_TwoDimWriter.h index e554805fcf..228a2e03e9 100644 --- a/fxbarcode/BC_TwoDimWriter.h +++ b/fxbarcode/BC_TwoDimWriter.h @@ -31,7 +31,17 @@ class CBC_TwoDimWriter : public CBC_Writer { protected: int32_t m_iCorrectLevel; bool m_bFixedSize; + + private: std::unique_ptr m_output; + int32_t m_multiX; + int32_t m_multiY; + int32_t m_leftPadding; + int32_t m_topPadding; + int32_t m_inputWidth; + int32_t m_inputHeight; + int32_t m_outputWidth; + int32_t m_outputHeight; }; #endif // FXBARCODE_BC_TWODIMWRITER_H_ diff --git a/fxbarcode/common/BC_CommonBitMatrix.cpp b/fxbarcode/common/BC_CommonBitMatrix.cpp index a8a6e53bc1..54b1affa71 100644 --- a/fxbarcode/common/BC_CommonBitMatrix.cpp +++ b/fxbarcode/common/BC_CommonBitMatrix.cpp @@ -59,8 +59,8 @@ bool CBC_CommonBitMatrix::Get(int32_t x, int32_t y) const { void CBC_CommonBitMatrix::Set(int32_t x, int32_t y) { int32_t offset = y * m_rowSize + (x >> 5); - if (offset >= m_rowSize * m_height || offset < 0) - return; + ASSERT(offset >= 0); + ASSERT(offset < m_rowSize * m_height); m_bits[offset] |= 1 << (x & 0x1f); } diff --git a/testing/resources/pixel/xfa_specific/barcode_test_expected.pdf.0.png b/testing/resources/pixel/xfa_specific/barcode_test_expected.pdf.0.png index b56b826839..4138165e64 100644 Binary files a/testing/resources/pixel/xfa_specific/barcode_test_expected.pdf.0.png and b/testing/resources/pixel/xfa_specific/barcode_test_expected.pdf.0.png differ diff --git a/xfa/fwl/cfx_barcode_unittest.cpp b/xfa/fwl/cfx_barcode_unittest.cpp index 1113f39475..38aafeebf8 100644 --- a/xfa/fwl/cfx_barcode_unittest.cpp +++ b/xfa/fwl/cfx_barcode_unittest.cpp @@ -131,7 +131,7 @@ TEST_F(BarcodeTest, Pdf417) { EXPECT_TRUE(Create(BC_PDF417)); EXPECT_TRUE(barcode()->Encode(L"clams")); RenderDevice(); - EXPECT_EQ("fdd7d7ad325551927d9207a725b3832b", BitmapChecksum()); + EXPECT_EQ("35e94f2da8649fb1402534cc65085999", BitmapChecksum()); } TEST_F(BarcodeTest, DataMatrix) { -- cgit v1.2.3