LCOV - code coverage report
Current view: top level - alg - gdalpansharpen.h (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 1 1 100.0 %
Date: 2025-10-15 17:20:55 Functions: 1 1 100.0 %

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

Generated by: LCOV version 1.14