Line data Source code
1 : /******************************************************************************
2 : * $Id$
3 : *
4 : * Project: GDAL Pansharpening module
5 : * Purpose: Prototypes, and definitions for pansharpening related work.
6 : * Author: Even Rouault <even.rouault at spatialys.com>
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2015, Even Rouault <even.rouault at spatialys.com>
10 : *
11 : * SPDX-License-Identifier: MIT
12 : ****************************************************************************/
13 :
14 : #ifndef GDALPANSHARPEN_H_INCLUDED
15 : #define GDALPANSHARPEN_H_INCLUDED
16 :
17 : #include "gdal.h"
18 :
19 : CPL_C_START
20 :
21 : /**
22 : * \file gdalpansharpen.h
23 : *
24 : * GDAL pansharpening related entry points and definitions.
25 : *
26 : * @since GDAL 2.1
27 : */
28 :
29 : /** Pansharpening algorithms.
30 : */
31 : typedef enum
32 : {
33 : /*! Weighted Brovery. */
34 : GDAL_PSH_WEIGHTED_BROVEY
35 : } GDALPansharpenAlg;
36 :
37 : /** Pansharpening options.
38 : */
39 : typedef struct
40 : {
41 : /*! Pan sharpening algorithm/method. Only weighed Brovey for now. */
42 : GDALPansharpenAlg ePansharpenAlg;
43 :
44 : /*! Resampling algorithm to upsample spectral bands to pan band resolution.
45 : */
46 : GDALRIOResampleAlg eResampleAlg;
47 :
48 : /*! Bit depth of the spectral bands. Can be let to 0 for default behavior.
49 : */
50 : int nBitDepth;
51 :
52 : /*! Number of weight coefficients in padfWeights. */
53 : int nWeightCount;
54 :
55 : /*! Array of nWeightCount weights used by weighted Brovey. */
56 : double *padfWeights;
57 :
58 : /*! Panchromatic band. */
59 : GDALRasterBandH hPanchroBand;
60 :
61 : /*! Number of input spectral bands. */
62 : int nInputSpectralBands;
63 :
64 : /** Array of nInputSpectralBands input spectral bands. The spectral band
65 : * have generally a coarser resolution than the panchromatic band, but they
66 : * are assumed to have the same spatial extent (and projection) at that
67 : * point. Necessary spatial adjustments must be done beforehand, for example
68 : * by wrapping inside a VRT dataset.
69 : */
70 : GDALRasterBandH *pahInputSpectralBands;
71 :
72 : /*! Number of output pansharpened spectral bands. */
73 : int nOutPansharpenedBands;
74 :
75 : /*! Array of nOutPansharpendBands values such as panOutPansharpenedBands[k]
76 : * is a value in the range [0,nInputSpectralBands-1] . */
77 : int *panOutPansharpenedBands;
78 :
79 : /*! Whether the panchromatic and spectral bands have a noData value. */
80 : int bHasNoData;
81 :
82 : /** NoData value of the panchromatic and spectral bands (only taken into
83 : account if bHasNoData = TRUE). This will also be use has the output
84 : nodata value. */
85 : double dfNoData;
86 :
87 : /** Number of threads or -1 to mean ALL_CPUS. By default (0), single
88 : * threaded mode is enabled unless the GDAL_NUM_THREADS configuration option
89 : * is set to an integer or ALL_CPUS. */
90 : int nThreads;
91 : } GDALPansharpenOptions;
92 :
93 : GDALPansharpenOptions CPL_DLL *GDALCreatePansharpenOptions(void);
94 : void CPL_DLL GDALDestroyPansharpenOptions(GDALPansharpenOptions *);
95 : GDALPansharpenOptions CPL_DLL *
96 : GDALClonePansharpenOptions(const GDALPansharpenOptions *psOptions);
97 :
98 : /*! Pansharpening operation handle. */
99 : typedef void *GDALPansharpenOperationH;
100 :
101 : GDALPansharpenOperationH CPL_DLL
102 : GDALCreatePansharpenOperation(const GDALPansharpenOptions *);
103 : void CPL_DLL GDALDestroyPansharpenOperation(GDALPansharpenOperationH);
104 : CPLErr CPL_DLL GDALPansharpenProcessRegion(GDALPansharpenOperationH hOperation,
105 : int nXOff, int nYOff, int nXSize,
106 : int nYSize, void *pDataBuf,
107 : GDALDataType eBufDataType);
108 :
109 : CPL_C_END
110 :
111 : #ifdef __cplusplus
112 :
113 : #include <array>
114 : #include <vector>
115 : #include "gdal_priv.h"
116 :
117 : #ifdef DEBUG_TIMING
118 : #include <sys/time.h>
119 : #endif
120 :
121 : class GDALPansharpenOperation;
122 :
123 : //! @cond Doxygen_Suppress
124 : typedef struct
125 : {
126 : GDALPansharpenOperation *poPansharpenOperation;
127 : GDALDataType eWorkDataType;
128 : GDALDataType eBufDataType;
129 : const void *pPanBuffer;
130 : const void *pUpsampledSpectralBuffer;
131 : void *pDataBuf;
132 : size_t nValues;
133 : size_t nBandValues;
134 : GUInt32 nMaxValue;
135 :
136 : #ifdef DEBUG_TIMING
137 : struct timeval *ptv;
138 : #endif
139 :
140 : CPLErr eErr;
141 : } GDALPansharpenJob;
142 :
143 : typedef struct
144 : {
145 : GDALDataset *poMEMDS;
146 : int nXOff;
147 : int nYOff;
148 : int nXSize;
149 : int nYSize;
150 : double dfXOff;
151 : double dfYOff;
152 : double dfXSize;
153 : double dfYSize;
154 : void *pBuffer;
155 : GDALDataType eDT;
156 : int nBufXSize;
157 : int nBufYSize;
158 : int nBandCount;
159 : GDALRIOResampleAlg eResampleAlg;
160 : GSpacing nBandSpace;
161 :
162 : #ifdef DEBUG_TIMING
163 : struct timeval *ptv;
164 : #endif
165 : } GDALPansharpenResampleJob;
166 :
167 : class CPLWorkerThreadPool;
168 :
169 : //! @endcond
170 :
171 : /** Pansharpening operation class.
172 : */
173 56 : class GDALPansharpenOperation
174 : {
175 : CPL_DISALLOW_COPY_ASSIGN(GDALPansharpenOperation)
176 :
177 : GDALPansharpenOptions *psOptions = nullptr;
178 : std::vector<int> anInputBands{};
179 : std::vector<GDALDataset *> aVDS{}; // to destroy
180 : std::vector<GDALRasterBand *> aMSBands{}; // original multispectral bands
181 : // potentially warped into a VRT
182 : int bPositiveWeights = TRUE;
183 : CPLWorkerThreadPool *poThreadPool = nullptr;
184 : int nKernelRadius = 0;
185 : std::array<double, 6> m_adfPanToMSGT = {{0.0, 1.0, 0, 0.0, 0.0, 1.0}};
186 :
187 : static void PansharpenJobThreadFunc(void *pUserData);
188 : static void PansharpenResampleJobThreadFunc(void *pUserData);
189 :
190 : template <class WorkDataType, class OutDataType>
191 : void WeightedBroveyWithNoData(const WorkDataType *pPanBuffer,
192 : const WorkDataType *pUpsampledSpectralBuffer,
193 : OutDataType *pDataBuf, size_t nValues,
194 : size_t nBandValues,
195 : WorkDataType nMaxValue) const;
196 : template <class WorkDataType, class OutDataType, int bHasBitDepth>
197 : void WeightedBrovey3(const WorkDataType *pPanBuffer,
198 : const WorkDataType *pUpsampledSpectralBuffer,
199 : OutDataType *pDataBuf, size_t nValues,
200 : size_t nBandValues, WorkDataType nMaxValue) const;
201 :
202 : // cppcheck-suppress functionStatic
203 : template <class WorkDataType, class OutDataType>
204 : void WeightedBrovey(const WorkDataType *pPanBuffer,
205 : const WorkDataType *pUpsampledSpectralBuffer,
206 : OutDataType *pDataBuf, size_t nValues,
207 : size_t nBandValues, WorkDataType nMaxValue) const;
208 : template <class WorkDataType>
209 : CPLErr WeightedBrovey(const WorkDataType *pPanBuffer,
210 : const WorkDataType *pUpsampledSpectralBuffer,
211 : void *pDataBuf, GDALDataType eBufDataType,
212 : size_t nValues, size_t nBandValues,
213 : WorkDataType nMaxValue) const;
214 :
215 : // cppcheck-suppress functionStatic
216 : template <class WorkDataType>
217 : CPLErr WeightedBrovey(const WorkDataType *pPanBuffer,
218 : const WorkDataType *pUpsampledSpectralBuffer,
219 : void *pDataBuf, GDALDataType eBufDataType,
220 : size_t nValues, size_t nBandValues) const;
221 : template <class T>
222 : void WeightedBroveyPositiveWeights(const T *pPanBuffer,
223 : const T *pUpsampledSpectralBuffer,
224 : T *pDataBuf, size_t nValues,
225 : size_t nBandValues, T nMaxValue) const;
226 :
227 : template <class T, int NINPUT, int NOUTPUT>
228 : size_t WeightedBroveyPositiveWeightsInternal(
229 : const T *pPanBuffer, const T *pUpsampledSpectralBuffer, T *pDataBuf,
230 : size_t nValues, size_t nBandValues, T nMaxValue) const;
231 :
232 : // cppcheck-suppress unusedPrivateFunction
233 : template <class T>
234 : void WeightedBroveyGByteOrUInt16(const T *pPanBuffer,
235 : const T *pUpsampledSpectralBuffer,
236 : T *pDataBuf, size_t nValues,
237 : size_t nBandValues, T nMaxValue) const;
238 :
239 : // cppcheck-suppress functionStatic
240 : CPLErr PansharpenChunk(GDALDataType eWorkDataType,
241 : GDALDataType eBufDataType, const void *pPanBuffer,
242 : const void *pUpsampledSpectralBuffer, void *pDataBuf,
243 : size_t nValues, size_t nBandValues,
244 : GUInt32 nMaxValue) const;
245 :
246 : public:
247 : GDALPansharpenOperation();
248 : ~GDALPansharpenOperation();
249 :
250 : CPLErr Initialize(const GDALPansharpenOptions *psOptions);
251 : CPLErr ProcessRegion(int nXOff, int nYOff, int nXSize, int nYSize,
252 : void *pDataBuf, GDALDataType eBufDataType);
253 : GDALPansharpenOptions *GetOptions();
254 : };
255 :
256 : #endif /* __cplusplus */
257 :
258 : #endif /* GDALPANSHARPEN_H_INCLUDED */
|