ImageOp.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood 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 
40 namespace drain {
41 
42 namespace image {
43 
45 
49 class ImageOp : public ImageMod {
50 
51 public:
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
70  void process(const ImageFrame & src, const ImageFrame & srcWeight, Image & dst, Image & dstWeight) const;
71 
72  /*
73  virtual inline
74  void process(const ImageTray<const Channel> & src, ImageTray<Image> & dst) const {
75  processConditional(src, dst, true);
76  }
77  */
78 
79 
81 
84  virtual
85  void process(const ImageTray<const Channel> & src, ImageTray<Image> & dst, bool checkOverlap = true) const; // could be Tray<ModifiableChannel> ?
86 
87  // Note: only a default implementation.
88  virtual inline
89  void traverseChannels(const ImageTray<const Channel> & src, ImageTray<Channel> & dst) const {
90  drain::Logger mout(getImgLog(), __FILE__, __FUNCTION__); //REPL this->name+"(ImageOp::)[const ChannelTray &, ChannelTray &]", __FUNCTION__);
91  mout.error() << "not implemented; consider processChannels[Equally|Repeated|Separately]" << mout.endl;
92  // traverseChannelsEqually(src, dst);
93  // traverseChannelsRepeated(src, dst);
94  // traverseChannelsSeparately(src, dst);
95  // traverseMultiChannel(src, dst);
96  }
97 
98 
100  virtual inline
101  void traverseChannel(const Channel & src, Channel &dst) const {
102  throw std::runtime_error(name+"(ImageOp)::"+__FUNCTION__+"(src,dst) unimplemented.");
103  };
104 
106  virtual inline
107  void traverseChannel(const Channel &src, const Channel &srcAlpha, Channel &dst, Channel &dstAlpha) const {
108  //throw std::runtime_error(name+"(ImageOp)::"+__FUNCTION__+"(src,srcAlpha,dst,dstAlpha) unimplemented.");
109  Logger mout(getImgLog(), __FILE__, __FUNCTION__);
110  mout.note("using image channels only (discarding alpha channels)" );
111  traverseChannel(src, dst);
112  };
113 
115 
120  virtual
121  void getDstConf(const ImageConf & src, ImageConf & dst) const;
122 
123 
125  /* The old policy was to have this default implementation
126  * -# returns immediately, if dst==src.
127  * -# sets dst type, if unset, to that of src
128  * -# sets dst geometry to that of src
129  */
130  virtual // TODO: non-virtual, ie, final!
131  void makeCompatible(const ImageConf & src, Image & dst) const;
132 
134  /*
135  * This default implementation
136  * - returns immediately, if dst has overlap with src1 or src2.
137  * - else, calls makeCompatible(src1, dst); src2 has no effect.
138  */
139  virtual
140  void makeCompatible2(const ImageFrame & src1, const ImageFrame & src2, Image & dst) const;
141 
142 
144  virtual inline
145  void help(std::ostream & ostr = std::cout) const {
146  toStream(ostr);
147  }
148 
149 protected:
150 
155  inline // = std::string(__FUNCTION__).substr(0, ::strlen(__FUNCTION__)-2
156  ImageOp(const std::string &name = __FUNCTION__, const std::string &description="") : ImageMod(name, description){
157  };
158 
162  inline
163  ImageOp(const ImageOp & op) : ImageMod(op.name, op.description){
164  };
165 
166  virtual
167  bool processOverlappingWithTemp(const ImageFrame & src, Image & dst) const;
168 
169  virtual
170  bool processOverlappingWithTemp(const ImageTray<const Channel> & src, ImageTray<Image> & dst) const;
171 
172 
173  // Final ?
174  bool traverseOverlappingWithTemp(const Channel & src, Channel & dst) const;
175 
176  // Final ?
177  bool traverseOverlappingWithTemp(const Channel & src, const Channel & srcWeight, Channel & dst, Channel & dstWeight) const;
178 
179 
181  // Final
183  //bool processOverlappingWithTemp(const ImageFrame & srcFrame, Image & dstImage) const; // needed?
184 
186  // Final
187  //bool processOverlappingWithTemp(const ImageFrame & src, const ImageFrame & srcWeight, Image & dst, Image & dstWeight) const; // needed?
188 
190  //virtual void traverse rame(const ImageFrame & src, ImageFrame & dst) const;
191  // USE
192 
194 
198 
201 
203 
206 
207 
209  inline
210  void traverseAsChannelTrays(const ImageFrame &src, ImageFrame &dst) const {
211  Logger mout(getImgLog(), __FILE__, __FUNCTION__);
212  mout.debug("restoring to trays" );
213 
215  s.setChannels(src);
216 
218  d.setChannels(dst);
219 
220  traverseChannels(s, d);
221  }
222 
224  inline
225  void traverseAsChannelTrays(const ImageFrame &src, const ImageFrame & srcWeight, ImageFrame &dst, ImageFrame &dstWeight) const {
226 
227  Logger mout(getImgLog(), __FILE__, __FUNCTION__);
228  mout.debug("restoring to trays" );
229 
231  s.setChannels(src, srcWeight);
232 
234  d.setChannels(dst, dstWeight);
235 
236  traverseChannels(s, d);
237  }
238 
239 
240 
241  static inline
242  void adaptCoordinateHandler(const Channel & src, CoordinateHandler2D & handler){
243  handler.set(src.getGeometry(), src.getCoordinatePolicy());
244  // handler.setLimits(src.getWidth(), src.getHeight());
245  // handler.setPolicy(src.getCoordinatePolicy());
246  }
247 
248 
249 
250 
251 protected:
252 
253 
255  virtual inline
256  void initializeParameters(const ImageFrame &src, const ImageFrame &dst) const {
257  drain::Logger mout(getImgLog(), __FILE__, __FUNCTION__); // name+"(ImageOp) src,dst"
258  mout.debug("nothing defined (ok)" );
259  }
260 
262  inline virtual
263  void initializeParameters(const ImageFrame &src, const ImageFrame &src2, const ImageFrame &dst) const {
264  //drain::Logger mout(getImgLog(), __FILE__, __FUNCTION__); //REPL getImgLog(), name+"(ImageOp) src,src2,dst", __FUNCTION__);
265  drain::Logger mout(getImgLog(), __FILE__, __FUNCTION__);
266  mout.debug("nothing defined (ok)" );
267  }
268 
269 
270 };
271 
272 
273 
274 inline
275 std::ostream & operator<<(std::ostream & ostr, const ImageOp &op){
276  //op.help(ostr); // todo: name & params only
277  ostr << op.getName() << '(' << op.getParameters() << ')';
278  return ostr;
279 }
280 
281 } // namespace image
282 
283 } // namespace drain
284 
285 
286 #endif /* IMAGE_OP_H_ */
287 
288 // Drain
virtual const std::string & getName() const
Return the name of an instance.
Definition: BeanLike.h:69
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:308
Logger & error(const TT &... args)
Echoes.
Definition: Log.h:412
Logger & note(const TT &... args)
For top-level information.
Definition: Log.h:485
Logger & debug(const TT &... args)
Public, yet typically used "internally", when TIMING=true.
Definition: Log.h:676
Image with static geometry.
Definition: ImageChannel.h:60
Definition: CoordinateHandler.h:62
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:101
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:163
virtual void initializeParameters(const ImageFrame &src, const ImageFrame &dst) const
Set applicable internal parameters before calling traverse().
Definition: ImageOp.h:256
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:156
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:225
void traverseAsChannelTrays(const ImageFrame &src, ImageFrame &dst) const
Redirect to processing as trays. This is the opposite of processChannels...() functions.
Definition: ImageOp.h:210
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:263
virtual void traverseChannel(const Channel &src, const Channel &srcAlpha, Channel &dst, Channel &dstAlpha) const
Apply to single channel with alpha.
Definition: ImageOp.h:107
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:145
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