1 /******************************************************************************
2  * $Id: gdal_alg_priv.h 47fcc5b4d15f1ac172b3b7b60f26f95905fa001a 2020-10-28 00:34:26 +0100 Even Rouault $
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 "gdal_alg.h"
38 #include "ogr_spatialref.h"
39 
40 CPL_C_START
41 
42 /** Source of the burn value */
43 typedef enum {
44     /*! Use value from padfBurnValue */    GBV_UserBurnValue = 0,
45     /*! Use value from the Z coordinate */ GBV_Z = 1,
46     /*! Use value form the M value */    GBV_M = 2
47 } GDALBurnValueSrc;
48 
49 typedef enum {
50     GRMA_Replace = 0,
51     GRMA_Add = 1,
52 } GDALRasterMergeAlg;
53 
54 typedef struct {
55     unsigned char * pabyChunkBuf;
56     int nXSize;
57     int nYSize;
58     int nBands;
59     GDALDataType eType;
60     int nPixelSpace;
61     GSpacing nLineSpace;
62     GSpacing nBandSpace;
63     const double *padfBurnValue;
64     GDALBurnValueSrc eBurnValueSource;
65     GDALRasterMergeAlg eMergeAlg;
66 } GDALRasterizeInfo;
67 
68 typedef enum {
69     GRO_Raster = 0,
70     GRO_Vector = 1,
71     GRO_Auto = 2,
72 } GDALRasterizeOptim;
73 
74 
75 /************************************************************************/
76 /*      Low level rasterizer API.                                       */
77 /************************************************************************/
78 
79 typedef void (*llScanlineFunc)( void *, int, int, int, double );
80 typedef void (*llPointFunc)( void *, int, int, double );
81 
82 void GDALdllImagePoint( int nRasterXSize, int nRasterYSize,
83                         int nPartCount, const int *panPartSize,
84                         const double *padfX, const double *padfY,
85                         const double *padfVariant,
86                         llPointFunc pfnPointFunc, void *pCBData );
87 
88 void GDALdllImageLine( int nRasterXSize, int nRasterYSize,
89                        int nPartCount, const int *panPartSize,
90                        const double *padfX, const double *padfY,
91                        const double *padfVariant,
92                        llPointFunc pfnPointFunc, void *pCBData );
93 
94 void GDALdllImageLineAllTouched( int nRasterXSize, int nRasterYSize,
95                                  int nPartCount, const int *panPartSize,
96                                  const double *padfX, const double *padfY,
97                                  const double *padfVariant,
98                                  llPointFunc pfnPointFunc, void *pCBData,
99                                  int bAvoidBurningSamePoints );
100 
101 void GDALdllImageFilledPolygon( int nRasterXSize, int nRasterYSize,
102                                 int nPartCount, const int *panPartSize,
103                                 const double *padfX, const double *padfY,
104                                 const double *padfVariant,
105                                 llScanlineFunc pfnScanlineFunc, void *pCBData );
106 
107 CPL_C_END
108 
109 /************************************************************************/
110 /*                          Polygon Enumerator                          */
111 /************************************************************************/
112 
113 #define GP_NODATA_MARKER -51502112
114 
115 template<class DataType, class EqualityTest> class GDALRasterPolygonEnumeratorT
116 
117 {
118 private:
119     void     MergePolygon( int nSrcId, int nDstId );
120     int      NewPolygon( DataType nValue );
121 
122     CPL_DISALLOW_COPY_ASSIGN(GDALRasterPolygonEnumeratorT)
123 
124 public:  // these are intended to be readonly.
125 
126     GInt32   *panPolyIdMap = nullptr;
127     DataType   *panPolyValue = nullptr;
128 
129     int      nNextPolygonId = 0;
130     int      nPolyAlloc = 0;
131 
132     int      nConnectedness = 0;
133 
134 public:
135     explicit GDALRasterPolygonEnumeratorT( int nConnectedness=4 );
136             ~GDALRasterPolygonEnumeratorT();
137 
138     void     ProcessLine( DataType *panLastLineVal, DataType *panThisLineVal,
139                           GInt32 *panLastLineId,  GInt32 *panThisLineId,
140                           int nXSize );
141 
142     void     CompleteMerges();
143 
144     void     Clear();
145 };
146 
147 struct IntEqualityTest
148 {
operatorIntEqualityTest149     bool operator()(GInt32 a, GInt32 b) const { return a == b; }
150 };
151 
152 typedef GDALRasterPolygonEnumeratorT<GInt32, IntEqualityTest> GDALRasterPolygonEnumerator;
153 
154 typedef void* (*GDALTransformDeserializeFunc)( CPLXMLNode *psTree );
155 
156 void CPL_DLL *GDALRegisterTransformDeserializer(const char* pszTransformName,
157                                        GDALTransformerFunc pfnTransformerFunc,
158                                        GDALTransformDeserializeFunc pfnDeserializeFunc);
159 void CPL_DLL GDALUnregisterTransformDeserializer(void* pData);
160 
161 void GDALCleanupTransformDeserializerMutex();
162 
163 /* Transformer cloning */
164 
165 void* GDALCreateTPSTransformerInt( int nGCPCount, const GDAL_GCP *pasGCPList,
166                                    int bReversed, char** papszOptions );
167 
168 void CPL_DLL * GDALCloneTransformer( void *pTransformerArg );
169 
170 void GDALRefreshGenImgProjTransformer(void* hTransformArg);
171 void GDALRefreshApproxTransformer(void* hTransformArg);
172 
173 /************************************************************************/
174 /*      Color table related                                             */
175 /************************************************************************/
176 
177 // Definitions exists for T = GUInt32 and T = GUIntBig.
178 template<class T> int
179 GDALComputeMedianCutPCTInternal( GDALRasterBandH hRed,
180                                  GDALRasterBandH hGreen,
181                                  GDALRasterBandH hBlue,
182                                  GByte* pabyRedBand,
183                                  GByte* pabyGreenBand,
184                                  GByte* pabyBlueBand,
185                                  int (*pfnIncludePixel)(int,int,void*),
186                                  int nColors,
187                                  int nBits,
188                                  T* panHistogram,
189                                  GDALColorTableH hColorTable,
190                                  GDALProgressFunc pfnProgress,
191                                  void * pProgressArg );
192 
193 int GDALDitherRGB2PCTInternal( GDALRasterBandH hRed,
194                                GDALRasterBandH hGreen,
195                                GDALRasterBandH hBlue,
196                                GDALRasterBandH hTarget,
197                                GDALColorTableH hColorTable,
198                                int nBits,
199                                GInt16* pasDynamicColorMap,
200                                int bDither,
201                                GDALProgressFunc pfnProgress,
202                                void * pProgressArg );
203 
204 #define PRIME_FOR_65536 98317
205 
206 // See HashHistogram structure in gdalmediancut.cpp and ColorIndex structure in
207 // gdaldither.cpp 6 * sizeof(int) should be the size of the largest of both
208 // structures.
209 #define MEDIAN_CUT_AND_DITHER_BUFFER_SIZE_65536 (6 * sizeof(int) * PRIME_FOR_65536)
210 
211 /************************************************************************/
212 /*      Float comparison function.                                      */
213 /************************************************************************/
214 
215 /**
216  * Units in the Last Place. This specifies how big an error we are willing to
217  * accept in terms of the value of the least significant digit of the floating
218  * point number’s representation. MAX_ULPS can also be interpreted in terms of
219  * how many representable floats we are willing to accept between A and B.
220  */
221 #define MAX_ULPS 10
222 
223 GBool GDALFloatEquals(float A, float B);
224 
225 struct FloatEqualityTest
226 {
operatorFloatEqualityTest227     bool operator()(float a, float b) { return GDALFloatEquals(a,b) == TRUE; }
228 };
229 
230 bool GDALComputeAreaOfInterest(OGRSpatialReference* poSRS,
231                                double adfGT[6],
232                                int nXSize,
233                                int nYSize,
234                                double& dfWestLongitudeDeg,
235                                double& dfSouthLatitudeDeg,
236                                double& dfEastLongitudeDeg,
237                                double& dfNorthLatitudeDeg );
238 
239 bool GDALComputeAreaOfInterest(OGRSpatialReference* poSRS,
240                                double dfX1,
241                                double dfY1,
242                                double dfX2,
243                                double dfY2,
244                                double& dfWestLongitudeDeg,
245                                double& dfSouthLatitudeDeg,
246                                double& dfEastLongitudeDeg,
247                                double& dfNorthLatitudeDeg );
248 
249 
250 #endif /* #ifndef DOXYGEN_SKIP */
251 
252 #endif /* ndef GDAL_ALG_PRIV_H_INCLUDED */
253