[gtkada] How to access the alpha channel in a Pixbuf

Michal Svoboda pht at spatium.org
Wed Nov 14 12:21:42 CET 2007


Dmitry A. Kazakov wrote 988 bytes:
> > 1. How should I access the alpha channel data?
> > Should I do unchecked_conversion to array of guchars? (Or packed record
> > with R, G, B, and A members)
> > [...]
> The bindings could be a bit thicker here, or maybe thinner, returning a
> pointer to guchar, obtained from Interfaces.C.Pointers.

Thanks for the reply. We created a generic unit (see below) to obtain a
constrained array of RGBA pixels. Not a big deal, but might be useful to
someone else too. 

with Gdk.Pixbuf; use Gdk.Pixbuf;
with Glib; use Glib;

generic
   Num_Pixels : Natural;

package RGBA_Buffer is

   type RGBA_Record is record
      Red, Green, Blue, Alpha : Guchar;
   end record;
   Pragma Pack(RGBA_Record);

   subtype Pixel_Range is Natural range 0 .. Num_Pixels - 1;

   type RGBA_Array is array(Pixel_Range) of RGBA_Record;
   type RGBA_Access is access RGBA_Array;
   Pragma Convention(C, RGBA_Array);
   Pragma Convention(C, RGBA_Access);

   function Get_Pixels(P : Gdk_Pixbuf) return RGBA_Access;

private

   pragma Import (C, Get_Pixels, "gdk_pixbuf_get_pixels");

end RGBA_Buffer;





More information about the gtkada mailing list