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
|
/******************************************************************************
* $Id: gdalsimplewarp.cpp,v 1.9 2006/03/21 20:56:00 fwarmerdam Exp $
*
* Project: Mapinfo Image Warper
* Purpose: Simple (source in memory) warp algorithm.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* 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.
******************************************************************************
*
* $Log: gdalsimplewarp.cpp,v $
* Revision 1.9 2006/03/21 20:56:00 fwarmerdam
* cleanup headers
*
* Revision 1.8 2005/04/04 15:24:16 fwarmerdam
* added CPL_STDCALL to some functions
*
* Revision 1.7 2003/07/08 15:33:04 warmerda
* avoid warnings
*
* Revision 1.6 2003/02/24 17:30:58 warmerda
* added REMAP support
*
* Revision 1.5 2003/02/06 04:56:35 warmerda
* added documentation
*
* Revision 1.4 2002/12/07 22:58:42 warmerda
* added initialization support for simple warper
*
* Revision 1.3 2002/12/06 21:43:28 warmerda
* removed luts
*
* Revision 1.2 2002/12/05 21:44:19 warmerda
* initial implementation
*
* Revision 1.1 2002/12/05 05:43:23 warmerda
* New
*
*/
#include "gdal_priv.h"
#include "gdal_alg.h"
#include "cpl_string.h"
CPL_CVSID("$Id: gdalsimplewarp.cpp,v 1.9 2006/03/21 20:56:00 fwarmerdam Exp $");
static void
GDALSimpleWarpRemapping( int nBandCount, GByte **papabySrcData,
int nSrcXSize, int nSrcYSize,
char **papszWarpOptions );
/************************************************************************/
/* 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 )
{
int iBand, bCancelled = FALSE;
/* -------------------------------------------------------------------- */
/* If no bands provided assume we should process all bands. */
/* -------------------------------------------------------------------- */
if( nBandCount == 0 )
{
int nResult;
nBandCount = GDALGetRasterCount( hSrcDS );
panBandList = (int *) CPLCalloc(sizeof(int),nBandCount);
for( iBand = 0; iBand < nBandCount; iBand++ )
panBandList[iBand] = iBand+1;
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). */
/* -------------------------------------------------------------------- */
int nSrcXSize = GDALGetRasterXSize(hSrcDS);
int nSrcYSize = GDALGetRasterYSize(hSrcDS);
GByte **papabySrcData;
papabySrcData = (GByte **) CPLCalloc(nBandCount,sizeof(int));
for( iBand = 0; iBand < nBandCount; iBand++ )
{
papabySrcData[iBand] = (GByte *) VSIMalloc(nSrcXSize*nSrcYSize);
GDALRasterIO( GDALGetRasterBand(hSrcDS,panBandList[iBand]), GF_Read,
0, 0, nSrcXSize, nSrcYSize,
papabySrcData[iBand], nSrcXSize, nSrcYSize, GDT_Byte,
0, 0 );
}
/* -------------------------------------------------------------------- */
/* Check for remap request(s). */
/* -------------------------------------------------------------------- */
GDALSimpleWarpRemapping( nBandCount, papabySrcData, nSrcXSize, nSrcYSize,
papszWarpOptions );
/* -------------------------------------------------------------------- */
/* Allocate scanline buffers for output image. */
/* -------------------------------------------------------------------- */
int nDstXSize = GDALGetRasterXSize( hDstDS );
int nDstYSize = GDALGetRasterYSize( hDstDS );
GByte **papabyDstLine;
papabyDstLine = (GByte **) CPLCalloc(nBandCount,sizeof(int));
for( iBand = 0; iBand < nBandCount; iBand++ )
papabyDstLine[iBand] = (GByte *) CPLMalloc( nDstXSize );
/* -------------------------------------------------------------------- */
/* Allocate x,y,z coordinate arrays for transformation ... one */
/* scanlines worth of positions. */
/* -------------------------------------------------------------------- */
double *padfX, *padfY, *padfZ;
int *pabSuccess;
padfX = (double *) CPLMalloc(sizeof(double) * nDstXSize);
padfY = (double *) CPLMalloc(sizeof(double) * nDstXSize);
padfZ = (double *) CPLMalloc(sizeof(double) * nDstXSize);
pabSuccess = (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 *panBandInit;
panBandInit = (int *) CPLCalloc(sizeof(int),nBandCount);
if( CSLFetchNameValue( papszWarpOptions, "INIT" ) )
{
int iBand, nTokenCount;
char **papszTokens =
CSLTokenizeStringComplex( CSLFetchNameValue( papszWarpOptions,
"INIT" ),
" ,", FALSE, FALSE );
nTokenCount = CSLCount(papszTokens);
for( iBand = 0; iBand < nBandCount; iBand++ )
{
if( nTokenCount == 0 )
panBandInit[iBand] = 0;
else
panBandInit[iBand] =
atoi(papszTokens[MIN(iBand,nTokenCount-1)]);
}
CSLDestroy(papszTokens);
}
/* -------------------------------------------------------------------- */
/* Loop over all the scanlines in the output image. */
/* -------------------------------------------------------------------- */
int iDstY;
for( iDstY = 0; iDstY < nDstYSize; iDstY++ )
{
int iDstX;
// Clear output buffer to "transparent" value. Shouldn't we
// really be reading from the destination file to support overlay?
for( iBand = 0; iBand < nBandCount; iBand++ )
{
if( panBandInit[iBand] == -1 )
GDALRasterIO( GDALGetRasterBand(hDstDS,iBand+1), GF_Read,
0, iDstY, nDstXSize, 1,
papabyDstLine[iBand], nDstXSize, 1, GDT_Byte,
0, 0 );
else
memset( papabyDstLine[iBand], panBandInit[iBand], nDstXSize );
}
// Set point to transform.
for( 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( 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;
int iSrcX, iSrcY, iSrcOffset;
iSrcX = (int) padfX[iDstX];
iSrcY = (int) padfY[iDstX];
if( iSrcX >= nSrcXSize || iSrcY >= nSrcYSize )
continue;
iSrcOffset = iSrcX + iSrcY * nSrcXSize;
for( iBand = 0; iBand < nBandCount; iBand++ )
papabyDstLine[iBand][iDstX] = papabySrcData[iBand][iSrcOffset];
}
// Write scanline to disk.
for( iBand = 0; iBand < nBandCount; iBand++ )
{
GDALRasterIO( GDALGetRasterBand(hDstDS,iBand+1), GF_Write,
0, iDstY, nDstXSize, 1,
papabyDstLine[iBand], nDstXSize, 1, GDT_Byte, 0, 0 );
}
if( pfnProgress != NULL )
{
if( !pfnProgress( (iDstY+1) / (double) nDstYSize,
"", pProgressArg ) )
{
CPLError( CE_Failure, CPLE_UserInterrupt, "User terminated" );
bCancelled = TRUE;
break;
}
}
}
/* -------------------------------------------------------------------- */
/* Cleanup working buffers. */
/* -------------------------------------------------------------------- */
for( iBand = 0; iBand < nBandCount; iBand++ )
{
CPLFree( papabyDstLine[iBand] );
CPLFree( papabySrcData[iBand] );
}
CPLFree( papabyDstLine );
CPLFree( papabySrcData );
CPLFree( padfX );
CPLFree( padfY );
CPLFree( padfZ );
CPLFree( pabSuccess );
return !bCancelled;
}
/************************************************************************/
/* 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 (ie. 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. */
/* ==================================================================== */
int iRemap;
char **papszRemaps = CSLFetchNameValueMultiple( papszWarpOptions,
"REMAP" );
for( iRemap = 0; iRemap < CSLCount(papszRemaps); iRemap++ )
{
/* -------------------------------------------------------------------- */
/* What are the pixel values to map from and to? */
/* -------------------------------------------------------------------- */
char **papszTokens = CSLTokenizeString( papszRemaps[iRemap] );
int nFromValue, nToValue;
if( CSLCount(papszTokens) != 2 )
{
CPLError( CE_Warning, CPLE_AppDefined,
"Ill formed REMAP `%s' ignored in GDALSimpleWarpRemapping()",
papszRemaps[iRemap] );
continue;
}
nFromValue = atoi(papszTokens[0]);
nToValue = atoi(papszTokens[1]);
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 = (GByte) nToValue;
pabyData++;
nPixelCount--;
}
}
}
CSLDestroy( papszRemaps );
/* ==================================================================== */
/* Process any and all REMAP_MULTI commands. */
/* ==================================================================== */
papszRemaps = CSLFetchNameValueMultiple( papszWarpOptions,
"REMAP_MULTI" );
for( iRemap = 0; iRemap < CSLCount(papszRemaps); iRemap++ )
{
/* -------------------------------------------------------------------- */
/* What are the pixel values to map from and to? */
/* -------------------------------------------------------------------- */
char **papszTokens = CSLTokenizeString( papszRemaps[iRemap] );
int *panFromValue, *panToValue;
int nMapBandCount, iBand;
if( CSLCount(papszTokens) % 2 == 1
|| CSLCount(papszTokens) == 0
|| CSLCount(papszTokens) > nBandCount * 2 )
{
CPLError( CE_Warning, CPLE_AppDefined,
"Ill formed REMAP_MULTI `%s' ignored in GDALSimpleWarpRemapping()",
papszRemaps[iRemap] );
continue;
}
nMapBandCount = CSLCount(papszTokens) / 2;
panFromValue = (int *) CPLMalloc(sizeof(int) * nMapBandCount );
panToValue = (int *) CPLMalloc(sizeof(int) * nMapBandCount );
for( iBand = 0; iBand < nMapBandCount; iBand++ )
{
panFromValue[iBand] = atoi(papszTokens[iBand]);
panToValue[iBand] = atoi(papszTokens[iBand+nMapBandCount]);
}
CSLDestroy( papszTokens );
/* -------------------------------------------------------------------- */
/* Search for matching values to replace. */
/* -------------------------------------------------------------------- */
int nPixelCount = nSrcXSize * nSrcYSize;
int iPixel;
for( iPixel = 0; iPixel < nPixelCount; iPixel++ )
{
if( papabySrcData[0][iPixel] != panFromValue[0] )
continue;
int bMatch = TRUE;
for( iBand = 1; iBand < nMapBandCount; iBand++ )
{
if( papabySrcData[iBand][iPixel] != panFromValue[iBand] )
bMatch = FALSE;
}
if( !bMatch )
continue;
for( iBand = 0; iBand < nMapBandCount; iBand++ )
papabySrcData[iBand][iPixel] = (GByte) panToValue[iBand];
}
CPLFree( panFromValue );
CPLFree( panToValue );
}
CSLDestroy( papszRemaps );
}
|