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