1 /***************************************************************************
2      qgsimagewarper.h
3      --------------------------------------
4    Date                 : Sun Sep 16 12:03:20 AKDT 2007
5     Copyright            : (C) 2007 by Gary E. Sherman
6     Email                : sherman at mrcc dot com
7  ***************************************************************************
8  *                                                                         *
9  *   This program is free software; you can redistribute it and/or modify  *
10  *   it under the terms of the GNU General Public License as published by  *
11  *   the Free Software Foundation; either version 2 of the License, or     *
12  *   (at your option) any later version.                                   *
13  *                                                                         *
14  ***************************************************************************/
15 #ifndef QGSIMAGEWARPER_H
16 #define QGSIMAGEWARPER_H
17 
18 #include <QCoreApplication>
19 #include <QString>
20 
21 #include <gdalwarper.h>
22 #include <vector>
23 #include "qgspointxy.h"
24 #include "qgscoordinatereferencesystem.h"
25 #include "qgsogrutils.h"
26 
27 class QgsGeorefTransform;
28 class QProgressDialog;
29 class QWidget;
30 
31 class QgsImageWarper
32 {
33     Q_DECLARE_TR_FUNCTIONS( QgsImageWarper )
34 
35   public:
36     explicit QgsImageWarper( QWidget *parent );
37 
38     enum ResamplingMethod
39     {
40       NearestNeighbour = GRA_NearestNeighbour,
41       Bilinear         = GRA_Bilinear,
42       Cubic            = GRA_Cubic,
43       CubicSpline      = GRA_CubicSpline,
44       Lanczos          = GRA_Lanczos
45     };
46 
47     /**
48      * Warp the file specified by "input" and write the resulting raster to the file "output".
49      * \param georefTransform Specified the warp transformation which should be applied to "input".
50      * \param resampling Specifies image resampling algorithm to use.
51      * \param useZeroAsTrans Specifies whether to mark transparent areas with a value of "zero".
52      * \param destResX The desired horizontal resolution of the output file, in target georeferenced units. A value of zero means automatic selection.
53      * \param destResY The desired vertical resolution of the output file, in target georeferenced units. A value of zero means automatic selection.
54      */
55     int warpFile( const QString &input,
56                   const QString &output,
57                   const QgsGeorefTransform &georefTransform,
58                   ResamplingMethod resampling,
59                   bool useZeroAsTrans,
60                   const QString &compression,
61                   const QgsCoordinateReferenceSystem &crs,
62                   double destResX = 0.0, double destResY = 0.0 );
63   private:
64     struct TransformChain
65     {
66       GDALTransformerFunc GDALTransformer;
67       void               *GDALTransformerArg = nullptr;
68       double              adfGeotransform[6];
69       double              adfInvGeotransform[6];
70     };
71 
72     //! \sa addGeoToPixelTransform
73     static int GeoToPixelTransform( void *pTransformerArg, int bDstToSrc, int nPointCount,
74                                     double *x, double *y, double *z, int *panSuccess );
75 
76     /**
77      * \brief Appends a transform from geocoordinates to pixel/line coordinates to the given GDAL transformer.
78      *
79      * The resulting transform is the functional composition of the given GDAL transformer and the
80      * inverse geo transform.
81      * \sa destroyGeoToPixelTransform
82      * \returns Argument to use with the static GDAL callback \ref GeoToPixelTransform
83      */
84     void *addGeoToPixelTransform( GDALTransformerFunc GDALTransformer, void *GDALTransformerArg, double *padfGeotransform ) const;
85     void destroyGeoToPixelTransform( void *GeoToPixelTransformArg ) const;
86 
87     bool openSrcDSAndGetWarpOpt( const QString &input, ResamplingMethod resampling,
88                                  const GDALTransformerFunc &pfnTransform, gdal::dataset_unique_ptr &hSrcDS,
89                                  gdal::warp_options_unique_ptr &psWarpOptions );
90 
91     bool createDestinationDataset( const QString &outputName, GDALDatasetH hSrcDS, gdal::dataset_unique_ptr &hDstDS, uint resX, uint resY,
92                                    double *adfGeoTransform, bool useZeroAsTrans, const QString &compression, const QgsCoordinateReferenceSystem &crs );
93 
94     QWidget *mParent = nullptr;
95     void      *createWarpProgressArg( QProgressDialog *progressDialog ) const;
96     //! \brief GDAL progress callback, used to display warping progress via a QProgressDialog
97     static int CPL_STDCALL updateWarpProgress( double dfComplete, const char *pszMessage, void *pProgressArg );
98 
99     static bool sWarpCanceled;
100 
101     GDALResampleAlg toGDALResampleAlg( ResamplingMethod method ) const;
102 };
103 
104 
105 #endif
106