Opened 6 years ago

Closed 6 years ago

#6784 closed defect (invalid)

GDAL : how to properly get image pixel value in an 2D array

Reported by: TremblayGIS Owned by: warmerdam
Priority: normal Milestone:
Component: GDAL_Raster Version: unspecified
Severity: normal Keywords: gdal, array, raster, memory
Cc:

Description

Hello, i want to export the image pixel values in a 2D dimensions array.

i have a gdalfile class which contains the export function and the main class.

When the appli is running, it crash when the array ger arounf 100000 value. Here is the message : ERROR 2: CPLMalloc(): Out of memory allocating 65536 bytes.

Here is the function, located in the gdalfile class, based on the gdal tutorial. I simplified it in order to be easier to read :

float **gdalfile::GetFileAsAnArray()
 {

//read raster
    ...
//Fetching a Raster Band
  ...
//Read band and put value in pafScanline
     float *pafScanline;
     int   nXSize = poBand->GetXSize();
     int   nYSize = poBand->GetYSize();
     float nbPixel = nXSize*nYSize;
     pafScanline = (float *) CPLMalloc(sizeof(float)*nbPixel);
     poBand->RasterIO( GF_Read, 0, 0, nXSize, nYSize,
                       pafScanline, nXSize, nYSize, GDT_Float32,
                       0, 0 );

.....
// Creat the array
 int count(0);
 int nColonnes=nXSize;
 int  nLignes=nYSize;

 float **t;
  t = new float* [ nLignes ];
  for (int i=0; i < nLignes; i++)
    t[i] = new float[ nColonnes ];

 // fill the array with pafScanline value
  for (int i=0; i < nLignes; i++)
    for (int j=0; j < nColonnes; j++)
    {
    t[i][j] = pafScanline[count];
    count++;
    }

  return t ;

here is the main (simplified)

//I create the object with the constructor
gdalfile gdalTest("D:/XXXX.tif") ;

    float** p= gdalTest.GetFileAsAnArray();

     // i just go throug the array and add 1500 to every pixel, it's just a test
     for (int i=0; i < gdalTest.getYLine(); i++)
       for (int j=0; j < gdalTest.getXColonne(); j++)
       {
       p[i][j]= p[i][j]+1500;
        qDebug()<< p[i][j];
       }

Maybe i do not properly manage the memory. I think the issue is the way how i pass the array... could you help me to solve this issue ?

Regards

Change History (1)

comment:1 by Even Rouault, 6 years ago

Resolution: invalid
Status: newclosed

This bug tracker is not aimed at questions. You can use other forums for that like gis.stackexchange or the gdal-dev mailing list.

Note: See TracTickets for help on using tickets.