1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkPixelTransfer.h
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 // .NAME vtkPixelTransfer -- For movement of pixel data described by
16 // pixel extents
17 //
18 // .SECTION Description
19 // Class to handle non-contiguous data transfers of data described
20 // by pixel extents within a process. For transfering data between
21 // processes see vtkPPixelTransfer.
22 //
23 // .SECTION See also
24 // vtkPixelExtent vtkPPixelTransfer
25 
26 #ifndef vtkPixelTransfer_h
27 #define vtkPixelTransfer_h
28 
29 #include "vtkRenderingLICModule.h" // for export
30 #include "vtkSetGet.h" // for macros
31 #include "vtkPixelExtent.h" // for pixel extent
32 #include <cstring> // for memcpy
33 
34 class VTKRENDERINGLIC_EXPORT vtkPixelTransfer
35 {
36 public:
vtkPixelTransfer()37   vtkPixelTransfer(){}
~vtkPixelTransfer()38   ~vtkPixelTransfer(){}
39 
40   // Description:
41   // for  memory to memory transfers. Conveinience api for working
42   // with vtk type enum rather than c-data types and simple extents.
43   static
44   int Blit(
45          const vtkPixelExtent &ext,
46          int nComps,
47          int srcType,
48          void *srcData,
49          int destType,
50          void *destData);
51 
52   // Description:
53   // for  memory to memory transfers. Conveinience api for working
54   // with vtk type enum rather than c-data types.
55   static
56   int Blit(
57          const vtkPixelExtent &srcWhole,
58          const vtkPixelExtent &srcSubset,
59          const vtkPixelExtent &destWhole,
60          const vtkPixelExtent &destSubset,
61          int nSrcComps,
62          int srcType,
63          void *srcData,
64          int nDestComps,
65          int destType,
66          void *destData);
67 
68   // Description:
69   // for local memory to memory transfers
70   template<typename SOURCE_TYPE, typename DEST_TYPE>
71   static
72   int Blit(
73          const vtkPixelExtent &srcWhole,
74          const vtkPixelExtent &srcSubset,
75          const vtkPixelExtent &destWhole,
76          const vtkPixelExtent &destSubset,
77          int nSrcComps,
78          SOURCE_TYPE *srcData,
79          int nDestComps,
80          DEST_TYPE *destData);
81 
82 private:
83   // distpatch helper for vtk data type enum
84   template<typename SOURCE_TYPE>
85   static
86   int Blit(
87          const vtkPixelExtent &srcWhole,
88          const vtkPixelExtent &srcSubset,
89          const vtkPixelExtent &destWhole,
90          const vtkPixelExtent &destSubset,
91          int nSrcComps,
92          SOURCE_TYPE *srcData,
93          int nDestComps,
94          int destType,
95          void *destData);
96 };
97 
98 //-----------------------------------------------------------------------------
99 inline
Blit(const vtkPixelExtent & ext,int nComps,int srcType,void * srcData,int destType,void * destData)100 int vtkPixelTransfer::Blit(
101          const vtkPixelExtent &ext,
102          int nComps,
103          int srcType,
104          void *srcData,
105          int destType,
106          void *destData)
107 {
108   return vtkPixelTransfer::Blit(
109         ext,
110         ext,
111         ext,
112         ext,
113         nComps,
114         srcType,
115         srcData,
116         nComps,
117         destType,
118         destData);
119 }
120 
121 
122 //-----------------------------------------------------------------------------
123 template<typename SOURCE_TYPE>
Blit(const vtkPixelExtent & srcWholeExt,const vtkPixelExtent & srcExt,const vtkPixelExtent & destWholeExt,const vtkPixelExtent & destExt,int nSrcComps,SOURCE_TYPE * srcData,int nDestComps,int destType,void * destData)124 int vtkPixelTransfer::Blit(
125        const vtkPixelExtent &srcWholeExt,
126        const vtkPixelExtent &srcExt,
127        const vtkPixelExtent &destWholeExt,
128        const vtkPixelExtent &destExt,
129        int nSrcComps,
130        SOURCE_TYPE *srcData,
131        int nDestComps,
132        int destType,
133        void *destData)
134 {
135   // second layer of dispatch
136   switch(destType)
137     {
138     vtkTemplateMacro(
139         return vtkPixelTransfer::Blit(
140             srcWholeExt,
141             srcExt,
142             destWholeExt,
143             destExt,
144             nSrcComps,
145             srcData,
146             nDestComps,
147             (VTK_TT*)destData););
148     }
149   return 0;
150 }
151 
152 //-----------------------------------------------------------------------------
153 template<typename SOURCE_TYPE, typename DEST_TYPE>
Blit(const vtkPixelExtent & srcWholeExt,const vtkPixelExtent & srcSubset,const vtkPixelExtent & destWholeExt,const vtkPixelExtent & destSubset,int nSrcComps,SOURCE_TYPE * srcData,int nDestComps,DEST_TYPE * destData)154 int vtkPixelTransfer::Blit(
155        const vtkPixelExtent &srcWholeExt,
156        const vtkPixelExtent &srcSubset,
157        const vtkPixelExtent &destWholeExt,
158        const vtkPixelExtent &destSubset,
159        int nSrcComps,
160        SOURCE_TYPE *srcData,
161        int nDestComps,
162        DEST_TYPE *destData)
163 {
164   if ( (srcData == NULL) || (destData == NULL) )
165     {
166     return -1;
167     }
168   if ( (srcWholeExt == srcSubset)
169     && (destWholeExt == destSubset)
170     && (nSrcComps == nDestComps) )
171     {
172     // buffers are contiguous
173     size_t n = srcWholeExt.Size()*nSrcComps;
174     for (size_t i=0; i<n; ++i)
175       {
176       destData[i] = static_cast<DEST_TYPE>(srcData[i]);
177       }
178     }
179   else
180     {
181     // buffers are not contiguous
182     int tmp[2];
183 
184     // get the dimensions of the arrays
185     srcWholeExt.Size(tmp);
186     int swnx = tmp[0];
187 
188     destWholeExt.Size(tmp);
189     int dwnx = tmp[0];
190 
191     // move from logical extent to memory extent
192     vtkPixelExtent srcExt(srcSubset);
193     srcExt.Shift(srcWholeExt);
194 
195     vtkPixelExtent destExt(destSubset);
196     destExt.Shift(destWholeExt);
197 
198     // get size of sub-set to copy (it's the same in src and dest)
199     int nxny[2];
200     srcExt.Size(nxny);
201 
202     // use smaller ncomps for loop index to avoid reading/writing
203     // invalid mem
204     int nCopyComps = nSrcComps < nDestComps ? nSrcComps : nDestComps;
205 
206     for (int j=0; j<nxny[1]; ++j)
207       {
208       int sjj = swnx*(srcExt[2]+j)+srcExt[0];
209       int djj = dwnx*(destExt[2]+j)+destExt[0];
210       for (int i=0; i<nxny[0]; ++i)
211         {
212         int sidx = nSrcComps*(sjj+i);
213         int didx = nDestComps*(djj+i);
214         // copy values from source
215         for (int p=0; p<nCopyComps; ++p)
216           {
217           destData[didx+p] = static_cast<DEST_TYPE>(srcData[sidx+p]);
218           }
219         // ensure all dest comps are initialized
220         for (int p=nCopyComps; p<nDestComps; ++p)
221           {
222           destData[didx+p] = static_cast<DEST_TYPE>(0);
223           }
224         }
225       }
226     }
227   return 0;
228 }
229 
230 ostream &operator<<(ostream &os, const vtkPixelTransfer &gt);
231 
232 #endif
233 // VTK-HeaderTest-Exclude: vtkPixelTransfer.h
234