1. ----------------------------------------------------------------------- 
  2. --               GtkAda - Ada95 binding for Gtk+/Gnome               -- 
  3. --                                                                   -- 
  4. --                    Copyright (C) 2010-2013, AdaCore               -- 
  5. --                                                                   -- 
  6. -- This library is free software; you can redistribute it and/or     -- 
  7. -- modify it under the terms of the GNU General Public               -- 
  8. -- License as published by the Free Software Foundation; either      -- 
  9. -- version 2 of the License, or (at your option) any later version.  -- 
  10. --                                                                   -- 
  11. -- This library is distributed in the hope that it will be useful,   -- 
  12. -- but WITHOUT ANY WARRANTY; without even the implied warranty of    -- 
  13. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU -- 
  14. -- General Public License for more details.                          -- 
  15. --                                                                   -- 
  16. -- You should have received a copy of the GNU General Public         -- 
  17. -- License along with this library; if not, write to the             -- 
  18. -- Free Software Foundation, Inc., 59 Temple Place - Suite 330,      -- 
  19. -- Boston, MA 02111-1307, USA.                                       -- 
  20. --                                                                   -- 
  21. -- As a special exception, if other files instantiate generics from  -- 
  22. -- this unit, or you link this unit with other files to produce an   -- 
  23. -- executable, this  unit  does not  by itself cause  the resulting  -- 
  24. -- executable to be covered by the GNU General Public License. This  -- 
  25. -- exception does not however invalidate any other reasons why the   -- 
  26. -- executable file  might be covered by the  GNU Public License.     -- 
  27. ----------------------------------------------------------------------- 
  28.  
  29. --  <description> 
  30. --  Image surfaces provide the ability to render to memory buffers either 
  31. --  allocated by Cairo or by the calling code. The supported image formats are 
  32. --  those defined in Cairo_Format. 
  33. --  </description> 
  34. -- 
  35. --  <c_version>1.8.8</c_version> 
  36. --  <group>Cairo</group> 
  37.  
  38. package Cairo.Image_Surface is 
  39.  
  40.    --  Cairo_Format is used to identify the memory format of 
  41.    --  image data. 
  42.    -- 
  43.    --  New entries may be added in future versions. 
  44.    type Cairo_Format is 
  45.      (Cairo_Format_ARGB32, 
  46.       --  Each pixel is a 32-bit quantity, with 
  47.       --  alpha in the upper 8 bits, then red, then green, then blue. 
  48.       --  The 32-bit quantities are stored native-endian. Pre-multiplied 
  49.       --  alpha is used. (That is, 50 transparent red is 0x80800000, 
  50.       --  not 0x80ff0000.) 
  51.  
  52.       Cairo_Format_RGB24, 
  53.       --  Each pixel is a 32-bit quantity, with 
  54.       --  the upper 8 bits unused. Red, Green, and Blue are stored 
  55.       --  in the remaining 24 bits in that order. 
  56.  
  57.       Cairo_Format_A8, 
  58.       --  Each pixel is a 8-bit quantity holding an alpha value. 
  59.  
  60.       Cairo_Format_A1, 
  61.       --  Each pixel is a 1-bit quantity holding 
  62.       --  an alpha value. Pixels are packed together into 32-bit 
  63.       --  quantities. The ordering of the bits matches the 
  64.       --  endianess of the platform. On a big-endian machine, the 
  65.       --  first pixel is in the uppermost bit, on a little-endian 
  66.       --  machine the first pixel is in the least-significant bit. 
  67.  
  68.       CAIRO_FORMAT_RGB16_565_Deprecated_Do_Not_Use 
  69.       --  This value is deprecated 
  70.      ); 
  71.    pragma Convention (C, Cairo_Format); 
  72.  
  73.    type Byte is range 0 .. 255; 
  74.  
  75.    type ARGB32_Data is record 
  76.       Alpha : Byte; 
  77.       Red   : Byte; 
  78.       Green : Byte; 
  79.       Blue  : Byte; 
  80.    end record; 
  81.    --  One pixel in ARGB32 format 
  82.  
  83.    type RGB24_Data is record 
  84.       Red   : Byte; 
  85.       Green : Byte; 
  86.       Blue  : Byte; 
  87.    end record; 
  88.    --  One pixel in RGB24 format 
  89.  
  90.    type ARGB32_Array is array (Natural range <>) of ARGB32_Data; 
  91.    type ARGB32_Array_Access is access ARGB32_Array; 
  92.  
  93.    type RGB24_Array is array (Natural range <>) of RGB24_Data; 
  94.    type RGB24_Array_Access is access RGB24_Array; 
  95.  
  96.    type Byte_Array is array (Natural range <>) of Byte; 
  97.    type Byte_Array_Access is access Byte_Array; 
  98.  
  99.    function Create 
  100.      (Format : Cairo_Format; 
  101.       Width  : Gint; 
  102.       Height : Gint) 
  103.       return   Cairo_Surface; 
  104.    --  Format: Format of pixels in the surface to create 
  105.    --  Width: Width of the surface, in pixels 
  106.    --  Height: Height of the surface, in pixels 
  107.    -- 
  108.    --  Creates an image surface of the specified format and 
  109.    --  dimensions. Initially the surface contents are all 
  110.    --  0. (Specifically, within each pixel, each color or alpha channel 
  111.    --  belonging to format will be 0. The contents of bits within a pixel, 
  112.    --  but not belonging to the given format are undefined). 
  113.    -- 
  114.    --  Return value: a pointer to the newly created surface. The caller 
  115.    --  owns the surface and should call Cairo.Surface.Destroy when done 
  116.    --  with it. 
  117.    -- 
  118.    --  This function always returns a valid pointer, but it will return a 
  119.    --  pointer to a "nil" surface if an error such as out of memory 
  120.    --  occurs. You can use Cairo.Surface.Status to check for this. 
  121.  
  122.    function Cairo_Format_Stride_For_Width 
  123.      (Format : Cairo_Format; 
  124.       Width  : Gint) 
  125.       return   Gint; 
  126.    --  Format: A Cairo_Format value 
  127.    --  Width: The desired Width of an image surface to be created. 
  128.    -- 
  129.    --  This function provides a stride value that will respect all 
  130.    --  alignment requirements of the accelerated image-rendering code 
  131.    --  within cairo. 
  132.    -- 
  133.    --  Return value: the appropriate stride to use given the desired 
  134.    --  format and width, or -1 if either the format is invalid or the width 
  135.    --  too large. 
  136.    -- 
  137.    --  Since: 1.6 
  138.  
  139.    function Create_For_Data_Generic 
  140.      (Data   : System.Address; 
  141.       Format : Cairo_Format; 
  142.       Width  : Gint; 
  143.       Height : Gint; 
  144.       Stride : Gint) 
  145.       return   Cairo_Surface; 
  146.    --  Data: a pointer to a buffer supplied by the application in which 
  147.    --      to write contents. This pointer must be suitably aligned for any 
  148.    --      kind of variable, (for example, a pointer returned by malloc). 
  149.    --  Format: the Format of pixels in the buffer 
  150.    --  Width: the Width of the image to be stored in the buffer 
  151.    --  Height: the Height of the image to be stored in the buffer 
  152.    --  Stride: the number of bytes between the start of rows in the 
  153.    --      buffer as allocated. This value should always be computed by 
  154.    --      Cairo_Format_Stride_For_Width before allocating the data 
  155.    --      buffer. 
  156.    -- 
  157.    --  This function is a low-level binding to the C function: see also 
  158.    --  Create_For_Data_[ARGB32|RGB24|A8|A1] 
  159.    -- 
  160.    --  Creates an image surface for the provided pixel data. The output 
  161.    --  buffer must be kept around until the Cairo_Surface is destroyed 
  162.    --  or Cairo.Surface.Finish is called on the surface.  The initial 
  163.    --  contents of data will be used as the initial image contents; you 
  164.    --  must explicitly clear the buffer, using, for example, 
  165.    --  Cairo.Rectangle and Cairo.Fill if you want it cleared. 
  166.    -- 
  167.    --  Note that the stride may be larger than Width*bytes_per_pixel to provide 
  168.    --  proper alignment for each pixel and row. This alignment is required to 
  169.    --  allow high-performance rendering within cairo. The correct way to obtain 
  170.    --  a legal stride value is to call Cairo_Format_Stride_For_Width with the 
  171.    --  desired format and maximum image width value, and the use the resulting 
  172.    --  stride value to allocate the data and to create the image surface. See 
  173.    --  Cairo_Format_Stride_For_Width for example code. 
  174.    -- 
  175.    --  Return value: a pointer to the newly created surface. The caller 
  176.    --  owns the surface and should call Cairo.Surface.Destroy when done 
  177.    --  with it. 
  178.    -- 
  179.    --  This function always returns a valid pointer, but it will return a 
  180.    --  pointer to a "nil" surface in the case of an error such as out of 
  181.    --  memory or an invalid stride value. In case of invalid stride value 
  182.    --  the error status of the returned surface will be 
  183.    --  Cairo_Status_Invalid_Stride.  You can use 
  184.    --  Cairo.Surface.Status to check for this. 
  185.    -- 
  186.    --  See Cairo.Surface.Set_User_Data for a means of attaching a 
  187.    --  destroy-notification fallback to the surface if necessary. 
  188.  
  189.    function Create_For_Data_ARGB32 
  190.      (Data   : ARGB32_Array_Access; 
  191.       Width  : Gint; 
  192.       Height : Gint) 
  193.       return   Cairo_Surface; 
  194.    --  Same as above, working on ARGB32 format. 
  195.  
  196.    function Create_For_Data_RGB24 
  197.      (Data   : RGB24_Array_Access; 
  198.       Width  : Gint; 
  199.       Height : Gint) 
  200.       return   Cairo_Surface; 
  201.    --  Same as above, working on RGB24 format. 
  202.  
  203.    function Create_For_Data_A8 
  204.      (Data   : Byte_Array_Access; 
  205.       Width  : Gint; 
  206.       Height : Gint) 
  207.       return   Cairo_Surface; 
  208.    --  Same as above, working on A8 format. 
  209.  
  210.    function Get_Data_Generic (Surface : Cairo_Surface) return System.Address; 
  211.    --  Surface: a Cairo_Image_Surface 
  212.    -- 
  213.    --  Get a pointer to the data of the image surface, for direct 
  214.    --  inspection or modification. 
  215.    -- 
  216.    --  Return value: a pointer to the image data of this surface or 
  217.    --  System.Null_Address if surface is not an image surface, or if 
  218.    --  Cairo.Surface.Finish has been called. 
  219.    -- 
  220.    --  Since: 1.2 
  221.  
  222.    function Get_Format (Surface : Cairo_Surface) return Cairo_Format; 
  223.    --  Surface: a Cairo_Image_Surface 
  224.    -- 
  225.    --  Get the format of the surface. 
  226.    -- 
  227.    --  Return value: the format of the surface 
  228.    -- 
  229.    --  Since: 1.2 
  230.  
  231.    function Get_Width (Surface : Cairo_Surface) return Gint; 
  232.    --  Surface: a Cairo_Image_Surface 
  233.    -- 
  234.    --  Get the width of the image surface in pixels. 
  235.    -- 
  236.    --  Return value: the width of the surface in pixels. 
  237.  
  238.    function Get_Height (Surface : Cairo_Surface) return Gint; 
  239.    --  Surface: a Cairo_Image_Surface 
  240.    -- 
  241.    --  Get the height of the image surface in pixels. 
  242.    -- 
  243.    --  Return value: the height of the surface in pixels. 
  244.  
  245.    function Get_Stride (Surface : Cairo_Surface) return Gint; 
  246.    --  Surface: a Cairo_Image_Surface 
  247.    -- 
  248.    --  Get the stride of the image surface in bytes 
  249.    -- 
  250.    --  Return value: the stride of the image surface in bytes (or 0 if 
  251.    --  surface is not an image surface). The stride is the distance in 
  252.    --  bytes from the beginning of one row of the image data to the 
  253.    --  beginning of the next row. 
  254.    -- 
  255.    --  Since: 1.2 
  256.  
  257. private 
  258.  
  259.    for Byte'Size use 8; 
  260.  
  261.    --  Representation working with all endiannesses 
  262.  
  263.    BOP  : constant := System.Bit_Order'Pos (System.Default_Bit_Order); 
  264.    NBOP : constant := 1 - BOP; 
  265.  
  266.    for ARGB32_Data use record 
  267.       Alpha at BOP * 3 + NBOP * 0 range 0 .. 7; 
  268.       Red   at BOP * 2 + NBOP * 1 range 0 .. 7; 
  269.       Green at BOP * 1 + NBOP * 2 range 0 .. 7; 
  270.       Blue  at BOP * 0 + NBOP * 3 range 0 .. 7; 
  271.    end record; 
  272.  
  273.    for RGB24_Data use record 
  274.       Red   at BOP * 2 + NBOP * 1 range 0 .. 7; 
  275.       Green at BOP * 1 + NBOP * 2 range 0 .. 7; 
  276.       Blue  at BOP * 0 + NBOP * 3 range 0 .. 7; 
  277.    end record; 
  278.    for RGB24_Data'Size use 32; 
  279.  
  280.    pragma Import (C, Create, "cairo_image_surface_create"); 
  281.    pragma Import 
  282.      (C, Cairo_Format_Stride_For_Width, "cairo_format_stride_for_width"); 
  283.  
  284.    pragma Import 
  285.      (C, Create_For_Data_Generic, "cairo_image_surface_create_for_data"); 
  286.  
  287.    pragma Import (C, Get_Data_Generic, "cairo_image_surface_get_data"); 
  288.    pragma Import (C, Get_Format, "cairo_image_surface_get_format"); 
  289.    pragma Import (C, Get_Width, "cairo_image_surface_get_width"); 
  290.    pragma Import (C, Get_Height, "cairo_image_surface_get_height"); 
  291.    pragma Import (C, Get_Stride, "cairo_image_surface_get_stride"); 
  292.  
  293. end Cairo.Image_Surface;