天天看点

java gdal dataset.readraster_#3073 ([PATCH - C#] Dataset.ReadRaster()/WriteRaster() doesn't work on ...

Tamas,

The Marshal.AllocHGlobal and Marshal.Copy calls in Dataset.ReadRaster?()/WriteRaster?() mainly forget to multiply the pixel count by the number of bands, thus only the data for the first band is correctly read/written for pixel-interleaved buffers. Attached a patch that fixes the issue (adapted from gdal_java.i). I've also better taken into account the pixelSpace, lineSpace and bandSpace parameters to compute the number of elements to copy, as done in BandRasterIO_Validate and DatasetRasterIO_Validate in gdal_java.i. Currently, this wouldn't work with exotic values of spacings.

With that patch, the following code snippet does produce a pink image :

using System;

using OSGeo.GDAL;

class test

{

public static void Main(string[] args)

{

Gdal.AllRegister();

Driver drv = Gdal.GetDriverByName("GTiff");

int iWidth = 100;

int iHeight = 100;

Dataset ds_new = drv.Create("testrgb.tif", iWidth, iHeight, 3, DataType.GDT_Byte, new string[] { });

byte[] buffers = new byte[(iHeight * iWidth) * 3];

int i, j;

for(i=0;i

{

for(j=0;j

{

buffers[(i*iWidth+j)] = 255;

buffers[100*100+(i*iWidth+j)] = 0;

buffers[2*100*100+(i*iWidth+j)] = 255;

}

}

int[] iBandMap = { 1, 2, 3 };

ds_new.WriteRaster(0, 0, iWidth, iHeight, buffers, iWidth, iHeight, 3, iBandMap, 0, 0, 0);

ds_new = null;

}

}