blob: 40b551975086aa024d99b3a6b48286bdfaab75fa [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
7#include <algorithm>
8
thestig25fa42f2016-05-25 21:39:46 -07009#include "core/fxge/include/fx_ge.h"
Dan Sinclair1770c022016-03-14 14:14:16 -040010#include "third_party/base/numerics/safe_math.h"
11#include "xfa/fxbarcode/BC_TwoDimWriter.h"
12#include "xfa/fxbarcode/BC_Writer.h"
13#include "xfa/fxbarcode/common/BC_CommonBitMatrix.h"
14
15CBC_TwoDimWriter::CBC_TwoDimWriter() {
16 m_iCorrectLevel = 1;
17 m_bFixedSize = TRUE;
thestig6dc1d772016-06-09 18:39:33 -070018 m_output = nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -040019}
20CBC_TwoDimWriter::~CBC_TwoDimWriter() {
21 delete m_output;
22}
23void CBC_TwoDimWriter::RenderDeviceResult(CFX_RenderDevice* device,
24 const CFX_Matrix* matrix) {
25 CFX_GraphStateData stateData;
26 CFX_PathData path;
27 path.AppendRect(0, 0, (FX_FLOAT)m_Width, (FX_FLOAT)m_Height);
28 device->DrawPath(&path, matrix, &stateData, m_backgroundColor,
29 m_backgroundColor, FXFILL_ALTERNATE);
30 int32_t leftPos = 0;
31 int32_t topPos = 0;
32 if (m_bFixedSize) {
33 leftPos = (m_Width - m_output->GetWidth()) / 2;
34 topPos = (m_Height - m_output->GetHeight()) / 2;
35 }
36 CFX_Matrix matri = *matrix;
37 if (m_Width < m_output->GetWidth() && m_Height < m_output->GetHeight()) {
38 CFX_Matrix matriScale(
39 (FX_FLOAT)m_Width / (FX_FLOAT)m_output->GetWidth(), 0.0, 0.0,
40 (FX_FLOAT)m_Height / (FX_FLOAT)m_output->GetHeight(), 0.0, 0.0);
41 matriScale.Concat(*matrix);
42 matri = matriScale;
43 }
44 for (int32_t x = 0; x < m_output->GetWidth(); x++) {
45 for (int32_t y = 0; y < m_output->GetHeight(); y++) {
46 CFX_PathData rect;
47 rect.AppendRect((FX_FLOAT)leftPos + x, (FX_FLOAT)topPos + y,
48 (FX_FLOAT)(leftPos + x + 1), (FX_FLOAT)(topPos + y + 1));
Dan Sinclair1770c022016-03-14 14:14:16 -040049 if (m_output->Get(x, y)) {
weilidb444d22016-06-02 15:48:15 -070050 CFX_GraphStateData data;
51 device->DrawPath(&rect, &matri, &data, m_barColor, 0, FXFILL_WINDING);
Dan Sinclair1770c022016-03-14 14:14:16 -040052 }
53 }
54 }
55}
thestig495bda12016-04-28 17:29:19 -070056
weili29b8ad02016-06-14 18:20:04 -070057int32_t CBC_TwoDimWriter::GetErrorCorrectionLevel() {
58 return m_iCorrectLevel;
59}
60
Dan Sinclair1770c022016-03-14 14:14:16 -040061void CBC_TwoDimWriter::RenderBitmapResult(CFX_DIBitmap*& pOutBitmap,
62 int32_t& e) {
63 if (m_bFixedSize) {
64 pOutBitmap = CreateDIBitmap(m_Width, m_Height);
65 } else {
66 pOutBitmap = CreateDIBitmap(m_output->GetWidth(), m_output->GetHeight());
67 }
68 if (!pOutBitmap) {
69 e = BCExceptionFailToCreateBitmap;
70 return;
71 }
72 pOutBitmap->Clear(m_backgroundColor);
73 int32_t leftPos = 0;
74 int32_t topPos = 0;
75 if (m_bFixedSize) {
76 leftPos = (m_Width - m_output->GetWidth()) / 2;
77 topPos = (m_Height - m_output->GetHeight()) / 2;
78 }
79 for (int32_t x = 0; x < m_output->GetWidth(); x++) {
80 for (int32_t y = 0; y < m_output->GetHeight(); y++) {
81 if (m_output->Get(x, y)) {
82 pOutBitmap->SetPixel(leftPos + x, topPos + y, m_barColor);
83 }
84 }
85 }
86 if (!m_bFixedSize) {
87 CFX_DIBitmap* pStretchBitmap = pOutBitmap->StretchTo(m_Width, m_Height);
thestig495bda12016-04-28 17:29:19 -070088 delete pOutBitmap;
Dan Sinclair1770c022016-03-14 14:14:16 -040089 pOutBitmap = pStretchBitmap;
90 }
91}
thestig495bda12016-04-28 17:29:19 -070092
Dan Sinclair1770c022016-03-14 14:14:16 -040093void CBC_TwoDimWriter::RenderResult(uint8_t* code,
94 int32_t codeWidth,
95 int32_t codeHeight,
96 int32_t& e) {
97 int32_t inputWidth = codeWidth;
98 int32_t inputHeight = codeHeight;
99 int32_t tempWidth = inputWidth + 2;
100 int32_t tempHeight = inputHeight + 2;
101 FX_FLOAT moduleHSize = std::min(m_ModuleWidth, m_ModuleHeight);
102 moduleHSize = std::min(moduleHSize, 8.0f);
103 moduleHSize = std::max(moduleHSize, 1.0f);
104 pdfium::base::CheckedNumeric<int32_t> scaledWidth = tempWidth;
105 pdfium::base::CheckedNumeric<int32_t> scaledHeight = tempHeight;
106 scaledWidth *= moduleHSize;
107 scaledHeight *= moduleHSize;
108
109 int32_t outputWidth = scaledWidth.ValueOrDie();
110 int32_t outputHeight = scaledHeight.ValueOrDie();
111 if (m_bFixedSize) {
112 if (m_Width < outputWidth || m_Height < outputHeight) {
113 e = BCExceptionBitmapSizeError;
114 return;
115 }
116 } else {
117 if (m_Width > outputWidth || m_Height > outputHeight) {
118 outputWidth = (int32_t)(outputWidth *
119 ceil((FX_FLOAT)m_Width / (FX_FLOAT)outputWidth));
120 outputHeight = (int32_t)(
121 outputHeight * ceil((FX_FLOAT)m_Height / (FX_FLOAT)outputHeight));
122 }
123 }
124 int32_t multiX = (int32_t)ceil((FX_FLOAT)outputWidth / (FX_FLOAT)tempWidth);
125 int32_t multiY = (int32_t)ceil((FX_FLOAT)outputHeight / (FX_FLOAT)tempHeight);
126 if (m_bFixedSize) {
127 multiX = std::min(multiX, multiY);
128 multiY = multiX;
129 }
130 int32_t leftPadding = (outputWidth - (inputWidth * multiX)) / 2;
131 int32_t topPadding = (outputHeight - (inputHeight * multiY)) / 2;
132 if (leftPadding < 0) {
133 leftPadding = 0;
134 }
135 if (topPadding < 0) {
136 topPadding = 0;
137 }
138 m_output = new CBC_CommonBitMatrix;
139 m_output->Init(outputWidth, outputHeight);
140 for (int32_t inputY = 0, outputY = topPadding;
141 (inputY < inputHeight) && (outputY < outputHeight - multiY);
142 inputY++, outputY += multiY) {
143 for (int32_t inputX = 0, outputX = leftPadding;
144 (inputX < inputWidth) && (outputX < outputWidth - multiX);
145 inputX++, outputX += multiX) {
146 if (code[inputX + inputY * inputWidth] == 1) {
147 m_output->SetRegion(outputX, outputY, multiX, multiY, e);
148 BC_EXCEPTION_CHECK_ReturnVoid(e);
149 }
150 }
151 }
152}