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