SMIL  0.9.1
DLineArith.hpp
1 /*
2  * Copyright (c) 2011-2016, Matthieu FAESSEL and ARMINES
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Matthieu FAESSEL, or ARMINES nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 
30 #ifndef _D_LINE_ARITH_HPP
31 #define _D_LINE_ARITH_HPP
32 
33 
34 #include "DBaseLineOperations.hpp"
35 
36 
37 namespace smil
38 {
39 
48  template <class T1, class T2>
49  inline void copyLine(const typename unaryLineFunctionBase<T1>::lineInType lIn, const size_t size, typename unaryLineFunctionBase<T2>::lineInType lOut)
50  {
51  for (size_t i=0;i<size;i++)
52  lOut[i] = lIn[i];
53  }
54 
55  template <class T>
56  inline void copyLine(const typename unaryLineFunctionBase<T>::lineInType lIn, const size_t size, typename unaryLineFunctionBase<T>::lineInType lOut)
57  {
58  for (size_t i=0;i<size;i++)
59  lOut[i] = lIn[i];
60  }
61 
62 
63  template <class T>
64  struct fillLine : public unaryLineFunctionBase<T>
65  {
66  typedef typename unaryLineFunctionBase<T>::lineType lineType;
67  fillLine() {}
68  fillLine(const lineType lIn, const size_t size, const T value) { this->_exec(lIn, size, value); }
69 
70  virtual void _exec(const lineType lIn, const size_t size, lineType lOut)
71  {
72  return this->fill(lIn, size, lOut);
73  }
74  virtual void _exec(lineType lInOut, const size_t size, const T value)
75  {
76  return this->fill(lInOut, size, value);
77  }
78  static void fill(const lineType lIn, const size_t size, lineType lOut)
79  {
80  for (size_t i=0;i<size;i++)
81  lOut[i] = lIn[i];
82  }
83  static void fill(lineType lInOut, const size_t size, const T value)
84  {
85  for (size_t i=0;i<size;i++)
86  lInOut[i] = value;
87  }
88  };
89 
90  template <class T>
91  inline void shiftLine(const typename unaryLineFunctionBase<T>::lineInType lIn, int dx, size_t lineLen, typename unaryLineFunctionBase<T>::lineInType lOut, T borderValue = ImDtTypes<T>::min())
92  {
93  if (dx==0)
94  copyLine<T>(lIn, lineLen, lOut);
95  else if (dx>0)
96  {
97  fillLine<T>::fill(lOut, dx, borderValue);
98  typename Image<T>::restrictLineType tmpL = lOut+dx;
99  copyLine<T>(lIn, lineLen-dx, tmpL);
100  }
101  else
102  {
103  typename Image<T>::restrictLineType tmpL = lIn-dx;
104  copyLine<T>(tmpL, lineLen+dx, lOut);
105  fillLine<T>::fill(lOut+(lineLen+dx), -dx, borderValue);
106  }
107  }
108 
109  template <class T>
110  struct invLine : public unaryLineFunctionBase<T>
111  {
112  typedef typename unaryLineFunctionBase<T>::lineType lineType;
113  inline void _exec(const lineType lineIn, const size_t size, lineType lOut)
114  {
115 
116  for (size_t i=0;i<size;i++)
117  lOut[i] = ImDtTypes<T>::max() - lineIn[i] - ImDtTypes<T>::min();
118  }
119  };
120 
121  template <class T>
122  struct addLine : public binaryLineFunctionBase<T>
123  {
124  typedef typename binaryLineFunctionBase<T>::lineType lineType;
125  virtual void _exec(const lineType lIn1, const lineType lIn2, const size_t size, lineType lOut)
126  {
127  for (size_t i=0;i<size;i++)
128  lOut[i] = lIn1[i] > (T)(ImDtTypes<T>::max()- lIn2[i]) ? ImDtTypes<T>::max() : lIn1[i] + lIn2[i];
129  }
130  };
131 
132  template <class T>
134  {
135  typedef typename binaryLineFunctionBase<T>::lineType lineType;
136  virtual void _exec(const lineType lIn1, const lineType lIn2, const size_t size, lineType lOut)
137  {
138  for (size_t i=0;i<size;i++)
139  lOut[i] = lIn1[i] + lIn2[i];
140  }
141  };
142 
143  template <class T>
144  struct subLine : public binaryLineFunctionBase<T>
145  {
146  typedef typename binaryLineFunctionBase<T>::lineType lineType;
147  virtual void _exec(const lineType lIn1, const lineType lIn2, const size_t size, lineType lOut)
148  {
149  for (size_t i=0;i<size;i++)
150  lOut[i] = lIn1[i] < (T)(ImDtTypes<T>::min() + lIn2[i]) ? ImDtTypes<T>::min() : lIn1[i] - lIn2[i];
151  }
152  };
153 
154  template <class T>
156  {
157  typedef typename binaryLineFunctionBase<T>::lineType lineType;
158  virtual void _exec(const lineType lIn1, const lineType lIn2, const size_t size, lineType lOut)
159  {
160  for (size_t i=0;i<size;i++)
161  lOut[i] = lIn1[i] - lIn2[i];
162  }
163  };
164 
165  template <class T>
166  struct supLine : public binaryLineFunctionBase<T>
167  {
168  typedef typename binaryLineFunctionBase<T>::lineType lineType;
169  virtual void _exec(const lineType lIn1, const lineType lIn2, const size_t size, lineType lOut)
170  {
171  for (size_t i=0;i<size;i++)
172  lOut[i] = lIn1[i] > lIn2[i] ? lIn1[i] : lIn2[i];
173  }
174  };
175 
176  template <class T>
177  struct infLine : public binaryLineFunctionBase<T>
178  {
179  typedef typename binaryLineFunctionBase<T>::lineType lineType;
180  virtual void _exec(const lineType lIn1, const lineType lIn2, const size_t size, lineType lOut)
181  {
182  for (size_t i=0;i<size;i++)
183  lOut[i] = lIn1[i] < lIn2[i] ? lIn1[i] : lIn2[i];
184  }
185  };
186 
187  template <class T>
188  struct grtLine : public binaryLineFunctionBase<T>
189  {
190  grtLine()
191  : trueVal(ImDtTypes<T>::max()), falseVal(0) {}
192 
193  T trueVal, falseVal;
194 
195  typedef typename binaryLineFunctionBase<T>::lineType lineType;
196  virtual void _exec(const lineType lIn1, const lineType lIn2, const size_t size, lineType lOut)
197  {
198  T _trueVal(trueVal), _falseVal(falseVal);
199  for (size_t i=0;i<size;i++)
200  lOut[i] = lIn1[i] > lIn2[i] ? _trueVal : _falseVal;
201  }
202  };
203 
204  template <class T_in, class T_out=T_in>
205  struct grtSupLine : public binaryLineFunctionBase<T_in, T_in, T_out>
206  {
207  grtSupLine()
208  : trueVal(ImDtTypes<T_out>::max()), falseVal(0) {}
209 
210  T_out trueVal, falseVal;
211 
213  typedef typename parentClass::lineType lineInType;
214  typedef typename parentClass::lineOutType lineOutType;
215 
216  virtual void _exec(const lineInType lIn1, const lineInType lIn2, const size_t size, lineOutType lOut)
217  {
218  T_out _trueVal(trueVal), _falseVal(falseVal);
219  for (size_t i=0;i<size;i++)
220  lOut[i] |= lIn1[i] > lIn2[i] ? _trueVal : _falseVal;
221  }
222  };
223 
224  template <class T>
226  {
227  grtOrEquLine()
228  : trueVal(ImDtTypes<T>::max()), falseVal(0) {}
229 
230  T trueVal, falseVal;
231 
232  typedef typename binaryLineFunctionBase<T>::lineType lineType;
233  virtual void _exec(const lineType lIn1, const lineType lIn2, const size_t size, lineType lOut)
234  {
235  T _trueVal(trueVal), _falseVal(falseVal);
236  for (size_t i=0;i<size;i++)
237  lOut[i] = lIn1[i] >= lIn2[i] ? _trueVal : _falseVal;
238  }
239  };
240 
241  template <class T_in, class T_out=T_in>
242  struct grtOrEquSupLine : public binaryLineFunctionBase<T_in, T_in, T_out>
243  {
244  grtOrEquSupLine()
245  : trueVal(ImDtTypes<T_out>::max()), falseVal(0) {}
246 
247  T_out trueVal, falseVal;
248 
250  typedef typename parentClass::lineType lineInType;
251  typedef typename parentClass::lineOutType lineOutType;
252 
253  virtual void _exec(const lineInType lIn1, const lineInType lIn2, const size_t size, lineOutType lOut)
254  {
255  T_out _trueVal(trueVal), _falseVal(falseVal);
256  for (size_t i=0;i<size;i++)
257  lOut[i] |= lIn1[i] >= lIn2[i] ? _trueVal : _falseVal;
258  }
259  };
260 
261  template <class T>
262  struct lowLine : public binaryLineFunctionBase<T>
263  {
264  lowLine()
265  : trueVal(ImDtTypes<T>::max()), falseVal(0) {}
266 
267  T trueVal, falseVal;
268 
269  typedef typename binaryLineFunctionBase<T>::lineType lineType;
270  virtual void _exec(const lineType lIn1, const lineType lIn2, const size_t size, lineType lOut)
271  {
272  T _trueVal(trueVal), _falseVal(falseVal);
273  for (size_t i=0;i<size;i++)
274  lOut[i] = lIn1[i] < lIn2[i] ? _trueVal : _falseVal;
275  }
276  };
277 
278  template <class T_in, class T_out=T_in>
279  struct lowSupLine : public binaryLineFunctionBase<T_in, T_in, T_out>
280  {
281  lowSupLine()
282  : trueVal(ImDtTypes<T_out>::max()), falseVal(0) {}
283 
284  T_out trueVal, falseVal;
285 
287  typedef typename parentClass::lineType lineInType;
288  typedef typename parentClass::lineOutType lineOutType;
289 
290  virtual void _exec(const lineInType lIn1, const lineInType lIn2, const size_t size, lineOutType lOut)
291  {
292  T_out _trueVal(trueVal), _falseVal(falseVal);
293  for (size_t i=0;i<size;i++)
294  lOut[i] |= lIn1[i] < lIn2[i] ? _trueVal : _falseVal;
295  }
296  };
297 
298  template <class T>
300  {
301  lowOrEquLine()
302  : trueVal(ImDtTypes<T>::max()), falseVal(0) {}
303 
304  T trueVal, falseVal;
305 
306  typedef typename binaryLineFunctionBase<T>::lineType lineType;
307  virtual void _exec(const lineType lIn1, const lineType lIn2, const size_t size, lineType lOut)
308  {
309  T _trueVal(trueVal), _falseVal(falseVal);
310  for (size_t i=0;i<size;i++)
311  lOut[i] = lIn1[i] <= lIn2[i] ? _trueVal : _falseVal;
312  }
313  };
314 
315  template <class T_in, class T_out=T_in>
316  struct lowOrEquSupLine : public binaryLineFunctionBase<T_in, T_in, T_out>
317  {
318  lowOrEquSupLine()
319  : trueVal(ImDtTypes<T_out>::max()), falseVal(0) {}
320 
321  T_out trueVal, falseVal;
322 
324  typedef typename parentClass::lineType lineInType;
325  typedef typename parentClass::lineOutType lineOutType;
326 
327  virtual void _exec(const lineInType lIn1, const lineInType lIn2, const size_t size, lineOutType lOut)
328  {
329  T_out _trueVal(trueVal), _falseVal(falseVal);
330  for (size_t i=0;i<size;i++)
331  lOut[i] |= lIn1[i] <= lIn2[i] ? _trueVal : _falseVal;
332  }
333  };
334 
335 
336  template <class T>
337  struct equLine : public binaryLineFunctionBase<T>
338  {
339  equLine()
340  : trueVal(ImDtTypes<T>::max()), falseVal(0) {}
341 
342  T trueVal, falseVal;
343 
344  typedef typename binaryLineFunctionBase<T>::lineType lineType;
345  virtual void _exec(const lineType lIn1, const lineType lIn2, const size_t size, lineType lOut)
346  {
347  T _trueVal(trueVal), _falseVal(falseVal);
348  for (size_t i=0;i<size;i++)
349  lOut[i] = lIn1[i] == lIn2[i] ? _trueVal : _falseVal;
350  }
351  };
352 
353  template <class T>
354  struct diffLine : public binaryLineFunctionBase<T>
355  {
356  diffLine()
357  : trueVal(ImDtTypes<T>::max()), falseVal(0) {}
358 
359  T trueVal, falseVal;
360 
361  typedef typename binaryLineFunctionBase<T>::lineType lineType;
362  virtual void _exec(const lineType lIn1, const lineType lIn2, const size_t size, lineType lOut)
363  {
364  T _trueVal(trueVal), _falseVal(falseVal);
365  for (size_t i=0;i<size;i++)
366  lOut[i] = (lIn1[i] == lIn2[i]) ? _falseVal : _trueVal;
367  }
368  };
369 
370  template <class T_in, class T_out=T_in>
371  struct equSupLine : public binaryLineFunctionBase<T_in, T_in, T_out>
372  {
373  equSupLine()
374  : trueVal(ImDtTypes<T_out>::max()), falseVal(0) {}
375 
376  T_out trueVal, falseVal;
377 
379  typedef typename parentClass::lineType lineInType;
380  typedef typename parentClass::lineOutType lineOutType;
381 
382  virtual void _exec(const lineInType lIn1, const lineInType lIn2, const size_t size, lineOutType lOut)
383  {
384  T_out _trueVal(trueVal), _falseVal(falseVal);
385  for (size_t i=0;i<size;i++)
386  lOut[i] |= lIn1[i] == lIn2[i] ? _trueVal : _falseVal;
387  }
388  };
389 
390 
391  /*
392  * Difference ("vertical distance") between two lines.
393  *
394  * Returns abs(p1-p2) for each pixels pair
395  */
396 
397  template <class T>
399  {
400  typedef typename binaryLineFunctionBase<T>::lineType lineType;
401  virtual void _exec(const lineType lIn1, const lineType lIn2, const size_t size, lineType lOut)
402  {
403  for (size_t i=0;i<size;i++)
404  lOut[i] = lIn1[i] > lIn2[i] ? lIn1[i]-lIn2[i] : lIn2[i]-lIn1[i];
405  }
406  };
407 
408 
409  template <class T>
410  struct mulLine : public binaryLineFunctionBase<T>
411  {
412  typedef typename binaryLineFunctionBase<T>::lineType lineType;
413  virtual void _exec(const lineType lIn1, const lineType lIn2, const size_t size, lineType lOut)
414  {
415  for (size_t i=0;i<size;i++)
416  lOut[i] = double(lIn1[i]) * double(lIn2[i]) > double(ImDtTypes<T>::max()) ? ImDtTypes<T>::max() : lIn1[i] * lIn2[i];
417  }
418  };
419 
420  template <class T>
422  {
423  typedef typename binaryLineFunctionBase<T>::lineType lineType;
424  virtual void _exec(const lineType lIn1, const lineType lIn2, const size_t size, lineType lOut)
425  {
426  for (size_t i=0;i<size;i++)
427  lOut[i] = (T)(lIn1[i] * lIn2[i]);
428  }
429  };
430 
431  template <class T>
432  struct divLine : public binaryLineFunctionBase<T>
433  {
434  typedef typename binaryLineFunctionBase<T>::lineType lineType;
435  virtual void _exec(const lineType lIn1, const lineType lIn2, const size_t size, lineType lOut)
436  {
437  for (size_t i=0;i<size;i++)
438  {
439  lOut[i] = lIn2[i]==T(0) ? ImDtTypes<T>::max() : lIn1[i] / lIn2[i];
440  }
441  }
442  };
443 
444  template <class T>
445  struct logLine : public unaryLineFunctionBase<T>
446  {
447  typedef typename unaryLineFunctionBase<T>::lineType lineType;
448  int base;
449  logLine() : base(0) {}
450  virtual void _exec(const lineType lIn, const size_t size, lineType lOut)
451  {
452  if (base!=0)
453  {
454  double baseLog = std::log(double(base));
455  for (size_t i=0;i<size;i++)
456  lOut[i] = std::log(lIn[i]) / baseLog;
457  }
458  else
459  {
460  for (size_t i=0;i<size;i++)
461  lOut[i] = std::log(lIn[i]);
462  }
463  }
464  };
465 
466  template <class T>
468  {
469  typedef typename binaryLineFunctionBase<T>::lineType lineType;
470  virtual void _exec(const lineType lIn1, const lineType lIn2, const size_t size, lineType lOut)
471  {
472  for (size_t i=0;i<size;i++)
473  lOut[i] = (T)(lIn1[i] && lIn2[i]);
474  }
475  };
476 
477  template <class T>
479  {
480  typedef typename binaryLineFunctionBase<T>::lineType lineType;
481  virtual void _exec(const lineType lIn1, const lineType lIn2, const size_t size, lineType lOut)
482  {
483  for (size_t i=0;i<size;i++)
484  lOut[i] = (T)(lIn1[i] & lIn2[i]);
485  }
486  };
487 
488  template <class T>
490  {
491  typedef typename binaryLineFunctionBase<T>::lineType lineType;
492  virtual void _exec(const lineType lIn1, const lineType lIn2, const size_t size, lineType lOut)
493  {
494  for (size_t i=0;i<size;i++)
495  lOut[i] = (T)(lIn1[i] || lIn2[i]);
496  }
497  };
498 
499  template <class T>
501  {
502  typedef typename binaryLineFunctionBase<T>::lineType lineType;
503  virtual void _exec(const lineType lIn1, const lineType lIn2, const size_t size, lineType lOut)
504  {
505  for (size_t i=0;i<size;i++)
506  lOut[i] = (T)(lIn1[i] | lIn2[i]);
507  }
508  };
509 
510  template <class T>
512  {
513  typedef typename binaryLineFunctionBase<T>::lineType lineType;
514  virtual void _exec(const lineType lIn1, const lineType lIn2, const size_t size, lineType lOut)
515  {
516  for (size_t i=0;i<size;i++)
517  lOut[i] = (T)((lIn1[i] && !lIn2[i]) || (!lIn1[i] && lIn2[i]));
518  }
519  };
520 
521  template <class T>
523  {
524  typedef typename binaryLineFunctionBase<T>::lineType lineType;
525  virtual void _exec(const lineType lIn1, const lineType lIn2, const size_t size, lineType lOut)
526  {
527  for (size_t i=0;i<size;i++)
528  lOut[i] = (T)(lIn1[i] ^ lIn2[i]);
529  }
530  };
531 
532  template <class T>
534  {
535  typedef typename unaryLineFunctionBase<T>::lineType lineType;
536  virtual void _exec(const lineType, const size_t, lineType) {}
537  virtual void _exec(const lineType lIn1, const UINT shift , const size_t size, lineType lOut)
538  {
539  for (size_t i=0;i<size;i++)
540  lOut[i] = (T)(lIn1[i] << shift);
541  }
542  };
543 
544  template <class T>
546  {
548  typedef typename unaryLineFunctionBase<T>::lineType lineType;
549  virtual void _exec(const lineType, const size_t, lineType) {}
550  virtual void _exec(const lineType lIn1, const UINT shift , const size_t size, lineType lOut)
551  {
552  for (size_t i=0;i<size;i++)
553  lOut[i] = (T)(lIn1[i] >> shift);
554  }
555  };
556 
557  template <class T1, class T2>
558  struct testLine : public tertiaryLineFunctionBase<T1,T2,T2,T2>
559  {
561  typedef typename parentClass::lineType1 lineType1;
562  typedef typename parentClass::lineOutType lineType2;
563 
564  virtual void _exec(const lineType1 lIn1, const lineType2 lIn2, const lineType2 lIn3, const size_t size, lineType2 lOut)
565  {
566  for (size_t i=0;i<size;i++)
567  {
568  lOut[i] = lIn1[i] ? lIn2[i] : lIn3[i];
569  }
570  }
571  };
572 
575 } // namespace smil
576 
577 
578 #endif // _D_LINE_ARITH_HPP
Definition: DLineArith.hpp:478
Definition: DColorConvert.h:38
Definition: DLineArith.hpp:545
Definition: DLineArith.hpp:398
Definition: DLineArith.hpp:533
Definition: DLineArith.hpp:371
Definition: DLineArith.hpp:445
Definition: DLineArith.hpp:166
Definition: DLineArith.hpp:500
Definition: DLineArith.hpp:242
RES_T log(const Image< T > &imIn, Image< T > &imOut, int base=0)
Logarithm.
Definition: DImageArith.hpp:833
Definition: DLineArith.hpp:188
Definition: DBaseLineOperations.hpp:45
Definition: DLineArith.hpp:421
Definition: DLineArith.hpp:467
Definition: DLineArith.hpp:144
Definition: DBaseImageOperations.hxx:39
void copyLine(const typename unaryLineFunctionBase< T1 >::lineInType lIn, const size_t size, typename unaryLineFunctionBase< T2 >::lineInType lOut)
copy line
Definition: DLineArith.hpp:49
Definition: DLineArith.hpp:511
Definition: DLineArith.hpp:225
Definition: DLineArith.hpp:354
Definition: DLineArith.hpp:155
Definition: DLineArith.hpp:262
Definition: DLineArith.hpp:522
Definition: DTypes.hpp:78
Definition: DLineArith.hpp:558
Definition: DLineArith.hpp:337
Definition: DLineArith.hpp:205
Definition: DLineArith.hpp:177
Definition: DBaseLineOperations.hpp:109
Definition: DLineArith.hpp:489
Definition: DLineArith.hpp:279
Definition: DLineArith.hpp:299
Definition: DLineArith.hpp:410
Definition: DBaseLineOperations.hpp:186
Definition: DLineArith.hpp:122
Definition: DLineArith.hpp:316
Definition: DLineArith.hpp:133
Definition: DLineArith.hpp:110
Definition: DLineArith.hpp:432