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) : 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) : 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
246 // template <class T>
247 /*
248 virtual inline
249 void setGeometry(const Geometry & g){
250 geometry.setGeometry(g);
251 }
252 */
253
254 inline
255 size_t getChannelCount() const {
256 return channels.getChannelCount(); // getGeometry unneeded?
257 };
258
259 inline
260 void setChannelCount(size_t imageChannels, size_t alphaChannels){
261 channels.set(imageChannels, alphaChannels);
262 };
263
264 inline
265 void setChannelCount(const ChannelGeometry & geom){
266 channels.setChannelCount(geom);
267 };
268
269 inline
270 const size_t getImageChannelCount() const {
271 return channels.getImageChannelCount();
272 };
273
274 inline
275 void setImageChannelCount(size_t imageChannels){
276 channels.set(imageChannels);
277 };
278
279 inline
280 const size_t getAlphaChannelCount() const {
281 return channels.getAlphaChannelCount();
282 };
283
284 inline
285 void setAlphaChannelCount(size_t alphaChannels){
286 channels.setAlphaChannelCount(alphaChannels);
287 };
288
289
290 inline
291 bool hasAlphaChannel() const {
292 return channels.getAlphaChannelCount() > 0;
293 };
294
296
299 size_t getChannelIndex(const std::string & index) const;
300
301
302
303 inline
304 size_t getVolume() const {
305 return area.getArea() * channels.getChannelCount();
306 };
307
308 inline
309 bool isEmpty() const {
310 return (getVolume() == 0);
311 };
312
313
314
315 // to-be-protected? :
316 /*
317 inline
318 Geometry & operator=(const Geometry &g){
319 assign(g);
320 return *this;
321 }
322
323 // needed?
324 template <class T>
325 inline
326 Geometry & operator=(const std::vector<T> &v){
327 assign(v);
328 return *this;
329 }
330 */
331
332 //inline
333 //Geometry & operator=(const Geometry &g);
334 inline
335 bool operator==(const Geometry &g) const {
336 return (tuple() == g.tuple());
337 /*
338 return ((width==g.getWidth()) && (height==g.getHeight())
339 && (imageChannelCount==g.getImageChannelCount()) && (alphaChannelCount==g.getAlphaChannelCount()));
340 */
341 };
342
343 inline
344 bool operator!=(const Geometry &g) const {
345 return !((*this)==g);
346 };
347
348 /*
349 inline
350 void toStream(std::ostream &ostr) const {
351 AreaGeometry::toStream(ostr);
352 if (getChannelCount() != 1){
353 ostr << "×";
354 ChannelGeometry::toOStr(ostr);
355 }
356 }
357 */
358
359 /*
360 inline
361 std::string toString(std::string & s) const {
362 return this->tuple().toStr();
363 }
364 */
365
366
367protected:
368
370 /*
371 virtual inline
372 void updateTuple(){
373 area.updateTuple();
374 channels.updateTuple();
375 }
376 */
377
378 //size_t volume;
379
380
381};
382
383
384
386/*
387template <class T>
388Geometry & Geometry::operator=(const std::vector<T> &v){
389 int d = v.size();
390 imageChannelCount = 1;
391 alphaChannelCount = 0;
392 switch (d) {
393 case 4:
394 alphaChannelCount = v[3];
395 break;
396 case 3:
397 imageChannelCount = v[2];
398 break;
399 case 2:
400 this->height = v[1];
401 break;
402 case 1:
403 this->width = v[0];
404 break;
405 default:
406 break;
407 }
408 channelCount = imageChannelCount + alphaChannelCount;
409 update();
410 return (*this);
411}
412 */
413
414/*
415inline
416std::ostream & operator<<(std::ostream &ostr, const Geometry &geometry){
417 geometry.toOStr(ostr);
418 return ostr;
419}
420*/
421
422
423}
424
425}
426
427#endif /*GEOMETRY_H_*/
428
429// 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:255
Definition DataSelector.cpp:1277