MotionIllustratorOp.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 MOTIONIllustrator_OP_H_
32 #define MOTIONIllustrator_OP_H_
33 
34 
35 #include <math.h>
36 
37 #include "ImageOp.h"
38 
39 namespace drain
40 {
41 
42 namespace image
43 {
44 
45 
53 template <class T=unsigned char,class T2=unsigned char>
54 class MotionIllustratorOp : public ImageOp<T,T2>
55 {
56 public:
57 
58 
59  MotionIllustratorOp(const std::string &p="10,10,1.0,1.0,0.0") : ImageOp<T,T2>("MotionIllustratorOp",
60  "Illustrates an image using a motion field in injection.",
61  "m,n,scale,contrast,brightness",p){};
62 
66  void process(const ImageT<T> &motion,ImageT<T2> &dst) const {
67  process(motion,dst,dst);
68  };
69 
73  void process(const ImageT<T> &motion,ImageT<T2> &src,ImageT<T2> &dst) const {
74 
75  const int width = src.getWidth();
76  const int height = src.getHeight();
77 
78  const int m = this->getParameter("m",20);
79  const int n = this->getParameter("n",20);
80 
81  const float scale = this->getParameter("scale",1.0f);
82 
83  // Background image
84  const float contrast = this->getParameter("contrast",1.0f);
85  const float brightness = this->getParameter("brightness",0.0f);
87  op.setGrayPalette(3,0,brightness,contrast);
88  op.process(src,dst);
89 
90  const ImageT<T> &u = motion.getChannel(0);
91  const ImageT<T> &v = motion.getChannel(1);
92  const ImageT<T> &w = motion.getChannel(motion.getChannelCount()-1);
93 
94  const bool weighted = (motion.getChannelCount()>2);
95 
96  CoordinateHandler c(width,height);
97  Point2D<int> p;
98  unsigned int a;
99  int i2,j2,magn; //,l;
100  const int iOffset = width/m/2;
101  const int jOffset = height/n/2;
102  float u2,v2,w2;
103 
104 
105 
106  for (int j = 0; j < n; ++j) {
107  for (int i = 0; i < m; ++i) {
108 
109  //std::cerr << "spot " << i << '\t' << j << '\n';
110  i2 = (width*i)/m + iOffset;
111  j2 = (height*j)/n + jOffset;
112  a = src.address(i2,j2);
113  dst.at(a) = 128;
114  dst.at(i2+1,j2+0) = 128;
115  dst.at(i2+0,j2+1) = 128;
116  dst.at(i2-1,j2+0) = 128;
117  dst.at(i2+0,j2-1) = 128;
118  //dst.at(i2,j2,1) = 255;
119 
120  u2 = u.at(a);
121  v2 = v.at(a);
122  magn = static_cast<int>(scale * sqrt(u2*u2 + v2*v2));
123  for (int k=0; k<magn; k++){
124  p.setLocation(i2 + (u2*k)/magn,j2 + (v2*k)/magn);
125  //if ((i==5)&&(j==5))std::cerr << p << '\n';
126  // Still inside image?
127  //if (true){ //
128  if (c.handle(p) == CoordinateHandler::UNCHANGED){
129  w2 = weighted ? 255-(255*16)/(16+w.at(a)) : 128;
130  dst.at(p.x,p.y,0) = static_cast<T2>(127+w2/2);
131  dst.at(p.x,p.y,1) = static_cast<T2>(w2);
132  dst.at(p.x,p.y,2) = static_cast<T2>(127+w2/2);
133  }
134  }
135  }
136  }
137 
138 
139  };
140 
141 };
142 
143 }
144 }
145 
146 
147 #endif /*MotionIllustratorOP_H_*/
148 
149 // Drain
size_t address(size_t i) const
Computes the index location from image coordinates. Does not involve bit resolution.
Definition: ImageFrame.h:148
Base class for image processing functions.
Definition: ImageOp.h:49
virtual void process(const ImageFrame &src, Image &dst) const
Main interface. Typically splits processing to each channel.
Definition: ImageOp.cpp:126
A template class for images with static storage type.
Definition: ImageT.h:67
Definition: MotionIllustratorOp.h:55
Colorizes an image of 1 channel to an image of N channels by using a palette image as a lookup table.
Definition: PaletteOp.h:55
Definition: DataSelector.cpp:1277