SMIL  1.0.4
DMultichannelTypes.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_MULTI_CHANNEL_TYPES_H
31 #define _D_MULTI_CHANNEL_TYPES_H
32 
33 
34 #include "Core/include/DTypes.h"
35 #include "Core/include/DErrors.h"
36 
37 #include <cstdarg>
38 
39 namespace smil
40 {
41  template <class T=UINT8, UINT N=3>
42  class MultichannelArray;
43 
44  template <class T, UINT N>
45  class MultichannelArrayItem;
46 
47 // template <class T, UINT N>
48 // class VirtualMultichannelType
49  template <class T, UINT N>
51  {
52  protected:
53  T c[N];
54  public:
55  typedef T DataType;
56  static UINT channelNumber() { return N; }
57 
59  {
60  for (UINT i=0;i<N;i++)
61  c[i] = 0;
62  }
63  MultichannelType(const T &val)
64  {
65  for (UINT i=0;i<N;i++)
66  c[i] = val;
67  }
68  MultichannelType(const T &_r, const T &_g, const T &_b)
69  {
70  c[0] = _r;
71  c[1] = _g;
72  c[2] = _b;
73  }
75  {
76  for (UINT i=0;i<N;i++)
77  c[i] = mc.value(i);
78  }
79  virtual ~MultichannelType() {}
80 
81  MultichannelType& operator =(const MultichannelType &mc)
82  {
83  for (UINT i=0;i<N;i++)
84  this->value(i) = mc.value(i);
85  return *this;
86  }
87 
88  bool operator ==(const MultichannelType &mc) const
89  {
90  for (UINT i=0;i<N;i++)
91  if (this->value(i) != mc.value(i))
92  return false;
93  return true;
94  }
95  bool operator ==(const int &val) const
96  {
97  for (UINT i=0;i<N;i++)
98  if (this->value(i) != val)
99  return false;
100  return true;
101  }
102  bool operator !=(const MultichannelType &mc) const
103  {
104  for (UINT i=0;i<N;i++)
105  if (this->value(i) == mc.value(i))
106  return false;
107  return true;
108  }
109  bool operator !=(const int &val) const
110  {
111  return this->operator!=(MultichannelType(val));
112  }
113  bool operator <(const MultichannelType &mc) const
114  {
115  for (UINT i=0;i<N;i++)
116  if (this->value(i) >= mc.value(i))
117  return false;
118  return true;
119  }
120  bool operator <=(const MultichannelType &mc) const
121  {
122  for (UINT i=0;i<N;i++)
123  if (this->value(i) > mc.value(i))
124  return false;
125  return true;
126  }
127  bool operator >(const MultichannelType &mc) const
128  {
129  for (UINT i=0;i<N;i++)
130  if (this->value(i) <= mc.value(i))
131  return false;
132  return true;
133  }
134  bool operator >=(const MultichannelType &mc) const
135  {
136  for (UINT i=0;i<N;i++)
137  if (this->value(i) < mc.value(i))
138  return false;
139  return true;
140  }
141  MultichannelType operator ~()
142  {
143  MultichannelType newmc;
144  for (UINT i=0;i<N;i++)
145  newmc.value(i) = ~this->value(i);
146  return newmc;
147  }
148 #ifndef SWIG
149  MultichannelType operator !()
150  {
151  MultichannelType newmc;
152  for (UINT i=0;i<N;i++)
153  newmc.value(i) = !this->value(i);
154  return newmc;
155  }
156 #endif // SWIG
157  MultichannelType operator -(const MultichannelType &mc) const
158  {
159  MultichannelType newmc;
160  for (UINT i=0;i<N;i++)
161  newmc.value(i) = this->value(i) - mc.value(i);
162  return newmc;
163  }
164  MultichannelType operator -(const int &val) const
165  {
166  MultichannelType newmc;
167  for (UINT i=0;i<N;i++)
168  newmc.value(i) = this->value(i) - val;
169  return newmc;
170  }
171  MultichannelType operator -(const size_t &val) const
172  {
173  MultichannelType newmc;
174  for (UINT i=0;i<N;i++)
175  newmc.value(i) = this->value(i) - val;
176  return newmc;
177  }
178  MultichannelType operator -()
179  {
180  MultichannelType newmc;
181  for (UINT i=0;i<N;i++)
182  newmc.value(i) = -this->value(i);
183  return newmc;
184  }
185  MultichannelType operator +(const MultichannelType &mc) const
186  {
187  MultichannelType newmc;
188  for (UINT i=0;i<N;i++)
189  newmc.value(i) = this->value(i) + mc.value(i);
190  return newmc;
191  }
192  MultichannelType operator +(const int &val) const
193  {
194  MultichannelType newmc;
195  for (UINT i=0;i<N;i++)
196  newmc.value(i) = this->value(i) + val;
197  return newmc;
198  }
199  MultichannelType& operator +=(const MultichannelType &mc)
200  {
201  for (UINT i=0;i<N;i++)
202  this->value(i) += mc.value(i);
203  return *this;
204  }
205  MultichannelType& operator -=(const MultichannelType &mc)
206  {
207  for (UINT i=0;i<N;i++)
208  this->value(i) -= mc.value(i);
209  return *this;
210  }
211  MultichannelType operator *(const MultichannelType &mc) const
212  {
213  MultichannelType newmc;
214  for (UINT i=0;i<N;i++)
215  newmc.value(i) = this->value(i) * mc.value(i);
216  return newmc;
217  }
218  MultichannelType operator *(const double &val) const
219  {
220  MultichannelType newmc;
221  for (UINT i=0;i<N;i++)
222  newmc.value(i) = static_cast<T>(this->value(i) * val);
223  return newmc;
224  }
225  MultichannelType operator *(const size_t &val) const
226  {
227  MultichannelType newmc;
228  for (UINT i=0;i<N;i++)
229  newmc.value(i) = this->value(i) * val;
230  return newmc;
231  }
232  MultichannelType operator /(const MultichannelType &mc) const
233  {
234  MultichannelType newmc;
235  for (UINT i=0;i<N;i++)
236  newmc.value(i) = this->value(i) / mc.value(i);
237  return newmc;
238  }
239  MultichannelType operator /(const double &val) const
240  {
241  MultichannelType newmc;
242  for (UINT i=0;i<N;i++)
243  newmc.value(i) = static_cast<T>(this->value(i) / val);
244  return newmc;
245  }
246  MultichannelType operator /(const size_t &val) const { return this->operator/(double(val)); }
247  MultichannelType operator &(const MultichannelType &mc)
248  {
249  MultichannelType newmc;
250  for (UINT i=0;i<N;i++)
251  newmc.value(i) = this->value(i) & mc.value(i);
252  return newmc;
253  }
254  MultichannelType operator ^(const MultichannelType &mc)
255  {
256  MultichannelType newmc;
257  for (UINT i=0;i<N;i++)
258  newmc.value(i) = this->value(i) ^ mc.value(i);
259  return newmc;
260  }
261  MultichannelType operator |(const MultichannelType &mc)
262  {
263  MultichannelType newmc;
264  for (UINT i=0;i<N;i++)
265  newmc.value(i) = this->value(i) | mc.value(i);
266  return newmc;
267  }
268  MultichannelType operator |=(const MultichannelType &mc)
269  {
270  for (UINT i=0;i<N;i++)
271  this->value(i) |= mc.value(i);
272  return *this;
273  }
274 #ifndef SWIG
275  MultichannelType& operator ++(int) // postfix
276  {
277  for (UINT i=0;i<N;i++)
278  this->value(i) ++;
279  return *this;
280  }
281  MultichannelType operator &&(const MultichannelType &mc)
282  {
283  MultichannelType newmc;
284  for (UINT i=0;i<N;i++)
285  newmc.value(i) = this->value(i) && mc.value(i);
286  return newmc;
287  }
288  MultichannelType operator ||(const MultichannelType &mc)
289  {
290  MultichannelType newmc;
291  for (UINT i=0;i<N;i++)
292  newmc.value(i) = this->value(i) || mc.value(i);
293  return newmc;
294  }
295  inline T& operator[] (UINT i)
296  {
297  return this->value(i);
298  }
299  inline const T& operator[] (UINT i) const
300  {
301  return this->value(i);
302  }
303 #endif // SWIG
304 
305  operator double() const
306  {
307  double dval = 0;
308  for (UINT i=0;i<N;i++)
309  dval += c[i];
310  return dval/N;
311  }
312 
313  operator int() const { return static_cast<int>(double(*this)); }
314  operator UINT() const { return static_cast<UINT>(double(*this)); }
315 #ifdef USE_64BIT_IDS
316  operator size_t() const { return static_cast<size_t>(double(*this)); }
317 #endif // USE_64BIT_IDS
318  operator UINT8() const { return static_cast<UINT8>(double(*this)); }
319  operator UINT16() const { return static_cast<UINT16>(double(*this)); }
320  operator bool() const {
321  if (double(*this))
322  return true;
323  return false;
324  }
325  operator signed char() const {
326  return static_cast<signed char>(double(*this));
327  }
328  operator char() const { return static_cast<char>(double(*this)); }
329  operator long int() const {
330  return static_cast<long int>(double(*this));
331  }
332  operator short int() const {
333  return static_cast<short int>(double(*this));
334  }
335 
336  virtual const T& value(const UINT &i) const
337  {
338  return c[i];
339  }
340  virtual T& value(const UINT &i)
341  {
342  return c[i];
343  }
344  };
345 
346  template <class T, UINT N>
347  std::ostream& operator<<(std::ostream& stream, const MultichannelType<T,N> &mc)
348  {
349  for (UINT i=0;i<N-1;i++)
350  stream << double(mc.value(i)) << ", ";
351  stream << double(mc.value(N-1));
352  return stream;
353  }
354 
355 
356  template <class T, UINT N>
358  {
359  public:
361  typedef T* Tptr;
362  Tptr _c[N];
363 
364  MultichannelArrayItem(const MultichannelArray<T,N> &mcArray, const UINT &index)
365  {
366  for (UINT i=0;i<N;i++)
367  _c[i] = &mcArray.arrays[i][index];
368  }
369  MultichannelArrayItem& operator =(const MCType &mc)
370  {
371  for (UINT i=0;i<N;i++)
372  *_c[i] = mc.value(i);
373  return *this;
374  }
375  virtual const T& value(const UINT &i) const
376  {
377  return *_c[i];
378  }
379  virtual T& value(const UINT &i)
380  {
381  return *_c[i];
382  }
383  };
384 
385 
386  template <class T, UINT N>
388  {
389  public:
390 // friend MultichannelArrayItem<T,N>;
391 
393 
394  typedef T *lineType;
395  lineType arrays[N];
396 // string subtypeName;
397 
399  : size(0), index(0), allocatedData(false)
400  {
401  resetArrays();
402  }
404  : size(rhs.size-rhs.index), index(0), allocatedData(false)
405  {
406  for (UINT i=0;i<N;i++)
407  arrays[i] = rhs.arrays[i] + rhs.index;
408  }
409  MultichannelArray(const MultichannelArray &rhs, const UINT &newindex)
410  : size(rhs.size-rhs.index-newindex), index(0), allocatedData(false)
411  {
412  for (UINT i=0;i<N;i++)
413  arrays[i] = rhs.arrays[i] + rhs.index + newindex;
414  }
415  MultichannelArray(T *arrayValsPtr, size_t size)
416  : size(size), index(0), allocatedData(false)
417  {
418  for (UINT i=0;i<N;i++)
419  arrays[i] = arrayValsPtr + size*i;
420  }
422  {
423 // if (allocatedData)
424 // deleteArrays();
425  }
426 
427 
428  void resetArrays()
429  {
430  for (UINT i=0;i<N;i++)
431  arrays[i] = NULL;
432  }
433 
434  bool isAllocated() { return allocatedData; }
435 
436  void createArrays(UINT len)
437  {
438  if (allocatedData)
439  deleteArrays();
440  for (UINT i=0;i<N;i++)
441  arrays[i] = createAlignedBuffer<T>(len);
442  size = len;
443  allocatedData = true;
444  }
445  void deleteArrays()
446  {
447  if (!allocatedData)
448  return;
449  for (UINT i=0;i<N;i++)
450  deleteAlignedBuffer<T>(arrays[i]);
451  size = 0;
452  allocatedData = false;
453  }
454 
455 
456 
457 #ifndef SWIG
458  MultichannelArrayItem<T,N> operator [] (const int &i) // lValue
459  {
460  return MultichannelArrayItem<T,N>(*this, index+i);
461  }
462  const MCType operator [] (const int &i) const // rValue
463  {
464  return MultichannelArrayItem<T,N>(*this, index+i);
465  }
466 #endif // SWIG
467 
468  inline MultichannelArrayItem<T,N> operator * ()
469  {
470  return MultichannelArrayItem<T,N>(*this, index);
471  }
472 
473  bool operator != (void *ptr)
474  {
475  return arrays[0]!=ptr;
476  }
477  bool operator == (void *ptr)
478  {
479  return arrays[0]==ptr;
480  }
481 
482  MultichannelArray& operator = (void *ptr)
483  {
484  ASSERT((ptr==NULL), "Do not assign pointer to MultichannelArray", *this);
485 
486  deleteArrays();
487  resetArrays();
488  return *this;
489  }
490 
491  inline MultichannelArray operator + (int dp) const
492  {
493  MultichannelArray ba(*this, this->index + dp);
494  return ba;
495  }
496 #ifndef USE_64BIT_IDS
497  inline MultichannelArray operator + (long unsigned int dp) const
498  {
499  MultichannelArray ba(*this, this->index + dp);
500  return ba;
501  }
502 #endif // USE_64BIT_IDS
503  inline MultichannelArray operator + (size_t dp) const
504  {
505  MultichannelArray ba(*this, this->index + dp);
506  return ba;
507  }
508  MultichannelArray operator - (int dp);
509 #ifndef SWIG
510  MultichannelArray& operator ++ (int)
511  {
512  index++;
513  return *this;
514  }
515 #endif // SWIG
516  size_t operator - (const MultichannelArray<T,N> &arr)
517  {
518  return arrays[0]+index - (arr.arrays[0]+arr.index);
519  }
520 // MultichannelArray& operator ++ ();
521 
522 // MultichannelArray& operator += (int dp) {}
523 
524  inline MultichannelArray& operator = (const MultichannelArray &rhs)
525  {
526  for (UINT i=0;i<N;i++)
527  arrays[i] = rhs.arrays[i] + rhs.index;
528  index = 0;
529  size = rhs.size - rhs.index;
530  allocatedData = rhs.allocatedData;
531  return *this;
532  }
533  operator void *()
534  {
535  return arrays;
536  }
537  operator char *()
538  {
539  return (char *)arrays;
540  }
541 
542  ostream& printSelf(ostream &os=cout);
543 
544  protected:
545  UINT size;
546  UINT index;
547  bool allocatedData;
548  };
549 
550 
551 
552 } // namespace smil
553 
554 #endif // _D_MULTI_CHANNEL_TYPES_H
555 
Definition: DMultichannelTypes.hpp:388
Definition: DMultichannelTypes.hpp:358
Definition: DMultichannelTypes.hpp:51
Definition: DUtils.h:13