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