Loading...
Searching...
No Matches
ImageOp.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 DRAIN_IMAGE_OP_H_
32#define DRAIN_IMAGE_OP_H_
33
34#include "drain/util/BeanLike.h"
35#include "drain/image/CoordinateHandler.h"
36#include "ImageMod.h"
37
38
39
40namespace drain {
41
42namespace image {
43
45
49class ImageOp : public ImageMod {
50
51public:
52
53
54 inline
55 virtual ~ImageOp(){};
56
57
58 // const std::string & getName();
59
61
66 virtual
67 void process(const ImageFrame & src, Image & dst) const;
68
69 virtual inline
70 void process(Image & dst) const override final {
71 process(dst, dst);
72 }
73
74
75 virtual
76 void process(const ImageFrame & src, const ImageFrame & srcWeight, Image & dst, Image & dstWeight) const;
77
78 /*
79 virtual inline
80 void process(const ImageTray<const Channel> & src, ImageTray<Image> & dst) const {
81 processConditional(src, dst, true);
82 }
83 */
84
85
87
90 virtual
91 void process(const ImageTray<const Channel> & src, ImageTray<Image> & dst, bool checkOverlap = true) const; // could be Tray<ModifiableChannel> ?
92
93 // Note: only a default implementation.
94 virtual inline
95 void traverseChannels(const ImageTray<const Channel> & src, ImageTray<Channel> & dst) const {
96 drain::Logger mout(getImgLog(), __FILE__, __FUNCTION__); //REPL this->name+"(ImageOp::)[const ChannelTray &, ChannelTray &]", __FUNCTION__);
97 mout.error() << "not implemented; consider processChannels[Equally|Repeated|Separately]" << mout.endl;
98 // traverseChannelsEqually(src, dst);
99 // traverseChannelsRepeated(src, dst);
100 // traverseChannelsSeparately(src, dst);
101 // traverseMultiChannel(src, dst);
102 }
103
104
106 virtual inline
107 void traverseChannel(const Channel & src, Channel &dst) const {
108 throw std::runtime_error(name+"(ImageOp)::"+__FUNCTION__+"(src,dst) unimplemented.");
109 };
110
112 virtual inline
113 void traverseChannel(const Channel &src, const Channel &srcAlpha, Channel &dst, Channel &dstAlpha) const {
114 //throw std::runtime_error(name+"(ImageOp)::"+__FUNCTION__+"(src,srcAlpha,dst,dstAlpha) unimplemented.");
115 Logger mout(getImgLog(), __FILE__, __FUNCTION__);
116 mout.note("using image channels only (discarding alpha channels)" );
117 traverseChannel(src, dst);
118 };
119
121
126 virtual
127 void getDstConf(const ImageConf & src, ImageConf & dst) const;
128
129
131 /* The old policy was to have this default implementation
132 * -# returns immediately, if dst==src.
133 * -# sets dst type, if unset, to that of src
134 * -# sets dst geometry to that of src
135 */
136 virtual // TODO: non-virtual, ie, final!
137 void makeCompatible(const ImageConf & src, Image & dst) const;
138
140 /*
141 * This default implementation
142 * - returns immediately, if dst has overlap with src1 or src2.
143 * - else, calls makeCompatible(src1, dst); src2 has no effect.
144 */
145 virtual
146 void makeCompatible2(const ImageFrame & src1, const ImageFrame & src2, Image & dst) const;
147
148
150 virtual inline
151 void help(std::ostream & ostr = std::cout) const {
152 toStream(ostr);
153 }
154
155protected:
156
161 inline // = std::string(__FUNCTION__).substr(0, ::strlen(__FUNCTION__)-2
162 ImageOp(const std::string &name = __FUNCTION__, const std::string &description="") : ImageMod(name, description){
163 };
164
168 inline
169 ImageOp(const ImageOp & op) : ImageMod(op.name, op.description){
170 };
171
172 virtual
173 bool processOverlappingWithTemp(const ImageFrame & src, Image & dst) const;
174
175 virtual
176 bool processOverlappingWithTemp(const ImageTray<const Channel> & src, ImageTray<Image> & dst) const;
177
178
179 // Final ?
180 bool traverseOverlappingWithTemp(const Channel & src, Channel & dst) const;
181
182 // Final ?
183 bool traverseOverlappingWithTemp(const Channel & src, const Channel & srcWeight, Channel & dst, Channel & dstWeight) const;
184
185
187 // Final
189 //bool processOverlappingWithTemp(const ImageFrame & srcFrame, Image & dstImage) const; // needed?
190
192 // Final
193 //bool processOverlappingWithTemp(const ImageFrame & src, const ImageFrame & srcWeight, Image & dst, Image & dstWeight) const; // needed?
194
196 //virtual void traverse rame(const ImageFrame & src, ImageFrame & dst) const;
197 // USE
198
200
204
207
209
212
213
215 inline
216 void traverseAsChannelTrays(const ImageFrame &src, ImageFrame &dst) const {
217 Logger mout(getImgLog(), __FILE__, __FUNCTION__);
218 mout.debug("restoring to trays" );
219
221 s.setChannels(src);
222
224 d.setChannels(dst);
225
226 traverseChannels(s, d);
227 }
228
230 inline
231 void traverseAsChannelTrays(const ImageFrame &src, const ImageFrame & srcWeight, ImageFrame &dst, ImageFrame &dstWeight) const {
232
233 Logger mout(getImgLog(), __FILE__, __FUNCTION__);
234 mout.debug("restoring to trays" );
235
237 s.setChannels(src, srcWeight);
238
240 d.setChannels(dst, dstWeight);
241
242 traverseChannels(s, d);
243 }
244
245
246
247 static inline
248 void adaptCoordinateHandler(const Channel & src, CoordinateHandler2D & handler){
249 handler.set(src.getGeometry(), src.getCoordinatePolicy());
250 // handler.setLimits(src.getWidth(), src.getHeight());
251 // handler.setPolicy(src.getCoordinatePolicy());
252 }
253
254
255
256
257protected:
258
259
261 virtual inline
262 void initializeParameters(const ImageFrame &src, const ImageFrame &dst) const {
263 drain::Logger mout(getImgLog(), __FILE__, __FUNCTION__); // name+"(ImageOp) src,dst"
264 mout.debug("nothing defined (ok)" );
265 }
266
268 inline virtual
269 void initializeParameters(const ImageFrame &src, const ImageFrame &src2, const ImageFrame &dst) const {
270 //drain::Logger mout(getImgLog(), __FILE__, __FUNCTION__); //REPL getImgLog(), name+"(ImageOp) src,src2,dst", __FUNCTION__);
271 drain::Logger mout(getImgLog(), __FILE__, __FUNCTION__);
272 mout.debug("nothing defined (ok)" );
273 }
274
275
276};
277
278
279
280inline
281std::ostream & operator<<(std::ostream & ostr, const ImageOp &op){
282 //op.help(ostr); // todo: name & params only
283 ostr << op.getName() << '(' << op.getParameters() << ')';
284 return ostr;
285}
286
287} // namespace image
288
289} // namespace drain
290
291
292#endif /* IMAGE_OP_H_ */
293
294// Drain
virtual const std::string & getName() const
Return the name of an instance.
Definition BeanLike.h:82
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:312
Logger & debug(const TT &... args)
Debug information.
Definition Log.h:666
Logger & note(const TT &... args)
For top-level information.
Definition Log.h:489
Image with static geometry.
Definition ImageChannel.h:60
Definition CoordinateHandler.h:77
Struct for image (excluding data)
Definition ImageConf.h:333
Image with static geometry.
Definition ImageFrame.h:67
const CoordinatePolicy & getCoordinatePolicy() const
Coord policy.
Definition ImageLike.h:167
Class for operations that modify an existing image instead of producing a new image.
Definition ImageMod.h:52
virtual int srcAlpha() const
Tell if alpha channel(s) is required in input.
Definition ImageMod.h:66
Base class for image processing functions.
Definition ImageOp.h:49
virtual void traverseChannel(const Channel &src, Channel &dst) const
Apply to single channel.
Definition ImageOp.h:107
virtual void makeCompatible(const ImageConf &src, Image &dst) const
Depending on the operator, modifies the geometry and type of dst.
Definition ImageOp.cpp:54
void traverseChannelsEqually(const ImageTray< const Channel > &src, ImageTray< Channel > &dst) const
Calls processWithTemp() if the frames overlap.
Definition ImageOp.cpp:414
virtual void process(const ImageFrame &src, Image &dst) const
Main interface. Typically splits processing to each channel.
Definition ImageOp.cpp:126
ImageOp(const ImageOp &op)
Definition ImageOp.h:169
virtual void process(Image &dst) const override final
Run this modifier for an image.
Definition ImageOp.h:70
virtual void initializeParameters(const ImageFrame &src, const ImageFrame &dst) const
Set applicable internal parameters before calling traverse().
Definition ImageOp.h:262
void traverseChannelsRepeated(const ImageTray< const Channel > &src, ImageTray< Channel > &dst) const
Recycle channels until all dst channels completed.
Definition ImageOp.cpp:454
ImageOp(const std::string &name=__FUNCTION__, const std::string &description="")
Definition ImageOp.h:162
void traverseAsChannelTrays(const ImageFrame &src, const ImageFrame &srcWeight, ImageFrame &dst, ImageFrame &dstWeight) const
Redirect to processing as trays. This is the opposite of processChannels...() functions.
Definition ImageOp.h:231
void traverseAsChannelTrays(const ImageFrame &src, ImageFrame &dst) const
Redirect to processing as trays. This is the opposite of processChannels...() functions.
Definition ImageOp.h:216
void traverseChannelsSeparately(const ImageTray< const Channel > &src, ImageTray< Channel > &dst) const
Process each (src,dst) channel pair independently. Raise error if their counts differ.
Definition ImageOp.cpp:340
virtual void makeCompatible2(const ImageFrame &src1, const ImageFrame &src2, Image &dst) const
Modifies the geometry and type of dst to fit the computation result.
Definition ImageOp.cpp:104
virtual void initializeParameters(const ImageFrame &src, const ImageFrame &src2, const ImageFrame &dst) const
Set applicable internal parameters before calling traverse().
Definition ImageOp.h:269
virtual void traverseChannel(const Channel &src, const Channel &srcAlpha, Channel &dst, Channel &dstAlpha) const
Apply to single channel with alpha.
Definition ImageOp.h:113
virtual void getDstConf(const ImageConf &src, ImageConf &dst) const
Given source image, determine respective dest image configuration.
Definition ImageOp.cpp:42
virtual void help(std::ostream &ostr=std::cout) const
Prints name, description and parameters using BeanLike::toOStr(). Virtual, so derived classes may ext...
Definition ImageOp.h:151
Container applicable for Channels and Images, with alpha support.
Definition ImageTray.h:267
void setChannels(T2 &img)
Splits.
Definition ImageTray.h:402
Class for multi-channel digital images. Supports dynamic typing with base types (char,...
Definition Image.h:184
Definition DataSelector.cpp:1277