[gtkada] Problems with type Rgb_Buffer in Gdk.Rgb and function Gtk_New in Gtk.File_Selection
Dmitry A. Kazakov
mailbox at dmitry-kazakov.de
Sat Mar 10 15:49:49 CET 2007
On Sat, 10 Mar 2007 15:00:25 +0100, you wrote:
> 1. My first problem is about package Gdk.Rgb and type Rgb_Buffer. This
> type seems already constrained, thus I can't constrain myself. But when I
> don't constraint it a storage error is raised. There's no function which
> return rgb_buffer, so I don't understanf how use it. I have seen one
> report on this problem but the solution doesn't work (see this mail list :
> subject : "Using Gdk.RGB.Draw_RGB_Image". May 2001). Is somebody have an
> explanation ?
It is constrained because only constrained arrays can be passed down to C.
Unconstrained Ada arrays have a dope C would not understand.
If you want to use Gdk.RGB to manipulate client-side images, you probably
should make a pair of helper types of your own.
I am using the following (for color images, Gdk.RGB offers other image
types as well):
-- The pixel encoded as draw_rgb_image expects
type RGB_Pixel is record
Red : GUChar;
Green : GUChar;
Blue : GUChar;
end record;
pragma Convention (C, RGB_Pixel);
-- A function to convert Gdk_Color to RGB pixel
function To_RGB_Pixel (Color : Gdk_Color) return RGB_Pixel is
function To_GUChar (Value : GUInt16) return GUChar is
pragma Inline (To_GUChar);
begin
return GUChar (Value / 256);
end To_GUChar;
begin
return
( Red => To_GUChar (Red (Color)),
Green => To_GUChar (Green (Color)),
Blue => To_GUChar (Blue (Color))
);
end To_RGB_Pixel;
-- An RGB image
type RGB_Image is
array (Rows range <>, Colums range <>)
of RGB_Pixel;
pragma Convention (C, RGB_Image);
The RGB_Image is a two-dimensional array indexed by integer types of rows
and columns. They could be any, but I prefer to have them distinct types.
To draw this array onto a drawable (server-side) use
procedure Draw_Pixels
( Drawable : Gdk_Drawable;
GC : Gdk.Gdk_GC;
X : GInt;
Y : GInt;
Width : GInt;
Height : GInt;
Dith : Gdk.RGB.Gdk_Rgb_Dither;
Rgb_Buf : System.Address; -- System address, note
Rowstride : GInt
);
pragma Import (C, Draw_Pixels, "gdk_draw_rgb_image");
Let you have:
Image : aliased RGB_Image (1..Height, 1..Width);
Then you can call Draw_Pixels as
Draw_Pixels
( Drawable => ...,
GC => ...,
X => ...,
Y => ...,
RGB_Buf => Image'Address,
Dith => Gdk.RGB.Dither_None,
Rowstride => GInt (3 * Image'Length (2)),
Width => Image'Length (2), -- You might also wish to clip image
Height => Image'Length (1) -- here
);
You can carelessly take Address from Image because Ada RM requires that
Image'Address would return the address of the first array element rather
than of the array.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
More information about the gtkada
mailing list