Loading...
Searching...
No Matches
Geometry.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31#ifndef GEOMETRY_H_
32#define GEOMETRY_H_
33
34#include <drain/UniTuple.h>
35
36#include "drain/util/Frame.h"
37
38namespace drain
39{
40
41namespace image
42{
43
44
45
46class ChannelGeometry : public drain::UniTuple<size_t,2>
47{
48public:
49
50 inline
51 ChannelGeometry(size_t images=1, size_t alphas=0) : imageChannels(next()), alphaChannels(next()){ //, channels(0) {
52 this->set(images, alphas);
53 };
54
55 ChannelGeometry(const ChannelGeometry & g) : drain::UniTuple<size_t,2>(), imageChannels(next()), alphaChannels(next()){ //, channels(0) {
56 this->set(g.tuple());
57 };
58
59 template <size_t N>
62 imageChannels(this->next()),
63 alphaChannels(this->next()){
64 //updateTuple();
65 }
66
67 inline
68 virtual ~ChannelGeometry(){};
69
70
71 inline
72 ChannelGeometry & operator=(const ChannelGeometry & g){
73 set(1,0); // could be init!
75 return *this;
76 }
77
78 /*
79 template <class T>
80 inline
81 ChannelGeometry & operator=(const T & g){
82 set(1,0); // could be init!
83 assign(g);
84 return *this;
85 }
86 */
87
88
89 inline
90 void setChannelCount(const ChannelGeometry & g){
92 }
93
94 inline
95 void setChannelCount(size_t imageChannelCount, size_t alphaChannelCount = 0){
96 this->set(imageChannelCount, alphaChannelCount);
97 }
98
99 inline
100 void setAlphaChannelCount(size_t a){
101 alphaChannels = a;
102 }
103
105 inline
106 size_t getChannelCount() const {
107 return imageChannels + alphaChannels;
108 };
109
110
112 inline
113 size_t getImageChannelCount() const {
114 return imageChannels;
115 };
116
118 inline
119 size_t getAlphaChannelCount() const {
120 return alphaChannels;
121 };
122
123protected:
124
125 size_t & imageChannels;
126 size_t & alphaChannels;
127
128};
129
130
132
133
143class Geometry : public drain::UniTuple<size_t,4> { //: public AreaGeometry, public ChannelGeometry {
144
145public:
146
147 AreaGeometry area;
148
149 ChannelGeometry channels;
150
151 // condider:
152 // CoordinatePolicy policy?
153
155
157 Geometry(size_t width=0, size_t height=0, size_t channels=1, size_t alphas=0) : area(tuple(),0), channels(tuple(),2) {
158 set(width, height, channels, alphas);
159 //updateTuple();
160 }
161
162 Geometry(const Geometry &g) : drain::UniTuple<size_t,4>(), area(tuple(),0), channels(tuple(),2) {
163 assignSequence(g.tuple());
164 //updateTuple();
165 }
166
167 //Geometry(const std::vector<int> &vector, int a = 0);
168
169 virtual inline
170 ~Geometry(){};
171
172 virtual inline // CHECK if virtual needed
173 const Geometry & getGeometry() const {
174 return *this;
175 }
176
177 virtual inline // CHECK if virtual needed
178 Geometry & getGeometry() {
179 return *this;
180 }
181
182 template <class T>
183 inline
184 void setGeometry(const T &g){
185 //Geometry & operator=(const T & g){
186 set(0,0,1,0); // could be init!
187 assignSequence(g.tuple());
188 //return *this;
189 }
190
191 inline
192 void setGeometry(const Geometry &g){
193 //setGeometry(g.width, g.height, g.imageChannelCount, g.alphaChannelCount);
194 set(g);
195 }
196
197 //bool
198 void setGeometry(size_t width, size_t height, size_t imageChannelCount = 1,size_t alphaChannelCount = 0){
199 set(width, height, imageChannelCount, alphaChannelCount);
200 }
201
202 inline
203 operator const AreaGeometry &() const {
204 return area;
205 }
206
207
208 inline
209 size_t getWidth() const {
210 return area.getWidth();
211 };
212
213 inline
214 size_t getHeight() const {
215 return area.getHeight();
216 };
217
218 /*
219 template <class T>
220 inline
221 void setArea(const T & a){
222 area.set(a.tuple()); // 2024/09/??
223 };
224 */
225 // template <typename S, size_t N> TupleBase
226 template <typename S, size_t N>
227 inline
228 void setArea(const TupleBase<S,N> & a){
229 //void setArea(const AreaGeometry & a){
230 area.set(a); // 2024/09/??
231 }
232
233
234 inline
235 void setArea(size_t width, size_t height){
236 area.set(width, height);
237 };
238
239 inline
240 size_t getArea() const {
241 return area.getArea();
242 };
243
244 inline
245 const AreaGeometry & getAreaGeometry() const {
246 return area;
247 }
248
249
251 // template <class T>
252 /*
253 virtual inline
254 void setGeometry(const Geometry & g){
255 geometry.setGeometry(g);
256 }
257 */
258
259 inline
260 size_t getChannelCount() const {
261 return channels.getChannelCount(); // getGeometry unneeded?
262 };
263
264 inline
265 void setChannelCount(size_t imageChannels, size_t alphaChannels){
266 channels.set(imageChannels, alphaChannels);
267 };
268
269 inline
270 void setChannelCount(const ChannelGeometry & geom){
271 channels.setChannelCount(geom);
272 };
273
274 inline
275 size_t getImageChannelCount() const {
276 return channels.getImageChannelCount();
277 };
278
279 inline
280 void setImageChannelCount(size_t imageChannels){
281 channels.set(imageChannels);
282 };
283
284 inline
285 size_t getAlphaChannelCount() const {
286 return channels.getAlphaChannelCount();
287 };
288
289 inline
290 void setAlphaChannelCount(size_t alphaChannels){
291 channels.setAlphaChannelCount(alphaChannels);
292 };
293
294
295 inline
296 bool hasAlphaChannel() const {
297 return channels.getAlphaChannelCount() > 0;
298 };
299
301
304 size_t getChannelIndex(const std::string & index) const;
305
306
307
308 inline
309 size_t getVolume() const {
310 return area.getArea() * channels.getChannelCount();
311 };
312
313 inline
314 bool isEmpty() const {
315 return (getVolume() == 0);
316 };
317
318
319
320 // to-be-protected? :
321 /*
322 inline
323 Geometry & operator=(const Geometry &g){
324 assign(g);
325 return *this;
326 }
327
328 // needed?
329 template <class T>
330 inline
331 Geometry & operator=(const std::vector<T> &v){
332 assign(v);
333 return *this;
334 }
335 */
336
337 //inline
338 //Geometry & operator=(const Geometry &g);
339 inline
340 bool operator==(const Geometry &g) const {
341 return (tuple() == g.tuple());
342 /*
343 return ((width==g.getWidth()) && (height==g.getHeight())
344 && (imageChannelCount==g.getImageChannelCount()) && (alphaChannelCount==g.getAlphaChannelCount()));
345 */
346 };
347
348 inline
349 bool operator!=(const Geometry &g) const {
350 return !((*this)==g);
351 };
352
353 /*
354 inline
355 void toStream(std::ostream &ostr) const {
356 AreaGeometry::toStream(ostr);
357 if (getChannelCount() != 1){
358 ostr << "×";
359 ChannelGeometry::toOStr(ostr);
360 }
361 }
362 */
363
364 /*
365 inline
366 std::string toString(std::string & s) const {
367 return this->tuple().toStr();
368 }
369 */
370
371
372protected:
373
375 /*
376 virtual inline
377 void updateTuple(){
378 area.updateTuple();
379 channels.updateTuple();
380 }
381 */
382
383 //size_t volume;
384
385
386};
387
388
389
391/*
392template <class T>
393Geometry & Geometry::operator=(const std::vector<T> &v){
394 int d = v.size();
395 imageChannelCount = 1;
396 alphaChannelCount = 0;
397 switch (d) {
398 case 4:
399 alphaChannelCount = v[3];
400 break;
401 case 3:
402 imageChannelCount = v[2];
403 break;
404 case 2:
405 this->height = v[1];
406 break;
407 case 1:
408 this->width = v[0];
409 break;
410 default:
411 break;
412 }
413 channelCount = imageChannelCount + alphaChannelCount;
414 update();
415 return (*this);
416}
417 */
418
419/*
420inline
421std::ostream & operator<<(std::ostream &ostr, const Geometry &geometry){
422 geometry.toOStr(ostr);
423 return ostr;
424}
425*/
426
427
428}
429
430}
431
432#endif /*GEOMETRY_H_*/
433
434// Drain
Something that has width and height.
Definition Frame.h:53
tuplebase_t & assignSequence(T &sequence, bool LENIENT=false)
Proposed for tuples only; derived classes should not shadow this.
Definition TupleBase.h:287
Tuple of N elements of type T.
Definition UniTuple.h:65
Definition Geometry.h:47
size_t getAlphaChannelCount() const
Return the number of alpha channels.
Definition Geometry.h:119
size_t getImageChannelCount() const
Return the number of actual image channels (excluding alphas)
Definition Geometry.h:113
size_t getChannelCount() const
Return the number of channels (image and alpha)
Definition Geometry.h:106
Definition Geometry.h:143
size_t getChannelIndex(const std::string &index) const
Returns numeric channel index from "r", "g", "b", or "a" or a non-negative number in string format.
Definition Geometry.cpp:45
Geometry(size_t width=0, size_t height=0, size_t channels=1, size_t alphas=0)
Constructor with dimensions. Channel count is one by default, allowing construction with width and he...
Definition Geometry.h:157
size_t getChannelCount() const
Set...
Definition Geometry.h:260
Definition Data.h:1473