[Gdal-dev] How to scale an image
Sonson Sonson
sonson25 at gmail.com
Fri Nov 9 11:59:28 EST 2007
Thanks Frank for your response.
I would like to do this at the API level.
Here is what I am trying to do. I am trying to read an image, set of
size 1000x1000 and write it to a new file of with a bigger size of say
1024x1024 or a smaller size like 512x512. Below a sample code of how I
try to achieve this. newsize is the width and length of my destination
image. I read an image, of size rxsize x rysize, with
poBand->RasterIO(GF_Read, 0, 0, rxsize, rysize, data, dxsize, dysize,
eType, 0, 0); and create a file of size newsize x newsize, and later
write the pixed I read to the new file. I am sure there is something
wrong with the code, but I cannot seem to figure it out. Please help.
GDALDataset *poDataset = (GDALDataset
*) GDALOpen( strInputFile, GA_ReadOnly );
.................
.................
GDALRasterBand *poBand = poDataset->GetRasterBand(1);
.........
.........
//Load raster.
char *data;
nXSize = dxsize* dysize;
data = (char *) CPLMalloc(nXSize*eType);
poBand->RasterIO(GF_Read, 0, 0, rxsize, rysize, data, dxsize,
dysize, eType, 0, 0);
..............
..............
GDALDataset *poDstDS = poDriver->Create( filename, newsize,
newsize, bands, eType, papszOptions );
poDstDS->SetGeoTransform( geotransform );
poDstDS->SetProjection(projection);
poDstDS->GetRasterBand(1)->RasterIO(GF_Write, 0, 0, rxsize,
rysize, data, dxsize, dysize, eType, 0, 0);
poDstDS->FlushCache();
delete poDstDS;
On Nov 9, 2007 10:54 AM, Frank Warmerdam <warmerdam at pobox.com> wrote:
>
> Sonson Sonson wrote:
> > Hi all,
> > I have a question. I would like to know how to scale an image. I am
> > writing a program where I need to scale an image before processing it,
> > but I cannot find any sample program, or documentation on how to do
> > that. Say if my image's size is 6579x3289 pixel and I want it to be
> > scaled to 6656x3328, can anybody point me to documentation and sample
> > codes on how to do it.
>
> Sonson,
>
> What sort of sampling approach do you want to take? If you just
> want nearest neighbour resampling you can do this as:
>
> gdal_translate -outsize 6656 3328 in.tif out.tif
>
> or at the API level by specifying 6656,3328 as your buffer size when
> doing a RasterIO() to read into memory.
>
> If you want to control the resampling kernel you need to use the warp
> api. At the commandline:
>
> gdalwarp -rc in.tif out.tif -ts 6656 3328
>
> At the API level it is fairly involved. You could review:
>
> http://www.gdal.org/warptut.html
>
> Best regards,
> --
> ---------------------------------------+--------------------------------------
> I set the clouds in motion - turn up | Frank Warmerdam, warmerdam at pobox.com
> light and sound - activate the windows | http://pobox.com/~warmerdam
> and watch the world go round - Rush | President OSGeo, http://osgeo.org
>
>
More information about the gdal-dev
mailing list