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 2007 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 | 8a24b25 | 2017-04-06 14:23:26 -0700 | [diff] [blame^] | 23 | #include "fxbarcode/qrcode/BC_QRCoderVersion.h" |
| 24 | |
| 25 | #include <memory> |
| 26 | #include <vector> |
| 27 | |
Dan Sinclair | e778668 | 2017-03-29 15:18:41 -0400 | [diff] [blame] | 28 | #include "fxbarcode/common/BC_CommonBitMatrix.h" |
| 29 | #include "fxbarcode/qrcode/BC_QRCoderBitVector.h" |
Lei Zhang | 8a24b25 | 2017-04-06 14:23:26 -0700 | [diff] [blame^] | 30 | #include "fxbarcode/qrcode/BC_QRCoderECBlocksData.h" |
Dan Sinclair | e778668 | 2017-03-29 15:18:41 -0400 | [diff] [blame] | 31 | #include "fxbarcode/qrcode/BC_QRCoderErrorCorrectionLevel.h" |
Dan Sinclair | e778668 | 2017-03-29 15:18:41 -0400 | [diff] [blame] | 32 | #include "fxbarcode/utils.h" |
Lei Zhang | 8a24b25 | 2017-04-06 14:23:26 -0700 | [diff] [blame^] | 33 | #include "third_party/base/ptr_util.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 34 | |
Lei Zhang | 8a24b25 | 2017-04-06 14:23:26 -0700 | [diff] [blame^] | 35 | namespace { |
tsepez | 6020e45 | 2016-05-02 13:47:17 -0700 | [diff] [blame] | 36 | |
Lei Zhang | 8a24b25 | 2017-04-06 14:23:26 -0700 | [diff] [blame^] | 37 | std::vector<std::unique_ptr<CBC_QRCoderVersion>>* g_VERSION = nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 38 | |
Lei Zhang | 8a24b25 | 2017-04-06 14:23:26 -0700 | [diff] [blame^] | 39 | } // namespace |
tsepez | 6020e45 | 2016-05-02 13:47:17 -0700 | [diff] [blame] | 40 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 41 | CBC_QRCoderVersion::CBC_QRCoderVersion(int32_t versionNumber, |
Lei Zhang | 8a24b25 | 2017-04-06 14:23:26 -0700 | [diff] [blame^] | 42 | const CBC_QRCoderECBlockData data[4]) |
| 43 | : m_versionNumber(versionNumber) { |
| 44 | m_ecBlocksArray[0] = pdfium::MakeUnique<CBC_QRCoderECBlocks>(data[0]); |
| 45 | m_ecBlocksArray[1] = pdfium::MakeUnique<CBC_QRCoderECBlocks>(data[1]); |
| 46 | m_ecBlocksArray[2] = pdfium::MakeUnique<CBC_QRCoderECBlocks>(data[2]); |
| 47 | m_ecBlocksArray[3] = pdfium::MakeUnique<CBC_QRCoderECBlocks>(data[3]); |
| 48 | m_totalCodeWords = m_ecBlocksArray[0]->GetTotalDataCodeWords(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 49 | } |
tsepez | aef780d | 2016-04-28 14:56:27 -0700 | [diff] [blame] | 50 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 51 | CBC_QRCoderVersion::~CBC_QRCoderVersion() { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 52 | } |
tsepez | aef780d | 2016-04-28 14:56:27 -0700 | [diff] [blame] | 53 | |
Lei Zhang | 8a24b25 | 2017-04-06 14:23:26 -0700 | [diff] [blame^] | 54 | // static |
| 55 | void CBC_QRCoderVersion::Initialize() { |
| 56 | g_VERSION = new std::vector<std::unique_ptr<CBC_QRCoderVersion>>(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 57 | } |
Lei Zhang | 1b22880 | 2017-04-05 18:42:22 -0700 | [diff] [blame] | 58 | |
Lei Zhang | 8a24b25 | 2017-04-06 14:23:26 -0700 | [diff] [blame^] | 59 | // static |
| 60 | void CBC_QRCoderVersion::Finalize() { |
| 61 | delete g_VERSION; |
| 62 | g_VERSION = nullptr; |
| 63 | } |
| 64 | |
| 65 | // static |
| 66 | const CBC_QRCoderVersion* CBC_QRCoderVersion::GetVersionForNumber( |
| 67 | int32_t versionNumber) { |
| 68 | if (g_VERSION->empty()) { |
| 69 | for (int i = 0; i < kMaxVersion; ++i) { |
| 70 | g_VERSION->push_back( |
| 71 | pdfium::MakeUnique<CBC_QRCoderVersion>(i + 1, g_ECBData[i])); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 72 | } |
| 73 | } |
Lei Zhang | 8a24b25 | 2017-04-06 14:23:26 -0700 | [diff] [blame^] | 74 | if (versionNumber < 1 || versionNumber > kMaxVersion) |
Tom Sepez | c8017b2 | 2017-01-31 13:02:10 -0800 | [diff] [blame] | 75 | return nullptr; |
Lei Zhang | 8a24b25 | 2017-04-06 14:23:26 -0700 | [diff] [blame^] | 76 | return (*g_VERSION)[versionNumber - 1].get(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 77 | } |
Lei Zhang | 1b22880 | 2017-04-05 18:42:22 -0700 | [diff] [blame] | 78 | |
Lei Zhang | 8a24b25 | 2017-04-06 14:23:26 -0700 | [diff] [blame^] | 79 | // static |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 80 | void CBC_QRCoderVersion::Destroy() { |
Lei Zhang | 8a24b25 | 2017-04-06 14:23:26 -0700 | [diff] [blame^] | 81 | g_VERSION->clear(); |
| 82 | } |
| 83 | |
| 84 | int32_t CBC_QRCoderVersion::GetVersionNumber() const { |
| 85 | return m_versionNumber; |
| 86 | } |
| 87 | |
| 88 | int32_t CBC_QRCoderVersion::GetTotalCodeWords() const { |
| 89 | return m_totalCodeWords; |
| 90 | } |
| 91 | |
| 92 | int32_t CBC_QRCoderVersion::GetDimensionForVersion() const { |
| 93 | return 17 + 4 * m_versionNumber; |
| 94 | } |
| 95 | |
| 96 | const CBC_QRCoderECBlocks* CBC_QRCoderVersion::GetECBlocksForLevel( |
| 97 | const CBC_QRCoderErrorCorrectionLevel& ecLevel) const { |
| 98 | return m_ecBlocksArray[ecLevel.Ordinal()].get(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 99 | } |