blob: 54b7312abc8b34984a8828d000590f1ec06ff466 [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// Original code is licensed as follows:
7/*
8 * Copyright 2008 ZXing authors
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
Lei Zhang1badb852017-04-20 15:58:56 -070023#include "fxbarcode/datamatrix/BC_DataMatrixWriter.h"
24
25#include <memory>
26
Dan Sinclaire7786682017-03-29 15:18:41 -040027#include "fxbarcode/BC_Dimension.h"
28#include "fxbarcode/BC_TwoDimWriter.h"
29#include "fxbarcode/BC_UtilCodingConvert.h"
30#include "fxbarcode/BC_Writer.h"
31#include "fxbarcode/common/BC_CommonBitMatrix.h"
32#include "fxbarcode/common/BC_CommonByteMatrix.h"
33#include "fxbarcode/datamatrix/BC_ASCIIEncoder.h"
34#include "fxbarcode/datamatrix/BC_Base256Encoder.h"
35#include "fxbarcode/datamatrix/BC_C40Encoder.h"
36#include "fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.h"
Dan Sinclaire7786682017-03-29 15:18:41 -040037#include "fxbarcode/datamatrix/BC_DefaultPlacement.h"
38#include "fxbarcode/datamatrix/BC_EdifactEncoder.h"
39#include "fxbarcode/datamatrix/BC_Encoder.h"
40#include "fxbarcode/datamatrix/BC_EncoderContext.h"
41#include "fxbarcode/datamatrix/BC_ErrorCorrection.h"
42#include "fxbarcode/datamatrix/BC_HighLevelEncoder.h"
43#include "fxbarcode/datamatrix/BC_SymbolInfo.h"
44#include "fxbarcode/datamatrix/BC_SymbolShapeHint.h"
45#include "fxbarcode/datamatrix/BC_TextEncoder.h"
46#include "fxbarcode/datamatrix/BC_X12Encoder.h"
Lei Zhang1badb852017-04-20 15:58:56 -070047#include "third_party/base/ptr_util.h"
Dan Sinclair1770c022016-03-14 14:14:16 -040048
Lei Zhang1badb852017-04-20 15:58:56 -070049namespace {
50
51std::unique_ptr<CBC_CommonByteMatrix> encodeLowLevel(
Dan Sinclair1770c022016-03-14 14:14:16 -040052 CBC_DefaultPlacement* placement,
Lei Zhang1badb852017-04-20 15:58:56 -070053 CBC_SymbolInfo* symbolInfo) {
54 int32_t e = BCExceptionNO;
Dan Sinclair1770c022016-03-14 14:14:16 -040055 int32_t symbolWidth = symbolInfo->getSymbolDataWidth(e);
Tom Sepezc8017b22017-01-31 13:02:10 -080056 if (e != BCExceptionNO)
57 return nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -040058 int32_t symbolHeight = symbolInfo->getSymbolDataHeight(e);
Tom Sepezc8017b22017-01-31 13:02:10 -080059 if (e != BCExceptionNO)
60 return nullptr;
Lei Zhang1badb852017-04-20 15:58:56 -070061 int32_t width = symbolInfo->getSymbolWidth(e);
Tom Sepezc8017b22017-01-31 13:02:10 -080062 if (e != BCExceptionNO)
63 return nullptr;
Lei Zhang1badb852017-04-20 15:58:56 -070064 int32_t height = symbolInfo->getSymbolHeight(e);
65 if (e != BCExceptionNO)
66 return nullptr;
67
68 auto matrix = pdfium::MakeUnique<CBC_CommonByteMatrix>(width, height);
Dan Sinclair1770c022016-03-14 14:14:16 -040069 matrix->Init();
70 int32_t matrixY = 0;
71 for (int32_t y = 0; y < symbolHeight; y++) {
72 int32_t matrixX;
73 if ((y % symbolInfo->m_matrixHeight) == 0) {
74 matrixX = 0;
75 for (int32_t x = 0; x < symbolInfo->getSymbolWidth(e); x++) {
76 matrix->Set(matrixX, matrixY, (x % 2) == 0);
77 matrixX++;
78 }
79 matrixY++;
80 }
81 matrixX = 0;
82 for (int32_t x = 0; x < symbolWidth; x++) {
83 if ((x % symbolInfo->m_matrixWidth) == 0) {
tsepezd19e9122016-11-02 15:43:18 -070084 matrix->Set(matrixX, matrixY, true);
Dan Sinclair1770c022016-03-14 14:14:16 -040085 matrixX++;
86 }
87 matrix->Set(matrixX, matrixY, placement->getBit(x, y));
88 matrixX++;
89 if ((x % symbolInfo->m_matrixWidth) == symbolInfo->m_matrixWidth - 1) {
90 matrix->Set(matrixX, matrixY, (y % 2) == 0);
91 matrixX++;
92 }
93 }
94 matrixY++;
95 if ((y % symbolInfo->m_matrixHeight) == symbolInfo->m_matrixHeight - 1) {
96 matrixX = 0;
97 for (int32_t x = 0; x < symbolInfo->getSymbolWidth(e); x++) {
tsepezd19e9122016-11-02 15:43:18 -070098 matrix->Set(matrixX, matrixY, true);
Dan Sinclair1770c022016-03-14 14:14:16 -040099 matrixX++;
100 }
101 matrixY++;
102 }
103 }
104 return matrix;
105}
Lei Zhang1badb852017-04-20 15:58:56 -0700106
107} // namespace
108
109CBC_DataMatrixWriter::CBC_DataMatrixWriter() {}
110CBC_DataMatrixWriter::~CBC_DataMatrixWriter() {}
111bool CBC_DataMatrixWriter::SetErrorCorrectionLevel(int32_t level) {
112 m_iCorrectLevel = level;
113 return true;
114}
115
116uint8_t* CBC_DataMatrixWriter::Encode(const CFX_WideString& contents,
117 int32_t& outWidth,
118 int32_t& outHeight) {
119 if (outWidth < 0 || outHeight < 0)
120 return nullptr;
121
122 CBC_SymbolShapeHint::SymbolShapeHint shape =
123 CBC_SymbolShapeHint::FORCE_SQUARE;
124 CBC_Dimension* minSize = nullptr;
125 CBC_Dimension* maxSize = nullptr;
126 CFX_WideString ecLevel;
127 int32_t e = BCExceptionNO;
128 CFX_WideString encoded = CBC_HighLevelEncoder::encodeHighLevel(
129 contents, ecLevel, shape, minSize, maxSize, e);
130 if (e != BCExceptionNO)
131 return nullptr;
132 CBC_SymbolInfo* symbolInfo = CBC_SymbolInfo::lookup(
133 encoded.GetLength(), shape, minSize, maxSize, true, e);
134 if (e != BCExceptionNO)
135 return nullptr;
136 CFX_WideString codewords =
137 CBC_ErrorCorrection::encodeECC200(encoded, symbolInfo, e);
138 if (e != BCExceptionNO)
139 return nullptr;
140
141 int32_t width = symbolInfo->getSymbolDataWidth(e);
142 if (e != BCExceptionNO)
143 return nullptr;
144
145 int32_t height = symbolInfo->getSymbolDataHeight(e);
146 if (e != BCExceptionNO)
147 return nullptr;
148
149 auto placement =
150 pdfium::MakeUnique<CBC_DefaultPlacement>(codewords, width, height);
151 placement->place();
152 auto bytematrix = encodeLowLevel(placement.get(), symbolInfo);
153 if (!bytematrix)
154 return nullptr;
155
156 outWidth = bytematrix->GetWidth();
157 outHeight = bytematrix->GetHeight();
158 uint8_t* result = FX_Alloc2D(uint8_t, outWidth, outHeight);
159 memcpy(result, bytematrix->GetArray(), outWidth * outHeight);
160 return result;
161}