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
23#include <utility>
24
25#include "fxbarcode/common/BC_CommonByteMatrix.h"
26#include "fxbarcode/qrcode/BC_QRCoder.h"
27#include "fxbarcode/qrcode/BC_QRCoderErrorCorrectionLevel.h"
28#include "fxbarcode/qrcode/BC_QRCoderMode.h"
29#include "fxbarcode/utils.h"
30
31CBC_QRCoder::CBC_QRCoder()
32    : m_mode(nullptr),
33      m_ecLevel(nullptr),
34      m_version(-1),
35      m_matrixWidth(-1),
36      m_maskPattern(-1),
37      m_numTotalBytes(-1),
38      m_numDataBytes(-1),
39      m_numECBytes(-1),
40      m_numRSBlocks(-1) {}
41
42CBC_QRCoder::~CBC_QRCoder() {}
43
44CBC_QRCoderMode* CBC_QRCoder::GetMode() const {
45  return m_mode.Get();
46}
47
48const CBC_QRCoderErrorCorrectionLevel* CBC_QRCoder::GetECLevel() const {
49  return m_ecLevel.Get();
50}
51
52int32_t CBC_QRCoder::GetVersion() const {
53  return m_version;
54}
55
56int32_t CBC_QRCoder::GetMatrixWidth() const {
57  return m_matrixWidth;
58}
59
60int32_t CBC_QRCoder::GetMaskPattern() const {
61  return m_maskPattern;
62}
63
64int32_t CBC_QRCoder::GetNumTotalBytes() const {
65  return m_numTotalBytes;
66}
67
68int32_t CBC_QRCoder::GetNumDataBytes() const {
69  return m_numDataBytes;
70}
71
72int32_t CBC_QRCoder::GetNumECBytes() const {
73  return m_numECBytes;
74}
75
76int32_t CBC_QRCoder::GetNumRSBlocks() const {
77  return m_numRSBlocks;
78}
79
80CBC_CommonByteMatrix* CBC_QRCoder::GetMatrix() const {
81  return m_matrix.get();
82}
83
84int32_t CBC_QRCoder::At(int32_t x, int32_t y, int32_t& e) {
85  int32_t value = m_matrix->Get(x, y);
86  if (!(value == 0 || value == 1)) {
87    e = BCExceptionValueMustBeEither0or1;
88    return 0;
89  }
90  return value;
91}
92
93bool CBC_QRCoder::IsValid() {
94  return m_mode && m_ecLevel && m_version != -1 && m_matrixWidth != -1 &&
95         m_maskPattern != -1 && m_numTotalBytes != -1 && m_numDataBytes != -1 &&
96         m_numECBytes != -1 && m_numRSBlocks != -1 &&
97         IsValidMaskPattern(m_maskPattern) &&
98         m_numTotalBytes == m_numDataBytes + m_numECBytes && m_matrix &&
99         m_matrixWidth == m_matrix->GetWidth() &&
100         m_matrix->GetWidth() == m_matrix->GetHeight();
101}
102
103void CBC_QRCoder::SetMode(CBC_QRCoderMode* value) {
104  m_mode = value;
105}
106
107void CBC_QRCoder::SetECLevel(const CBC_QRCoderErrorCorrectionLevel* ecLevel) {
108  m_ecLevel = ecLevel;
109}
110
111void CBC_QRCoder::SetVersion(int32_t version) {
112  m_version = version;
113}
114
115void CBC_QRCoder::SetMatrixWidth(int32_t width) {
116  m_matrixWidth = width;
117}
118
119void CBC_QRCoder::SetMaskPattern(int32_t pattern) {
120  m_maskPattern = pattern;
121}
122
123void CBC_QRCoder::SetNumDataBytes(int32_t bytes) {
124  m_numDataBytes = bytes;
125}
126
127void CBC_QRCoder::SetNumTotalBytes(int32_t value) {
128  m_numTotalBytes = value;
129}
130
131void CBC_QRCoder::SetNumRSBlocks(int32_t block) {
132  m_numRSBlocks = block;
133}
134
135void CBC_QRCoder::SetNumECBytes(int32_t value) {
136  m_numECBytes = value;
137}
138
139bool CBC_QRCoder::IsValidMaskPattern(int32_t maskPattern) {
140  return maskPattern >= 0 && maskPattern < kNumMaskPatterns;
141}
142
143void CBC_QRCoder::SetMatrix(std::unique_ptr<CBC_CommonByteMatrix> pMatrix) {
144  m_matrix = std::move(pMatrix);
145}
146