VTK  9.3.20240328
vtkPixelTransfer.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
19 #ifndef vtkPixelTransfer_h
20 #define vtkPixelTransfer_h
21 
22 #include "vtkCommonDataModelModule.h" // for export
23 #include "vtkPixelExtent.h" // for pixel extent
24 #include "vtkSetGet.h" // for macros
25 #include <cstring> // for memcpy
26 
27 VTK_ABI_NAMESPACE_BEGIN
28 class VTKCOMMONDATAMODEL_EXPORT vtkPixelTransfer
29 {
30 public:
31  vtkPixelTransfer() = default;
32 
37  static int Blit(const vtkPixelExtent& ext, int nComps, int srcType, void* srcData, int destType,
38  void* destData);
39 
44  static int Blit(const vtkPixelExtent& srcWhole, const vtkPixelExtent& srcSubset,
45  const vtkPixelExtent& destWhole, const vtkPixelExtent& destSubset, int nSrcComps, int srcType,
46  void* srcData, int nDestComps, int destType, void* destData);
47 
51  template <typename SOURCE_TYPE, typename DEST_TYPE>
52  static int Blit(const vtkPixelExtent& srcWhole, const vtkPixelExtent& srcSubset,
53  const vtkPixelExtent& destWhole, const vtkPixelExtent& destSubset, int nSrcComps,
54  SOURCE_TYPE* srcData, int nDestComps, DEST_TYPE* destData);
55 
56 private:
57  // distpatch helper for vtk data type enum
58  template <typename SOURCE_TYPE>
59  static int Blit(const vtkPixelExtent& srcWhole, const vtkPixelExtent& srcSubset,
60  const vtkPixelExtent& destWhole, const vtkPixelExtent& destSubset, int nSrcComps,
61  SOURCE_TYPE* srcData, int nDestComps, int destType, void* destData);
62 };
63 
64 //-----------------------------------------------------------------------------
66  const vtkPixelExtent& ext, int nComps, int srcType, void* srcData, int destType, void* destData)
67 {
69  ext, ext, ext, ext, nComps, srcType, srcData, nComps, destType, destData);
70 }
71 
72 //-----------------------------------------------------------------------------
73 template <typename SOURCE_TYPE>
74 int vtkPixelTransfer::Blit(const vtkPixelExtent& srcWholeExt, const vtkPixelExtent& srcExt,
75  const vtkPixelExtent& destWholeExt, const vtkPixelExtent& destExt, int nSrcComps,
76  SOURCE_TYPE* srcData, int nDestComps, int destType, void* destData)
77 {
78  // second layer of dispatch
79  switch (destType)
80  {
81  vtkTemplateMacro(return vtkPixelTransfer::Blit(srcWholeExt, srcExt, destWholeExt, destExt,
82  nSrcComps, srcData, nDestComps, (VTK_TT*)destData););
83  }
84  return 0;
85 }
86 
87 //-----------------------------------------------------------------------------
88 template <typename SOURCE_TYPE, typename DEST_TYPE>
89 int vtkPixelTransfer::Blit(const vtkPixelExtent& srcWholeExt, const vtkPixelExtent& srcSubset,
90  const vtkPixelExtent& destWholeExt, const vtkPixelExtent& destSubset, int nSrcComps,
91  SOURCE_TYPE* srcData, int nDestComps, DEST_TYPE* destData)
92 {
93  if ((srcData == nullptr) || (destData == nullptr))
94  {
95  return -1;
96  }
97  if ((srcWholeExt == srcSubset) && (destWholeExt == destSubset) && (nSrcComps == nDestComps))
98  {
99  // buffers are contiguous
100  size_t n = srcWholeExt.Size() * nSrcComps;
101  for (size_t i = 0; i < n; ++i)
102  {
103  destData[i] = static_cast<DEST_TYPE>(srcData[i]);
104  }
105  }
106  else
107  {
108  // buffers are not contiguous
109  int tmp[2];
110 
111  // get the dimensions of the arrays
112  srcWholeExt.Size(tmp);
113  int swnx = tmp[0];
114 
115  destWholeExt.Size(tmp);
116  int dwnx = tmp[0];
117 
118  // move from logical extent to memory extent
119  vtkPixelExtent srcExt(srcSubset);
120  srcExt.Shift(srcWholeExt);
121 
122  vtkPixelExtent destExt(destSubset);
123  destExt.Shift(destWholeExt);
124 
125  // get size of sub-set to copy (it's the same in src and dest)
126  int nxny[2];
127  srcExt.Size(nxny);
128 
129  // use smaller ncomps for loop index to avoid reading/writing
130  // invalid mem
131  int nCopyComps = nSrcComps < nDestComps ? nSrcComps : nDestComps;
132 
133  for (int j = 0; j < nxny[1]; ++j)
134  {
135  int sjj = swnx * (srcExt[2] + j) + srcExt[0];
136  int djj = dwnx * (destExt[2] + j) + destExt[0];
137  for (int i = 0; i < nxny[0]; ++i)
138  {
139  int sidx = nSrcComps * (sjj + i);
140  int didx = nDestComps * (djj + i);
141  // copy values from source
142  for (int p = 0; p < nCopyComps; ++p)
143  {
144  destData[didx + p] = static_cast<DEST_TYPE>(srcData[sidx + p]);
145  }
146  // ensure all dest comps are initialized
147  for (int p = nCopyComps; p < nDestComps; ++p)
148  {
149  destData[didx + p] = static_cast<DEST_TYPE>(0);
150  }
151  }
152  }
153  }
154  return 0;
155 }
156 
157 VTK_ABI_NAMESPACE_END
158 #endif
159 // VTK-HeaderTest-Exclude: vtkPixelTransfer.h
Representation of a cartesian pixel plane and common operations on it.
void Shift()
Shifts by low corner of this, moving to the origin.
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.
static int Blit(const vtkPixelExtent &srcWhole, const vtkPixelExtent &srcSubset, const vtkPixelExtent &destWhole, const vtkPixelExtent &destSubset, int nSrcComps, int srcType, void *srcData, int nDestComps, int destType, void *destData)
for memory to memory transfers.
vtkPixelTransfer()=default