Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1 | // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Lei Zhang | 176c0f8 | 2018-12-05 18:29:36 +0000 | [diff] [blame] | 7 | #include "fxbarcode/BC_TwoDimWriter.h" |
| 8 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
dsinclair | 74a34fc | 2016-09-29 16:41:42 -0700 | [diff] [blame] | 11 | #include "core/fxge/cfx_graphstatedata.h" |
| 12 | #include "core/fxge/cfx_pathdata.h" |
| 13 | #include "core/fxge/cfx_renderdevice.h" |
Dan Sinclair | e778668 | 2017-03-29 15:18:41 -0400 | [diff] [blame] | 14 | #include "fxbarcode/BC_Writer.h" |
| 15 | #include "fxbarcode/common/BC_CommonBitMatrix.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 16 | #include "third_party/base/numerics/safe_math.h" |
tsepez | a9caab9 | 2016-12-14 05:57:10 -0800 | [diff] [blame] | 17 | #include "third_party/base/ptr_util.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 18 | |
Lei Zhang | 04bd91f | 2018-12-04 23:52:04 +0000 | [diff] [blame] | 19 | CBC_TwoDimWriter::CBC_TwoDimWriter(bool bFixedSize) |
| 20 | : m_bFixedSize(bFixedSize) {} |
weili | e76203d | 2016-08-09 13:45:03 -0700 | [diff] [blame] | 21 | |
Lei Zhang | 04bd91f | 2018-12-04 23:52:04 +0000 | [diff] [blame] | 22 | CBC_TwoDimWriter::~CBC_TwoDimWriter() = default; |
weili | 29b8ad0 | 2016-06-14 18:20:04 -0700 | [diff] [blame] | 23 | |
Tom Sepez | 8a11888 | 2019-11-20 23:54:33 +0000 | [diff] [blame] | 24 | bool CBC_TwoDimWriter::RenderResult(pdfium::span<const uint8_t> code, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 25 | int32_t codeWidth, |
Lei Zhang | 1badb85 | 2017-04-20 15:58:56 -0700 | [diff] [blame] | 26 | int32_t codeHeight) { |
Lei Zhang | 176c0f8 | 2018-12-05 18:29:36 +0000 | [diff] [blame] | 27 | if (code.empty()) |
| 28 | return false; |
| 29 | |
Henrique Nakashima | dc2bb9a | 2018-08-21 19:50:17 +0000 | [diff] [blame] | 30 | m_inputWidth = codeWidth; |
| 31 | m_inputHeight = codeHeight; |
| 32 | int32_t tempWidth = m_inputWidth + 2; |
| 33 | int32_t tempHeight = m_inputHeight + 2; |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 34 | float moduleHSize = std::min(m_ModuleWidth, m_ModuleHeight); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 35 | moduleHSize = std::min(moduleHSize, 8.0f); |
| 36 | moduleHSize = std::max(moduleHSize, 1.0f); |
| 37 | pdfium::base::CheckedNumeric<int32_t> scaledWidth = tempWidth; |
| 38 | pdfium::base::CheckedNumeric<int32_t> scaledHeight = tempHeight; |
| 39 | scaledWidth *= moduleHSize; |
| 40 | scaledHeight *= moduleHSize; |
Henrique Nakashima | dc2bb9a | 2018-08-21 19:50:17 +0000 | [diff] [blame] | 41 | m_outputWidth = scaledWidth.ValueOrDie(); |
| 42 | m_outputHeight = scaledHeight.ValueOrDie(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 43 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 44 | if (m_bFixedSize) { |
Henrique Nakashima | dc2bb9a | 2018-08-21 19:50:17 +0000 | [diff] [blame] | 45 | if (m_Width < m_outputWidth || m_Height < m_outputHeight) { |
Lei Zhang | 1badb85 | 2017-04-20 15:58:56 -0700 | [diff] [blame] | 46 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 47 | } |
| 48 | } else { |
Henrique Nakashima | dc2bb9a | 2018-08-21 19:50:17 +0000 | [diff] [blame] | 49 | if (m_Width > m_outputWidth || m_Height > m_outputHeight) { |
| 50 | int32_t width_factor = static_cast<int32_t>( |
| 51 | floor(static_cast<float>(m_Width) / m_outputWidth)); |
| 52 | int32_t height_factor = static_cast<int32_t>( |
| 53 | floor(static_cast<float>(m_Height) / m_outputHeight)); |
| 54 | width_factor = std::max(width_factor, 1); |
| 55 | height_factor = std::max(height_factor, 1); |
| 56 | |
| 57 | m_outputWidth *= width_factor; |
| 58 | m_outputHeight *= height_factor; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 59 | } |
| 60 | } |
Henrique Nakashima | dc2bb9a | 2018-08-21 19:50:17 +0000 | [diff] [blame] | 61 | m_multiX = |
| 62 | static_cast<int32_t>(ceil(static_cast<float>(m_outputWidth) / tempWidth)); |
| 63 | m_multiY = static_cast<int32_t>( |
| 64 | ceil(static_cast<float>(m_outputHeight) / tempHeight)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 65 | if (m_bFixedSize) { |
Henrique Nakashima | dc2bb9a | 2018-08-21 19:50:17 +0000 | [diff] [blame] | 66 | m_multiX = std::min(m_multiX, m_multiY); |
| 67 | m_multiY = m_multiX; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 68 | } |
Henrique Nakashima | dc2bb9a | 2018-08-21 19:50:17 +0000 | [diff] [blame] | 69 | |
| 70 | m_leftPadding = std::max((m_Width - m_outputWidth) / 2, 0); |
| 71 | m_topPadding = std::max((m_Height - m_outputHeight) / 2, 0); |
| 72 | |
tsepez | a9caab9 | 2016-12-14 05:57:10 -0800 | [diff] [blame] | 73 | m_output = pdfium::MakeUnique<CBC_CommonBitMatrix>(); |
Henrique Nakashima | dc2bb9a | 2018-08-21 19:50:17 +0000 | [diff] [blame] | 74 | m_output->Init(m_inputWidth, m_inputHeight); |
| 75 | |
| 76 | for (int32_t y = 0; y < m_inputHeight; ++y) { |
| 77 | for (int32_t x = 0; x < m_inputWidth; ++x) { |
| 78 | if (code[x + y * m_inputWidth] == 1) |
| 79 | m_output->Set(x, y); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 80 | } |
| 81 | } |
Lei Zhang | 1badb85 | 2017-04-20 15:58:56 -0700 | [diff] [blame] | 82 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 83 | } |
Henrique Nakashima | ce6a1cc | 2018-08-23 19:02:30 +0000 | [diff] [blame] | 84 | |
| 85 | void CBC_TwoDimWriter::RenderDeviceResult(CFX_RenderDevice* device, |
| 86 | const CFX_Matrix* matrix) { |
| 87 | ASSERT(m_output); |
| 88 | |
| 89 | CFX_GraphStateData stateData; |
| 90 | CFX_PathData path; |
Lei Zhang | 04bd91f | 2018-12-04 23:52:04 +0000 | [diff] [blame] | 91 | path.AppendRect(0, 0, m_Width, m_Height); |
Tom Sepez | ea8d8d8 | 2019-11-08 20:31:35 +0000 | [diff] [blame] | 92 | device->DrawPath(&path, matrix, &stateData, kBackgroundColor, |
| 93 | kBackgroundColor, FXFILL_ALTERNATE); |
Henrique Nakashima | ce6a1cc | 2018-08-23 19:02:30 +0000 | [diff] [blame] | 94 | int32_t leftPos = m_leftPadding; |
| 95 | int32_t topPos = m_topPadding; |
| 96 | |
| 97 | CFX_Matrix matri = *matrix; |
| 98 | if (m_Width < m_outputWidth && m_Height < m_outputHeight) { |
| 99 | CFX_Matrix matriScale(static_cast<float>(m_Width) / m_outputWidth, 0.0, 0.0, |
| 100 | static_cast<float>(m_Height) / m_outputHeight, 0.0, |
| 101 | 0.0); |
| 102 | matriScale.Concat(*matrix); |
| 103 | matri = matriScale; |
| 104 | } |
| 105 | |
| 106 | CFX_GraphStateData data; |
| 107 | for (int32_t x = 0; x < m_inputWidth; x++) { |
| 108 | for (int32_t y = 0; y < m_inputHeight; y++) { |
| 109 | if (m_output->Get(x, y)) { |
| 110 | // In the output, each module is shifted by 1 due to the one module |
| 111 | // padding added to create quiet areas. |
| 112 | int start_x_output = x + 1; |
| 113 | int end_x_output = x + 2; |
| 114 | int start_y_output = y + 1; |
| 115 | int end_y_output = y + 2; |
| 116 | |
| 117 | CFX_PathData rect; |
| 118 | rect.AppendRect(leftPos + start_x_output * m_multiX, |
| 119 | topPos + start_y_output * m_multiY, |
| 120 | leftPos + end_x_output * m_multiX, |
| 121 | topPos + end_y_output * m_multiY); |
Tom Sepez | ea8d8d8 | 2019-11-08 20:31:35 +0000 | [diff] [blame] | 122 | device->DrawPath(&rect, &matri, &data, kBarColor, 0, FXFILL_WINDING); |
Henrique Nakashima | ce6a1cc | 2018-08-23 19:02:30 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |