forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdalsimplewarp.cpp
489 lines (422 loc) · 18.7 KB
/
gdalsimplewarp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/******************************************************************************
*
* Project: Mapinfo Image Warper
* Purpose: Simple (source in memory) warp algorithm.
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* Copyright (c) 2002, i3 - information integration and imaging, Fort Collin, CO
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "cpl_port.h"
#include "gdal_alg.h"
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include "cpl_conv.h"
#include "cpl_error.h"
#include "cpl_progress.h"
#include "cpl_string.h"
#include "cpl_vsi.h"
#include "gdal.h"
#include "gdal_priv.h"
CPL_CVSID("$Id$")
/************************************************************************/
/* GDALSimpleWarpRemapping() */
/* */
/* This function implements any raster remapping requested in */
/* the options list. The remappings are applied to the source */
/* data before warping. Two kinds are support ... REMAP */
/* commands which remap selected pixel values for any band and */
/* REMAP_MULTI which only remap pixels matching the input in */
/* all bands at once (i.e. to remap an RGB value to another). */
/************************************************************************/
static void
GDALSimpleWarpRemapping( int nBandCount, GByte **papabySrcData,
int nSrcXSize, int nSrcYSize,
char **papszWarpOptions )
{
/* ==================================================================== */
/* Process any and all single value REMAP commands. */
/* ==================================================================== */
char **papszRemaps = CSLFetchNameValueMultiple( papszWarpOptions,
"REMAP" );
const int nRemaps = CSLCount(papszRemaps);
for( int iRemap = 0; iRemap < nRemaps; iRemap++ )
{
/* -------------------------------------------------------------------- */
/* What are the pixel values to map from and to? */
/* -------------------------------------------------------------------- */
char **papszTokens = CSLTokenizeString( papszRemaps[iRemap] );
if( CSLCount(papszTokens) != 2 )
{
CPLError(CE_Warning, CPLE_AppDefined,
"Ill formed REMAP `%s' ignored in "
"GDALSimpleWarpRemapping()",
papszRemaps[iRemap] );
CSLDestroy( papszTokens );
continue;
}
const int nFromValue = atoi(papszTokens[0]);
const int nToValue = atoi(papszTokens[1]);
// TODO(schwehr): Why is it ok to narrow ints to byte without checking?
const GByte byToValue = static_cast<GByte>(nToValue);
CSLDestroy( papszTokens );
/* -------------------------------------------------------------------- */
/* Pass over each band searching for matches. */
/* -------------------------------------------------------------------- */
for( int iBand = 0; iBand < nBandCount; iBand++ )
{
GByte *pabyData = papabySrcData[iBand];
int nPixelCount = nSrcXSize * nSrcYSize;
while( nPixelCount != 0 )
{
if( *pabyData == nFromValue )
*pabyData = byToValue;
pabyData++;
nPixelCount--;
}
}
}
CSLDestroy( papszRemaps );
/* ==================================================================== */
/* Process any and all REMAP_MULTI commands. */
/* ==================================================================== */
papszRemaps = CSLFetchNameValueMultiple( papszWarpOptions,
"REMAP_MULTI" );
const int nRemapsMulti = CSLCount(papszRemaps);
for( int iRemap = 0; iRemap < nRemapsMulti; iRemap++ )
{
/* -------------------------------------------------------------------- */
/* What are the pixel values to map from and to? */
/* -------------------------------------------------------------------- */
char **papszTokens = CSLTokenizeString( papszRemaps[iRemap] );
const int nTokens = CSLCount(papszTokens);
if( nTokens % 2 == 1 ||
nTokens == 0 ||
nTokens > nBandCount * 2 )
{
CPLError(CE_Warning, CPLE_AppDefined,
"Ill formed REMAP_MULTI `%s' ignored in "
"GDALSimpleWarpRemapping()",
papszRemaps[iRemap]);
CSLDestroy( papszTokens );
continue;
}
const int nMapBandCount = nTokens / 2;
int *panFromValue = static_cast<int *>(
CPLMalloc(sizeof(int) * nMapBandCount ) );
int *panToValue = static_cast<int *>(
CPLMalloc(sizeof(int) * nMapBandCount ) );
for( int iBand = 0; iBand < nMapBandCount; iBand++ )
{
panFromValue[iBand] = atoi(papszTokens[iBand]);
panToValue[iBand] = atoi(papszTokens[iBand+nMapBandCount]);
}
CSLDestroy( papszTokens );
/* -------------------------------------------------------------------- */
/* Search for matching values to replace. */
/* -------------------------------------------------------------------- */
const int nPixelCount = nSrcXSize * nSrcYSize;
for( int iPixel = 0; iPixel < nPixelCount; iPixel++ )
{
bool bMatch = true;
// Always check band 0.
for( int iBand = 0; bMatch && iBand < std::max(1, nMapBandCount);
iBand++ )
{
if( papabySrcData[iBand][iPixel] != panFromValue[iBand] )
bMatch = false;
}
if( !bMatch )
continue;
for( int iBand = 0; iBand < nMapBandCount; iBand++ )
papabySrcData[iBand][iPixel] =
static_cast<GByte>( panToValue[iBand] );
}
CPLFree( panFromValue );
CPLFree( panToValue );
}
CSLDestroy( papszRemaps );
}
/************************************************************************/
/* GDALSimpleImageWarp() */
/************************************************************************/
/**
* Perform simple image warp.
*
* Copies an image from a source dataset to a destination dataset applying
* an application defined transformation. This algorithm is called simple
* because it lacks many options such as resampling kernels (other than
* nearest neighbour), support for data types other than 8bit, and the
* ability to warp images without holding the entire source and destination
* image in memory.
*
* The following option(s) may be passed in papszWarpOptions.
* <ul>
* <li> "INIT=v[,v...]": This option indicates that the output dataset should
* be initialized to the indicated value in any area valid data is not written.
* Distinct values may be listed for each band separated by columns.
* </ul>
*
* @param hSrcDS the source image dataset.
* @param hDstDS the destination image dataset.
* @param nBandCount the number of bands to be warped. If zero, all bands
* will be processed.
* @param panBandList the list of bands to translate.
* @param pfnTransform the transformation function to call. See
* GDALTransformerFunc().
* @param pTransformArg the callback handle to pass to pfnTransform.
* @param pfnProgress the function used to report progress. See
* GDALProgressFunc().
* @param pProgressArg the callback handle to pass to pfnProgress.
* @param papszWarpOptions additional options controlling the warp.
*
* @return TRUE if the operation completes, or FALSE if an error occurs.
*/
int CPL_STDCALL
GDALSimpleImageWarp( GDALDatasetH hSrcDS, GDALDatasetH hDstDS,
int nBandCount, int *panBandList,
GDALTransformerFunc pfnTransform, void *pTransformArg,
GDALProgressFunc pfnProgress, void *pProgressArg,
char **papszWarpOptions )
{
VALIDATE_POINTER1( hSrcDS, "GDALSimpleImageWarp", 0 );
VALIDATE_POINTER1( hDstDS, "GDALSimpleImageWarp", 0 );
bool bError = false;
/* -------------------------------------------------------------------- */
/* If no bands provided assume we should process all bands. */
/* -------------------------------------------------------------------- */
if( nBandCount == 0 )
{
nBandCount = GDALGetRasterCount( hSrcDS );
if( nBandCount == 0 )
{
CPLError(CE_Failure, CPLE_AppDefined,
"No raster band in source dataset");
return FALSE;
}
panBandList = static_cast<int *>(CPLCalloc(sizeof(int), nBandCount));
for( int iBand = 0; iBand < nBandCount; iBand++ )
panBandList[iBand] = iBand + 1;
const int nResult =
GDALSimpleImageWarp( hSrcDS, hDstDS, nBandCount, panBandList,
pfnTransform, pTransformArg,
pfnProgress, pProgressArg,
papszWarpOptions );
CPLFree( panBandList );
return nResult;
}
/* -------------------------------------------------------------------- */
/* Post initial progress. */
/* -------------------------------------------------------------------- */
if( pfnProgress )
{
if( !pfnProgress( 0.0, "", pProgressArg ) )
return FALSE;
}
/* -------------------------------------------------------------------- */
/* Load the source image band(s). */
/* -------------------------------------------------------------------- */
const int nSrcXSize = GDALGetRasterXSize(hSrcDS);
const int nSrcYSize = GDALGetRasterYSize(hSrcDS);
GByte **papabySrcData = static_cast<GByte **>(
CPLCalloc(nBandCount, sizeof(GByte*)) );
bool ok = true;
for( int iBand = 0; iBand < nBandCount; iBand++ )
{
papabySrcData[iBand] = static_cast<GByte *>(
VSI_MALLOC2_VERBOSE(nSrcXSize, nSrcYSize) );
if( papabySrcData[iBand] == nullptr )
{
CPLError( CE_Failure, CPLE_OutOfMemory,
"GDALSimpleImageWarp out of memory." );
ok = false;
break;
}
if( GDALRasterIO(
GDALGetRasterBand(hSrcDS,panBandList[iBand]), GF_Read,
0, 0, nSrcXSize, nSrcYSize,
papabySrcData[iBand], nSrcXSize, nSrcYSize, GDT_Byte,
0, 0 ) != CE_None )
{
CPLError( CE_Failure, CPLE_FileIO,
"GDALSimpleImageWarp GDALRasterIO failure %s",
CPLGetLastErrorMsg() );
ok = false;
break;
}
}
if( !ok )
{
for( int i=0; i <= nBandCount; i++ )
{
VSIFree(papabySrcData[i]);
}
CPLFree(papabySrcData);
return FALSE;
}
/* -------------------------------------------------------------------- */
/* Check for remap request(s). */
/* -------------------------------------------------------------------- */
GDALSimpleWarpRemapping( nBandCount, papabySrcData, nSrcXSize, nSrcYSize,
papszWarpOptions );
/* -------------------------------------------------------------------- */
/* Allocate scanline buffers for output image. */
/* -------------------------------------------------------------------- */
const int nDstXSize = GDALGetRasterXSize( hDstDS );
const int nDstYSize = GDALGetRasterYSize( hDstDS );
GByte **papabyDstLine = static_cast<GByte **>(
CPLCalloc(nBandCount, sizeof(GByte*)) );
for( int iBand = 0; iBand < nBandCount; iBand++ )
papabyDstLine[iBand] = static_cast<GByte *>(CPLMalloc( nDstXSize ));
/* -------------------------------------------------------------------- */
/* Allocate x,y,z coordinate arrays for transformation ... one */
/* scanlines worth of positions. */
/* -------------------------------------------------------------------- */
double *padfX = static_cast<double *>(
CPLMalloc(sizeof(double) * nDstXSize) );
double *padfY = static_cast<double *>(
CPLMalloc(sizeof(double) * nDstXSize) );
double *padfZ = static_cast<double *>(
CPLMalloc(sizeof(double) * nDstXSize) );
int *pabSuccess = static_cast<int *>( CPLMalloc(sizeof(int) * nDstXSize) );
/* -------------------------------------------------------------------- */
/* Establish the value we will use to initialize the bands. We */
/* default to -1 indicating the initial value should be read */
/* and preserved from the source file, but allow this to be */
/* overridden by passed */
/* option(s). */
/* -------------------------------------------------------------------- */
int * const panBandInit =
static_cast<int *>( CPLCalloc(sizeof(int), nBandCount) );
if( CSLFetchNameValue( papszWarpOptions, "INIT" ) )
{
char **papszTokens =
CSLTokenizeStringComplex( CSLFetchNameValue( papszWarpOptions,
"INIT" ),
" ,", FALSE, FALSE );
const int nTokenCount = CSLCount(papszTokens);
for( int iBand = 0; iBand < nBandCount; iBand++ )
{
if( nTokenCount == 0 )
panBandInit[iBand] = 0;
else
panBandInit[iBand] =
atoi(papszTokens[std::min(iBand, nTokenCount- 1)]);
}
CSLDestroy(papszTokens);
}
/* -------------------------------------------------------------------- */
/* Loop over all the scanlines in the output image. */
/* -------------------------------------------------------------------- */
for( int iDstY = 0; iDstY < nDstYSize; iDstY++ )
{
// Clear output buffer to "transparent" value. Should not we
// really be reading from the destination file to support overlay?
for( int iBand = 0; iBand < nBandCount; iBand++ )
{
if( panBandInit[iBand] == -1 )
{
if( GDALRasterIO(
GDALGetRasterBand(hDstDS,iBand + 1), GF_Read,
0, iDstY, nDstXSize, 1,
papabyDstLine[iBand], nDstXSize, 1, GDT_Byte,
0, 0 ) != CE_None )
{
bError = TRUE;
break;
}
}
else
{
memset( papabyDstLine[iBand], panBandInit[iBand], nDstXSize );
}
}
// Set point to transform.
for( int iDstX = 0; iDstX < nDstXSize; iDstX++ )
{
padfX[iDstX] = iDstX + 0.5;
padfY[iDstX] = iDstY + 0.5;
padfZ[iDstX] = 0.0;
}
// Transform the points from destination pixel/line coordinates
// to source pixel/line coordinates.
pfnTransform( pTransformArg, TRUE, nDstXSize,
padfX, padfY, padfZ, pabSuccess );
// Loop over the output scanline.
for( int iDstX = 0; iDstX < nDstXSize; iDstX++ )
{
if( !pabSuccess[iDstX] )
continue;
// We test against the value before casting to avoid the
// problem of asymmetric truncation effects around zero. That is
// -0.5 will be 0 when cast to an int.
if( padfX[iDstX] < 0.0 || padfY[iDstX] < 0.0 )
continue;
const int iSrcX = static_cast<int>( padfX[iDstX] );
const int iSrcY = static_cast<int>( padfY[iDstX] );
if( iSrcX >= nSrcXSize || iSrcY >= nSrcYSize )
continue;
const int iSrcOffset = iSrcX + iSrcY * nSrcXSize;
for( int iBand = 0; iBand < nBandCount; iBand++ )
papabyDstLine[iBand][iDstX] = papabySrcData[iBand][iSrcOffset];
}
// Write scanline to disk.
for( int iBand = 0; iBand < nBandCount; iBand++ )
{
if( GDALRasterIO(
GDALGetRasterBand(hDstDS,iBand+1), GF_Write,
0, iDstY, nDstXSize, 1,
papabyDstLine[iBand], nDstXSize,
1, GDT_Byte, 0, 0 ) != CE_None )
{
bError = TRUE;
break;
}
}
if( pfnProgress != nullptr )
{
if( !pfnProgress( (iDstY + 1) / static_cast<double>(nDstYSize),
"", pProgressArg ) )
{
CPLError( CE_Failure, CPLE_UserInterrupt, "User terminated" );
bError = TRUE;
break;
}
}
}
/* -------------------------------------------------------------------- */
/* Cleanup working buffers. */
/* -------------------------------------------------------------------- */
for( int iBand = 0; iBand < nBandCount; iBand++ )
{
CPLFree( papabyDstLine[iBand] );
CPLFree( papabySrcData[iBand] );
}
CPLFree( panBandInit );
CPLFree( papabyDstLine );
CPLFree( papabySrcData );
CPLFree( padfX );
CPLFree( padfY );
CPLFree( padfZ );
CPLFree( pabSuccess );
return !bError;
}