Kstars

imagemask.cpp
1/*
2 SPDX-FileCopyrightText: 2023 Wolfgang Reissenberger <sterne-jaeger@openfuture.de>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include "imagemask.h"
8#include "cmath"
9
10ImageMask::ImageMask(const uint16_t width, const uint16_t height)
11{
12 m_width = width;
13 m_height = height;
14}
15
16void ImageMask::setImageGeometry(const uint16_t width, const uint16_t height)
17{
18 m_width = width;
19 m_height = height;
20 refresh();
21}
22
23bool ImageMask::isVisible(uint16_t posX, uint16_t posY)
24{
25 return posX >= 0 && posX < m_width && posY >= 0 && posY < m_height;
26}
27
28uint16_t ImageMask::width() const
29{
30 return m_width;
31}
32
33uint16_t ImageMask::height() const
34{
35 return m_height;
36}
37
38ImageRingMask::ImageRingMask(const float innerRadius, const float outerRadius, const uint16_t width,
39 const uint16_t height) : ImageMask(width, height)
40{
41 m_innerRadius = innerRadius;
42 m_outerRadius = outerRadius;
43}
44
45void ImageRingMask::refresh()
46{
47 long const sqDiagonal = (long) (m_width * m_width / 4 + m_height * m_height / 4);
48 m_InnerRadiusSquare = std::lround(sqDiagonal * innerRadius() * innerRadius());
49 m_OuterRadiusSquare = std::lround(sqDiagonal * outerRadius() * outerRadius());
50}
51
52bool ImageRingMask::isVisible(uint16_t posX, uint16_t posY)
53{
54 bool result = ImageMask::isVisible(posX, posY);
55 // outside of the image?
56 if (result == false)
57 return false;
58
59 double const x = posX - m_width / 2;
60 double const y = posY - m_height / 2;
61
62 double const sqRadius = x * x + y * y;
63
64 return sqRadius >= m_InnerRadiusSquare && sqRadius < m_OuterRadiusSquare;
65}
66
67float ImageRingMask::innerRadius() const
68{
69 return m_innerRadius;
70}
71
72void ImageRingMask::setInnerRadius(float newInnerRadius)
73{
74 m_innerRadius = newInnerRadius;
75 refresh();
76}
77
78float ImageRingMask::outerRadius() const
79{
80 return m_outerRadius;
81}
82
83void ImageRingMask::setOuterRadius(float newOuterRadius)
84{
85 m_outerRadius = newOuterRadius;
86 refresh();
87}
88
89ImageMosaicMask::ImageMosaicMask(const uint16_t tileWidth, const uint16_t space, const uint16_t width,
90 const uint16_t height) : ImageMask(width, height)
91{
92 m_tileWidth = tileWidth;
93 m_space = space;
94 // initialize the tiles
95 for (int i = 0; i < 9; i++)
96 m_tiles.append(QRect());
97}
98
99bool ImageMosaicMask::isVisible(uint16_t posX, uint16_t posY)
100{
101 for (auto it = m_tiles.begin(); it != m_tiles.end(); it++)
102 if (it->contains(posX, posY))
103 return true;
104 // no matching tile found
105 return false;
106}
107
108QPointF ImageMosaicMask::translate(QPointF original)
109{
110 int pos = 0;
111 const auto tileWidth = std::lround(m_width * m_tileWidth / 100);
112 const float spacex = (m_width - 3 * tileWidth - 2 * m_space) / 2;
113 const float spacey = (m_height - 3 * tileWidth - 2 * m_space) / 2;
114 for (QRect tile : m_tiles)
115 {
116 // matrix tile position
117 int posx = pos % 3;
118 int posy = pos++ / 3;
119 if (tile.contains(original.x(), original.y()))
120 return QPointF(original.x() - posx * spacex, original.y() - posy * spacey);
121 }
122 // this should not happen for filtered positions
123 return QPointF(-1, -1);
124}
125
126const QVector<QRect> ImageMosaicMask::tiles()
127{
128 return m_tiles;
129}
130
131float ImageMosaicMask::tileWidth() const
132{
133 return m_tileWidth;
134}
135
136void ImageMosaicMask::setTileWidth(float newTileWidth)
137{
138 m_tileWidth = newTileWidth;
139 refresh();
140}
141
142uint16_t ImageMosaicMask::space() const
143{
144 return m_space;
145}
146
147void ImageMosaicMask::setSpace(uint16_t newSpace)
148{
149 m_space = newSpace;
150 refresh();
151}
152
153void ImageMosaicMask::refresh()
154{
155 m_tiles.clear();
156 uint16_t tileWidth = std::lround(m_width * m_tileWidth / 100);
157 if (m_width > 0 && m_height > 0)
158 {
159 const auto x1 = std::lround((m_width - tileWidth) / 2);
160 const auto x2 = m_width - tileWidth - 1;
161 const auto y1 = std::lround((m_height - tileWidth) / 2);
162 const auto y2 = m_height - tileWidth - 1;
163
164 m_tiles.append(QRect(0, 0, tileWidth, tileWidth));
165 m_tiles.append(QRect(x1, 0, tileWidth, tileWidth));
166 m_tiles.append(QRect(x2, 0, tileWidth, tileWidth));
167 m_tiles.append(QRect(0, y1, tileWidth, tileWidth));
168 m_tiles.append(QRect(x1, y1, tileWidth, tileWidth));
169 m_tiles.append(QRect(x2, y1, tileWidth, tileWidth));
170 m_tiles.append(QRect(0, y2, tileWidth, tileWidth));
171 m_tiles.append(QRect(x1, y2, tileWidth, tileWidth));
172 m_tiles.append(QRect(x2, y2, tileWidth, tileWidth));
173 }
174}
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:49:20 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.