xref: /aosp_15_r20/external/pdfium/fxbarcode/qrcode/BC_QRCodeWriter.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2014 The PDFium Authors
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/qrcode/BC_QRCodeWriter.h"
24 
25 #include <stdint.h>
26 
27 #include <memory>
28 
29 #include "core/fxcrt/data_vector.h"
30 #include "fxbarcode/common/BC_CommonByteMatrix.h"
31 #include "fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h"
32 #include "fxbarcode/qrcode/BC_QRCoder.h"
33 #include "fxbarcode/qrcode/BC_QRCoderEncoder.h"
34 #include "fxbarcode/qrcode/BC_QRCoderErrorCorrectionLevel.h"
35 #include "fxbarcode/qrcode/BC_QRCoderMode.h"
36 #include "fxbarcode/qrcode/BC_QRCoderVersion.h"
37 
CBC_QRCodeWriter()38 CBC_QRCodeWriter::CBC_QRCodeWriter() : CBC_TwoDimWriter(true) {}
39 
40 CBC_QRCodeWriter::~CBC_QRCodeWriter() = default;
41 
SetErrorCorrectionLevel(int32_t level)42 bool CBC_QRCodeWriter::SetErrorCorrectionLevel(int32_t level) {
43   if (level < 0 || level > 3) {
44     return false;
45   }
46   set_error_correction_level(level);
47   return true;
48 }
49 
Encode(WideStringView contents,int32_t ecLevel,int32_t * pOutWidth,int32_t * pOutHeight)50 DataVector<uint8_t> CBC_QRCodeWriter::Encode(WideStringView contents,
51                                              int32_t ecLevel,
52                                              int32_t* pOutWidth,
53                                              int32_t* pOutHeight) {
54   CBC_QRCoderErrorCorrectionLevel* ec = nullptr;
55   switch (ecLevel) {
56     case 0:
57       ec = CBC_QRCoderErrorCorrectionLevel::L;
58       break;
59     case 1:
60       ec = CBC_QRCoderErrorCorrectionLevel::M;
61       break;
62     case 2:
63       ec = CBC_QRCoderErrorCorrectionLevel::Q;
64       break;
65     case 3:
66       ec = CBC_QRCoderErrorCorrectionLevel::H;
67       break;
68     default:
69       return DataVector<uint8_t>();
70   }
71   CBC_QRCoder qr;
72   if (!CBC_QRCoderEncoder::Encode(contents, ec, &qr))
73     return DataVector<uint8_t>();
74 
75   *pOutWidth = qr.GetMatrixWidth();
76   *pOutHeight = qr.GetMatrixWidth();
77   std::unique_ptr<CBC_CommonByteMatrix> matrix = qr.TakeMatrix();
78   return matrix->TakeArray();
79 }
80