K. Moon | 832a694 | 2022-10-31 20:11:31 +0000 | [diff] [blame] | 1 | // Copyright 2014 The PDFium Authors |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 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 | |
Lei Zhang | 834863a | 2018-12-04 22:53:48 +0000 | [diff] [blame] | 23 | #include "fxbarcode/qrcode/BC_QRCodeWriter.h" |
| 24 | |
Lei Zhang | b53f060 | 2022-08-11 19:50:39 +0000 | [diff] [blame] | 25 | #include <stdint.h> |
| 26 | |
Lei Zhang | dd063d0 | 2022-09-12 20:30:17 +0000 | [diff] [blame] | 27 | #include <memory> |
| 28 | |
Lei Zhang | b53f060 | 2022-08-11 19:50:39 +0000 | [diff] [blame] | 29 | #include "core/fxcrt/data_vector.h" |
Dan Sinclair | e778668 | 2017-03-29 15:18:41 -0400 | [diff] [blame] | 30 | #include "fxbarcode/common/BC_CommonByteMatrix.h" |
| 31 | #include "fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h" |
Dan Sinclair | e778668 | 2017-03-29 15:18:41 -0400 | [diff] [blame] | 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" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 37 | |
Lei Zhang | 04bd91f | 2018-12-04 23:52:04 +0000 | [diff] [blame] | 38 | CBC_QRCodeWriter::CBC_QRCodeWriter() : CBC_TwoDimWriter(true) {} |
weili | e76203d | 2016-08-09 13:45:03 -0700 | [diff] [blame] | 39 | |
Lei Zhang | 504deda | 2018-12-04 20:41:05 +0000 | [diff] [blame] | 40 | CBC_QRCodeWriter::~CBC_QRCodeWriter() = default; |
weili | e76203d | 2016-08-09 13:45:03 -0700 | [diff] [blame] | 41 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 42 | bool CBC_QRCodeWriter::SetErrorCorrectionLevel(int32_t level) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 43 | if (level < 0 || level > 3) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 44 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 45 | } |
Lei Zhang | 04bd91f | 2018-12-04 23:52:04 +0000 | [diff] [blame] | 46 | set_error_correction_level(level); |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 47 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 48 | } |
weili | e76203d | 2016-08-09 13:45:03 -0700 | [diff] [blame] | 49 | |
Lei Zhang | b53f060 | 2022-08-11 19:50:39 +0000 | [diff] [blame] | 50 | DataVector<uint8_t> CBC_QRCodeWriter::Encode(WideStringView contents, |
| 51 | int32_t ecLevel, |
| 52 | int32_t* pOutWidth, |
| 53 | int32_t* pOutHeight) { |
thestig | 6dc1d77 | 2016-06-09 18:39:33 -0700 | [diff] [blame] | 54 | CBC_QRCoderErrorCorrectionLevel* ec = nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 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; |
Lei Zhang | 1badb85 | 2017-04-20 15:58:56 -0700 | [diff] [blame] | 68 | default: |
Lei Zhang | dd063d0 | 2022-09-12 20:30:17 +0000 | [diff] [blame] | 69 | return DataVector<uint8_t>(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 70 | } |
| 71 | CBC_QRCoder qr; |
Lei Zhang | 1badb85 | 2017-04-20 15:58:56 -0700 | [diff] [blame] | 72 | if (!CBC_QRCoderEncoder::Encode(contents, ec, &qr)) |
Lei Zhang | dd063d0 | 2022-09-12 20:30:17 +0000 | [diff] [blame] | 73 | return DataVector<uint8_t>(); |
Lei Zhang | 03d5893 | 2017-04-03 16:40:51 -0700 | [diff] [blame] | 74 | |
Lei Zhang | 8d0d982 | 2018-12-05 00:47:22 +0000 | [diff] [blame] | 75 | *pOutWidth = qr.GetMatrixWidth(); |
| 76 | *pOutHeight = qr.GetMatrixWidth(); |
Lei Zhang | dd063d0 | 2022-09-12 20:30:17 +0000 | [diff] [blame] | 77 | std::unique_ptr<CBC_CommonByteMatrix> matrix = qr.TakeMatrix(); |
| 78 | return matrix->TakeArray(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 79 | } |