Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 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 | // 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 Zhang | 1badb85 | 2017-04-20 15:58:56 -0700 | [diff] [blame] | 23 | #include "fxbarcode/datamatrix/BC_DataMatrixWriter.h" |
| 24 | |
| 25 | #include <memory> |
| 26 | |
Dan Sinclair | e778668 | 2017-03-29 15:18:41 -0400 | [diff] [blame] | 27 | #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 Sinclair | e778668 | 2017-03-29 15:18:41 -0400 | [diff] [blame] | 36 | #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 Zhang | 1badb85 | 2017-04-20 15:58:56 -0700 | [diff] [blame] | 46 | #include "third_party/base/ptr_util.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 47 | |
Lei Zhang | 1badb85 | 2017-04-20 15:58:56 -0700 | [diff] [blame] | 48 | namespace { |
| 49 | |
| 50 | std::unique_ptr<CBC_CommonByteMatrix> encodeLowLevel( |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 51 | CBC_DefaultPlacement* placement, |
Lei Zhang | 1badb85 | 2017-04-20 15:58:56 -0700 | [diff] [blame] | 52 | CBC_SymbolInfo* symbolInfo) { |
Lei Zhang | abc83aa | 2017-05-22 18:47:12 -0700 | [diff] [blame^] | 53 | int32_t symbolWidth = symbolInfo->getSymbolDataWidth(); |
| 54 | ASSERT(symbolWidth); |
| 55 | int32_t symbolHeight = symbolInfo->getSymbolDataHeight(); |
| 56 | ASSERT(symbolHeight); |
| 57 | int32_t width = symbolInfo->getSymbolWidth(); |
| 58 | ASSERT(width); |
| 59 | int32_t height = symbolInfo->getSymbolHeight(); |
| 60 | ASSERT(height); |
Lei Zhang | 1badb85 | 2017-04-20 15:58:56 -0700 | [diff] [blame] | 61 | |
| 62 | auto matrix = pdfium::MakeUnique<CBC_CommonByteMatrix>(width, height); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 63 | matrix->Init(); |
| 64 | int32_t matrixY = 0; |
| 65 | for (int32_t y = 0; y < symbolHeight; y++) { |
| 66 | int32_t matrixX; |
Lei Zhang | abc83aa | 2017-05-22 18:47:12 -0700 | [diff] [blame^] | 67 | if ((y % symbolInfo->matrixHeight()) == 0) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 68 | matrixX = 0; |
Lei Zhang | abc83aa | 2017-05-22 18:47:12 -0700 | [diff] [blame^] | 69 | for (int32_t x = 0; x < width; x++) { |
| 70 | matrix->Set(matrixX, matrixY, x % 2 == 0); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 71 | matrixX++; |
| 72 | } |
| 73 | matrixY++; |
| 74 | } |
| 75 | matrixX = 0; |
| 76 | for (int32_t x = 0; x < symbolWidth; x++) { |
Lei Zhang | abc83aa | 2017-05-22 18:47:12 -0700 | [diff] [blame^] | 77 | if (x % symbolInfo->matrixWidth() == 0) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 78 | matrix->Set(matrixX, matrixY, true); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 79 | matrixX++; |
| 80 | } |
| 81 | matrix->Set(matrixX, matrixY, placement->getBit(x, y)); |
| 82 | matrixX++; |
Lei Zhang | abc83aa | 2017-05-22 18:47:12 -0700 | [diff] [blame^] | 83 | if (x % symbolInfo->matrixWidth() == symbolInfo->matrixWidth() - 1) { |
| 84 | matrix->Set(matrixX, matrixY, y % 2 == 0); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 85 | matrixX++; |
| 86 | } |
| 87 | } |
| 88 | matrixY++; |
Lei Zhang | abc83aa | 2017-05-22 18:47:12 -0700 | [diff] [blame^] | 89 | if (y % symbolInfo->matrixHeight() == symbolInfo->matrixHeight() - 1) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 90 | matrixX = 0; |
Lei Zhang | abc83aa | 2017-05-22 18:47:12 -0700 | [diff] [blame^] | 91 | for (int32_t x = 0; x < width; x++) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 92 | matrix->Set(matrixX, matrixY, true); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 93 | matrixX++; |
| 94 | } |
| 95 | matrixY++; |
| 96 | } |
| 97 | } |
| 98 | return matrix; |
| 99 | } |
Lei Zhang | 1badb85 | 2017-04-20 15:58:56 -0700 | [diff] [blame] | 100 | |
| 101 | } // namespace |
| 102 | |
| 103 | CBC_DataMatrixWriter::CBC_DataMatrixWriter() {} |
Lei Zhang | abc83aa | 2017-05-22 18:47:12 -0700 | [diff] [blame^] | 104 | |
Lei Zhang | 1badb85 | 2017-04-20 15:58:56 -0700 | [diff] [blame] | 105 | CBC_DataMatrixWriter::~CBC_DataMatrixWriter() {} |
Lei Zhang | abc83aa | 2017-05-22 18:47:12 -0700 | [diff] [blame^] | 106 | |
Lei Zhang | 1badb85 | 2017-04-20 15:58:56 -0700 | [diff] [blame] | 107 | bool CBC_DataMatrixWriter::SetErrorCorrectionLevel(int32_t level) { |
| 108 | m_iCorrectLevel = level; |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | uint8_t* CBC_DataMatrixWriter::Encode(const CFX_WideString& contents, |
| 113 | int32_t& outWidth, |
| 114 | int32_t& outHeight) { |
| 115 | if (outWidth < 0 || outHeight < 0) |
| 116 | return nullptr; |
| 117 | |
| 118 | CBC_SymbolShapeHint::SymbolShapeHint shape = |
| 119 | CBC_SymbolShapeHint::FORCE_SQUARE; |
Lei Zhang | 1badb85 | 2017-04-20 15:58:56 -0700 | [diff] [blame] | 120 | CFX_WideString ecLevel; |
| 121 | int32_t e = BCExceptionNO; |
Lei Zhang | 60cd033 | 2017-04-27 23:58:03 -0700 | [diff] [blame] | 122 | CFX_WideString encoded = |
| 123 | CBC_HighLevelEncoder::encodeHighLevel(contents, ecLevel, shape, e); |
Lei Zhang | 1badb85 | 2017-04-20 15:58:56 -0700 | [diff] [blame] | 124 | if (e != BCExceptionNO) |
| 125 | return nullptr; |
Lei Zhang | 60cd033 | 2017-04-27 23:58:03 -0700 | [diff] [blame] | 126 | CBC_SymbolInfo* symbolInfo = |
| 127 | CBC_SymbolInfo::lookup(encoded.GetLength(), shape, true, e); |
Lei Zhang | 1badb85 | 2017-04-20 15:58:56 -0700 | [diff] [blame] | 128 | if (e != BCExceptionNO) |
| 129 | return nullptr; |
| 130 | CFX_WideString codewords = |
| 131 | CBC_ErrorCorrection::encodeECC200(encoded, symbolInfo, e); |
| 132 | if (e != BCExceptionNO) |
| 133 | return nullptr; |
| 134 | |
Lei Zhang | abc83aa | 2017-05-22 18:47:12 -0700 | [diff] [blame^] | 135 | int32_t width = symbolInfo->getSymbolDataWidth(); |
| 136 | ASSERT(width); |
| 137 | int32_t height = symbolInfo->getSymbolDataHeight(); |
| 138 | ASSERT(height); |
Lei Zhang | 1badb85 | 2017-04-20 15:58:56 -0700 | [diff] [blame] | 139 | |
| 140 | auto placement = |
| 141 | pdfium::MakeUnique<CBC_DefaultPlacement>(codewords, width, height); |
| 142 | placement->place(); |
| 143 | auto bytematrix = encodeLowLevel(placement.get(), symbolInfo); |
| 144 | if (!bytematrix) |
| 145 | return nullptr; |
| 146 | |
| 147 | outWidth = bytematrix->GetWidth(); |
| 148 | outHeight = bytematrix->GetHeight(); |
| 149 | uint8_t* result = FX_Alloc2D(uint8_t, outWidth, outHeight); |
| 150 | memcpy(result, bytematrix->GetArray(), outWidth * outHeight); |
| 151 | return result; |
| 152 | } |