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 : struct GDALPansharpenResampleJob
144 : {
145 : GDALDataset *poMEMDS = nullptr;
146 : int nXOff = 0;
147 : int nYOff = 0;
148 : int nXSize = 0;
149 : int nYSize = 0;
150 : double dfXOff = 0;
151 : double dfYOff = 0;
152 : double dfXSize = 0;
153 : double dfYSize = 0;
154 : void *pBuffer = nullptr;
155 : GDALDataType eDT = GDT_Unknown;
156 : int nBufXSize = 0;
157 : int nBufYSize = 0;
158 : int nBandCount = 0;
159 : GDALRIOResampleAlg eResampleAlg = GRIORA_NearestNeighbour;
160 : GSpacing nBandSpace = 0;
161 :
162 : #ifdef DEBUG_TIMING
163 : struct timeval *ptv = nullptr;
164 : #endif
165 :
166 : CPLErr eErr = CE_Failure;
167 : std::string osLastErrorMsg{};
168 : };
169 :
170 : class CPLWorkerThreadPool;
171 :
172 : //! @endcond
173 :
174 : /** Pansharpening operation class.
175 : */
176 57 : class GDALPansharpenOperation
177 : {
178 : CPL_DISALLOW_COPY_ASSIGN(GDALPansharpenOperation)
179 :
180 : GDALPansharpenOptions *psOptions = nullptr;
181 : std::vector<int> anInputBands{};
182 : std::vector<GDALDataset *> aVDS{}; // to destroy
183 : std::vector<GDALRasterBand *> aMSBands{}; // original multispectral bands
184 : // potentially warped into a VRT
185 : int bPositiveWeights = TRUE;
186 : CPLWorkerThreadPool *poThreadPool = nullptr;
187 : int nKernelRadius = 0;
188 : std::array<double, 6> m_adfPanToMSGT = {{0.0, 1.0, 0, 0.0, 0.0, 1.0}};
189 :
190 : static void PansharpenJobThreadFunc(void *pUserData);
191 : static void PansharpenResampleJobThreadFunc(void *pUserData);
192 :
193 : template <class WorkDataType, class OutDataType>
194 : void WeightedBroveyWithNoData(const WorkDataType *pPanBuffer,
195 : const WorkDataType *pUpsampledSpectralBuffer,
196 : OutDataType *pDataBuf, size_t nValues,
197 : size_t nBandValues,
198 : WorkDataType nMaxValue) const;
199 : template <class WorkDataType, class OutDataType, int bHasBitDepth>
200 : void WeightedBrovey3(const WorkDataType *pPanBuffer,
201 : const WorkDataType *pUpsampledSpectralBuffer,
202 : OutDataType *pDataBuf, size_t nValues,
203 : size_t nBandValues, WorkDataType nMaxValue) const;
204 :
205 : // cppcheck-suppress functionStatic
206 : template <class WorkDataType, class OutDataType>
207 : void WeightedBrovey(const WorkDataType *pPanBuffer,
208 : const WorkDataType *pUpsampledSpectralBuffer,
209 : OutDataType *pDataBuf, size_t nValues,
210 : size_t nBandValues, WorkDataType nMaxValue) const;
211 : template <class WorkDataType>
212 : CPLErr WeightedBrovey(const WorkDataType *pPanBuffer,
213 : const WorkDataType *pUpsampledSpectralBuffer,
214 : void *pDataBuf, GDALDataType eBufDataType,
215 : size_t nValues, size_t nBandValues,
216 : WorkDataType nMaxValue) const;
217 :
218 : // cppcheck-suppress functionStatic
219 : template <class WorkDataType>
220 : CPLErr WeightedBrovey(const WorkDataType *pPanBuffer,
221 : const WorkDataType *pUpsampledSpectralBuffer,
222 : void *pDataBuf, GDALDataType eBufDataType,
223 : size_t nValues, size_t nBandValues) const;
224 : template <class T>
225 : void WeightedBroveyPositiveWeights(const T *pPanBuffer,
226 : const T *pUpsampledSpectralBuffer,
227 : T *pDataBuf, size_t nValues,
228 : size_t nBandValues, T nMaxValue) const;
229 :
230 : template <class T, int NINPUT, int NOUTPUT>
231 : size_t WeightedBroveyPositiveWeightsInternal(
232 : const T *pPanBuffer, const T *pUpsampledSpectralBuffer, T *pDataBuf,
233 : size_t nValues, size_t nBandValues, T nMaxValue) const;
234 :
235 : // cppcheck-suppress unusedPrivateFunction
236 : template <class T>
237 : void WeightedBroveyGByteOrUInt16(const T *pPanBuffer,
238 : const T *pUpsampledSpectralBuffer,
239 : T *pDataBuf, size_t nValues,
240 : size_t nBandValues, T nMaxValue) const;
241 :
242 : // cppcheck-suppress functionStatic
243 : CPLErr PansharpenChunk(GDALDataType eWorkDataType,
244 : GDALDataType eBufDataType, const void *pPanBuffer,
245 : const void *pUpsampledSpectralBuffer, void *pDataBuf,
246 : size_t nValues, size_t nBandValues,
247 : GUInt32 nMaxValue) const;
248 :
249 : public:
250 : GDALPansharpenOperation();
251 : ~GDALPansharpenOperation();
252 :
253 : CPLErr Initialize(const GDALPansharpenOptions *psOptions);
254 : CPLErr ProcessRegion(int nXOff, int nYOff, int nXSize, int nYSize,
255 : void *pDataBuf, GDALDataType eBufDataType);
256 : GDALPansharpenOptions *GetOptions();
257 : };
258 :
259 : #endif /* __cplusplus */
260 :
261 : #endif /* GDALPANSHARPEN_H_INCLUDED */
|