VTK
|
00001 /*========================================================================= 00002 00003 Program: Visualization Toolkit 00004 Module: vtkPixelTransfer.h 00005 00006 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 00007 All rights reserved. 00008 See Copyright.txt or http://www.kitware.com/Copyright.htm for details. 00009 00010 This software is distributed WITHOUT ANY WARRANTY; without even 00011 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00012 PURPOSE. See the above copyright notice for more information. 00013 00014 =========================================================================*/ 00030 #ifndef __vtkPixelTransfer_h 00031 #define __vtkPixelTransfer_h 00032 00033 #include "vtkRenderingLICModule.h" // for export 00034 #include "vtkSetGet.h" // for macros 00035 #include "vtkPixelExtent.h" // for pixel extent 00036 #include <cstring> // for memcpy 00037 00038 class VTKRENDERINGLIC_EXPORT vtkPixelTransfer 00039 { 00040 public: 00041 vtkPixelTransfer(){} 00042 ~vtkPixelTransfer(){} 00043 00045 00047 static 00048 int Blit( 00049 const vtkPixelExtent &ext, 00050 int nComps, 00051 int srcType, 00052 void *srcData, 00053 int destType, 00054 void *destData); 00056 00058 00060 static 00061 int Blit( 00062 const vtkPixelExtent &srcWhole, 00063 const vtkPixelExtent &srcSubset, 00064 const vtkPixelExtent &destWhole, 00065 const vtkPixelExtent &destSubset, 00066 int nSrcComps, 00067 int srcType, 00068 void *srcData, 00069 int nDestComps, 00070 int destType, 00071 void *destData); 00073 00075 00076 template<typename SOURCE_TYPE, typename DEST_TYPE> 00077 static 00078 int Blit( 00079 const vtkPixelExtent &srcWhole, 00080 const vtkPixelExtent &srcSubset, 00081 const vtkPixelExtent &destWhole, 00082 const vtkPixelExtent &destSubset, 00083 int nSrcComps, 00084 SOURCE_TYPE *srcData, 00085 int nDestComps, 00086 DEST_TYPE *destData); 00088 00089 private: 00090 // distpatch helper for vtk data type enum 00091 template<typename SOURCE_TYPE> 00092 static 00093 int Blit( 00094 const vtkPixelExtent &srcWhole, 00095 const vtkPixelExtent &srcSubset, 00096 const vtkPixelExtent &destWhole, 00097 const vtkPixelExtent &destSubset, 00098 int nSrcComps, 00099 SOURCE_TYPE *srcData, 00100 int nDestComps, 00101 int destType, 00102 void *destData); 00103 }; 00104 00105 //----------------------------------------------------------------------------- 00106 inline 00107 int vtkPixelTransfer::Blit( 00108 const vtkPixelExtent &ext, 00109 int nComps, 00110 int srcType, 00111 void *srcData, 00112 int destType, 00113 void *destData) 00114 { 00115 return vtkPixelTransfer::Blit( 00116 ext, 00117 ext, 00118 ext, 00119 ext, 00120 nComps, 00121 srcType, 00122 srcData, 00123 nComps, 00124 destType, 00125 destData); 00126 } 00127 00128 00129 //----------------------------------------------------------------------------- 00130 template<typename SOURCE_TYPE> 00131 int vtkPixelTransfer::Blit( 00132 const vtkPixelExtent &srcWholeExt, 00133 const vtkPixelExtent &srcExt, 00134 const vtkPixelExtent &destWholeExt, 00135 const vtkPixelExtent &destExt, 00136 int nSrcComps, 00137 SOURCE_TYPE *srcData, 00138 int nDestComps, 00139 int destType, 00140 void *destData) 00141 { 00142 // second layer of dispatch 00143 switch(destType) 00144 { 00145 vtkTemplateMacro( 00146 return vtkPixelTransfer::Blit( 00147 srcWholeExt, 00148 srcExt, 00149 destWholeExt, 00150 destExt, 00151 nSrcComps, 00152 srcData, 00153 nDestComps, 00154 (VTK_TT*)destData);); 00155 } 00156 return 0; 00157 } 00158 00159 //----------------------------------------------------------------------------- 00160 template<typename SOURCE_TYPE, typename DEST_TYPE> 00161 int vtkPixelTransfer::Blit( 00162 const vtkPixelExtent &srcWholeExt, 00163 const vtkPixelExtent &srcSubset, 00164 const vtkPixelExtent &destWholeExt, 00165 const vtkPixelExtent &destSubset, 00166 int nSrcComps, 00167 SOURCE_TYPE *srcData, 00168 int nDestComps, 00169 DEST_TYPE *destData) 00170 { 00171 if ( (srcData == NULL) || (destData == NULL) ) 00172 { 00173 return -1; 00174 } 00175 if ( (srcWholeExt == srcSubset) 00176 && (destWholeExt == destSubset) 00177 && (nSrcComps == nDestComps) ) 00178 { 00179 // buffers are contiguous 00180 size_t n = srcWholeExt.Size()*nSrcComps; 00181 for (size_t i=0; i<n; ++i) 00182 { 00183 destData[i] = static_cast<DEST_TYPE>(srcData[i]); 00184 } 00185 } 00186 else 00187 { 00188 // buffers are not contiguous 00189 int tmp[2]; 00190 00191 // get the dimensions of the arrays 00192 srcWholeExt.Size(tmp); 00193 int swnx = tmp[0]; 00194 00195 destWholeExt.Size(tmp); 00196 int dwnx = tmp[0]; 00197 00198 // move from logical extent to memory extent 00199 vtkPixelExtent srcExt(srcSubset); 00200 srcExt.Shift(srcWholeExt); 00201 00202 vtkPixelExtent destExt(destSubset); 00203 destExt.Shift(destWholeExt); 00204 00205 // get size of sub-set to copy (it's the same in src and dest) 00206 int nxny[2]; 00207 srcExt.Size(nxny); 00208 00209 // use smaller ncomps for loop index to avoid reading/writing 00210 // invalid mem 00211 int nCopyComps = nSrcComps < nDestComps ? nSrcComps : nDestComps; 00212 00213 for (int j=0; j<nxny[1]; ++j) 00214 { 00215 int sjj = swnx*(srcExt[2]+j)+srcExt[0]; 00216 int djj = dwnx*(destExt[2]+j)+destExt[0]; 00217 for (int i=0; i<nxny[0]; ++i) 00218 { 00219 int sidx = nSrcComps*(sjj+i); 00220 int didx = nDestComps*(djj+i); 00221 // copy values from source 00222 for (int p=0; p<nCopyComps; ++p) 00223 { 00224 destData[didx+p] = static_cast<DEST_TYPE>(srcData[sidx+p]); 00225 } 00226 // ensure all dest comps are initialized 00227 for (int p=nCopyComps; p<nDestComps; ++p) 00228 { 00229 destData[didx+p] = static_cast<DEST_TYPE>(0); 00230 } 00231 } 00232 } 00233 } 00234 return 0; 00235 } 00236 00237 ostream &operator<<(ostream &os, const vtkPixelTransfer >); 00238 00239 #endif 00240 // VTK-HeaderTest-Exclude: vtkPixelTransfer.h