gdalwarper.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /******************************************************************************
  2. * $Id: gdalwarper.h 29330 2015-06-14 12:11:11Z rouault $
  3. *
  4. * Project: GDAL High Performance Warper
  5. * Purpose: Prototypes, and definitions for warping related work.
  6. * Author: Frank Warmerdam, warmerdam@pobox.com
  7. *
  8. ******************************************************************************
  9. * Copyright (c) 2003, Frank Warmerdam
  10. * Copyright (c) 2009-2012, Even Rouault <even dot rouault at mines-paris dot org>
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a
  13. * copy of this software and associated documentation files (the "Software"),
  14. * to deal in the Software without restriction, including without limitation
  15. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  16. * and/or sell copies of the Software, and to permit persons to whom the
  17. * Software is furnished to do so, subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included
  20. * in all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  23. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  25. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  27. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  28. * DEALINGS IN THE SOFTWARE.
  29. ****************************************************************************/
  30. #ifndef GDALWARPER_H_INCLUDED
  31. #define GDALWARPER_H_INCLUDED
  32. /**
  33. * \file gdalwarper.h
  34. *
  35. * GDAL warper related entry points and definitions. Eventually it is
  36. * expected that this file will be mostly private to the implementation,
  37. * and the public C entry points will be available in gdal_alg.h.
  38. */
  39. #include "gdal_alg.h"
  40. #include "cpl_minixml.h"
  41. #include "cpl_multiproc.h"
  42. CPL_C_START
  43. /* Note: values are selected to be consistent with GDALRIOResampleAlg of gcore/gdal.h */
  44. /*! Warp Resampling Algorithm */
  45. typedef enum {
  46. /*! Nearest neighbour (select on one input pixel) */ GRA_NearestNeighbour=0,
  47. /*! Bilinear (2x2 kernel) */ GRA_Bilinear=1,
  48. /*! Cubic Convolution Approximation (4x4 kernel) */ GRA_Cubic=2,
  49. /*! Cubic B-Spline Approximation (4x4 kernel) */ GRA_CubicSpline=3,
  50. /*! Lanczos windowed sinc interpolation (6x6 kernel) */ GRA_Lanczos=4,
  51. /*! Average (computes the average of all non-NODATA contributing pixels) */ GRA_Average=5,
  52. /*! Mode (selects the value which appears most often of all the sampled points) */ GRA_Mode=6,
  53. // GRA_Gauss=7 reserved.
  54. /*! Max (selects maximum of all non-NODATA contributing pixels) */ GRA_Max=8,
  55. /*! Min (selects minimum of all non-NODATA contributing pixels) */ GRA_Min=9,
  56. /*! Med (selects median of all non-NODATA contributing pixels) */ GRA_Med=10,
  57. /*! Q1 (selects first quartile of all non-NODATA contributing pixels) */ GRA_Q1=11,
  58. /*! Q3 (selects third quartile of all non-NODATA contributing pixels) */ GRA_Q3=12
  59. } GDALResampleAlg;
  60. /*! GWKAverageOrMode Algorithm */
  61. typedef enum {
  62. /*! Average */ GWKAOM_Average=1,
  63. /*! Mode */ GWKAOM_Fmode=2,
  64. /*! Mode of GDT_Byte, GDT_UInt16, or GDT_Int16 */ GWKAOM_Imode=3,
  65. /*! Maximum */ GWKAOM_Max=4,
  66. /*! Minimum */ GWKAOM_Min=5,
  67. /*! Quantile */ GWKAOM_Quant=6
  68. } GWKAverageOrModeAlg;
  69. typedef int
  70. (*GDALMaskFunc)( void *pMaskFuncArg,
  71. int nBandCount, GDALDataType eType,
  72. int nXOff, int nYOff,
  73. int nXSize, int nYSize,
  74. GByte **papabyImageData,
  75. int bMaskIsFloat, void *pMask );
  76. CPLErr CPL_DLL
  77. GDALWarpNoDataMasker( void *pMaskFuncArg, int nBandCount, GDALDataType eType,
  78. int nXOff, int nYOff, int nXSize, int nYSize,
  79. GByte **papabyImageData, int bMaskIsFloat,
  80. void *pValidityMask, int* pbOutAllValid );
  81. CPLErr CPL_DLL
  82. GDALWarpDstAlphaMasker( void *pMaskFuncArg, int nBandCount, GDALDataType eType,
  83. int nXOff, int nYOff, int nXSize, int nYSize,
  84. GByte ** /*ppImageData */,
  85. int bMaskIsFloat, void *pValidityMask );
  86. CPLErr CPL_DLL
  87. GDALWarpSrcAlphaMasker( void *pMaskFuncArg, int nBandCount, GDALDataType eType,
  88. int nXOff, int nYOff, int nXSize, int nYSize,
  89. GByte ** /*ppImageData */,
  90. int bMaskIsFloat, void *pValidityMask, int* pbOutAllOpaque );
  91. CPLErr CPL_DLL
  92. GDALWarpSrcMaskMasker( void *pMaskFuncArg, int nBandCount, GDALDataType eType,
  93. int nXOff, int nYOff, int nXSize, int nYSize,
  94. GByte ** /*ppImageData */,
  95. int bMaskIsFloat, void *pValidityMask );
  96. CPLErr CPL_DLL
  97. GDALWarpCutlineMasker( void *pMaskFuncArg, int nBandCount, GDALDataType eType,
  98. int nXOff, int nYOff, int nXSize, int nYSize,
  99. GByte ** /* ppImageData */,
  100. int bMaskIsFloat, void *pValidityMask );
  101. /************************************************************************/
  102. /* GDALWarpOptions */
  103. /************************************************************************/
  104. /** Warp control options for use with GDALWarpOperation::Initialize() */
  105. typedef struct {
  106. char **papszWarpOptions;
  107. /*! In bytes, 0.0 for internal default */
  108. double dfWarpMemoryLimit;
  109. /*! Resampling algorithm to use */
  110. GDALResampleAlg eResampleAlg;
  111. /*! data type to use during warp operation, GDT_Unknown lets the algorithm
  112. select the type */
  113. GDALDataType eWorkingDataType;
  114. /*! Source image dataset. */
  115. GDALDatasetH hSrcDS;
  116. /*! Destination image dataset - may be NULL if only using GDALWarpOperation::WarpRegionToBuffer(). */
  117. GDALDatasetH hDstDS;
  118. /*! Number of bands to process, may be 0 to select all bands. */
  119. int nBandCount;
  120. /*! The band numbers for the source bands to process (1 based) */
  121. int *panSrcBands;
  122. /*! The band numbers for the destination bands to process (1 based) */
  123. int *panDstBands;
  124. /*! The source band so use as an alpha (transparency) value, 0=disabled */
  125. int nSrcAlphaBand;
  126. /*! The dest. band so use as an alpha (transparency) value, 0=disabled */
  127. int nDstAlphaBand;
  128. /*! The "nodata" value real component for each input band, if NULL there isn't one */
  129. double *padfSrcNoDataReal;
  130. /*! The "nodata" value imaginary component - may be NULL even if real
  131. component is provided. */
  132. double *padfSrcNoDataImag;
  133. /*! The "nodata" value real component for each output band, if NULL there isn't one */
  134. double *padfDstNoDataReal;
  135. /*! The "nodata" value imaginary component - may be NULL even if real
  136. component is provided. */
  137. double *padfDstNoDataImag;
  138. /*! GDALProgressFunc() compatible progress reporting function, or NULL
  139. if there isn't one. */
  140. GDALProgressFunc pfnProgress;
  141. /*! Callback argument to be passed to pfnProgress. */
  142. void *pProgressArg;
  143. /*! Type of spatial point transformer function */
  144. GDALTransformerFunc pfnTransformer;
  145. /*! Handle to image transformer setup structure */
  146. void *pTransformerArg;
  147. GDALMaskFunc *papfnSrcPerBandValidityMaskFunc;
  148. void **papSrcPerBandValidityMaskFuncArg;
  149. GDALMaskFunc pfnSrcValidityMaskFunc;
  150. void *pSrcValidityMaskFuncArg;
  151. GDALMaskFunc pfnSrcDensityMaskFunc;
  152. void *pSrcDensityMaskFuncArg;
  153. GDALMaskFunc pfnDstDensityMaskFunc;
  154. void *pDstDensityMaskFuncArg;
  155. GDALMaskFunc pfnDstValidityMaskFunc;
  156. void *pDstValidityMaskFuncArg;
  157. CPLErr (*pfnPreWarpChunkProcessor)( void *pKern, void *pArg );
  158. void *pPreWarpProcessorArg;
  159. CPLErr (*pfnPostWarpChunkProcessor)( void *pKern, void *pArg);
  160. void *pPostWarpProcessorArg;
  161. /*! Optional OGRPolygonH for a masking cutline. */
  162. void *hCutline;
  163. /*! Optional blending distance to apply across cutline in pixels, default is zero. */
  164. double dfCutlineBlendDist;
  165. } GDALWarpOptions;
  166. GDALWarpOptions CPL_DLL * CPL_STDCALL GDALCreateWarpOptions(void);
  167. void CPL_DLL CPL_STDCALL GDALDestroyWarpOptions( GDALWarpOptions * );
  168. GDALWarpOptions CPL_DLL * CPL_STDCALL
  169. GDALCloneWarpOptions( const GDALWarpOptions * );
  170. CPLXMLNode CPL_DLL * CPL_STDCALL
  171. GDALSerializeWarpOptions( const GDALWarpOptions * );
  172. GDALWarpOptions CPL_DLL * CPL_STDCALL
  173. GDALDeserializeWarpOptions( CPLXMLNode * );
  174. /************************************************************************/
  175. /* GDALReprojectImage() */
  176. /************************************************************************/
  177. CPLErr CPL_DLL CPL_STDCALL
  178. GDALReprojectImage( GDALDatasetH hSrcDS, const char *pszSrcWKT,
  179. GDALDatasetH hDstDS, const char *pszDstWKT,
  180. GDALResampleAlg eResampleAlg, double dfWarpMemoryLimit,
  181. double dfMaxError,
  182. GDALProgressFunc pfnProgress, void *pProgressArg,
  183. GDALWarpOptions *psOptions );
  184. CPLErr CPL_DLL CPL_STDCALL
  185. GDALCreateAndReprojectImage( GDALDatasetH hSrcDS, const char *pszSrcWKT,
  186. const char *pszDstFilename, const char *pszDstWKT,
  187. GDALDriverH hDstDriver, char **papszCreateOptions,
  188. GDALResampleAlg eResampleAlg, double dfWarpMemoryLimit,
  189. double dfMaxError,
  190. GDALProgressFunc pfnProgress, void *pProgressArg,
  191. GDALWarpOptions *psOptions );
  192. /************************************************************************/
  193. /* VRTWarpedDataset */
  194. /************************************************************************/
  195. GDALDatasetH CPL_DLL CPL_STDCALL
  196. GDALAutoCreateWarpedVRT( GDALDatasetH hSrcDS,
  197. const char *pszSrcWKT, const char *pszDstWKT,
  198. GDALResampleAlg eResampleAlg,
  199. double dfMaxError, const GDALWarpOptions *psOptions );
  200. GDALDatasetH CPL_DLL CPL_STDCALL
  201. GDALCreateWarpedVRT( GDALDatasetH hSrcDS,
  202. int nPixels, int nLines, double *padfGeoTransform,
  203. GDALWarpOptions *psOptions );
  204. CPLErr CPL_DLL CPL_STDCALL
  205. GDALInitializeWarpedVRT( GDALDatasetH hDS,
  206. GDALWarpOptions *psWO );
  207. CPL_C_END
  208. #ifdef __cplusplus
  209. /************************************************************************/
  210. /* GDALWarpKernel */
  211. /* */
  212. /* This class represents the lowest level of abstraction. It */
  213. /* is holds the imagery for one "chunk" of a warp, and the */
  214. /* pre-prepared masks. All IO is done before and after it's */
  215. /* operation. This class is not normally used by the */
  216. /* application. */
  217. /************************************************************************/
  218. // This is the number of dummy pixels that must be reserved in source arrays
  219. // in order to satisfy assumptions made in GWKResample(), and more specifically
  220. // by GWKGetPixelRow() that always read a even number of pixels. So if we are
  221. // in the situation to read the last pixel of the source array, we need 1 extra
  222. // dummy pixel to avoid reading out of bounds.
  223. #define WARP_EXTRA_ELTS 1
  224. class CPL_DLL GDALWarpKernel
  225. {
  226. public:
  227. char **papszWarpOptions;
  228. GDALResampleAlg eResample;
  229. GDALDataType eWorkingDataType;
  230. int nBands;
  231. int nSrcXSize;
  232. int nSrcYSize;
  233. int nSrcXExtraSize; /* extra pixels (included in nSrcXSize) reserved for filter window. Should be ignored in scale computation */
  234. int nSrcYExtraSize; /* extra pixels (included in nSrcYSize) reserved for filter window. Should be ignored in scale computation */
  235. GByte **papabySrcImage; /* each subarray must have WARP_EXTRA_ELTS at the end */
  236. GUInt32 **papanBandSrcValid; /* each subarray must have WARP_EXTRA_ELTS at the end */
  237. GUInt32 *panUnifiedSrcValid; /* must have WARP_EXTRA_ELTS at the end */
  238. float *pafUnifiedSrcDensity; /* must have WARP_EXTRA_ELTS at the end */
  239. int nDstXSize;
  240. int nDstYSize;
  241. GByte **papabyDstImage;
  242. GUInt32 *panDstValid;
  243. float *pafDstDensity;
  244. double dfXScale; // Resampling scale, i.e.
  245. double dfYScale; // nDstSize/nSrcSize.
  246. double dfXFilter; // Size of filter kernel.
  247. double dfYFilter;
  248. int nXRadius; // Size of window to filter.
  249. int nYRadius;
  250. int nFiltInitX; // Filtering offset
  251. int nFiltInitY;
  252. int nSrcXOff;
  253. int nSrcYOff;
  254. int nDstXOff;
  255. int nDstYOff;
  256. GDALTransformerFunc pfnTransformer;
  257. void *pTransformerArg;
  258. GDALProgressFunc pfnProgress;
  259. void *pProgress;
  260. double dfProgressBase;
  261. double dfProgressScale;
  262. double *padfDstNoDataReal;
  263. GDALWarpKernel();
  264. virtual ~GDALWarpKernel();
  265. CPLErr Validate();
  266. CPLErr PerformWarp();
  267. };
  268. /************************************************************************/
  269. /* GDALWarpOperation() */
  270. /* */
  271. /* This object is application created, or created by a higher */
  272. /* level convenience function. It is responsible for */
  273. /* subdividing the operation into chunks, loading and saving */
  274. /* imagery, and establishing the varios validity and density */
  275. /* masks. Actual resampling is done by the GDALWarpKernel. */
  276. /************************************************************************/
  277. typedef struct _GDALWarpChunk GDALWarpChunk;
  278. class CPL_DLL GDALWarpOperation {
  279. private:
  280. GDALWarpOptions *psOptions;
  281. void WipeOptions();
  282. int ValidateOptions();
  283. CPLErr ComputeSourceWindow( int nDstXOff, int nDstYOff,
  284. int nDstXSize, int nDstYSize,
  285. int *pnSrcXOff, int *pnSrcYOff,
  286. int *pnSrcXSize, int *pnSrcYSize,
  287. int *pnSrcXExtraSize, int *pnSrcYExtraSize,
  288. double* pdfSrcFillRatio );
  289. CPLErr CreateKernelMask( GDALWarpKernel *, int iBand,
  290. const char *pszType );
  291. CPLMutex *hIOMutex;
  292. CPLMutex *hWarpMutex;
  293. int nChunkListCount;
  294. int nChunkListMax;
  295. GDALWarpChunk *pasChunkList;
  296. int bReportTimings;
  297. unsigned long nLastTimeReported;
  298. void WipeChunkList();
  299. CPLErr CollectChunkList( int nDstXOff, int nDstYOff,
  300. int nDstXSize, int nDstYSize );
  301. void ReportTiming( const char * );
  302. public:
  303. GDALWarpOperation();
  304. virtual ~GDALWarpOperation();
  305. CPLErr Initialize( const GDALWarpOptions *psNewOptions );
  306. const GDALWarpOptions *GetOptions();
  307. CPLErr ChunkAndWarpImage( int nDstXOff, int nDstYOff,
  308. int nDstXSize, int nDstYSize );
  309. CPLErr ChunkAndWarpMulti( int nDstXOff, int nDstYOff,
  310. int nDstXSize, int nDstYSize );
  311. CPLErr WarpRegion( int nDstXOff, int nDstYOff,
  312. int nDstXSize, int nDstYSize,
  313. int nSrcXOff=0, int nSrcYOff=0,
  314. int nSrcXSize=0, int nSrcYSize=0,
  315. double dfProgressBase=0.0, double dfProgressScale=1.0);
  316. CPLErr WarpRegion( int nDstXOff, int nDstYOff,
  317. int nDstXSize, int nDstYSize,
  318. int nSrcXOff, int nSrcYOff,
  319. int nSrcXSize, int nSrcYSize,
  320. int nSrcXExtraSize, int nSrcYExtraSize,
  321. double dfProgressBase, double dfProgressScale);
  322. CPLErr WarpRegionToBuffer( int nDstXOff, int nDstYOff,
  323. int nDstXSize, int nDstYSize,
  324. void *pDataBuf,
  325. GDALDataType eBufDataType,
  326. int nSrcXOff=0, int nSrcYOff=0,
  327. int nSrcXSize=0, int nSrcYSize=0,
  328. double dfProgressBase=0.0, double dfProgressScale=1.0);
  329. CPLErr WarpRegionToBuffer( int nDstXOff, int nDstYOff,
  330. int nDstXSize, int nDstYSize,
  331. void *pDataBuf,
  332. GDALDataType eBufDataType,
  333. int nSrcXOff, int nSrcYOff,
  334. int nSrcXSize, int nSrcYSize,
  335. int nSrcXExtraSize, int nSrcYExtraSize,
  336. double dfProgressBase, double dfProgressScale);
  337. };
  338. #endif /* def __cplusplus */
  339. CPL_C_START
  340. typedef void * GDALWarpOperationH;
  341. GDALWarpOperationH CPL_DLL GDALCreateWarpOperation(const GDALWarpOptions* );
  342. void CPL_DLL GDALDestroyWarpOperation( GDALWarpOperationH );
  343. CPLErr CPL_DLL GDALChunkAndWarpImage( GDALWarpOperationH, int, int, int, int );
  344. CPLErr CPL_DLL GDALChunkAndWarpMulti( GDALWarpOperationH, int, int, int, int );
  345. CPLErr CPL_DLL GDALWarpRegion( GDALWarpOperationH,
  346. int, int, int, int, int, int, int, int );
  347. CPLErr CPL_DLL GDALWarpRegionToBuffer( GDALWarpOperationH, int, int, int, int,
  348. void *, GDALDataType,
  349. int, int, int, int );
  350. /************************************************************************/
  351. /* Warping kernel functions */
  352. /************************************************************************/
  353. int GWKGetFilterRadius(GDALResampleAlg eResampleAlg);
  354. typedef double (*FilterFuncType)(double dfX);
  355. FilterFuncType GWKGetFilterFunc(GDALResampleAlg eResampleAlg);
  356. typedef double (*FilterFunc4ValuesType)(double* padfVals);
  357. FilterFunc4ValuesType GWKGetFilterFunc4Values(GDALResampleAlg eResampleAlg);
  358. CPL_C_END
  359. #endif /* ndef GDAL_ALG_H_INCLUDED */