Loading...
Searching...
No Matches
Image.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#include <drain/Log.h>
32#include <typeinfo>
33#include <iostream>
34
35
36#include <drain/CastableIterator.h>
37
38
39#include "Geometry.h"
40#include "ImageFrame.h"
41#include "ImageChannel.h"
42
43
44#ifndef DRAIN_IMAGE
45#define DRAIN_IMAGE "drain::Image v3.0 2017/10 Markus.Peura@fmi.fi"
46
47
48namespace drain {
49
50namespace image {
51
54
55public:
56
57
58
59 virtual inline
60 const ImageConf & getConf() const {
61 return conf;
62 }
63
64 /*
65 virtual inline
66 ImageConf & getConf() {
67 return conf;
68 }
69 */
70
71
72 inline
73 void setConf(const ImageConf &conf) {
74 this->conf.setConf(conf);
75 setStorageType(conf.getType());
76 update();
77 }
78
80
85 // Consider old feature: \return true if geometry was changed, otherwise false.
86 virtual inline
87 void setGeometry(size_t width, size_t height, size_t imageChannels=1, size_t alphaChannels=0){
88 conf.getGeometry().set(width, height, imageChannels, alphaChannels);
89 update();
90 }
91
93 inline
94 void setGeometry(const AreaGeometry &g, size_t imageChannels=1, size_t alphaChannels=0){
95 conf.channels.set(imageChannels, alphaChannels); // ? CONSIDER area geom?
96 conf.area.set(g);
97 update();
98 };
99
100 inline
101 void setGeometry(const AreaGeometry & areaGeom, const ChannelGeometry & channelGeom){
102 conf.channels.set(channelGeom); // ? CONSIDER area geom?
103 conf.area.set(areaGeom);
104 update();
105 };
106
107
109 inline
110 void setGeometry(const Geometry &g){
111 conf.getGeometry().assignSequence(g);
112 update();
113 };
114
115
118 setGeometry(0,0,0,0);
119 }
120
121
122 inline
123 void setChannelCount(size_t ni, size_t na = 0){
124 conf.channels.set(ni, na);
125 update();
126 };
127
128
129 inline
130 void setAlphaChannelCount(size_t k){
131 conf.channels.setAlphaChannelCount(k);
132 update();
133 //setGeometry(getWidth(), getHeight(), getImageChannelCount(), k);
134 };
135
136
138 virtual inline
139 void initialize(const std::type_info &t, const Geometry & g){
141 setGeometry(g);
142 //initialize(t, g.area.getWidth(), g.area.getHeight(), g.channels.getImageChannelCount(), g.channels.getAlphaChannelCount());
143 }
144
146 virtual inline
147 void initialize(const std::type_info &t, size_t width, size_t height, size_t imageChannels=1, size_t alphaChannels=0){
149 setGeometry(width, height, imageChannels, alphaChannels);
150 }
151
152
154
157 virtual
158 bool suggestType(const std::type_info &t) override;
159
161
164 virtual
165 bool requireGeometry(const Geometry & geometry) override;
166
167
168private:
169
170 void update();
171
172};
173
174
175
177
183class Image : public ModifiableImage { //, protected Castable {
184
185public:
186
188 inline
189 Image(const std::type_info & t = typeid(unsigned char), const Geometry & g = Geometry()){ //: buffer(1), bufferPtr(&buffer[0]), segmentBegin(&buffer[0]), segmentEnd(&buffer[0]) {
190 //Logger mout(getImgLog(), "Image(T, g)", __FUNCTION__);
191 initialize(t,g);
192 //mout.warn(*this );
193 };
194
196 inline
197 Image(const std::type_info & t, size_t width, size_t height, size_t channelCount=1, size_t alphaChannelCount=0){
198 // : buffer(1), bufferPtr(&buffer[0]), segmentBegin(&buffer[0]), segmentEnd(&buffer[0]) {
199 //Logger mout(getImgLog(), "Image(T, w,h,c,ca)", __FUNCTION__);
200 initialize(t, Geometry(width, height, channelCount, alphaChannelCount));
201 //mout.warn(*this );
202 };
203
205 inline
206 Image(size_t width, size_t height, size_t channelCount=1, size_t alphaChannelCount=0){
207 // buffer(1), bufferPtr(&buffer[0]), segmentBegin(&buffer[0]), segmentEnd(&buffer[0]) {
208 //Logger mout(getImgLog(), "Image(w,h,c,ca)", __FUNCTION__);
209 initialize(typeid(unsigned char), width, height, channelCount, alphaChannelCount);
210 //mout.warn(*this );
211 };
212
214 inline
215 Image(const Image & image){
216 copyShallow(image);
217 };
218
220 inline
221 Image(const ImageFrame & image){
222 copyShallow(image);
223 };
224
226 inline
227 Image(const ImageConf & conf){
228 setConf(conf);
229 //copyShallow(image);
230 };
231
232
233 inline
234 operator const Channel &() const {
235 if (conf.getChannelCount() == 0){
236 Logger mout(getImgLog(), __FILE__, __FUNCTION__);
237 mout.error("Image: no channels for cast op");
238 //throw std::runtime_error("Image: no channels for cast");
239 }
240 else if (conf.getChannelCount() > 1){
241 Logger mout(getImgLog(), __FILE__, __FUNCTION__);
242 mout.warn("several channels (" , conf.getChannelCount() , "), returning first." );
243 }
244 return getChannel(0);
245 }
246
247 inline
248 operator Channel &(){
249 if (conf.getChannelCount() == 0){
250 Logger mout(getImgLog(), __FILE__, __FUNCTION__);
251 mout.error("Image: no channels for cast op");
252 // throw std::runtime_error("Image: no channels for cast");
253 }
254 else if (conf.getChannelCount() > 1){
255 Logger mout(getImgLog(), __FILE__, __FUNCTION__);
256 mout.warn("several channels (" , conf.getChannelCount() , "), returning first." );
257 }
258 return getChannel(0);
259 }
260
261
263 /* Sets the type of pixel elements of the image.
264 *
265 */
266 inline
267 void setType(const std::type_info &type){
268 Logger mout(getImgLog(), __FILE__, __FUNCTION__);
269 setStorageType(type);
270 if (!conf.isEmpty()){
271 mout.note("STYLE/ changing type of allocated image" );
272 }
273 conf.setGeometry(getGeometry());
274 };
275
277 template <class T>
278 inline
279 void setType(){
280 setType(typeid(T));
281 }
282
284
287 template <class T>
288 inline
289 void setType(const T & t){
291 }
292
293
294
296 inline
297 void copyShallow(const ImageFrame & src){
298 setConf(src.getConf()); // update() must be called
299 //updateChannelVector();
300 /*
301 setStorageType(src.getType());
302 conf.setScaling(src.getScaling()); // NOTE: pointer-selected
303 conf.setGeometry(src.getGeometry());
304 setCoordinatePolicy(src.getCoordinatePolicy());
305 */
306 //initialize(src.getType(), src.getGeometry());
307 //setScaling(src.getScaling());
308 //setCoordinatePolicy(src.getCoordinatePolicy());
309 }
310
312 inline
313 void copyDeep(const ImageFrame &src){
314 copyShallow(src);
315 copyData(src);
316 }
317
318
320 void dump(std::ostream &ostr = std::cout) const;
321
322
323 inline
324 void adoptScaling(const ImageConf & src, const std::type_info & t = typeid(void)){
325 //conf.useOwnScaling(); // needed?
326 if (t == typeid(void))
327 conf.adoptScaling(src.getScaling(), src.getType(), conf.getType());
328 else
329 conf.adoptScaling(src.getScaling(), src.getType(), t);
330 }
331
332 void swap(Image & img);
333
334};
335
336inline
337std::ostream & operator<<(std::ostream &ostr, const Image &image){
338 image.toStream(ostr);
339 //ostr << image.getWidth() << 'X' << image.getHeight() << '\n';
340 return ostr;
341}
342
343} // image::
344
345} // drain::
346
347
348#endif
349
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:313
Logger & note(const TT &... args)
For top-level information.
Definition Log.h:490
tuplebase_t & assignSequence(T &sequence, bool LENIENT=false)
Proposed for tuples only; derived classes should not shadow this.
Definition TupleBase.h:287
static const std::type_info & getTypeInfo(char t)
Returns the base type associated with a character key.
Definition Type.h:131
void adoptScaling(const drain::ValueScaling &srcScaling, const std::type_info &srcType, const std::type_info &dstType=typeid(void))
Sets scale and offset according to physical range and current type.
Definition ValueScaling.cpp:73
virtual const ValueScaling & getScaling() const
Get linear scaling.
Definition ValueScaling.h:146
Definition Geometry.h:47
Image with static geometry.
Definition ImageChannel.h:58
const std::type_info & getType() const
Linear scaling.
Definition ImageConf.h:110
Definition Geometry.h:143
size_t getChannelCount() const
Set...
Definition Geometry.h:255
Struct for image (excluding data)
Definition ImageConf.h:333
Image with static geometry.
Definition ImageFrame.h:62
void setStorageType(const std::type_info &type)
Sets the storage type of the image - typically unsigned char, unsigned int or float....
Definition ImageFrame.cpp:62
void copyData(const ImageFrame &src)
Copies data. Does not change encoding, geometry, or coordinate policy.
Definition ImageFrame.cpp:236
Class for multi-channel digital images. Supports dynamic typing with base types (char,...
Definition Image.h:183
Image(size_t width, size_t height, size_t channelCount=1, size_t alphaChannelCount=0)
The constructor for unsigned char image of given size.
Definition Image.h:206
Image(const ImageFrame &image)
Copies the geometry of another image.
Definition Image.h:221
void setType(const std::type_info &type)
Sets the storage type of the image - typically unsigned char, unsigned int or float.
Definition Image.h:267
void copyDeep(const ImageFrame &src)
Copies the type, geometry, coordinate policy and data of the given image.
Definition Image.h:313
void setType(const T &t)
Sets type of the image, applying character mapping of Type::setType(char c) . TODO: drain::Type.
Definition Image.h:289
void copyShallow(const ImageFrame &src)
Copies type, geometry and coordinate under/overflow policy of the given image. Does not copy the data...
Definition Image.h:297
Image(const ImageConf &conf)
Copies the geometry of another image.
Definition Image.h:227
Image(const std::type_info &t=typeid(unsigned char), const Geometry &g=Geometry())
The constructor for an empty image.
Definition Image.h:189
void dump(std::ostream &ostr=std::cout) const
Prints images geometry, buffer size and type information, and dumps the array contents....
Definition Image.cpp:43
void setType()
Sets the storage type of the image - typically unsigned char, unsigned int or float.
Definition Image.h:279
Image(const Image &image)
Copies the geometry of another image.
Definition Image.h:215
Image(const std::type_info &t, size_t width, size_t height, size_t channelCount=1, size_t alphaChannelCount=0)
The constructor for an image of given type and size.
Definition Image.h:197
Image with modifiable geometry.
Definition Image.h:53
void setGeometry(const AreaGeometry &g, size_t imageChannels=1, size_t alphaChannels=0)
Resizes the image, keeps the current type.
Definition Image.h:94
virtual void setGeometry(size_t width, size_t height, size_t imageChannels=1, size_t alphaChannels=0)
Resizes the image, keeps the current type.
Definition Image.h:87
virtual void initialize(const std::type_info &t, size_t width, size_t height, size_t imageChannels=1, size_t alphaChannels=0)
Sets the type and allocates a data buffer.
Definition Image.h:147
void setGeometry(const Geometry &g)
Resizes the image, keeps the current type.
Definition Image.h:110
virtual bool requireGeometry(const Geometry &geometry) override
Change geometry. This overrides the behavior of ImageFrame::requireGeometry().
Definition Image.cpp:103
virtual bool suggestType(const std::type_info &t) override
Change type. This overrides the behavior of ImageFrame::suggestType().
Definition Image.cpp:93
virtual void initialize(const std::type_info &t, const Geometry &g)
Sets the type and allocates a data buffer.
Definition Image.h:139
void resetGeometry()
Collapses the image to undetectValue size, releasing memory.
Definition Image.h:117
Multi-channel ImageFrame.
Definition ImageChannel.h:148
Definition DataSelector.cpp:1277