VTK
vtkPixelTransfer.h
Go to the documentation of this file.
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 =========================================================================*/
31 #ifndef vtkPixelTransfer_h
32 #define vtkPixelTransfer_h
33 
34 #include "vtkCommonDataModelModule.h" // for export
35 #include "vtkSetGet.h" // for macros
36 #include "vtkPixelExtent.h" // for pixel extent
37 #include <cstring> // for memcpy
38 
39 class VTKCOMMONDATAMODEL_EXPORT vtkPixelTransfer
40 {
41 public:
43 
48  static
49  int Blit(
50  const vtkPixelExtent &ext,
51  int nComps,
52  int srcType,
53  void *srcData,
54  int destType,
55  void *destData);
56 
61  static
62  int Blit(
63  const vtkPixelExtent &srcWhole,
64  const vtkPixelExtent &srcSubset,
65  const vtkPixelExtent &destWhole,
66  const vtkPixelExtent &destSubset,
67  int nSrcComps,
68  int srcType,
69  void *srcData,
70  int nDestComps,
71  int destType,
72  void *destData);
73 
77  template<typename SOURCE_TYPE, typename DEST_TYPE>
78  static
79  int Blit(
80  const vtkPixelExtent &srcWhole,
81  const vtkPixelExtent &srcSubset,
82  const vtkPixelExtent &destWhole,
83  const vtkPixelExtent &destSubset,
84  int nSrcComps,
85  SOURCE_TYPE *srcData,
86  int nDestComps,
87  DEST_TYPE *destData);
88 
89 private:
90  // distpatch helper for vtk data type enum
91  template<typename SOURCE_TYPE>
92  static
93  int Blit(
94  const vtkPixelExtent &srcWhole,
95  const vtkPixelExtent &srcSubset,
96  const vtkPixelExtent &destWhole,
97  const vtkPixelExtent &destSubset,
98  int nSrcComps,
99  SOURCE_TYPE *srcData,
100  int nDestComps,
101  int destType,
102  void *destData);
103 };
104 
105 //-----------------------------------------------------------------------------
106 inline
108  const vtkPixelExtent &ext,
109  int nComps,
110  int srcType,
111  void *srcData,
112  int destType,
113  void *destData)
114 {
115  return vtkPixelTransfer::Blit(
116  ext,
117  ext,
118  ext,
119  ext,
120  nComps,
121  srcType,
122  srcData,
123  nComps,
124  destType,
125  destData);
126 }
127 
128 
129 //-----------------------------------------------------------------------------
130 template<typename SOURCE_TYPE>
132  const vtkPixelExtent &srcWholeExt,
133  const vtkPixelExtent &srcExt,
134  const vtkPixelExtent &destWholeExt,
135  const vtkPixelExtent &destExt,
136  int nSrcComps,
137  SOURCE_TYPE *srcData,
138  int nDestComps,
139  int destType,
140  void *destData)
141 {
142  // second layer of dispatch
143  switch(destType)
144  {
145  vtkTemplateMacro(
146  return vtkPixelTransfer::Blit(
147  srcWholeExt,
148  srcExt,
149  destWholeExt,
150  destExt,
151  nSrcComps,
152  srcData,
153  nDestComps,
154  (VTK_TT*)destData););
155  }
156  return 0;
157 }
158 
159 //-----------------------------------------------------------------------------
160 template<typename SOURCE_TYPE, typename DEST_TYPE>
162  const vtkPixelExtent &srcWholeExt,
163  const vtkPixelExtent &srcSubset,
164  const vtkPixelExtent &destWholeExt,
165  const vtkPixelExtent &destSubset,
166  int nSrcComps,
167  SOURCE_TYPE *srcData,
168  int nDestComps,
169  DEST_TYPE *destData)
170 {
171  if ( (srcData == NULL) || (destData == NULL) )
172  {
173  return -1;
174  }
175  if ( (srcWholeExt == srcSubset)
176  && (destWholeExt == destSubset)
177  && (nSrcComps == nDestComps) )
178  {
179  // buffers are contiguous
180  size_t n = srcWholeExt.Size()*nSrcComps;
181  for (size_t i=0; i<n; ++i)
182  {
183  destData[i] = static_cast<DEST_TYPE>(srcData[i]);
184  }
185  }
186  else
187  {
188  // buffers are not contiguous
189  int tmp[2];
190 
191  // get the dimensions of the arrays
192  srcWholeExt.Size(tmp);
193  int swnx = tmp[0];
194 
195  destWholeExt.Size(tmp);
196  int dwnx = tmp[0];
197 
198  // move from logical extent to memory extent
199  vtkPixelExtent srcExt(srcSubset);
200  srcExt.Shift(srcWholeExt);
201 
202  vtkPixelExtent destExt(destSubset);
203  destExt.Shift(destWholeExt);
204 
205  // get size of sub-set to copy (it's the same in src and dest)
206  int nxny[2];
207  srcExt.Size(nxny);
208 
209  // use smaller ncomps for loop index to avoid reading/writing
210  // invalid mem
211  int nCopyComps = nSrcComps < nDestComps ? nSrcComps : nDestComps;
212 
213  for (int j=0; j<nxny[1]; ++j)
214  {
215  int sjj = swnx*(srcExt[2]+j)+srcExt[0];
216  int djj = dwnx*(destExt[2]+j)+destExt[0];
217  for (int i=0; i<nxny[0]; ++i)
218  {
219  int sidx = nSrcComps*(sjj+i);
220  int didx = nDestComps*(djj+i);
221  // copy values from source
222  for (int p=0; p<nCopyComps; ++p)
223  {
224  destData[didx+p] = static_cast<DEST_TYPE>(srcData[sidx+p]);
225  }
226  // ensure all dest comps are initialized
227  for (int p=nCopyComps; p<nDestComps; ++p)
228  {
229  destData[didx+p] = static_cast<DEST_TYPE>(0);
230  }
231  }
232  }
233  }
234  return 0;
235 }
236 
237 #endif
238 // VTK-HeaderTest-Exclude: vtkPixelTransfer.h
pixel extents
static int Blit(const vtkPixelExtent &ext, int nComps, int srcType, void *srcData, int destType, void *destData)
for memory to memory transfers.
Representation of a cartesian pixel plane and common operations on it.
void Size(T nCells[2]) const
Get the number in each direction.
void Shift()
Shifts by low corner of this, moving to the origin.