blob: 1fe8018631459f13da376eb42e6d17ae0eaa973c [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
Dan Sinclair1770c022016-03-14 14:14:16 -040023#include "xfa/fxbarcode/BC_Dimension.h"
24#include "xfa/fxbarcode/BC_TwoDimWriter.h"
25#include "xfa/fxbarcode/BC_UtilCodingConvert.h"
26#include "xfa/fxbarcode/BC_Writer.h"
27#include "xfa/fxbarcode/common/BC_CommonBitMatrix.h"
28#include "xfa/fxbarcode/common/BC_CommonByteMatrix.h"
29#include "xfa/fxbarcode/datamatrix/BC_ASCIIEncoder.h"
30#include "xfa/fxbarcode/datamatrix/BC_Base256Encoder.h"
31#include "xfa/fxbarcode/datamatrix/BC_C40Encoder.h"
32#include "xfa/fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.h"
33#include "xfa/fxbarcode/datamatrix/BC_DataMatrixWriter.h"
34#include "xfa/fxbarcode/datamatrix/BC_DefaultPlacement.h"
35#include "xfa/fxbarcode/datamatrix/BC_EdifactEncoder.h"
36#include "xfa/fxbarcode/datamatrix/BC_Encoder.h"
37#include "xfa/fxbarcode/datamatrix/BC_EncoderContext.h"
38#include "xfa/fxbarcode/datamatrix/BC_ErrorCorrection.h"
39#include "xfa/fxbarcode/datamatrix/BC_HighLevelEncoder.h"
40#include "xfa/fxbarcode/datamatrix/BC_SymbolInfo.h"
41#include "xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.h"
42#include "xfa/fxbarcode/datamatrix/BC_TextEncoder.h"
43#include "xfa/fxbarcode/datamatrix/BC_X12Encoder.h"
44
45CBC_DataMatrixWriter::CBC_DataMatrixWriter() {}
46CBC_DataMatrixWriter::~CBC_DataMatrixWriter() {}
tsepezd19e9122016-11-02 15:43:18 -070047bool CBC_DataMatrixWriter::SetErrorCorrectionLevel(int32_t level) {
Dan Sinclair1770c022016-03-14 14:14:16 -040048 m_iCorrectLevel = level;
tsepezd19e9122016-11-02 15:43:18 -070049 return true;
Dan Sinclair1770c022016-03-14 14:14:16 -040050}
51uint8_t* CBC_DataMatrixWriter::Encode(const CFX_WideString& contents,
52 int32_t& outWidth,
53 int32_t& outHeight,
54 int32_t& e) {
55 if (outWidth < 0 || outHeight < 0) {
56 e = BCExceptionHeightAndWidthMustBeAtLeast1;
thestig6dc1d772016-06-09 18:39:33 -070057 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
Dan Sinclair1770c022016-03-14 14:14:16 -040058 }
59 CBC_SymbolShapeHint::SymbolShapeHint shape =
60 CBC_SymbolShapeHint::FORCE_SQUARE;
thestig6dc1d772016-06-09 18:39:33 -070061 CBC_Dimension* minSize = nullptr;
62 CBC_Dimension* maxSize = nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -040063 CFX_WideString ecLevel;
64 CFX_WideString encoded = CBC_HighLevelEncoder::encodeHighLevel(
65 contents, ecLevel, shape, minSize, maxSize, e);
thestig6dc1d772016-06-09 18:39:33 -070066 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
Dan Sinclair1770c022016-03-14 14:14:16 -040067 CBC_SymbolInfo* symbolInfo = CBC_SymbolInfo::lookup(
tsepezd19e9122016-11-02 15:43:18 -070068 encoded.GetLength(), shape, minSize, maxSize, true, e);
thestig6dc1d772016-06-09 18:39:33 -070069 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
Dan Sinclair1770c022016-03-14 14:14:16 -040070 CFX_WideString codewords =
71 CBC_ErrorCorrection::encodeECC200(encoded, symbolInfo, e);
thestig6dc1d772016-06-09 18:39:33 -070072 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
Dan Sinclair1770c022016-03-14 14:14:16 -040073 CBC_DefaultPlacement* placement =
74 new CBC_DefaultPlacement(codewords, symbolInfo->getSymbolDataWidth(e),
75 symbolInfo->getSymbolDataHeight(e));
thestig6dc1d772016-06-09 18:39:33 -070076 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
Dan Sinclair1770c022016-03-14 14:14:16 -040077 placement->place();
78 CBC_CommonByteMatrix* bytematrix = encodeLowLevel(placement, symbolInfo, e);
thestig6dc1d772016-06-09 18:39:33 -070079 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
Dan Sinclair1770c022016-03-14 14:14:16 -040080 outWidth = bytematrix->GetWidth();
81 outHeight = bytematrix->GetHeight();
82 uint8_t* result = FX_Alloc2D(uint8_t, outWidth, outHeight);
83 FXSYS_memcpy(result, bytematrix->GetArray(), outWidth * outHeight);
84 delete bytematrix;
85 delete placement;
86 return result;
87}
88CBC_CommonByteMatrix* CBC_DataMatrixWriter::encodeLowLevel(
89 CBC_DefaultPlacement* placement,
90 CBC_SymbolInfo* symbolInfo,
91 int32_t& e) {
92 int32_t symbolWidth = symbolInfo->getSymbolDataWidth(e);
thestig6dc1d772016-06-09 18:39:33 -070093 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
Dan Sinclair1770c022016-03-14 14:14:16 -040094 int32_t symbolHeight = symbolInfo->getSymbolDataHeight(e);
thestig6dc1d772016-06-09 18:39:33 -070095 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
Dan Sinclair1770c022016-03-14 14:14:16 -040096 CBC_CommonByteMatrix* matrix = new CBC_CommonByteMatrix(
97 symbolInfo->getSymbolWidth(e), symbolInfo->getSymbolHeight(e));
thestig6dc1d772016-06-09 18:39:33 -070098 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
Dan Sinclair1770c022016-03-14 14:14:16 -040099 matrix->Init();
100 int32_t matrixY = 0;
101 for (int32_t y = 0; y < symbolHeight; y++) {
102 int32_t matrixX;
103 if ((y % symbolInfo->m_matrixHeight) == 0) {
104 matrixX = 0;
105 for (int32_t x = 0; x < symbolInfo->getSymbolWidth(e); x++) {
106 matrix->Set(matrixX, matrixY, (x % 2) == 0);
107 matrixX++;
108 }
109 matrixY++;
110 }
111 matrixX = 0;
112 for (int32_t x = 0; x < symbolWidth; x++) {
113 if ((x % symbolInfo->m_matrixWidth) == 0) {
tsepezd19e9122016-11-02 15:43:18 -0700114 matrix->Set(matrixX, matrixY, true);
Dan Sinclair1770c022016-03-14 14:14:16 -0400115 matrixX++;
116 }
117 matrix->Set(matrixX, matrixY, placement->getBit(x, y));
118 matrixX++;
119 if ((x % symbolInfo->m_matrixWidth) == symbolInfo->m_matrixWidth - 1) {
120 matrix->Set(matrixX, matrixY, (y % 2) == 0);
121 matrixX++;
122 }
123 }
124 matrixY++;
125 if ((y % symbolInfo->m_matrixHeight) == symbolInfo->m_matrixHeight - 1) {
126 matrixX = 0;
127 for (int32_t x = 0; x < symbolInfo->getSymbolWidth(e); x++) {
tsepezd19e9122016-11-02 15:43:18 -0700128 matrix->Set(matrixX, matrixY, true);
Dan Sinclair1770c022016-03-14 14:14:16 -0400129 matrixX++;
130 }
131 matrixY++;
132 }
133 }
134 return matrix;
135}