comparison mupdf-source/thirdparty/zxing-cpp/core/src/ReedSolomonEncoder.cpp @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 /*
2 * Copyright 2016 Huy Cuong Nguyen
3 * Copyright 2016 ZXing authors
4 */
5 // SPDX-License-Identifier: Apache-2.0
6
7 #include "ReedSolomonEncoder.h"
8
9 #include "GenericGF.h"
10
11 #include <algorithm>
12 #include <iterator>
13 #include <stdexcept>
14
15 namespace ZXing {
16
17 ReedSolomonEncoder::ReedSolomonEncoder(const GenericGF& field)
18 : _field(&field)
19 {
20 _cachedGenerators.push_back(GenericGFPoly(field, { 1 }));
21 }
22
23 const GenericGFPoly&
24 ReedSolomonEncoder::buildGenerator(int degree)
25 {
26 int cachedGenSize = Size(_cachedGenerators);
27 if (degree >= cachedGenSize) {
28 GenericGFPoly lastGenerator = _cachedGenerators.back();
29 for (int d = cachedGenSize; d <= degree; d++) {
30 lastGenerator.multiply(GenericGFPoly(*_field, { 1, _field->exp(d - 1 + _field->generatorBase()) }));
31 _cachedGenerators.push_back(lastGenerator);
32 }
33 }
34
35 return *std::next(_cachedGenerators.begin(), degree);
36 }
37
38 void
39 ReedSolomonEncoder::encode(std::vector<int>& message, const int numECCodeWords)
40 {
41 if (numECCodeWords == 0 || numECCodeWords >= Size(message))
42 throw std::invalid_argument("Invalid number of error correction code words");
43
44 GenericGFPoly info = GenericGFPoly(*_field, std::vector<int>(message.begin(), message.end() - numECCodeWords));
45 info.multiplyByMonomial(1, numECCodeWords);
46 GenericGFPoly _;
47 info.divide(buildGenerator(numECCodeWords), _);
48 auto& coefficients = info.coefficients();
49 int numZeroCoefficients = numECCodeWords - Size(coefficients);
50 std::fill_n(message.end() - numECCodeWords, numZeroCoefficients, 0);
51 std::copy(coefficients.begin(), coefficients.end(), message.end() - numECCodeWords + numZeroCoefficients);
52 }
53
54 } // ZXing