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 "vtkRenderingLICOpenGL2Module.h" // for export
34 #include "vtkSetGet.h" // for macros
35 #include "vtkPixelExtent.h" // for pixel extent
36 #include <cstring> // for memcpy
37 
38 class VTKRENDERINGLICOPENGL2_EXPORT vtkPixelTransfer
39 {
40 public:
43 
45 
47  static
48  int Blit(
49  const vtkPixelExtent &ext,
50  int nComps,
51  int srcType,
52  void *srcData,
53  int destType,
54  void *destData);
56 
58 
60  static
61  int Blit(
62  const vtkPixelExtent &srcWhole,
63  const vtkPixelExtent &srcSubset,
64  const vtkPixelExtent &destWhole,
65  const vtkPixelExtent &destSubset,
66  int nSrcComps,
67  int srcType,
68  void *srcData,
69  int nDestComps,
70  int destType,
71  void *destData);
73 
75 
76  template<typename SOURCE_TYPE, typename DEST_TYPE>
77  static
78  int Blit(
79  const vtkPixelExtent &srcWhole,
80  const vtkPixelExtent &srcSubset,
81  const vtkPixelExtent &destWhole,
82  const vtkPixelExtent &destSubset,
83  int nSrcComps,
84  SOURCE_TYPE *srcData,
85  int nDestComps,
86  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 ostream &operator<<(ostream &os, const vtkPixelTransfer &gt);
238 
239 #endif
240 // VTK-HeaderTest-Exclude: vtkPixelTransfer.h
ostream & operator<<(ostream &os, const vtkPixelTransfer &gt)
static int Blit(const vtkPixelExtent &ext, int nComps, int srcType, void *srcData, int destType, void *destData)
void Size(T nCells[2]) const