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