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
68
69 virtual inline
70 const ImageConf & getConf() const {
71 return conf;
72 }
73
74 /*
75 virtual inline
76 ImageConf & getConf() {
77 return conf;
78 }
79 */
80
81
82 inline
83 void setConf(const ImageConf &conf) {
84 this->conf.setConf(conf);
85 setStorageType(conf.getType());
86 update();
87 }
88
90
95 // Consider old feature: \return true if geometry was changed, otherwise false.
96 virtual inline
97 void setGeometry(size_t width, size_t height, size_t imageChannels=1, size_t alphaChannels=0){
98 conf.getGeometry().set(width, height, imageChannels, alphaChannels);
99 update();
100 }
101
103 inline
104 void setGeometry(const AreaGeometry &g, size_t imageChannels=1, size_t alphaChannels=0){
105 conf.channels.set(imageChannels, alphaChannels); // ? CONSIDER area geom?
106 conf.area.set(g);
107 update();
108 };
109
110 inline
111 void setGeometry(const AreaGeometry & areaGeom, const ChannelGeometry & channelGeom){
112 conf.channels.set(channelGeom); // ? CONSIDER area geom?
113 conf.area.set(areaGeom);
114 update();
115 };
116
117
119 inline
120 void setGeometry(const Geometry &g){
121 conf.getGeometry().assignSequence(g);
122 update();
123 };
124
125
128 setGeometry(0,0,0,0);
129 }
130
131
132 inline
133 void setChannelCount(size_t ni, size_t na = 0){
134 conf.channels.set(ni, na);
135 update();
136 };
137
138
139 inline
140 void setAlphaChannelCount(size_t k){
141 conf.channels.setAlphaChannelCount(k);
142 update();
143 //setGeometry(getWidth(), getHeight(), getImageChannelCount(), k);
144 };
145
146
148 virtual inline
149 void initialize(const std::type_info &t, const Geometry & g){
151 setGeometry(g);
152 //initialize(t, g.area.getWidth(), g.area.getHeight(), g.channels.getImageChannelCount(), g.channels.getAlphaChannelCount());
153 }
154
156 virtual inline
157 void initialize(const std::type_info &t, size_t width, size_t height, size_t imageChannels=1, size_t alphaChannels=0){
159 setGeometry(width, height, imageChannels, alphaChannels);
160 }
161
162
164
167 virtual
168 bool suggestType(const std::type_info &t) override;
169
171
174 virtual
175 bool requireGeometry(const Geometry & geometry) override;
176
177
178private:
179
180 void update();
181
182};
183
184
185
187
193class Image : public ModifiableImage { //, protected Castable {
194
195public:
196
198 inline
199 Image(const std::type_info & t = typeid(unsigned char), const Geometry & g = Geometry()){ //: buffer(1), bufferPtr(&buffer[0]), segmentBegin(&buffer[0]), segmentEnd(&buffer[0]) {
200 //Logger mout(getImgLog(), "Image(T, g)", __FUNCTION__);
201 initialize(t,g);
202 //mout.warn(*this );
203 };
204
206 inline
207 Image(const std::type_info & t, 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(T, w,h,c,ca)", __FUNCTION__);
210 initialize(t, Geometry(width, height, channelCount, alphaChannelCount));
211 //mout.warn(*this );
212 };
213
215 inline
216 Image(size_t width, size_t height, size_t channelCount=1, size_t alphaChannelCount=0){
217 // buffer(1), bufferPtr(&buffer[0]), segmentBegin(&buffer[0]), segmentEnd(&buffer[0]) {
218 //Logger mout(getImgLog(), "Image(w,h,c,ca)", __FUNCTION__);
219 initialize(typeid(unsigned char), width, height, channelCount, alphaChannelCount);
220 //mout.warn(*this );
221 };
222
224 inline
225 Image(const Image & image){
226 copyShallow(image);
227 };
228
230 inline
231 Image(const ImageFrame & image){
232 copyShallow(image);
233 };
234
236 inline
237 Image(const ImageConf & conf){
238 setConf(conf);
239 //copyShallow(image);
240 };
241
242
243 inline
244 operator const Channel &() const {
245 if (conf.getChannelCount() == 0){
246 Logger mout(getImgLog(), __FILE__, __FUNCTION__);
247 mout.error("Image: no channels for cast op");
248 //throw std::runtime_error("Image: no channels for cast");
249 }
250 else if (conf.getChannelCount() > 1){
251 Logger mout(getImgLog(), __FILE__, __FUNCTION__);
252 mout.warn("several channels (" , conf.getChannelCount() , "), returning first." );
253 }
254 return getChannel(0);
255 }
256
257 inline
258 operator Channel &(){
259 if (conf.getChannelCount() == 0){
260 Logger mout(getImgLog(), __FILE__, __FUNCTION__);
261 mout.error("Image: no channels for cast op");
262 // throw std::runtime_error("Image: no channels for cast");
263 }
264 else if (conf.getChannelCount() > 1){
265 Logger mout(getImgLog(), __FILE__, __FUNCTION__);
266 mout.warn("several channels (" , conf.getChannelCount() , "), returning first." );
267 }
268 return getChannel(0);
269 }
270
271
273 /* Sets the type of pixel elements of the image.
274 *
275 */
276 inline
277 void setType(const std::type_info &type){
278 Logger mout(getImgLog(), __FILE__, __FUNCTION__);
279 setStorageType(type);
280 if (!conf.isEmpty()){
281 mout.note("STYLE/ changing type of allocated image" );
282 }
283 conf.setGeometry(getGeometry());
284 };
285
287 template <class T>
288 inline
289 void setType(){
290 setType(typeid(T));
291 }
292
294
297 template <class T>
298 inline
299 void setType(const T & t){
301 }
302
303
304
306 inline
307 void copyShallow(const ImageFrame & src){
308 setConf(src.getConf()); // update() must be called
309 //updateChannelVector();
310 /*
311 setStorageType(src.getType());
312 conf.setScaling(src.getScaling()); // NOTE: pointer-selected
313 conf.setGeometry(src.getGeometry());
314 setCoordinatePolicy(src.getCoordinatePolicy());
315 */
316 //initialize(src.getType(), src.getGeometry());
317 //setScaling(src.getScaling());
318 //setCoordinatePolicy(src.getCoordinatePolicy());
319 }
320
322 inline
323 void copyDeep(const ImageFrame &src){
324 copyShallow(src);
325 copyData(src);
326 }
327
328
330 void dump(std::ostream &ostr = std::cout) const;
331
332
333 inline
334 void adoptScaling(const ImageConf & src, const std::type_info & t = typeid(void)){
335 //conf.useOwnScaling(); // needed?
336 if (t == typeid(void))
337 conf.adoptScaling(src.getScaling(), src.getType(), conf.getType());
338 else
339 conf.adoptScaling(src.getScaling(), src.getType(), t);
340 }
341
342 void swap(Image & img);
343
344};
345
346inline
347std::ostream & operator<<(std::ostream &ostr, const Image &image){
348 image.toStream(ostr);
349 //ostr << image.getWidth() << 'X' << image.getHeight() << '\n';
350 return ostr;
351}
352
353} // image::
354
355} // drain::
356
357
358#endif
359
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:64
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:193
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:216
Image(const ImageFrame &image)
Copies the geometry of another image.
Definition Image.h:231
void setType(const std::type_info &type)
Sets the storage type of the image - typically unsigned char, unsigned int or float.
Definition Image.h:277
void copyDeep(const ImageFrame &src)
Copies the type, geometry, coordinate policy and data of the given image.
Definition Image.h:323
void setType(const T &t)
Sets type of the image, applying character mapping of Type::setType(char c) . TODO: drain::Type.
Definition Image.h:299
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:307
Image(const ImageConf &conf)
Copies the geometry of another image.
Definition Image.h:237
Image(const std::type_info &t=typeid(unsigned char), const Geometry &g=Geometry())
The constructor for an empty image.
Definition Image.h:199
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:289
Image(const Image &image)
Copies the geometry of another image.
Definition Image.h:225
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:207
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:104
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:97
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:157
void setGeometry(const Geometry &g)
Resizes the image, keeps the current type.
Definition Image.h:120
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:149
void resetGeometry()
Collapses the image to undetectValue size, releasing memory.
Definition Image.h:127
Multi-channel ImageFrame.
Definition ImageChannel.h:150
Definition DataSelector.cpp:1277