blob: f45c338a7c663ad1a5250caf6022128646627fca [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_TwoDimWriter.h"
28#include "fxbarcode/BC_UtilCodingConvert.h"
29#include "fxbarcode/BC_Writer.h"
30#include "fxbarcode/common/BC_CommonBitMatrix.h"
31#include "fxbarcode/common/BC_CommonByteMatrix.h"
32#include "fxbarcode/datamatrix/BC_ASCIIEncoder.h"
33#include "fxbarcode/datamatrix/BC_Base256Encoder.h"
34#include "fxbarcode/datamatrix/BC_C40Encoder.h"
35#include "fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.h"
Dan Sinclaire7786682017-03-29 15:18:41 -040036#include "fxbarcode/datamatrix/BC_DefaultPlacement.h"
37#include "fxbarcode/datamatrix/BC_EdifactEncoder.h"
38#include "fxbarcode/datamatrix/BC_Encoder.h"
39#include "fxbarcode/datamatrix/BC_EncoderContext.h"
40#include "fxbarcode/datamatrix/BC_ErrorCorrection.h"
41#include "fxbarcode/datamatrix/BC_HighLevelEncoder.h"
42#include "fxbarcode/datamatrix/BC_SymbolInfo.h"
43#include "fxbarcode/datamatrix/BC_SymbolShapeHint.h"
44#include "fxbarcode/datamatrix/BC_TextEncoder.h"
45#include "fxbarcode/datamatrix/BC_X12Encoder.h"
Lei Zhang1badb852017-04-20 15:58:56 -070046#include "third_party/base/ptr_util.h"
Dan Sinclair1770c022016-03-14 14:14:16 -040047
Lei Zhang1badb852017-04-20 15:58:56 -070048namespace {
49
50std::unique_ptr<CBC_CommonByteMatrix> encodeLowLevel(
Dan Sinclair1770c022016-03-14 14:14:16 -040051 CBC_DefaultPlacement* placement,
Lei Zhang1badb852017-04-20 15:58:56 -070052 CBC_SymbolInfo* symbolInfo) {
53 int32_t e = BCExceptionNO;
Dan Sinclair1770c022016-03-14 14:14:16 -040054 int32_t symbolWidth = symbolInfo->getSymbolDataWidth(e);
Tom Sepezc8017b22017-01-31 13:02:10 -080055 if (e != BCExceptionNO)
56 return nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -040057 int32_t symbolHeight = symbolInfo->getSymbolDataHeight(e);
Tom Sepezc8017b22017-01-31 13:02:10 -080058 if (e != BCExceptionNO)
59 return nullptr;
Lei Zhang1badb852017-04-20 15:58:56 -070060 int32_t width = symbolInfo->getSymbolWidth(e);
Tom Sepezc8017b22017-01-31 13:02:10 -080061 if (e != BCExceptionNO)
62 return nullptr;
Lei Zhang1badb852017-04-20 15:58:56 -070063 int32_t height = symbolInfo->getSymbolHeight(e);
64 if (e != BCExceptionNO)
65 return nullptr;
66
67 auto matrix = pdfium::MakeUnique<CBC_CommonByteMatrix>(width, height);
Dan Sinclair1770c022016-03-14 14:14:16 -040068 matrix->Init();
69 int32_t matrixY = 0;
70 for (int32_t y = 0; y < symbolHeight; y++) {
71 int32_t matrixX;
72 if ((y % symbolInfo->m_matrixHeight) == 0) {
73 matrixX = 0;
74 for (int32_t x = 0; x < symbolInfo->getSymbolWidth(e); x++) {
75 matrix->Set(matrixX, matrixY, (x % 2) == 0);
76 matrixX++;
77 }
78 matrixY++;
79 }
80 matrixX = 0;
81 for (int32_t x = 0; x < symbolWidth; x++) {
82 if ((x % symbolInfo->m_matrixWidth) == 0) {
tsepezd19e9122016-11-02 15:43:18 -070083 matrix->Set(matrixX, matrixY, true);
Dan Sinclair1770c022016-03-14 14:14:16 -040084 matrixX++;
85 }
86 matrix->Set(matrixX, matrixY, placement->getBit(x, y));
87 matrixX++;
88 if ((x % symbolInfo->m_matrixWidth) == symbolInfo->m_matrixWidth - 1) {
89 matrix->Set(matrixX, matrixY, (y % 2) == 0);
90 matrixX++;
91 }
92 }
93 matrixY++;
94 if ((y % symbolInfo->m_matrixHeight) == symbolInfo->m_matrixHeight - 1) {
95 matrixX = 0;
96 for (int32_t x = 0; x < symbolInfo->getSymbolWidth(e); x++) {
tsepezd19e9122016-11-02 15:43:18 -070097 matrix->Set(matrixX, matrixY, true);
Dan Sinclair1770c022016-03-14 14:14:16 -040098 matrixX++;
99 }
100 matrixY++;
101 }
102 }
103 return matrix;
104}
Lei Zhang1badb852017-04-20 15:58:56 -0700105
106} // namespace
107
108CBC_DataMatrixWriter::CBC_DataMatrixWriter() {}
109CBC_DataMatrixWriter::~CBC_DataMatrixWriter() {}
110bool CBC_DataMatrixWriter::SetErrorCorrectionLevel(int32_t level) {
111 m_iCorrectLevel = level;
112 return true;
113}
114
115uint8_t* CBC_DataMatrixWriter::Encode(const CFX_WideString& contents,
116 int32_t& outWidth,
117 int32_t& outHeight) {
118 if (outWidth < 0 || outHeight < 0)
119 return nullptr;
120
121 CBC_SymbolShapeHint::SymbolShapeHint shape =
122 CBC_SymbolShapeHint::FORCE_SQUARE;
Lei Zhang1badb852017-04-20 15:58:56 -0700123 CFX_WideString ecLevel;
124 int32_t e = BCExceptionNO;
Lei Zhang60cd0332017-04-27 23:58:03 -0700125 CFX_WideString encoded =
126 CBC_HighLevelEncoder::encodeHighLevel(contents, ecLevel, shape, e);
Lei Zhang1badb852017-04-20 15:58:56 -0700127 if (e != BCExceptionNO)
128 return nullptr;
Lei Zhang60cd0332017-04-27 23:58:03 -0700129 CBC_SymbolInfo* symbolInfo =
130 CBC_SymbolInfo::lookup(encoded.GetLength(), shape, true, e);
Lei Zhang1badb852017-04-20 15:58:56 -0700131 if (e != BCExceptionNO)
132 return nullptr;
133 CFX_WideString codewords =
134 CBC_ErrorCorrection::encodeECC200(encoded, symbolInfo, e);
135 if (e != BCExceptionNO)
136 return nullptr;
137
138 int32_t width = symbolInfo->getSymbolDataWidth(e);
139 if (e != BCExceptionNO)
140 return nullptr;
141
142 int32_t height = symbolInfo->getSymbolDataHeight(e);
143 if (e != BCExceptionNO)
144 return nullptr;
145
146 auto placement =
147 pdfium::MakeUnique<CBC_DefaultPlacement>(codewords, width, height);
148 placement->place();
149 auto bytematrix = encodeLowLevel(placement.get(), symbolInfo);
150 if (!bytematrix)
151 return nullptr;
152
153 outWidth = bytematrix->GetWidth();
154 outHeight = bytematrix->GetHeight();
155 uint8_t* result = FX_Alloc2D(uint8_t, outWidth, outHeight);
156 memcpy(result, bytematrix->GetArray(), outWidth * outHeight);
157 return result;
158}