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 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 23 | #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 | |
| 45 | CBC_DataMatrixWriter::CBC_DataMatrixWriter() {} |
| 46 | CBC_DataMatrixWriter::~CBC_DataMatrixWriter() {} |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 47 | bool CBC_DataMatrixWriter::SetErrorCorrectionLevel(int32_t level) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 48 | m_iCorrectLevel = level; |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 49 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 50 | } |
| 51 | uint8_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; |
thestig | 6dc1d77 | 2016-06-09 18:39:33 -0700 | [diff] [blame] | 57 | BC_EXCEPTION_CHECK_ReturnValue(e, nullptr); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 58 | } |
| 59 | CBC_SymbolShapeHint::SymbolShapeHint shape = |
| 60 | CBC_SymbolShapeHint::FORCE_SQUARE; |
thestig | 6dc1d77 | 2016-06-09 18:39:33 -0700 | [diff] [blame] | 61 | CBC_Dimension* minSize = nullptr; |
| 62 | CBC_Dimension* maxSize = nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 63 | CFX_WideString ecLevel; |
| 64 | CFX_WideString encoded = CBC_HighLevelEncoder::encodeHighLevel( |
| 65 | contents, ecLevel, shape, minSize, maxSize, e); |
thestig | 6dc1d77 | 2016-06-09 18:39:33 -0700 | [diff] [blame] | 66 | BC_EXCEPTION_CHECK_ReturnValue(e, nullptr); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 67 | CBC_SymbolInfo* symbolInfo = CBC_SymbolInfo::lookup( |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 68 | encoded.GetLength(), shape, minSize, maxSize, true, e); |
thestig | 6dc1d77 | 2016-06-09 18:39:33 -0700 | [diff] [blame] | 69 | BC_EXCEPTION_CHECK_ReturnValue(e, nullptr); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 70 | CFX_WideString codewords = |
| 71 | CBC_ErrorCorrection::encodeECC200(encoded, symbolInfo, e); |
thestig | 6dc1d77 | 2016-06-09 18:39:33 -0700 | [diff] [blame] | 72 | BC_EXCEPTION_CHECK_ReturnValue(e, nullptr); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 73 | CBC_DefaultPlacement* placement = |
| 74 | new CBC_DefaultPlacement(codewords, symbolInfo->getSymbolDataWidth(e), |
| 75 | symbolInfo->getSymbolDataHeight(e)); |
thestig | 6dc1d77 | 2016-06-09 18:39:33 -0700 | [diff] [blame] | 76 | BC_EXCEPTION_CHECK_ReturnValue(e, nullptr); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 77 | placement->place(); |
| 78 | CBC_CommonByteMatrix* bytematrix = encodeLowLevel(placement, symbolInfo, e); |
thestig | 6dc1d77 | 2016-06-09 18:39:33 -0700 | [diff] [blame] | 79 | BC_EXCEPTION_CHECK_ReturnValue(e, nullptr); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 80 | 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 | } |
| 88 | CBC_CommonByteMatrix* CBC_DataMatrixWriter::encodeLowLevel( |
| 89 | CBC_DefaultPlacement* placement, |
| 90 | CBC_SymbolInfo* symbolInfo, |
| 91 | int32_t& e) { |
| 92 | int32_t symbolWidth = symbolInfo->getSymbolDataWidth(e); |
thestig | 6dc1d77 | 2016-06-09 18:39:33 -0700 | [diff] [blame] | 93 | BC_EXCEPTION_CHECK_ReturnValue(e, nullptr); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 94 | int32_t symbolHeight = symbolInfo->getSymbolDataHeight(e); |
thestig | 6dc1d77 | 2016-06-09 18:39:33 -0700 | [diff] [blame] | 95 | BC_EXCEPTION_CHECK_ReturnValue(e, nullptr); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 96 | CBC_CommonByteMatrix* matrix = new CBC_CommonByteMatrix( |
| 97 | symbolInfo->getSymbolWidth(e), symbolInfo->getSymbolHeight(e)); |
thestig | 6dc1d77 | 2016-06-09 18:39:33 -0700 | [diff] [blame] | 98 | BC_EXCEPTION_CHECK_ReturnValue(e, nullptr); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 99 | 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) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 114 | matrix->Set(matrixX, matrixY, true); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 115 | 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++) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 128 | matrix->Set(matrixX, matrixY, true); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 129 | matrixX++; |
| 130 | } |
| 131 | matrixY++; |
| 132 | } |
| 133 | } |
| 134 | return matrix; |
| 135 | } |