SMIL  0.9.1
DImageMatrix.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_IMAGE_MATRIX_HPP
31 #define _D_IMAGE_MATRIX_HPP
32 
33 #include "Core/include/private/DImage.hpp"
34 #include "Core/include/DErrors.h"
35 
36 namespace smil
37 {
38 
48  template <class T>
49  RES_T matTrans(const Image<T> &imIn, Image<T> &imOut)
50  {
51  ASSERT_ALLOCATED(&imIn);
52  size_t w, h, d;
53  imIn.getSize(&w, &h, &d);
54 
55  ImageFreezer freezer(imOut);
56 
57 // typedef typename ImDtTypes<T>::sliceType sliceType;
58  typedef typename ImDtTypes<T>::lineType lineType;
59 
60  if (d==1) // 2D
61  {
62  ASSERT((imOut.setSize(h, w)==RES_OK));
63 
64  lineType pIn = imIn.getPixels();
65  lineType pOut = imOut.getPixels();
66 
67  for (size_t y=0;y<w;y++)
68  for (size_t x=0;x<h;x++,pOut++)
69  *pOut = pIn[x*w+y];
70 
71  return RES_OK;
72  }
73  else
74  return RES_ERR_NOT_IMPLEMENTED;
75  }
76 
83  template <class T>
84  RES_T matMul(const Image<T> &imIn1, const Image<T> &imIn2, Image<T> &imOut)
85  {
86  ASSERT_ALLOCATED(&imIn1, &imIn2);
87  size_t size1[3], size2[3];
88  imIn1.getSize(size1);
89  imIn2.getSize(size2);
90 
91  if (size1[2]!=1 || size2[2]!=1)
92  return RES_ERR_NOT_IMPLEMENTED;
93 
94  ImageFreezer freezer(imOut);
95 
96  // Verify that the number of columns m in imIn1 is equal to the number of rows m in imIn2
97  ASSERT((size1[0]==size2[1]), "Wrong matrix sizes!", RES_ERR);
98  ASSERT((imOut.setSize(size2[0], size1[1])==RES_OK));
99 
100  Image<T> transIm(size2[1], size2[0]);
101 
102  // Transpose imIn2 matrix to allow vectorization
103  ASSERT((matTrans(imIn2, transIm)==RES_OK));
104 
105  typedef typename ImDtTypes<T>::sliceType sliceType;
106  typedef typename ImDtTypes<T>::lineType lineType;
107 
108  sliceType lines = imIn1.getLines();
109  sliceType outLines = imOut.getLines();
110  sliceType cols = transIm.getLines();
111  lineType line;
112  lineType outLine;
113  lineType col;
114 
115 
116  size_t y;
117 
118  #ifdef USE_OPEN_MP
119  int nthreads = Core::getInstance()->getNumberOfThreads();
120  #pragma omp parallel private(line, outLine, col)
121  #endif // USE_OPEN_MP
122  {
123 
124  #ifdef USE_OPEN_MP
125  #pragma omp for schedule(dynamic,nthreads) nowait
126  #endif // USE_OPEN_MP
127  for (y=0;y<size1[1];y++)
128  {
129  line = lines[y];
130  outLine = outLines[y];
131  for (size_t x=0;x<size2[0];x++)
132  {
133  col = cols[x];
134  T outVal = 0;
135  for (size_t i=0;i<size1[0];i++)
136  outVal += line[i] * col[i];
137  outLine[x] = outVal;
138 
139  }
140  }
141  }
142 
143  return RES_OK;
144  }
145 
148 } // namespace smil
149 
150 
151 #endif // _D_IMAGE_MATRIX_HPP
152 
RES_T matMul(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
Matrix multiplication (for now, only in 2D)
Definition: DImageMatrix.hpp:84
Definition: DColorConvert.h:38
sliceType getLines() const
Get an array containing the start offset of each line.
Definition: DImage.hpp:114
Definition: DBaseImage.h:235
lineType getPixels() const
Get the pixels as a 1D array.
Definition: DImage.hpp:110
virtual RES_T setSize(size_t w, size_t h, size_t d=1, bool doAllocate=true)
Set the size of image.
Definition: DImage.hxx:319
RES_T matTrans(const Image< T > &imIn, Image< T > &imOut)
Matrix transposition (for now, only in 2D)
Definition: DImageMatrix.hpp:49
Main Image class.
Definition: DQVtkViewer.hpp:44