GDAL
gdal_alg_priv.h
1 /******************************************************************************
2  * $Id$
3  *
4  * Project: GDAL Image Processing Algorithms
5  * Purpose: Prototypes and definitions for various GDAL based algorithms:
6  * private declarations.
7  * Author: Andrey Kiselev, dron@ak4719.spb.edu
8  *
9  ******************************************************************************
10  * Copyright (c) 2008, Andrey Kiselev <dron@ak4719.spb.edu>
11  * Copyright (c) 2010-2013, Even Rouault <even dot rouault at spatialys.com>
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a
14  * copy of this software and associated documentation files (the "Software"),
15  * to deal in the Software without restriction, including without limitation
16  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  * and/or sell copies of the Software, and to permit persons to whom the
18  * Software is furnished to do so, subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included
21  * in all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29  * DEALINGS IN THE SOFTWARE.
30  ****************************************************************************/
31 
32 #ifndef GDAL_ALG_PRIV_H_INCLUDED
33 #define GDAL_ALG_PRIV_H_INCLUDED
34 
35 #ifndef DOXYGEN_SKIP
36 
37 #include <cstdint>
38 
39 #include <set>
40 
41 #include "gdal_alg.h"
42 #include "ogr_spatialref.h"
43 
45 
47 typedef enum
48 { GBV_UserBurnValue = 0, GBV_Z = 1, GBV_M = 2
52 } GDALBurnValueSrc;
53 
54 typedef enum
55 {
56  GRMA_Replace = 0,
57  GRMA_Add = 1,
58 } GDALRasterMergeAlg;
59 
60 typedef struct
61 {
62  unsigned char *pabyChunkBuf;
63  int nXSize;
64  int nYSize;
65  int nBands;
66  GDALDataType eType;
67  int nPixelSpace;
68  GSpacing nLineSpace;
69  GSpacing nBandSpace;
70  GDALDataType eBurnValueType;
71 
72  union
73  {
74  const std::int64_t *int64_values;
75  const double *double_values;
76  } burnValues;
77 
78  GDALBurnValueSrc eBurnValueSource;
79  GDALRasterMergeAlg eMergeAlg;
80  bool bFillSetVisitedPoints;
81  std::set<uint64_t> *poSetVisitedPoints;
82 } GDALRasterizeInfo;
83 
84 typedef enum
85 {
86  GRO_Raster = 0,
87  GRO_Vector = 1,
88  GRO_Auto = 2,
89 } GDALRasterizeOptim;
90 
91 /************************************************************************/
92 /* Low level rasterizer API. */
93 /************************************************************************/
94 
95 typedef void (*llScanlineFunc)(void *, int, int, int, double);
96 typedef void (*llPointFunc)(void *, int, int, double);
97 
98 void GDALdllImagePoint(int nRasterXSize, int nRasterYSize, int nPartCount,
99  const int *panPartSize, const double *padfX,
100  const double *padfY, const double *padfVariant,
101  llPointFunc pfnPointFunc, void *pCBData);
102 
103 void GDALdllImageLine(int nRasterXSize, int nRasterYSize, int nPartCount,
104  const int *panPartSize, const double *padfX,
105  const double *padfY, const double *padfVariant,
106  llPointFunc pfnPointFunc, void *pCBData);
107 
108 void GDALdllImageLineAllTouched(int nRasterXSize, int nRasterYSize,
109  int nPartCount, const int *panPartSize,
110  const double *padfX, const double *padfY,
111  const double *padfVariant,
112  llPointFunc pfnPointFunc, void *pCBData,
113  bool bAvoidBurningSamePoints,
114  bool bIntersectOnly);
115 
116 void GDALdllImageFilledPolygon(int nRasterXSize, int nRasterYSize,
117  int nPartCount, const int *panPartSize,
118  const double *padfX, const double *padfY,
119  const double *padfVariant,
120  llScanlineFunc pfnScanlineFunc, void *pCBData,
121  bool bAvoidBurningSamePoints);
122 
123 CPL_C_END
124 
125 /************************************************************************/
126 /* Polygon Enumerator */
127 /************************************************************************/
128 
129 #define GP_NODATA_MARKER -51502112
130 
131 template <class DataType, class EqualityTest> class GDALRasterPolygonEnumeratorT
132 
133 {
134  private:
135  void MergePolygon(int nSrcId, int nDstId);
136  int NewPolygon(DataType nValue);
137 
138  CPL_DISALLOW_COPY_ASSIGN(GDALRasterPolygonEnumeratorT)
139 
140  public: // these are intended to be readonly.
141  GInt32 *panPolyIdMap = nullptr;
142  DataType *panPolyValue = nullptr;
143 
144  int nNextPolygonId = 0;
145  int nPolyAlloc = 0;
146 
147  int nConnectedness = 0;
148 
149  public:
150  explicit GDALRasterPolygonEnumeratorT(int nConnectedness = 4);
151  ~GDALRasterPolygonEnumeratorT();
152 
153  bool ProcessLine(DataType *panLastLineVal, DataType *panThisLineVal,
154  GInt32 *panLastLineId, GInt32 *panThisLineId, int nXSize);
155 
156  void CompleteMerges();
157 
158  void Clear();
159 };
160 
161 struct IntEqualityTest
162 {
163  bool operator()(std::int64_t a, std::int64_t b) const
164  {
165  return a == b;
166  }
167 };
168 
169 typedef GDALRasterPolygonEnumeratorT<std::int64_t, IntEqualityTest>
170  GDALRasterPolygonEnumerator;
171 
172 constexpr const char *GDAL_APPROX_TRANSFORMER_CLASS_NAME =
173  "GDALApproxTransformer";
174 constexpr const char *GDAL_GEN_IMG_TRANSFORMER_CLASS_NAME =
175  "GDALGenImgProjTransformer";
176 
177 bool GDALIsTransformer(void *hTransformerArg, const char *pszClassName);
178 
179 typedef void *(*GDALTransformDeserializeFunc)(CPLXMLNode *psTree);
180 
181 void CPL_DLL *GDALRegisterTransformDeserializer(
182  const char *pszTransformName, GDALTransformerFunc pfnTransformerFunc,
183  GDALTransformDeserializeFunc pfnDeserializeFunc);
184 void CPL_DLL GDALUnregisterTransformDeserializer(void *pData);
185 
186 void GDALCleanupTransformDeserializerMutex();
187 
188 /* Transformer cloning */
189 
190 void *GDALCreateTPSTransformerInt(int nGCPCount, const GDAL_GCP *pasGCPList,
191  int bReversed, char **papszOptions);
192 
193 void CPL_DLL *GDALCloneTransformer(void *pTransformerArg);
194 
195 void GDALRefreshGenImgProjTransformer(void *hTransformArg);
196 void GDALRefreshApproxTransformer(void *hTransformArg);
197 
198 int GDALTransformLonLatToDestGenImgProjTransformer(void *hTransformArg,
199  double *pdfX, double *pdfY);
200 int GDALTransformLonLatToDestApproxTransformer(void *hTransformArg,
201  double *pdfX, double *pdfY);
202 
203 bool GDALTransformIsTranslationOnPixelBoundaries(
204  GDALTransformerFunc pfnTransformer, void *pTransformerArg);
205 
206 bool GDALTransformIsAffineNoRotation(GDALTransformerFunc pfnTransformer,
207  void *pTransformerArg);
208 
209 typedef struct _CPLQuadTree CPLQuadTree;
210 
211 typedef struct
212 {
213  GDALTransformerInfo sTI;
214 
215  bool bReversed;
216  double dfOversampleFactor;
217 
218  // Map from target georef coordinates back to geolocation array
219  // pixel line coordinates. Built only if needed.
220  int nBackMapWidth;
221  int nBackMapHeight;
222  double adfBackMapGeoTransform[6]; // Maps georef to pixel/line.
223 
224  bool bUseArray;
225  void *pAccessors;
226 
227  // Geolocation bands.
228  GDALDatasetH hDS_X;
229  GDALRasterBandH hBand_X;
230  GDALDatasetH hDS_Y;
231  GDALRasterBandH hBand_Y;
232  int bSwapXY;
233 
234  // Located geolocation data.
235  int nGeoLocXSize;
236  int nGeoLocYSize;
237  double dfMinX;
238  double dfYAtMinX;
239  double dfMinY;
240  double dfXAtMinY;
241  double dfMaxX;
242  double dfYAtMaxX;
243  double dfMaxY;
244  double dfXAtMaxY;
245 
246  int bHasNoData;
247  double dfNoDataX;
248 
249  // Geolocation <-> base image mapping.
250  double dfPIXEL_OFFSET;
251  double dfPIXEL_STEP;
252  double dfLINE_OFFSET;
253  double dfLINE_STEP;
254 
255  bool bOriginIsTopLeftCorner;
256  bool bGeographicSRSWithMinus180Plus180LongRange;
257  CPLQuadTree *hQuadTree;
258 
259  char **papszGeolocationInfo;
260 
261 } GDALGeoLocTransformInfo;
262 
263 /************************************************************************/
264 /* ==================================================================== */
265 /* GDALReprojectionTransformer */
266 /* ==================================================================== */
267 /************************************************************************/
268 
269 struct GDALReprojectionTransformInfo
270 {
271  GDALTransformerInfo sTI;
272  char **papszOptions = nullptr;
273  double dfTime = 0.0;
274 
275  OGRCoordinateTransformation *poForwardTransform = nullptr;
276  OGRCoordinateTransformation *poReverseTransform = nullptr;
277 
278  GDALReprojectionTransformInfo() : sTI()
279  {
280  memset(&sTI, 0, sizeof(sTI));
281  }
282 
283  GDALReprojectionTransformInfo(const GDALReprojectionTransformInfo &) =
284  delete;
285  GDALReprojectionTransformInfo &
286  operator=(const GDALReprojectionTransformInfo &) = delete;
287 };
288 
289 /************************************************************************/
290 /* ==================================================================== */
291 /* GDALGenImgProjTransformer */
292 /* ==================================================================== */
293 /************************************************************************/
294 
295 typedef struct
296 {
297 
298  GDALTransformerInfo sTI;
299 
300  double adfSrcGeoTransform[6];
301  double adfSrcInvGeoTransform[6];
302 
303  void *pSrcTransformArg;
304  GDALTransformerFunc pSrcTransformer;
305 
306  void *pReprojectArg;
307  GDALTransformerFunc pReproject;
308 
309  double adfDstGeoTransform[6];
310  double adfDstInvGeoTransform[6];
311 
312  void *pDstTransformArg;
313  GDALTransformerFunc pDstTransformer;
314 
315  // Memorize the value of the CHECK_WITH_INVERT_PROJ at the time we
316  // instantiated the object, to be able to decide if
317  // GDALRefreshGenImgProjTransformer() must do something or not.
318  bool bCheckWithInvertPROJ;
319 
320  // Set to TRUE when the transformation pipline is a custom one.
321  bool bHasCustomTransformationPipeline;
322 
323 } GDALGenImgProjTransformInfo;
324 
325 /************************************************************************/
326 /* Color table related */
327 /************************************************************************/
328 
329 // Definitions exists for T = GUInt32 and T = GUIntBig.
330 template <class T>
331 int GDALComputeMedianCutPCTInternal(
332  GDALRasterBandH hRed, GDALRasterBandH hGreen, GDALRasterBandH hBlue,
333  GByte *pabyRedBand, GByte *pabyGreenBand, GByte *pabyBlueBand,
334  int (*pfnIncludePixel)(int, int, void *), int nColors, int nBits,
335  T *panHistogram, GDALColorTableH hColorTable, GDALProgressFunc pfnProgress,
336  void *pProgressArg);
337 
338 int GDALDitherRGB2PCTInternal(GDALRasterBandH hRed, GDALRasterBandH hGreen,
339  GDALRasterBandH hBlue, GDALRasterBandH hTarget,
340  GDALColorTableH hColorTable, int nBits,
341  GInt16 *pasDynamicColorMap, int bDither,
342  GDALProgressFunc pfnProgress, void *pProgressArg);
343 
344 #define PRIME_FOR_65536 98317
345 
346 // See HashHistogram structure in gdalmediancut.cpp and ColorIndex structure in
347 // gdaldither.cpp 6 * sizeof(int) should be the size of the largest of both
348 // structures.
349 #define MEDIAN_CUT_AND_DITHER_BUFFER_SIZE_65536 \
350  (6 * sizeof(int) * PRIME_FOR_65536)
351 
352 /************************************************************************/
353 /* Float comparison function. */
354 /************************************************************************/
355 
362 #define MAX_ULPS 10
363 
364 GBool GDALFloatEquals(float A, float B);
365 
366 struct FloatEqualityTest
367 {
368  bool operator()(float a, float b)
369  {
370  return GDALFloatEquals(a, b) == TRUE;
371  }
372 };
373 
374 bool GDALComputeAreaOfInterest(OGRSpatialReference *poSRS, double adfGT[6],
375  int nXSize, int nYSize,
376  double &dfWestLongitudeDeg,
377  double &dfSouthLatitudeDeg,
378  double &dfEastLongitudeDeg,
379  double &dfNorthLatitudeDeg);
380 
381 bool GDALComputeAreaOfInterest(OGRSpatialReference *poSRS, double dfX1,
382  double dfY1, double dfX2, double dfY2,
383  double &dfWestLongitudeDeg,
384  double &dfSouthLatitudeDeg,
385  double &dfEastLongitudeDeg,
386  double &dfNorthLatitudeDeg);
387 
388 CPLStringList GDALCreateGeolocationMetadata(GDALDatasetH hBaseDS,
389  const char *pszGeolocationDataset,
390  bool bIsSource);
391 
392 void *GDALCreateGeoLocTransformerEx(GDALDatasetH hBaseDS,
393  CSLConstList papszGeolocationInfo,
394  int bReversed, const char *pszSourceDataset,
395  CSLConstList papszTransformOptions);
396 
397 #endif /* #ifndef DOXYGEN_SKIP */
398 
399 #endif /* ndef GDAL_ALG_PRIV_H_INCLUDED */
String list class designed around our use of C "char**" string lists.
Definition: cpl_string.h:449
Interface for transforming between coordinate systems.
Definition: ogr_spatialref.h:787
This class represents an OpenGIS Spatial Reference System, and contains methods for converting betwee...
Definition: ogr_spatialref.h:169
short GInt16
Int16 type.
Definition: cpl_port.h:181
#define CPL_C_END
Macro to end a block of C symbols.
Definition: cpl_port.h:299
#define CPL_C_START
Macro to start a block of C symbols.
Definition: cpl_port.h:295
int GBool
Type for boolean values (alias to int)
Definition: cpl_port.h:196
#define CPL_DISALLOW_COPY_ASSIGN(ClassName)
Helper to remove the copy and assignment constructors so that the compiler will not generate the defa...
Definition: cpl_port.h:1042
char ** CSLConstList
Type of a constant null-terminated list of nul terminated strings.
Definition: cpl_port.h:1183
unsigned char GByte
Unsigned byte type.
Definition: cpl_port.h:185
int GInt32
Int32 type.
Definition: cpl_port.h:175
struct _CPLQuadTree CPLQuadTree
Opaque type for a quad tree.
Definition: cpl_quad_tree.h:65
GIntBig GSpacing
Type to express pixel, line or band spacing.
Definition: gdal.h:315
GDALDataType
Definition: gdal.h:64
void * GDALDatasetH
Opaque type used for the C bindings of the C++ GDALDataset class.
Definition: gdal.h:291
void * GDALRasterBandH
Opaque type used for the C bindings of the C++ GDALRasterBand class.
Definition: gdal.h:294
void * GDALColorTableH
Opaque type used for the C bindings of the C++ GDALColorTable class.
Definition: gdal.h:300
Public (C callable) GDAL algorithm entry points, and definitions.
int(* GDALTransformerFunc)(void *pTransformerArg, int bDstToSrc, int nPointCount, double *x, double *y, double *z, int *panSuccess)
Definition: gdal_alg.h:95
Coordinate systems services.
Document node structure.
Definition: cpl_minixml.h:71
Ground Control Point.
Definition: gdal.h:1075