[Gdal-dev] how to write block by block to file

Pushkar Pradhan pushkar at GRI.MsState.Edu
Thu Jan 15 00:02:38 EST 2004


I would like to do a block by block read from one file, process the buffer
and write it to output file. However, my code fails with seg. fault, at the
end of this email is my code.
The offending line is poDstDS->poDriver->Create(...);

I'm not using CreateCopy at this moment because I don't want to do a plain
copy. Can someone tell me the correct way to do this? Thanks,

  poDataset = (GDALDataset *) GDALOpen(inFile, GA_ReadOnly);

  imgXSize = poDataset->GetRasterXSize();
  imgYSize = poDataset->GetRasterYSize();
  numBands = poDataset->GetRasterCount();

  poBand = poDataset->GetRasterBand(dwtBand);
  poBand->GetBlockSize(&blockXSize, &blockYSize);

  pData = (float *) CPLMalloc(SZFLOAT*blockXSize*blockYSize);

  poDstDS = poDriver->Create(dwtFile, imgXSize, imgYSize, 1, GDT_Byte,
NULL);

  poBand2 = poDstDS->GetRasterBand(dwtBand);

  for(row = 0; row < imgYSize; row += blockYSize) {
    for(col = 0; col < imgXSize; col += blockXSize) {
      rc = poBand->RasterIO(GF_Read, col, row, blockXSize, blockYSize,
                            pData, blockXSize, blockYSize, GDT_Float32, 0,
0);

	/* I will be putting code here to manipulate pData */

      rc = poBand2->RasterIO(GF_Write, col, row, blockXSize, blockYSize,
                            pData, blockXSize, blockYSize, GDT_Float32, 0,
0);
    }
  }

  GDALClose(poDataset);
  GDALClose(poDstDS);

Pushkar





More information about the Gdal-dev mailing list