comparison mupdf-source/thirdparty/zxing-cpp/core/src/BitMatrixIO.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 2017 Huy Cuong Nguyen
3 * Copyright 2017 Axel Waggershauser
4 */
5 // SPDX-License-Identifier: Apache-2.0
6
7 #include "BitMatrixIO.h"
8
9 #include <array>
10 #include <fstream>
11 #include <sstream>
12
13 namespace ZXing {
14
15 std::string ToString(const BitMatrix& matrix, char one, char zero, bool addSpace, bool printAsCString)
16 {
17 std::string result;
18 result.reserve((addSpace ? 2 : 1) * (matrix.width() * matrix.height()) + matrix.height());
19 for (int y = 0; y < matrix.height(); ++y) {
20 if (printAsCString)
21 result += '"';
22 for (auto bit : matrix.row(y)) {
23 result += bit ? one : zero;
24 if (addSpace)
25 result += ' ';
26 }
27 if (printAsCString)
28 result += "\\n\"";
29 result += '\n';
30 }
31 return result;
32 }
33
34 std::string ToString(const BitMatrix& matrix, bool inverted)
35 {
36 constexpr auto map = std::array{" ", "▀", "▄", "█"};
37 std::string res;
38
39 for (int y = 0; y < matrix.height(); y += 2) {
40 for (int x = 0; x < matrix.width(); ++x) {
41 int tp = matrix.get(x, y) ^ inverted;
42 int bt = (matrix.height() == 1 && tp) || (y + 1 < matrix.height() && (matrix.get(x, y + 1) ^ inverted));
43 res += map[tp | (bt << 1)];
44 }
45 res.push_back('\n');
46 }
47
48 return res;
49 }
50
51 std::string ToSVG(const BitMatrix& matrix)
52 {
53 // see https://stackoverflow.com/questions/10789059/create-qr-code-in-vector-image/60638350#60638350
54
55 const int width = matrix.width();
56 const int height = matrix.height();
57 std::ostringstream out;
58
59 out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
60 << "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 " << width << " " << height
61 << "\" stroke=\"none\">\n"
62 << "<path d=\"";
63
64 for (int y = 0; y < height; ++y)
65 for (int x = 0; x < width; ++x)
66 if (matrix.get(x, y))
67 out << "M" << x << "," << y << "h1v1h-1z";
68
69 out << "\"/>\n</svg>";
70
71 return out.str();
72 }
73
74 BitMatrix ParseBitMatrix(const std::string& str, char one, bool expectSpace)
75 {
76 auto lineLength = str.find('\n');
77 if (lineLength == std::string::npos)
78 return {};
79
80 int strStride = expectSpace ? 2 : 1;
81 int height = narrow_cast<int>(str.length() / (lineLength + 1));
82 int width = narrow_cast<int>(lineLength / strStride);
83 BitMatrix mat(width, height);
84 for (int y = 0; y < height; ++y) {
85 size_t offset = y * (lineLength + 1);
86 for (int x = 0; x < width; ++x, offset += strStride) {
87 if (str[offset] == one)
88 mat.set(x, y);
89 }
90 }
91 return mat;
92 }
93
94 void SaveAsPBM(const BitMatrix& matrix, const std::string filename, int quietZone)
95 {
96 auto out = ToMatrix<uint8_t>(Inflate(matrix.copy(), 0, 0, quietZone));
97 std::ofstream file(filename);
98 file << "P5\n" << out.width() << ' ' << out.height() << "\n255\n";
99 file.write(reinterpret_cast<const char*>(out.data()), out.size());
100 }
101
102 } // ZXing