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