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 "fxbarcode/BC_TwoDimWriter.h"
24 #include "fxbarcode/common/BC_CommonByteMatrix.h"
25 #include "fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h"
26 #include "fxbarcode/qrcode/BC_QRCodeWriter.h"
27 #include "fxbarcode/qrcode/BC_QRCoder.h"
28 #include "fxbarcode/qrcode/BC_QRCoderEncoder.h"
29 #include "fxbarcode/qrcode/BC_QRCoderErrorCorrectionLevel.h"
30 #include "fxbarcode/qrcode/BC_QRCoderMode.h"
31 #include "fxbarcode/qrcode/BC_QRCoderVersion.h"
32
CBC_QRCodeWriter()33 CBC_QRCodeWriter::CBC_QRCodeWriter() {
34 m_bFixedSize = true;
35 m_iCorrectLevel = 1;
36 }
37
~CBC_QRCodeWriter()38 CBC_QRCodeWriter::~CBC_QRCodeWriter() {}
39
ReleaseAll()40 void CBC_QRCodeWriter::ReleaseAll() {
41 delete CBC_ReedSolomonGF256::QRCodeField;
42 CBC_ReedSolomonGF256::QRCodeField = nullptr;
43 delete CBC_ReedSolomonGF256::DataMatrixField;
44 CBC_ReedSolomonGF256::DataMatrixField = nullptr;
45 CBC_QRCoderMode::Destroy();
46 CBC_QRCoderErrorCorrectionLevel::Destroy();
47 CBC_QRCoderVersion::Destroy();
48 }
49
SetErrorCorrectionLevel(int32_t level)50 bool CBC_QRCodeWriter::SetErrorCorrectionLevel(int32_t level) {
51 if (level < 0 || level > 3) {
52 return false;
53 }
54 m_iCorrectLevel = level;
55 return true;
56 }
57
Encode(const WideString & contents,int32_t ecLevel,int32_t & outWidth,int32_t & outHeight)58 uint8_t* CBC_QRCodeWriter::Encode(const WideString& contents,
59 int32_t ecLevel,
60 int32_t& outWidth,
61 int32_t& outHeight) {
62 CBC_QRCoderErrorCorrectionLevel* ec = nullptr;
63 switch (ecLevel) {
64 case 0:
65 ec = CBC_QRCoderErrorCorrectionLevel::L;
66 break;
67 case 1:
68 ec = CBC_QRCoderErrorCorrectionLevel::M;
69 break;
70 case 2:
71 ec = CBC_QRCoderErrorCorrectionLevel::Q;
72 break;
73 case 3:
74 ec = CBC_QRCoderErrorCorrectionLevel::H;
75 break;
76 default:
77 return nullptr;
78 }
79 CBC_QRCoder qr;
80 if (!CBC_QRCoderEncoder::Encode(contents, ec, &qr))
81 return nullptr;
82
83 outWidth = qr.GetMatrixWidth();
84 outHeight = qr.GetMatrixWidth();
85 uint8_t* result = FX_Alloc2D(uint8_t, outWidth, outHeight);
86 memcpy(result, qr.GetMatrix()->GetArray(), outWidth * outHeight);
87 return result;
88 }
89