19 #include "cpl_string.h"
20 #include <QProgressDialog>
22 #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
23 #define TO8(x) (x).toUtf8().constData()
25 #define TO8(x) (x).toLocal8Bit().constData()
29 mInputFile( inputFile ), mOutputFile( outputFile ), mOutputFormat( outputFormat ), mCellSizeX( -1 ), mCellSizeY( -1 ), mInputNodataValue( -1 ), mOutputNodataValue( -1 )
51 if ( inputDataset == NULL )
58 if ( outputDriver == 0 )
63 GDALDatasetH outputDataset =
openOutputFile( inputDataset, outputDriver );
64 if ( outputDataset == NULL )
70 GDALRasterBandH rasterBand = GDALGetRasterBand( inputDataset, 1 );
71 if ( rasterBand == NULL )
73 GDALClose( inputDataset );
74 GDALClose( outputDataset );
79 GDALRasterBandH outputRasterBand = GDALGetRasterBand( outputDataset, 1 );
80 if ( outputRasterBand == NULL )
82 GDALClose( inputDataset );
83 GDALClose( outputDataset );
87 GDALSetRasterNoDataValue( outputRasterBand, -9999 );
92 GDALClose( inputDataset );
93 GDALClose( outputDataset );
98 float* scanLine1 = (
float * ) CPLMalloc(
sizeof(
float ) * xSize );
99 float* scanLine2 = (
float * ) CPLMalloc(
sizeof(
float ) * xSize );
100 float* scanLine3 = (
float * ) CPLMalloc(
sizeof(
float ) * xSize );
102 float* resultLine = (
float * ) CPLMalloc(
sizeof(
float ) * xSize );
106 p->setMaximum( ySize );
110 for (
int i = 0; i < ySize; ++i )
117 if ( p && p->wasCanceled() )
125 for (
int a = 0; a < xSize; ++a )
129 GDALRasterIO( rasterBand, GF_Read, 0, 0, xSize, 1, scanLine2, xSize, 1, GDT_Float32, 0, 0 );
134 CPLFree( scanLine1 );
135 scanLine1 = scanLine2;
136 scanLine2 = scanLine3;
137 scanLine3 = (
float * ) CPLMalloc(
sizeof(
float ) * xSize );
140 if ( i == ySize - 1 )
142 for (
int a = 0; a < xSize; ++a )
149 GDALRasterIO( rasterBand, GF_Read, 0, i + 1, xSize, 1, scanLine3, xSize, 1, GDT_Float32, 0, 0 );
152 for (
int j = 0; j < xSize; ++j )
159 else if ( j == xSize - 1 )
166 resultLine[j] =
processNineCellWindow( &scanLine1[j-1], &scanLine1[j], &scanLine1[j+1], &scanLine2[j-1], &scanLine2[j], \
167 &scanLine2[j+1], &scanLine3[j-1], &scanLine3[j], &scanLine3[j+1] );
171 GDALRasterIO( outputRasterBand, GF_Write, 0, i, xSize, 1, resultLine, xSize, 1, GDT_Float32, 0, 0 );
176 p->setValue( ySize );
179 CPLFree( resultLine );
180 CPLFree( scanLine1 );
181 CPLFree( scanLine2 );
182 CPLFree( scanLine3 );
184 GDALClose( inputDataset );
186 if ( p && p->wasCanceled() )
189 GDALDeleteDataset( outputDriver,
mOutputFile.toLocal8Bit().data() );
192 GDALClose( outputDataset );
199 GDALDatasetH inputDataset = GDALOpen(
TO8(
mInputFile ), GA_ReadOnly );
200 if ( inputDataset != NULL )
202 nCellsX = GDALGetRasterXSize( inputDataset );
203 nCellsY = GDALGetRasterYSize( inputDataset );
206 if ( GDALGetRasterCount( inputDataset ) < 1 )
208 GDALClose( inputDataset );
217 char **driverMetadata;
220 GDALDriverH outputDriver = GDALGetDriverByName(
mOutputFormat.toLocal8Bit().data() );
222 if ( outputDriver == NULL )
227 driverMetadata = GDALGetMetadata( outputDriver, NULL );
228 if ( !CSLFetchBoolean( driverMetadata, GDAL_DCAP_CREATE,
false ) )
238 if ( inputDataset == NULL )
243 int xSize = GDALGetRasterXSize( inputDataset );
244 int ySize = GDALGetRasterYSize( inputDataset );;
247 char **papszOptions = NULL;
248 GDALDatasetH outputDataset = GDALCreate( outputDriver,
mOutputFile.toLocal8Bit().data(), xSize, ySize, 1, GDT_Float32, papszOptions );
249 if ( outputDataset == NULL )
251 return outputDataset;
255 double geotransform[6];
256 if ( GDALGetGeoTransform( inputDataset, geotransform ) != CE_None )
258 GDALClose( outputDataset );
261 GDALSetGeoTransform( outputDataset, geotransform );
275 const char* projection = GDALGetProjectionRef( inputDataset );
276 GDALSetProjection( outputDataset, projection );
278 return outputDataset;