[Gdal-dev] How to scale an image

Frank Warmerdam warmerdam at pobox.com
Fri Nov 9 12:17:02 EST 2007


Sonson Sonson wrote:
> 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.

Sonson,

I'll add a few inline comments, but of course it is hard to judge from
this subset.

>                                GDALDataset  *poDataset = (GDALDataset
> *) GDALOpen( strInputFile, GA_ReadOnly );
> 				.................
> 				.................
> 				GDALRasterBand  *poBand = poDataset->GetRasterBand(1);
> 				.........
> 				.........
> 				//Load raster.
> 				char *data;		
> 
> 				nXSize = dxsize* dysize;

Can I just say, it is painful for me to see this variable name?  How
about nBufSize!
> 				data = (char *) CPLMalloc(nXSize*eType);

You don't show what eType is, but if it is a GDALDataType value then this
is wrong.  You likely want (GDALGetDataTypeSize(eType)/8).  GDALGetDataTypeSize
returns the size of the passed data type (such as GDT_Float32) in bits.

> 				poBand->RasterIO(GF_Read, 0, 0, rxsize, rysize, data, dxsize,
> dysize,  eType, 0, 0);

Can I assume that rxsize,rysize are the sizes of the source dataset, and
dxsize,dysize are the desired output size?

  				..............
> 				..............
> 				
> 				GDALDataset *poDstDS = poDriver->Create( filename, newsize,
> newsize, bands, eType, papszOptions );

Why aren't you creating the output file as dxsize,dysize instead of
newsize,newsize?

> 				
> 				poDstDS->SetGeoTransform( geotransform );
> 				poDstDS->SetProjection(projection);
> 				poDstDS->GetRasterBand(1)->RasterIO(GF_Write, 0, 0, rxsize,
> rysize, data, dxsize, dysize,  eType, 0, 0);

The data in "data" is already dxsize x dysize, so assuming the output file is
dxsize x dysize you likely want to write it using:

  poDstDS->GetRasterBand(1)->RasterIO(GF_Write, 0, 0, dxsize, dysize,
                                      data, dxsize, dysize,  eType, 0, 0);


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