[Gdal-dev] How to scale an image

Sonson Sonson sonson25 at gmail.com
Fri Nov 9 13:13:19 EST 2007


Sorry. I have added some inline comments.
Please see the code below.

	                        GDALDataType eType = GDT_Byte;
				.................
				.................
				GDALDataset  *poDataset = (GDALDataset *) GDALOpen( strInputFile,
GA_ReadOnly );
				.................
				.................
				GDALRasterBand  *poBand = poDataset->GetRasterBand(1);
				.........
				.........
				//Load raster.
				char *data;		

				nXSize = dxsize* dysize; //Buffer size
				data = (char *) CPLMalloc(nXSize*eType);
				//rxsize and rysize are the size of the input dataset and dxsize *
dysize is the size in byte of the buffer
				// That will store the read data in the data.
				poBand->RasterIO(GF_Read, 0, 0, rxsize, rysize, data, dxsize,
dysize,  eType, 0, 0);
																																														
				..............
				..............
				
				//newsize is the size of my new image. It is very likely to be
different than rxsize and rysize.
				GDALDataset *poDstDS = poDriver->Create( filename, newsize,
newsize, bands, eType, papszOptions );
				
				poDstDS->SetGeoTransform( geotransform );
				poDstDS->SetProjection(projection);
				// I would like to write the data in a new image of size newsize.
Basically the idea is that,
				// If rxsize and rysize are: 1000x1000 and newsize is 512x512
				// I would like to have a new image that is scaled down to
512x512, but with the same data as
				//the original 1000x1000 image.
				poDstDS->GetRasterBand(1)->RasterIO(GF_Write, 0, 0, newsize,
newsize, data, dxsize, dysize,  eType, 0, 0);
				poDstDS->FlushCache();
				
				delete poDstDS;

On Nov 9, 2007 12:17 PM, Frank Warmerdam <warmerdam at pobox.com> wrote:
> 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.
Yes eType is GDALDataType of value GDT_Byte
>
> >                               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?
>
rxsize and rysize are the size of the source dataset and dxsize and
dysize are just the size of buffer to read. The desired output dataset
size is newsize.
>                                 ..............
> >                               ..............
> >
> >                               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);
My problem here, is that dxsize and newsize might be different.
>
>
>
> 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
>
>

Thanks.
Sonson



More information about the gdal-dev mailing list