blob: 98af282f8fec2a97d43b53a682cf087d336ab669 [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"
Dan Sinclaire7786682017-03-29 15:18:41 -040043#include "fxbarcode/datamatrix/BC_TextEncoder.h"
44#include "fxbarcode/datamatrix/BC_X12Encoder.h"
Lei Zhang1badb852017-04-20 15:58:56 -070045#include "third_party/base/ptr_util.h"
Dan Sinclair1770c022016-03-14 14:14:16 -040046
Lei Zhang1badb852017-04-20 15:58:56 -070047namespace {
48
49std::unique_ptr<CBC_CommonByteMatrix> encodeLowLevel(
Dan Sinclair1770c022016-03-14 14:14:16 -040050 CBC_DefaultPlacement* placement,
Lei Zhang1badb852017-04-20 15:58:56 -070051 CBC_SymbolInfo* symbolInfo) {
Lei Zhangabc83aa2017-05-22 18:47:12 -070052 int32_t symbolWidth = symbolInfo->getSymbolDataWidth();
53 ASSERT(symbolWidth);
54 int32_t symbolHeight = symbolInfo->getSymbolDataHeight();
55 ASSERT(symbolHeight);
56 int32_t width = symbolInfo->getSymbolWidth();
57 ASSERT(width);
58 int32_t height = symbolInfo->getSymbolHeight();
59 ASSERT(height);
Lei Zhang1badb852017-04-20 15:58:56 -070060
61 auto matrix = pdfium::MakeUnique<CBC_CommonByteMatrix>(width, height);
Dan Sinclair1770c022016-03-14 14:14:16 -040062 matrix->Init();
63 int32_t matrixY = 0;
64 for (int32_t y = 0; y < symbolHeight; y++) {
65 int32_t matrixX;
Lei Zhangabc83aa2017-05-22 18:47:12 -070066 if ((y % symbolInfo->matrixHeight()) == 0) {
Dan Sinclair1770c022016-03-14 14:14:16 -040067 matrixX = 0;
Lei Zhangabc83aa2017-05-22 18:47:12 -070068 for (int32_t x = 0; x < width; x++) {
69 matrix->Set(matrixX, matrixY, x % 2 == 0);
Dan Sinclair1770c022016-03-14 14:14:16 -040070 matrixX++;
71 }
72 matrixY++;
73 }
74 matrixX = 0;
75 for (int32_t x = 0; x < symbolWidth; x++) {
Lei Zhangabc83aa2017-05-22 18:47:12 -070076 if (x % symbolInfo->matrixWidth() == 0) {
tsepezd19e9122016-11-02 15:43:18 -070077 matrix->Set(matrixX, matrixY, true);
Dan Sinclair1770c022016-03-14 14:14:16 -040078 matrixX++;
79 }
80 matrix->Set(matrixX, matrixY, placement->getBit(x, y));
81 matrixX++;
Lei Zhangabc83aa2017-05-22 18:47:12 -070082 if (x % symbolInfo->matrixWidth() == symbolInfo->matrixWidth() - 1) {
83 matrix->Set(matrixX, matrixY, y % 2 == 0);
Dan Sinclair1770c022016-03-14 14:14:16 -040084 matrixX++;
85 }
86 }
87 matrixY++;
Lei Zhangabc83aa2017-05-22 18:47:12 -070088 if (y % symbolInfo->matrixHeight() == symbolInfo->matrixHeight() - 1) {
Dan Sinclair1770c022016-03-14 14:14:16 -040089 matrixX = 0;
Lei Zhangabc83aa2017-05-22 18:47:12 -070090 for (int32_t x = 0; x < width; x++) {
tsepezd19e9122016-11-02 15:43:18 -070091 matrix->Set(matrixX, matrixY, true);
Dan Sinclair1770c022016-03-14 14:14:16 -040092 matrixX++;
93 }
94 matrixY++;
95 }
96 }
97 return matrix;
98}
Lei Zhang1badb852017-04-20 15:58:56 -070099
100} // namespace
101
102CBC_DataMatrixWriter::CBC_DataMatrixWriter() {}
Lei Zhangabc83aa2017-05-22 18:47:12 -0700103
Lei Zhang1badb852017-04-20 15:58:56 -0700104CBC_DataMatrixWriter::~CBC_DataMatrixWriter() {}
Lei Zhangabc83aa2017-05-22 18:47:12 -0700105
Lei Zhang1badb852017-04-20 15:58:56 -0700106bool CBC_DataMatrixWriter::SetErrorCorrectionLevel(int32_t level) {
107 m_iCorrectLevel = level;
108 return true;
109}
110
111uint8_t* CBC_DataMatrixWriter::Encode(const CFX_WideString& contents,
112 int32_t& outWidth,
113 int32_t& outHeight) {
114 if (outWidth < 0 || outHeight < 0)
115 return nullptr;
116
Lei Zhang1badb852017-04-20 15:58:56 -0700117 CFX_WideString ecLevel;
118 int32_t e = BCExceptionNO;
Lei Zhang60cd0332017-04-27 23:58:03 -0700119 CFX_WideString encoded =
Lei Zhang1bbcb352017-05-25 18:43:44 -0700120 CBC_HighLevelEncoder::encodeHighLevel(contents, ecLevel, false, e);
Lei Zhang1badb852017-04-20 15:58:56 -0700121 if (e != BCExceptionNO)
122 return nullptr;
Lei Zhang60cd0332017-04-27 23:58:03 -0700123 CBC_SymbolInfo* symbolInfo =
Lei Zhang1bbcb352017-05-25 18:43:44 -0700124 CBC_SymbolInfo::lookup(encoded.GetLength(), false, e);
Lei Zhang1badb852017-04-20 15:58:56 -0700125 if (e != BCExceptionNO)
126 return nullptr;
127 CFX_WideString codewords =
128 CBC_ErrorCorrection::encodeECC200(encoded, symbolInfo, e);
129 if (e != BCExceptionNO)
130 return nullptr;
131
Lei Zhangabc83aa2017-05-22 18:47:12 -0700132 int32_t width = symbolInfo->getSymbolDataWidth();
133 ASSERT(width);
134 int32_t height = symbolInfo->getSymbolDataHeight();
135 ASSERT(height);
Lei Zhang1badb852017-04-20 15:58:56 -0700136
137 auto placement =
138 pdfium::MakeUnique<CBC_DefaultPlacement>(codewords, width, height);
139 placement->place();
140 auto bytematrix = encodeLowLevel(placement.get(), symbolInfo);
141 if (!bytematrix)
142 return nullptr;
143
144 outWidth = bytematrix->GetWidth();
145 outHeight = bytematrix->GetHeight();
146 uint8_t* result = FX_Alloc2D(uint8_t, outWidth, outHeight);
147 memcpy(result, bytematrix->GetArray(), outWidth * outHeight);
148 return result;
149}