VTK  9.0.1
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 "vtkPixelExtent.h" // for pixel extent
33 #include "vtkSetGet.h" // for macros
34 #include <cstring> // for memcpy
35 
36 class VTKCOMMONDATAMODEL_EXPORT vtkPixelTransfer
37 {
38 public:
40 
45  static int Blit(const vtkPixelExtent& ext, int nComps, int srcType, void* srcData, int destType,
46  void* destData);
47 
52  static int Blit(const vtkPixelExtent& srcWhole, const vtkPixelExtent& srcSubset,
53  const vtkPixelExtent& destWhole, const vtkPixelExtent& destSubset, int nSrcComps, int srcType,
54  void* srcData, int nDestComps, int destType, void* destData);
55 
59  template <typename SOURCE_TYPE, typename DEST_TYPE>
60  static int Blit(const vtkPixelExtent& srcWhole, const vtkPixelExtent& srcSubset,
61  const vtkPixelExtent& destWhole, const vtkPixelExtent& destSubset, int nSrcComps,
62  SOURCE_TYPE* srcData, int nDestComps, DEST_TYPE* destData);
63 
64 private:
65  // distpatch helper for vtk data type enum
66  template <typename SOURCE_TYPE>
67  static int Blit(const vtkPixelExtent& srcWhole, const vtkPixelExtent& srcSubset,
68  const vtkPixelExtent& destWhole, const vtkPixelExtent& destSubset, int nSrcComps,
69  SOURCE_TYPE* srcData, int nDestComps, int destType, void* destData);
70 };
71 
72 //-----------------------------------------------------------------------------
74  const vtkPixelExtent& ext, int nComps, int srcType, void* srcData, int destType, void* destData)
75 {
77  ext, ext, ext, ext, nComps, srcType, srcData, nComps, destType, destData);
78 }
79 
80 //-----------------------------------------------------------------------------
81 template <typename SOURCE_TYPE>
82 int vtkPixelTransfer::Blit(const vtkPixelExtent& srcWholeExt, const vtkPixelExtent& srcExt,
83  const vtkPixelExtent& destWholeExt, const vtkPixelExtent& destExt, int nSrcComps,
84  SOURCE_TYPE* srcData, int nDestComps, int destType, void* destData)
85 {
86  // second layer of dispatch
87  switch (destType)
88  {
89  vtkTemplateMacro(return vtkPixelTransfer::Blit(srcWholeExt, srcExt, destWholeExt, destExt,
90  nSrcComps, srcData, nDestComps, (VTK_TT*)destData););
91  }
92  return 0;
93 }
94 
95 //-----------------------------------------------------------------------------
96 template <typename SOURCE_TYPE, typename DEST_TYPE>
97 int vtkPixelTransfer::Blit(const vtkPixelExtent& srcWholeExt, const vtkPixelExtent& srcSubset,
98  const vtkPixelExtent& destWholeExt, const vtkPixelExtent& destSubset, int nSrcComps,
99  SOURCE_TYPE* srcData, int nDestComps, DEST_TYPE* destData)
100 {
101  if ((srcData == nullptr) || (destData == nullptr))
102  {
103  return -1;
104  }
105  if ((srcWholeExt == srcSubset) && (destWholeExt == destSubset) && (nSrcComps == nDestComps))
106  {
107  // buffers are contiguous
108  size_t n = srcWholeExt.Size() * nSrcComps;
109  for (size_t i = 0; i < n; ++i)
110  {
111  destData[i] = static_cast<DEST_TYPE>(srcData[i]);
112  }
113  }
114  else
115  {
116  // buffers are not contiguous
117  int tmp[2];
118 
119  // get the dimensions of the arrays
120  srcWholeExt.Size(tmp);
121  int swnx = tmp[0];
122 
123  destWholeExt.Size(tmp);
124  int dwnx = tmp[0];
125 
126  // move from logical extent to memory extent
127  vtkPixelExtent srcExt(srcSubset);
128  srcExt.Shift(srcWholeExt);
129 
130  vtkPixelExtent destExt(destSubset);
131  destExt.Shift(destWholeExt);
132 
133  // get size of sub-set to copy (it's the same in src and dest)
134  int nxny[2];
135  srcExt.Size(nxny);
136 
137  // use smaller ncomps for loop index to avoid reading/writing
138  // invalid mem
139  int nCopyComps = nSrcComps < nDestComps ? nSrcComps : nDestComps;
140 
141  for (int j = 0; j < nxny[1]; ++j)
142  {
143  int sjj = swnx * (srcExt[2] + j) + srcExt[0];
144  int djj = dwnx * (destExt[2] + j) + destExt[0];
145  for (int i = 0; i < nxny[0]; ++i)
146  {
147  int sidx = nSrcComps * (sjj + i);
148  int didx = nDestComps * (djj + i);
149  // copy values from source
150  for (int p = 0; p < nCopyComps; ++p)
151  {
152  destData[didx + p] = static_cast<DEST_TYPE>(srcData[sidx + p]);
153  }
154  // ensure all dest comps are initialized
155  for (int p = nCopyComps; p < nDestComps; ++p)
156  {
157  destData[didx + p] = static_cast<DEST_TYPE>(0);
158  }
159  }
160  }
161  }
162  return 0;
163 }
164 
165 #endif
166 // 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.