[Gdal-dev] C# Bindings

Tamas Szekeres szekerest at gmail.com
Fri Nov 3 06:04:37 EST 2006


>
> Fair enough. My initial thought was to suggest a signature like:
>
>   public void ReadRaster(int xOff, int yOff, int xSize, int ySize, byte*
> buffer, int bufXSize, int bufYSize, int pixelSpace, int lineSpace)
>
> and similarly for other data types (short, ushort, int, uint, long,
> ulong, float, double). Using a pointer rather than an array for the
> buffer allows the method to be used to fill Bitmaps without having to
> copy into an intermediate array.
>

I'm afraid we will not be able to map pointers to the C# interface
easily. Instead we could
use the InPtr type but the marshaling code should be written
externally in that case.
At the first sight to keep the interface obvious I would suggest to
support the array types at the C# side like:

public int ReadRaster(int xOff, int yOff, int xSize, int ySize, byte[]
buffer, ref int bufXSize, ref int bufYSize)

or something like this.

I couldn't understand exactly your problem about the need of the
double copy of the arrays. Would you
post a sample that suffers from this issue?

> So, that would be 9 methods, and that's not even including signed bytes
> or complex data types...
>
> It would also be nice not to have to specify all the arguments in common
> cases. Since we don't have default arguments in C#, we could use two
> more overloads for each of the above with signatures like:
>
>   // bufXSize == xSize, bufYSize == ySize, pixelSpace ==
> sizeof(datatype), lineSpace == sizeof(datatype) * bufXSize
>   public void ReadRaster(int xOff, int yOff, int xSize, int ySize, byte*
> buffer)
>
>   // pixelSpace == sizeof(datatype), lineSpace == sizeof(datatype) *
> bufXSize
>   public void ReadRaster(int xOff, int yOff, int xSize, int ySize, byte*
> buffer, int bufXSize, int bufYSize)
>
>
> So we're up to 27 different methods...
>
> And then all over again for WriteRaster(). Making a grand total of 54
> different method signatures for reading and writing data - not covering
> all the possible data types! Hmmm...
>
>

I have no problem about the number of the overloads. It would be more
important to keep
the interface obvious and clear.
We have no one by one mapping between the C++ and C# blittable types.
For a mapping table see for example the blittable types section at:
 http://www.mono-project.com/Interop_with_Native_Libraries

>
> OK, if that's way too many, how about this as an alternative: we follow
> the GDAL pattern and pass an IntPtr buffer, along with an enum argument
> that specifies the data type. We can probably also drop one of the above
> overloads, since this is a low level API. In that case, we would have a
> grand total of 4 signatures:
>
>   public void ReadRaster(int xOff, int yOff, int xSize, int ySize,
> IntPtr buffer, int bufXSize, int bufYSize, GDALDataType type)
>
>  public void ReadRaster(int xOff, int yOff, int xSize, int ySize, IntPtr
> buffer, int bufXSize, int bufYSize, GDALDataType type, int pixelSpace,
> int lineSpace)
>
>   public void WriteRaster(int xOff, int yOff, int xSize, int ySize,
> IntPtr buffer, int bufXSize, int bufYSize, GDALDataType type)
>
>  public void WriteRaster(int xOff, int yOff, int xSize, int ySize,
> IntPtr buffer, int bufXSize, int bufYSize, GDALDataType type, int
> pixelSpace, int lineSpace)
>

It would require manual marshaling by the user and may result in high
number of problems.

>
> How does that sound? Maybe at some point in the future when generics are
> the norm, we can add templated versions to get a  modicum of type safety
> and avoid having to specify a data type explicitly.
>

How could we restrict the usage of the templates to the array of the
simple blittable types?
However in the future it might be an option but now it's supported
only by MS.NET FW 2.0 AFAIK.


Best Regards,

Tamas



More information about the Gdal-dev mailing list