Scaling.h
1/* Copyright (C) 2015 Marc Boris Duerner
2
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License as published by the Free Software Foundation; either
6 version 2.1 of the License, or (at your option) any later version.
7
8 As a special exception, you may use this file as part of a free
9 software library without restriction. Specifically, if other files
10 instantiate templates or use macros or inline functions from this
11 file, or you compile this file and link it with other files to
12 produce an executable, this file does not by itself cause the
13 resulting executable to be covered by the GNU General Public
14 License. This exception does not however invalidate any other
15 reasons why the executable file might be covered by the GNU Library
16 General Public License.
17
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
22
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 MA 02110-1301 USA
27*/
28
29#ifndef Pt_Gfx_Scaling_h
30#define Pt_Gfx_Scaling_h
31
32#include <Pt/Gfx/Api.h>
33#include <Pt/Gfx/Point.h>
34#include <Pt/Gfx/Size.h>
35#include <Pt/Gfx/Rect.h>
36
37namespace Pt {
38
39namespace Gfx {
40
52{
53 friend bool operator ==(const Scaling&, const Scaling&);
54 friend bool operator <(const Scaling&, const Scaling&);
55
56 public:
59 Scaling(double scaleFactor = 1.0)
60 : _scaleFactor(scaleFactor)
61 { }
62
65 double scaleFactor() const
66 {
67 return _scaleFactor;
68 }
69
73 {
74 _scaleFactor = scaleFactor;
75 }
76
77 public:
80 double toPhysical(double n) const
81 {
82 return n * scaleFactor();
83 }
84
87 Gfx::PointF toPhysical(const Gfx::PointF& p) const
88 {
89 return p * scaleFactor();
90 }
91
94 Gfx::SizeF toPhysical(const Gfx::SizeF& s) const
95 {
96 return s * scaleFactor();
97 }
98
101 Gfx::RectF toPhysical(const Gfx::RectF& r) const
102 {
103 Gfx::PointF p = toPhysical( r.topLeft() );
104 Gfx::SizeF s = toPhysical( r.size() );
105 return Gfx::RectF(p, s);
106 }
107
110 double toLogical(double n) const
111 {
112 return n / scaleFactor();
113 }
114
117 Gfx::PointF toLogical(const Gfx::PointF& p) const
118 {
119 return p / scaleFactor();
120 }
121
124 Gfx::SizeF toLogical(const Gfx::SizeF& s) const
125 {
126 return s / scaleFactor();
127 }
128
131 Gfx::RectF toLogical(const Gfx::RectF& r) const
132 {
133 Gfx::PointF p = toLogical( r.topLeft() );
134 Gfx::SizeF s = toLogical( r.size() );
135 return Gfx::RectF(p, s);
136 }
137
138 public:
141 double align(double n) const
142 {
143 // better name: alignGrid()
144
145 double p = toPhysical(n);
146 p = lround(p);
147 return toLogical(p);
148 }
149
152 double alignPixel(double n) const
153 {
154 double p = toPhysical(n);
155 p = lround(p + 0.5) - 0.5;
156 return toLogical(p);
157 }
158
161 double alignContour(size_t n) const
162 {
163 // keep contour size when downscaling
164 if (_scaleFactor < 1.0)
165 return toLogical( static_cast<double>(n) );
166
167 double p = toPhysical( static_cast<double>(n) );
168 size_t s = static_cast<size_t>(p);
169 return toLogical( static_cast<double>(s) );
170 }
171
174 Gfx::PointF align(const Gfx::PointF& p) const
175 {
176 Gfx::PointF pos = toPhysical(p);
177 pos.setX(lround(pos.x()));
178 pos.setY(lround(pos.y()));
179 return toLogical(pos);
180 }
181
184 Gfx::SizeF align(const Gfx::SizeF& s) const
185 {
186 Gfx::SizeF size = toPhysical(s);
187 size.setWidth(lround(size.width()));
188 size.setHeight(lround(size.height()));
189 return toLogical(size);
190 }
191
194 Gfx::RectF align(const Gfx::RectF& rect) const
195 {
196 Gfx::PointF pos = toPhysical(rect.topLeft());
197 pos.setX(lround(pos.x()));
198 pos.setY(lround(pos.y()));
199
200 Gfx::SizeF size = toPhysical(rect.size());
201 size.setWidth(lround(size.width()));
202 size.setHeight(lround(size.height()));
203
204 return toLogical(Gfx::RectF(pos, size));
205 }
206
207 private:
208 double _scaleFactor;
209};
210
211
215inline bool operator == (const Scaling& a, const Scaling& b)
216{
217 return a._scaleFactor == b._scaleFactor;
218}
219
220
224inline bool operator != (const Scaling& a, const Scaling& b)
225{
226 return ! (a == b);
227}
228
229
233inline bool operator < (const Scaling& a, const Scaling& b)
234{
235 return a._scaleFactor < b._scaleFactor;
236}
237
238} // namespace
239
240} // namespace
241
242#endif
double toLogical(double n) const
Converts a scalar value to logical units.
Definition Scaling.h:110
Gfx::RectF toPhysical(const Gfx::RectF &r) const
Converts a rectangle to physical units.
Definition Scaling.h:101
double alignPixel(double n) const
Aligns a scalar value to a pixel center.
Definition Scaling.h:152
Gfx::PointF toPhysical(const Gfx::PointF &p) const
Converts a point to physical units.
Definition Scaling.h:87
void setScaleFactor(double scaleFactor)
Sets the scale factor.
Definition Scaling.h:72
Gfx::SizeF toPhysical(const Gfx::SizeF &s) const
Converts a size to physical units.
Definition Scaling.h:94
Gfx::SizeF align(const Gfx::SizeF &s) const
Aligns a size to physical pixels.
Definition Scaling.h:184
double scaleFactor() const
Returns the scale factor.
Definition Scaling.h:65
Gfx::PointF toLogical(const Gfx::PointF &p) const
Converts a point to logical units.
Definition Scaling.h:117
Gfx::PointF align(const Gfx::PointF &p) const
Aligns a point to physical pixels.
Definition Scaling.h:174
double alignContour(size_t n) const
Aligns a contour width while preserving visible thickness.
Definition Scaling.h:161
Scaling(double scaleFactor=1.0)
Constructs a scaling with the given factor.
Definition Scaling.h:59
double toPhysical(double n) const
Converts a scalar value to physical units.
Definition Scaling.h:80
Gfx::RectF align(const Gfx::RectF &rect) const
Aligns a rectangle to physical pixels.
Definition Scaling.h:194
Gfx::RectF toLogical(const Gfx::RectF &r) const
Converts a rectangle to logical units.
Definition Scaling.h:131
double align(double n) const
Aligns a scalar value to the nearest physical pixel.
Definition Scaling.h:141
Gfx::SizeF toLogical(const Gfx::SizeF &s) const
Converts a size to logical units.
Definition Scaling.h:124
Graphics and imaging services.
Definition Api-Argb32Image.h:12
Core module.
Definition Allocator.h:33
Pt::int32_t lround(float x)
Rounds to nearest integer value.
Definition Math.h:289