blob: 61d2b553446fab72fb700ad13bc71d6912597834 [file] [log] [blame]
Dan Sinclair1770c022016-03-14 14:14:16 -04001// 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 Zhang176c0f82018-12-05 18:29:36 +00007#include "fxbarcode/BC_TwoDimWriter.h"
8
Dan Sinclair1770c022016-03-14 14:14:16 -04009#include <algorithm>
10
dsinclair74a34fc2016-09-29 16:41:42 -070011#include "core/fxge/cfx_graphstatedata.h"
12#include "core/fxge/cfx_pathdata.h"
13#include "core/fxge/cfx_renderdevice.h"
Dan Sinclaire7786682017-03-29 15:18:41 -040014#include "fxbarcode/BC_Writer.h"
15#include "fxbarcode/common/BC_CommonBitMatrix.h"
Dan Sinclair1770c022016-03-14 14:14:16 -040016#include "third_party/base/numerics/safe_math.h"
tsepeza9caab92016-12-14 05:57:10 -080017#include "third_party/base/ptr_util.h"
Dan Sinclair1770c022016-03-14 14:14:16 -040018
Lei Zhang04bd91f2018-12-04 23:52:04 +000019CBC_TwoDimWriter::CBC_TwoDimWriter(bool bFixedSize)
20 : m_bFixedSize(bFixedSize) {}
weilie76203d2016-08-09 13:45:03 -070021
Lei Zhang04bd91f2018-12-04 23:52:04 +000022CBC_TwoDimWriter::~CBC_TwoDimWriter() = default;
weili29b8ad02016-06-14 18:20:04 -070023
Tom Sepez8a118882019-11-20 23:54:33 +000024bool CBC_TwoDimWriter::RenderResult(pdfium::span<const uint8_t> code,
Dan Sinclair1770c022016-03-14 14:14:16 -040025 int32_t codeWidth,
Lei Zhang1badb852017-04-20 15:58:56 -070026 int32_t codeHeight) {
Lei Zhang176c0f82018-12-05 18:29:36 +000027 if (code.empty())
28 return false;
29
Henrique Nakashimadc2bb9a2018-08-21 19:50:17 +000030 m_inputWidth = codeWidth;
31 m_inputHeight = codeHeight;
32 int32_t tempWidth = m_inputWidth + 2;
33 int32_t tempHeight = m_inputHeight + 2;
Dan Sinclair05df0752017-03-14 14:43:42 -040034 float moduleHSize = std::min(m_ModuleWidth, m_ModuleHeight);
Dan Sinclair1770c022016-03-14 14:14:16 -040035 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 Nakashimadc2bb9a2018-08-21 19:50:17 +000041 m_outputWidth = scaledWidth.ValueOrDie();
42 m_outputHeight = scaledHeight.ValueOrDie();
Dan Sinclair1770c022016-03-14 14:14:16 -040043
Dan Sinclair1770c022016-03-14 14:14:16 -040044 if (m_bFixedSize) {
Henrique Nakashimadc2bb9a2018-08-21 19:50:17 +000045 if (m_Width < m_outputWidth || m_Height < m_outputHeight) {
Lei Zhang1badb852017-04-20 15:58:56 -070046 return false;
Dan Sinclair1770c022016-03-14 14:14:16 -040047 }
48 } else {
Henrique Nakashimadc2bb9a2018-08-21 19:50:17 +000049 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 Sinclair1770c022016-03-14 14:14:16 -040059 }
60 }
Henrique Nakashimadc2bb9a2018-08-21 19:50:17 +000061 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 Sinclair1770c022016-03-14 14:14:16 -040065 if (m_bFixedSize) {
Henrique Nakashimadc2bb9a2018-08-21 19:50:17 +000066 m_multiX = std::min(m_multiX, m_multiY);
67 m_multiY = m_multiX;
Dan Sinclair1770c022016-03-14 14:14:16 -040068 }
Henrique Nakashimadc2bb9a2018-08-21 19:50:17 +000069
70 m_leftPadding = std::max((m_Width - m_outputWidth) / 2, 0);
71 m_topPadding = std::max((m_Height - m_outputHeight) / 2, 0);
72
tsepeza9caab92016-12-14 05:57:10 -080073 m_output = pdfium::MakeUnique<CBC_CommonBitMatrix>();
Henrique Nakashimadc2bb9a2018-08-21 19:50:17 +000074 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 Sinclair1770c022016-03-14 14:14:16 -040080 }
81 }
Lei Zhang1badb852017-04-20 15:58:56 -070082 return true;
Dan Sinclair1770c022016-03-14 14:14:16 -040083}
Henrique Nakashimace6a1cc2018-08-23 19:02:30 +000084
85void 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 Zhang04bd91f2018-12-04 23:52:04 +000091 path.AppendRect(0, 0, m_Width, m_Height);
Tom Sepezea8d8d82019-11-08 20:31:35 +000092 device->DrawPath(&path, matrix, &stateData, kBackgroundColor,
93 kBackgroundColor, FXFILL_ALTERNATE);
Henrique Nakashimace6a1cc2018-08-23 19:02:30 +000094 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 Sepezea8d8d82019-11-08 20:31:35 +0000122 device->DrawPath(&rect, &matri, &data, kBarColor, 0, FXFILL_WINDING);
Henrique Nakashimace6a1cc2018-08-23 19:02:30 +0000123 }
124 }
125 }
126}