scantools 1.0.8
Graphics manipulation with a view towards scanned documents
resolution.h
1/*
2 * Copyright © 2017 - 2020 Stefan Kebekus <stefan.kebekus@math.uni-freiburg.de>
3 *
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License as published by the Free Software
6 * Foundation, either version 3 of the License, or (at your option) any later
7 * version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 * details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18
19#ifndef RESOLUTION
20#define RESOLUTION
21
22#include <QMetaType>
23#include <QString>
24#include <QtGlobal>
25
26#include "length.h"
27
28
38
40{
49 friend resolution qMax(const resolution res1, const resolution res2);
50
51 public:
53 enum unit {
57 };
58
60 resolution() : _resolution(0) {
61 }
62
69 resolution(qreal l, unit u) {
70 set(l, u);
71 }
72
78 qreal get(unit u) const;
79
86 void set(qreal l, unit u);
87
92 bool isNonPositive() const {
93 return (_resolution <= 0);
94 }
95
100 bool isZero() const {
101 return qFuzzyIsNull(_resolution);
102 }
103
108 bool isValid() const {
109 qreal ridpi = get(dpi);
110 return (ridpi >= minResDPI) && (ridpi <= maxResDPI);
111 }
112
116 operator QString() const {
117 return QString("%1dpi").arg(get(dpi));
118 }
119
126 bool operator==(const resolution other) const
127 {
128 return _resolution == other._resolution;
129 }
130
137 static const int minResDPI = 10;
138
145 static const int maxResDPI = 10000;
146
147
148 private:
149 // Resolution in 1/100 mm
150 qreal _resolution;
151};
152
153
163
164inline length operator/(qreal numerator, const resolution denominator) {
165 return length(numerator/denominator.get(resolution::dpi), length::in);
166}
167
178
179inline resolution operator/(qreal numerator, const length denominator) {
180 return resolution(numerator/denominator.get(length::in), resolution::dpi);
181}
182
184
185inline resolution qMax(const resolution res1, const resolution res2)
186{
187 return res1._resolution > res2._resolution ? res1 : res2;
188}
189
190
191// Makes "resolution" known to QMetaType
192Q_DECLARE_METATYPE(resolution)
193
194#endif
The length stores a length and converts between units.
Definition length.h:38
@ in
Inch.
Definition length.h:43
qreal get(unit u) const
Returns length in given unit.
The resolution class stores a resolution and converts between units.
Definition resolution.h:40
bool isValid() const
Check if resolution is valid.
Definition resolution.h:108
length operator/(qreal numerator, const resolution denominator)
Divide a scalar ("number of dots") by a resolution to obtain a length.
Definition resolution.h:164
resolution(qreal l, unit u)
Constructs resolution of given value and unit.
Definition resolution.h:69
unit
List of supported units.
Definition resolution.h:53
@ dpcm
Pixel per Centimeter.
Definition resolution.h:54
@ dpi
Pixel per Inch.
Definition resolution.h:55
@ dpm
Pixel per Millimeter.
Definition resolution.h:56
bool operator==(const resolution other) const
Check for equality.
Definition resolution.h:126
bool isNonPositive() const
Check if resolution is zero or less.
Definition resolution.h:92
static const int maxResDPI
Maximal resolution handled by scantools.
Definition resolution.h:145
resolution operator/(qreal numerator, const length denominator)
Divide a scalar by a length to obtain a resolution.
Definition resolution.h:179
qreal get(unit u) const
Get numerical value for resolution.
void set(qreal l, unit u)
Sets resolution in given unit.
bool isZero() const
Fuzzy check if resolution is zero.
Definition resolution.h:100
resolution()
Constructs a zero resolution.
Definition resolution.h:60
friend resolution qMax(const resolution res1, const resolution res2)
Find the maximum of two resolutions.
Definition resolution.h:185
static const int minResDPI
Minimal resolution handled by scantools.
Definition resolution.h:137