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 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #ifndef GDALPANSHARPEN_H_INCLUDED
31 : #define GDALPANSHARPEN_H_INCLUDED
32 :
33 : #include "gdal.h"
34 :
35 : CPL_C_START
36 :
37 : /**
38 : * \file gdalpansharpen.h
39 : *
40 : * GDAL pansharpening related entry points and definitions.
41 : *
42 : * @since GDAL 2.1
43 : */
44 :
45 : /** Pansharpening algorithms.
46 : */
47 : typedef enum
48 : {
49 : /*! Weighted Brovery. */
50 : GDAL_PSH_WEIGHTED_BROVEY
51 : } GDALPansharpenAlg;
52 :
53 : /** Pansharpening options.
54 : */
55 : typedef struct
56 : {
57 : /*! Pan sharpening algorithm/method. Only weighed Brovey for now. */
58 : GDALPansharpenAlg ePansharpenAlg;
59 :
60 : /*! Resampling algorithm to upsample spectral bands to pan band resolution.
61 : */
62 : GDALRIOResampleAlg eResampleAlg;
63 :
64 : /*! Bit depth of the spectral bands. Can be let to 0 for default behavior.
65 : */
66 : int nBitDepth;
67 :
68 : /*! Number of weight coefficients in padfWeights. */
69 : int nWeightCount;
70 :
71 : /*! Array of nWeightCount weights used by weighted Brovey. */
72 : double *padfWeights;
73 :
74 : /*! Panchromatic band. */
75 : GDALRasterBandH hPanchroBand;
76 :
77 : /*! Number of input spectral bands. */
78 : int nInputSpectralBands;
79 :
80 : /** Array of nInputSpectralBands input spectral bands. The spectral band
81 : * have generally a coarser resolution than the panchromatic band, but they
82 : * are assumed to have the same spatial extent (and projection) at that
83 : * point. Necessary spatial adjustments must be done beforehand, for example
84 : * by wrapping inside a VRT dataset.
85 : */
86 : GDALRasterBandH *pahInputSpectralBands;
87 :
88 : /*! Number of output pansharpened spectral bands. */
89 : int nOutPansharpenedBands;
90 :
91 : /*! Array of nOutPansharpendBands values such as panOutPansharpenedBands[k]
92 : * is a value in the range [0,nInputSpectralBands-1] . */
93 : int *panOutPansharpenedBands;
94 :
95 : /*! Whether the panchromatic and spectral bands have a noData value. */
96 : int bHasNoData;
97 :
98 : /** NoData value of the panchromatic and spectral bands (only taken into
99 : account if bHasNoData = TRUE). This will also be use has the output
100 : nodata value. */
101 : double dfNoData;
102 :
103 : /** Number of threads or -1 to mean ALL_CPUS. By default (0), single
104 : * threaded mode is enabled unless the GDAL_NUM_THREADS configuration option
105 : * is set to an integer or ALL_CPUS. */
106 : int nThreads;
107 : } GDALPansharpenOptions;
108 :
109 : GDALPansharpenOptions CPL_DLL *GDALCreatePansharpenOptions(void);
110 : void CPL_DLL GDALDestroyPansharpenOptions(GDALPansharpenOptions *);
111 : GDALPansharpenOptions CPL_DLL *
112 : GDALClonePansharpenOptions(const GDALPansharpenOptions *psOptions);
113 :
114 : /*! Pansharpening operation handle. */
115 : typedef void *GDALPansharpenOperationH;
116 :
117 : GDALPansharpenOperationH CPL_DLL
118 : GDALCreatePansharpenOperation(const GDALPansharpenOptions *);
119 : void CPL_DLL GDALDestroyPansharpenOperation(GDALPansharpenOperationH);
120 : CPLErr CPL_DLL GDALPansharpenProcessRegion(GDALPansharpenOperationH hOperation,
121 : int nXOff, int nYOff, int nXSize,
122 : int nYSize, void *pDataBuf,
123 : GDALDataType eBufDataType);
124 :
125 : CPL_C_END
126 :
127 : #ifdef __cplusplus
128 :
129 : #include <array>
130 : #include <vector>
131 : #include "gdal_priv.h"
132 :
133 : #ifdef DEBUG_TIMING
134 : #include <sys/time.h>
135 : #endif
136 :
137 : class GDALPansharpenOperation;
138 :
139 : //! @cond Doxygen_Suppress
140 : typedef struct
141 : {
142 : GDALPansharpenOperation *poPansharpenOperation;
143 : GDALDataType eWorkDataType;
144 : GDALDataType eBufDataType;
145 : const void *pPanBuffer;
146 : const void *pUpsampledSpectralBuffer;
147 : void *pDataBuf;
148 : size_t nValues;
149 : size_t nBandValues;
150 : GUInt32 nMaxValue;
151 :
152 : #ifdef DEBUG_TIMING
153 : struct timeval *ptv;
154 : #endif
155 :
156 : CPLErr eErr;
157 : } GDALPansharpenJob;
158 :
159 : typedef struct
160 : {
161 : GDALDataset *poMEMDS;
162 : int nXOff;
163 : int nYOff;
164 : int nXSize;
165 : int nYSize;
166 : double dfXOff;
167 : double dfYOff;
168 : double dfXSize;
169 : double dfYSize;
170 : void *pBuffer;
171 : GDALDataType eDT;
172 : int nBufXSize;
173 : int nBufYSize;
174 : int nBandCount;
175 : GDALRIOResampleAlg eResampleAlg;
176 : GSpacing nBandSpace;
177 :
178 : #ifdef DEBUG_TIMING
179 : struct timeval *ptv;
180 : #endif
181 : } GDALPansharpenResampleJob;
182 :
183 : class CPLWorkerThreadPool;
184 :
185 : //! @endcond
186 :
187 : /** Pansharpening operation class.
188 : */
189 56 : class GDALPansharpenOperation
190 : {
191 : CPL_DISALLOW_COPY_ASSIGN(GDALPansharpenOperation)
192 :
193 : GDALPansharpenOptions *psOptions = nullptr;
194 : std::vector<int> anInputBands{};
195 : std::vector<GDALDataset *> aVDS{}; // to destroy
196 : std::vector<GDALRasterBand *> aMSBands{}; // original multispectral bands
197 : // potentially warped into a VRT
198 : int bPositiveWeights = TRUE;
199 : CPLWorkerThreadPool *poThreadPool = nullptr;
200 : int nKernelRadius = 0;
201 : std::array<double, 6> m_adfPanToMSGT = {{0.0, 1.0, 0, 0.0, 0.0, 1.0}};
202 :
203 : static void PansharpenJobThreadFunc(void *pUserData);
204 : static void PansharpenResampleJobThreadFunc(void *pUserData);
205 :
206 : template <class WorkDataType, class OutDataType>
207 : void WeightedBroveyWithNoData(const WorkDataType *pPanBuffer,
208 : const WorkDataType *pUpsampledSpectralBuffer,
209 : OutDataType *pDataBuf, size_t nValues,
210 : size_t nBandValues,
211 : WorkDataType nMaxValue) const;
212 : template <class WorkDataType, class OutDataType, int bHasBitDepth>
213 : void WeightedBrovey3(const WorkDataType *pPanBuffer,
214 : const WorkDataType *pUpsampledSpectralBuffer,
215 : OutDataType *pDataBuf, size_t nValues,
216 : size_t nBandValues, WorkDataType nMaxValue) const;
217 :
218 : // cppcheck-suppress functionStatic
219 : template <class WorkDataType, class OutDataType>
220 : void WeightedBrovey(const WorkDataType *pPanBuffer,
221 : const WorkDataType *pUpsampledSpectralBuffer,
222 : OutDataType *pDataBuf, size_t nValues,
223 : size_t nBandValues, WorkDataType nMaxValue) const;
224 : template <class WorkDataType>
225 : CPLErr WeightedBrovey(const WorkDataType *pPanBuffer,
226 : const WorkDataType *pUpsampledSpectralBuffer,
227 : void *pDataBuf, GDALDataType eBufDataType,
228 : size_t nValues, size_t nBandValues,
229 : WorkDataType nMaxValue) const;
230 :
231 : // cppcheck-suppress functionStatic
232 : template <class WorkDataType>
233 : CPLErr WeightedBrovey(const WorkDataType *pPanBuffer,
234 : const WorkDataType *pUpsampledSpectralBuffer,
235 : void *pDataBuf, GDALDataType eBufDataType,
236 : size_t nValues, size_t nBandValues) const;
237 : template <class T>
238 : void WeightedBroveyPositiveWeights(const T *pPanBuffer,
239 : const T *pUpsampledSpectralBuffer,
240 : T *pDataBuf, size_t nValues,
241 : size_t nBandValues, T nMaxValue) const;
242 :
243 : template <class T, int NINPUT, int NOUTPUT>
244 : size_t WeightedBroveyPositiveWeightsInternal(
245 : const T *pPanBuffer, const T *pUpsampledSpectralBuffer, T *pDataBuf,
246 : size_t nValues, size_t nBandValues, T nMaxValue) const;
247 :
248 : // cppcheck-suppress unusedPrivateFunction
249 : template <class T>
250 : void WeightedBroveyGByteOrUInt16(const T *pPanBuffer,
251 : const T *pUpsampledSpectralBuffer,
252 : T *pDataBuf, size_t nValues,
253 : size_t nBandValues, T nMaxValue) const;
254 :
255 : // cppcheck-suppress functionStatic
256 : CPLErr PansharpenChunk(GDALDataType eWorkDataType,
257 : GDALDataType eBufDataType, const void *pPanBuffer,
258 : const void *pUpsampledSpectralBuffer, void *pDataBuf,
259 : size_t nValues, size_t nBandValues,
260 : GUInt32 nMaxValue) const;
261 :
262 : public:
263 : GDALPansharpenOperation();
264 : ~GDALPansharpenOperation();
265 :
266 : CPLErr Initialize(const GDALPansharpenOptions *psOptions);
267 : CPLErr ProcessRegion(int nXOff, int nYOff, int nXSize, int nYSize,
268 : void *pDataBuf, GDALDataType eBufDataType);
269 : GDALPansharpenOptions *GetOptions();
270 : };
271 :
272 : #endif /* __cplusplus */
273 :
274 : #endif /* GDALPANSHARPEN_H_INCLUDED */
|