Line data Source code
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 : * SPDX-License-Identifier: MIT
14 : ****************************************************************************/
15 :
16 : #ifndef GDAL_ALG_PRIV_H_INCLUDED
17 : #define GDAL_ALG_PRIV_H_INCLUDED
18 :
19 : #ifndef DOXYGEN_SKIP
20 :
21 : #include <cstdint>
22 :
23 : #include <set>
24 :
25 : #include "gdal_alg.h"
26 : #include "ogr_spatialref.h"
27 :
28 : CPL_C_START
29 :
30 : /** Source of the burn value */
31 : typedef enum
32 : {
33 : /*! Use value from padfBurnValue */ GBV_UserBurnValue = 0,
34 : /*! Use value from the Z coordinate */ GBV_Z = 1,
35 : /*! Use value form the M value */ GBV_M = 2
36 : } GDALBurnValueSrc;
37 :
38 : typedef enum
39 : {
40 : GRMA_Replace = 0,
41 : GRMA_Add = 1,
42 : } GDALRasterMergeAlg;
43 :
44 : typedef struct
45 : {
46 : unsigned char *pabyChunkBuf;
47 : int nXSize;
48 : int nYSize;
49 : int nBands;
50 : GDALDataType eType;
51 : int nPixelSpace;
52 : GSpacing nLineSpace;
53 : GSpacing nBandSpace;
54 : GDALDataType eBurnValueType;
55 :
56 : union
57 : {
58 : const std::int64_t *int64_values;
59 : const double *double_values;
60 : } burnValues;
61 :
62 : GDALBurnValueSrc eBurnValueSource;
63 : GDALRasterMergeAlg eMergeAlg;
64 : bool bFillSetVisitedPoints;
65 : std::set<uint64_t> *poSetVisitedPoints;
66 : } GDALRasterizeInfo;
67 :
68 : typedef enum
69 : {
70 : GRO_Raster = 0,
71 : GRO_Vector = 1,
72 : GRO_Auto = 2,
73 : } GDALRasterizeOptim;
74 :
75 : /************************************************************************/
76 : /* Low level rasterizer API. */
77 : /************************************************************************/
78 :
79 : typedef void (*llScanlineFunc)(GDALRasterizeInfo *, int, int, int, double);
80 : typedef void (*llPointFunc)(GDALRasterizeInfo *, int, int, double);
81 :
82 : void GDALdllImagePoint(int nRasterXSize, int nRasterYSize, int nPartCount,
83 : const int *panPartSize, const double *padfX,
84 : const double *padfY, const double *padfVariant,
85 : llPointFunc pfnPointFunc, GDALRasterizeInfo *pCBData);
86 :
87 : void GDALdllImageLine(int nRasterXSize, int nRasterYSize, int nPartCount,
88 : const int *panPartSize, const double *padfX,
89 : const double *padfY, const double *padfVariant,
90 : llPointFunc pfnPointFunc, GDALRasterizeInfo *pCBData);
91 :
92 : void GDALdllImageLineAllTouched(
93 : int nRasterXSize, int nRasterYSize, int nPartCount, const int *panPartSize,
94 : const double *padfX, const double *padfY, const double *padfVariant,
95 : llPointFunc pfnPointFunc, GDALRasterizeInfo *pCBData,
96 : bool bAvoidBurningSamePoints, bool bIntersectOnly);
97 :
98 : void GDALdllImageFilledPolygon(int nRasterXSize, int nRasterYSize,
99 : int nPartCount, const int *panPartSize,
100 : const double *padfX, const double *padfY,
101 : const double *padfVariant,
102 : llScanlineFunc pfnScanlineFunc,
103 : GDALRasterizeInfo *pCBData,
104 : bool bAvoidBurningSamePoints);
105 :
106 : CPL_C_END
107 :
108 : /************************************************************************/
109 : /* Polygon Enumerator */
110 : /************************************************************************/
111 :
112 : #define GP_NODATA_MARKER -51502112
113 :
114 : template <class DataType, class EqualityTest> class GDALRasterPolygonEnumeratorT
115 :
116 : {
117 : private:
118 : void MergePolygon(int nSrcId, int nDstId);
119 : int NewPolygon(DataType nValue);
120 :
121 : CPL_DISALLOW_COPY_ASSIGN(GDALRasterPolygonEnumeratorT)
122 :
123 : public: // these are intended to be readonly.
124 : GInt32 *panPolyIdMap = nullptr;
125 : DataType *panPolyValue = nullptr;
126 :
127 : int nNextPolygonId = 0;
128 : int nPolyAlloc = 0;
129 :
130 : int nConnectedness = 0;
131 :
132 : public:
133 : explicit GDALRasterPolygonEnumeratorT(int nConnectedness = 4);
134 : ~GDALRasterPolygonEnumeratorT();
135 :
136 : bool ProcessLine(DataType *panLastLineVal, DataType *panThisLineVal,
137 : GInt32 *panLastLineId, GInt32 *panThisLineId, int nXSize);
138 :
139 : void CompleteMerges();
140 :
141 : void Clear();
142 : };
143 :
144 : struct IntEqualityTest
145 : {
146 1536900 : bool operator()(std::int64_t a, std::int64_t b) const
147 : {
148 1536900 : return a == b;
149 : }
150 : };
151 :
152 : typedef GDALRasterPolygonEnumeratorT<std::int64_t, IntEqualityTest>
153 : GDALRasterPolygonEnumerator;
154 :
155 : constexpr const char *GDAL_APPROX_TRANSFORMER_CLASS_NAME =
156 : "GDALApproxTransformer";
157 : constexpr const char *GDAL_GEN_IMG_TRANSFORMER_CLASS_NAME =
158 : "GDALGenImgProjTransformer";
159 : constexpr const char *GDAL_RPC_TRANSFORMER_CLASS_NAME = "GDALRPCTransformer";
160 :
161 : bool GDALIsTransformer(void *hTransformerArg, const char *pszClassName);
162 :
163 : typedef void *(*GDALTransformDeserializeFunc)(CPLXMLNode *psTree);
164 :
165 : void CPL_DLL *GDALRegisterTransformDeserializer(
166 : const char *pszTransformName, GDALTransformerFunc pfnTransformerFunc,
167 : GDALTransformDeserializeFunc pfnDeserializeFunc);
168 : void CPL_DLL GDALUnregisterTransformDeserializer(void *pData);
169 :
170 : void GDALCleanupTransformDeserializerMutex();
171 :
172 : /* Transformer cloning */
173 :
174 : void *GDALCreateTPSTransformerInt(int nGCPCount, const GDAL_GCP *pasGCPList,
175 : int bReversed, char **papszOptions);
176 :
177 : void CPL_DLL *GDALCloneTransformer(void *pTransformerArg);
178 :
179 : void GDALRefreshGenImgProjTransformer(void *hTransformArg);
180 : void GDALRefreshApproxTransformer(void *hTransformArg);
181 :
182 : int GDALTransformLonLatToDestGenImgProjTransformer(void *hTransformArg,
183 : double *pdfX, double *pdfY);
184 : int GDALTransformLonLatToDestApproxTransformer(void *hTransformArg,
185 : double *pdfX, double *pdfY);
186 :
187 : bool GDALTransformIsTranslationOnPixelBoundaries(
188 : GDALTransformerFunc pfnTransformer, void *pTransformerArg);
189 :
190 : bool GDALTransformIsAffineNoRotation(GDALTransformerFunc pfnTransformer,
191 : void *pTransformerArg);
192 :
193 : bool GDALTransformHasFastClone(void *pTransformerArg);
194 :
195 : typedef struct _CPLQuadTree CPLQuadTree;
196 :
197 : typedef struct
198 : {
199 : GDALTransformerInfo sTI;
200 :
201 : bool bReversed;
202 : double dfOversampleFactor;
203 :
204 : // Map from target georef coordinates back to geolocation array
205 : // pixel line coordinates. Built only if needed.
206 : int nBackMapWidth;
207 : int nBackMapHeight;
208 : double adfBackMapGeoTransform[6]; // Maps georef to pixel/line.
209 :
210 : bool bUseArray;
211 : void *pAccessors;
212 :
213 : // Geolocation bands.
214 : GDALDatasetH hDS_X;
215 : GDALRasterBandH hBand_X;
216 : GDALDatasetH hDS_Y;
217 : GDALRasterBandH hBand_Y;
218 : int bSwapXY;
219 :
220 : // Located geolocation data.
221 : int nGeoLocXSize;
222 : int nGeoLocYSize;
223 : double dfMinX;
224 : double dfYAtMinX;
225 : double dfMinY;
226 : double dfXAtMinY;
227 : double dfMaxX;
228 : double dfYAtMaxX;
229 : double dfMaxY;
230 : double dfXAtMaxY;
231 :
232 : int bHasNoData;
233 : double dfNoDataX;
234 :
235 : // Geolocation <-> base image mapping.
236 : double dfPIXEL_OFFSET;
237 : double dfPIXEL_STEP;
238 : double dfLINE_OFFSET;
239 : double dfLINE_STEP;
240 :
241 : bool bOriginIsTopLeftCorner;
242 : bool bGeographicSRSWithMinus180Plus180LongRange;
243 : CPLQuadTree *hQuadTree;
244 :
245 : char **papszGeolocationInfo;
246 :
247 : } GDALGeoLocTransformInfo;
248 :
249 : /************************************************************************/
250 : /* ==================================================================== */
251 : /* GDALReprojectionTransformer */
252 : /* ==================================================================== */
253 : /************************************************************************/
254 :
255 : struct GDALReprojectionTransformInfo
256 : {
257 : GDALTransformerInfo sTI;
258 : char **papszOptions = nullptr;
259 : double dfTime = 0.0;
260 :
261 : OGRCoordinateTransformation *poForwardTransform = nullptr;
262 : OGRCoordinateTransformation *poReverseTransform = nullptr;
263 :
264 908 : GDALReprojectionTransformInfo() : sTI()
265 : {
266 908 : memset(&sTI, 0, sizeof(sTI));
267 908 : }
268 :
269 : GDALReprojectionTransformInfo(const GDALReprojectionTransformInfo &) =
270 : delete;
271 : GDALReprojectionTransformInfo &
272 : operator=(const GDALReprojectionTransformInfo &) = delete;
273 : };
274 :
275 : /************************************************************************/
276 : /* ==================================================================== */
277 : /* GDALGenImgProjTransformer */
278 : /* ==================================================================== */
279 : /************************************************************************/
280 :
281 : typedef struct
282 : {
283 :
284 : GDALTransformerInfo sTI;
285 :
286 : double adfSrcGeoTransform[6];
287 : double adfSrcInvGeoTransform[6];
288 :
289 : void *pSrcTransformArg;
290 : GDALTransformerFunc pSrcTransformer;
291 :
292 : void *pReprojectArg;
293 : GDALTransformerFunc pReproject;
294 :
295 : double adfDstGeoTransform[6];
296 : double adfDstInvGeoTransform[6];
297 :
298 : void *pDstTransformArg;
299 : GDALTransformerFunc pDstTransformer;
300 :
301 : // Memorize the value of the CHECK_WITH_INVERT_PROJ at the time we
302 : // instantiated the object, to be able to decide if
303 : // GDALRefreshGenImgProjTransformer() must do something or not.
304 : bool bCheckWithInvertPROJ;
305 :
306 : // Set to TRUE when the transformation pipline is a custom one.
307 : bool bHasCustomTransformationPipeline;
308 :
309 : } GDALGenImgProjTransformInfo;
310 :
311 : /************************************************************************/
312 : /* Color table related */
313 : /************************************************************************/
314 :
315 : // Definitions exists for T = GUInt32 and T = GUIntBig.
316 : template <class T>
317 : int GDALComputeMedianCutPCTInternal(
318 : GDALRasterBandH hRed, GDALRasterBandH hGreen, GDALRasterBandH hBlue,
319 : GByte *pabyRedBand, GByte *pabyGreenBand, GByte *pabyBlueBand,
320 : int (*pfnIncludePixel)(int, int, void *), int nColors, int nBits,
321 : T *panHistogram, GDALColorTableH hColorTable, GDALProgressFunc pfnProgress,
322 : void *pProgressArg);
323 :
324 : int GDALDitherRGB2PCTInternal(GDALRasterBandH hRed, GDALRasterBandH hGreen,
325 : GDALRasterBandH hBlue, GDALRasterBandH hTarget,
326 : GDALColorTableH hColorTable, int nBits,
327 : GInt16 *pasDynamicColorMap, int bDither,
328 : GDALProgressFunc pfnProgress, void *pProgressArg);
329 :
330 : #define PRIME_FOR_65536 98317
331 :
332 : // See HashHistogram structure in gdalmediancut.cpp and ColorIndex structure in
333 : // gdaldither.cpp 6 * sizeof(int) should be the size of the largest of both
334 : // structures.
335 : #define MEDIAN_CUT_AND_DITHER_BUFFER_SIZE_65536 \
336 : (6 * sizeof(int) * PRIME_FOR_65536)
337 :
338 : /************************************************************************/
339 : /* Float comparison function. */
340 : /************************************************************************/
341 :
342 : /**
343 : * Units in the Last Place. This specifies how big an error we are willing to
344 : * accept in terms of the value of the least significant digit of the floating
345 : * point number’s representation. MAX_ULPS can also be interpreted in terms of
346 : * how many representable floats we are willing to accept between A and B.
347 : */
348 : #define MAX_ULPS 10
349 :
350 : GBool GDALFloatEquals(float A, float B);
351 :
352 : struct FloatEqualityTest
353 : {
354 88 : bool operator()(float a, float b)
355 : {
356 88 : return GDALFloatEquals(a, b) == TRUE;
357 : }
358 : };
359 :
360 : bool GDALComputeAreaOfInterest(OGRSpatialReference *poSRS, double adfGT[6],
361 : int nXSize, int nYSize,
362 : double &dfWestLongitudeDeg,
363 : double &dfSouthLatitudeDeg,
364 : double &dfEastLongitudeDeg,
365 : double &dfNorthLatitudeDeg);
366 :
367 : bool GDALComputeAreaOfInterest(OGRSpatialReference *poSRS, double dfX1,
368 : double dfY1, double dfX2, double dfY2,
369 : double &dfWestLongitudeDeg,
370 : double &dfSouthLatitudeDeg,
371 : double &dfEastLongitudeDeg,
372 : double &dfNorthLatitudeDeg);
373 :
374 : CPLStringList GDALCreateGeolocationMetadata(GDALDatasetH hBaseDS,
375 : const char *pszGeolocationDataset,
376 : bool bIsSource);
377 :
378 : void *GDALCreateGeoLocTransformerEx(GDALDatasetH hBaseDS,
379 : CSLConstList papszGeolocationInfo,
380 : int bReversed, const char *pszSourceDataset,
381 : CSLConstList papszTransformOptions);
382 :
383 : #endif /* #ifndef DOXYGEN_SKIP */
384 :
385 : #endif /* ndef GDAL_ALG_PRIV_H_INCLUDED */
|