[Gdal-dev] geotiff 2 png with c++

Lijian Shi lijian.shi at und.nodak.edu
Sun Aug 5 02:09:49 EDT 2007


Hi, all,

 

I want to convert data with geotiff format into png format and I know i can
do it with gdal_translate. Now I want to write a c++ code to do this job
because I need do some change on the raw data from geotiff file before I
write into png file.  The code is as followed but there seems to be some
wrong with it. Anyone could help me to figure out the mistake? Thanks in
advance!

 

Lijian

 

.............

float *srcdata = (float *) CPLMalloc(sizeof(float)*nXSize*nYSize);

 

...// the data has been read into srcdata from geotiff file

 

 

    //Creating PNG Files

    const char *dstFormat = "PNG";    

    GDALDriver *dstDriver;

    char **dstMetadata;

 

    dstDriver = GetGDALDriverManager()->GetDriverByName(dstFormat);

 

    if( dstDriver == NULL )

      exit(1);

 

    dstMetadata = dstDriver->GetMetadata();

    if( CSLFetchBoolean( dstMetadata, GDAL_DCAP_CREATE, FALSE ) )

      cout <<"Driver " <<dstFormat <<" supports Create() method.\n";

    if( CSLFetchBoolean( dstMetadata, GDAL_DCAP_CREATECOPY, FALSE ) )

      cout <<"Driver " <<dstFormat <<" supports CreateCopy() method.\n";

 

    

    GDALDataset *dstDS;

 

dstDS = dstDriver->CreateCopy(dstFilename, srcDataset, FALSE, NULL, NULL,
NULL); //srcDataset is from geotiff file. Is this step right?

//GDALDataset *srcDS = (GDALDataset *) GDALOpen(srcFilename, GA_ReadOnly);

 

            

    if( dstDS != NULL )

      delete dstDS;

 

    GDALRasterBand *dstBand;

    dstBand = dstDS->GetRasterBand(1);

    int i, j;

    unsigned *dstdata = (unsigned *)
CPLMalloc(sizeof(unsigned)*nXSize*nYSize);

    for (i=0; i<nXSize; i++)

            for (j=0; j<nYSize; j++)

            {

                        dstdata[j+i*nYSize] = unsigned
(srcdata[j+i*nYSize]); // here i just convert the data from float into
uint16

            }

  //          cout<< dstdata[0] << dstdata[4]<<"test\n";

dstBand->RasterIO(GF_Write, 0, 0, nXSize, nYSize, dstdata, nXSize, nYSize,
GDT_UInt16, 0, 0);

 

......

 

 

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20070805/4493a75c/attachment.html


More information about the Gdal-dev mailing list