comparison mupdf-source/thirdparty/zxing-cpp/core/src/LogMatrix.h @ 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 2020 Axel Waggershauser
3 */
4 // SPDX-License-Identifier: Apache-2.0
5
6 #pragma once
7
8 #include "BitMatrix.h"
9 #include "Matrix.h"
10 #include "Point.h"
11
12 #include <cassert>
13 #include <cstdint>
14 #include <cstdio>
15 #include <string>
16
17 namespace ZXing {
18
19 #ifdef PRINT_DEBUG
20
21 class LogMatrix
22 {
23 using LogBuffer = Matrix<uint8_t>;
24 LogBuffer _log;
25 const BitMatrix* _image = nullptr;
26 int _scale = 1;
27
28 public:
29 void init(const BitMatrix* image, int scale = 1)
30 {
31 _image = image;
32 _scale = scale;
33 _log = LogBuffer(_image->width() * _scale, _image->height() * _scale);
34 }
35
36 void write(const char* fn)
37 {
38 assert(_image);
39 FILE* f = fopen(fn, "wb");
40
41 // Write PPM header, P5 == grey, P6 == rgb
42 fprintf(f, "P6\n%d %d\n255\n", _log.width(), _log.height());
43
44 // Write pixels
45 for (int y = 0; y < _log.height(); ++y)
46 for (int x = 0; x < _log.width(); ++x) {
47 unsigned char pix[3];
48 unsigned char &r = pix[0], &g = pix[1], &b = pix[2];
49 r = g = b = _image->get(x / _scale, y / _scale) ? 0 : 255;
50 if (_scale > 1 && x % _scale == _scale / 2 && y % _scale == _scale / 2)
51 r = g = b = r ? 230 : 50;
52 switch (_log.get(x, y)) {
53 case 1: r = g = b = _scale > 1 ? 128 : (r ? 230 : 50); break;
54 case 2: r = b = 50, g = 220; break;
55 case 3: g = r = 100, b = 250; break;
56 case 4: g = b = 100, r = 250; break;
57 }
58 fwrite(&pix, 3, 1, f);
59 }
60 fclose(f);
61 }
62
63 template <typename T>
64 void operator()(const PointT<T>& p, int color = 1)
65 {
66 if (_image && _image->isIn(p))
67 _log.set(static_cast<int>(p.x * _scale), static_cast<int>(p.y * _scale), color);
68 }
69
70 void operator()(const PointT<int>& p, int color)
71 {
72 operator()(centered(p), color);
73 }
74
75 template <typename T>
76 void operator()(const std::vector<PointT<T>>& points, int color = 2)
77 {
78 for (auto p : points)
79 operator()(p, color);
80 }
81 };
82
83 extern LogMatrix log;
84
85 class LogMatrixWriter
86 {
87 LogMatrix &log;
88 std::string fn;
89
90 public:
91 LogMatrixWriter(LogMatrix& log, const BitMatrix& image, int scale, std::string fn) : log(log), fn(fn)
92 {
93 log.init(&image, scale);
94 }
95 ~LogMatrixWriter() { log.write(fn.c_str()); }
96 };
97
98 #else
99
100 template<typename T> void log(PointT<T>, int = 0) {}
101
102 #endif
103
104 } // namespace ZXing