1. ------------------------------------------------------------------------------ 
  2.  --  File :            GL - IO.ads 
  3.  --  Description :     I/O for (Open)GL graphics 
  4.  -- 
  5.  --                   This package provides currently: 
  6.  -- 
  7.  --                   ****************************************************** 
  8.  --                   * INPUT * from a file or a data stream, to a texture * 
  9.  --                   ****************************************************** 
  10.  -- 
  11.  --                    - TGA image : RGA, RGBA, Grey 
  12.  --                    - BMP image : B&W, 16 colours indexed (palette), 
  13.  --                        256 colours indexed 
  14.  -- 
  15.  --                   *************************************************** 
  16.  --                   * OUTPUT * from the GL active viewport, to a file * 
  17.  --                   *************************************************** 
  18.  -- 
  19.  --                    - BMP image : screenshot 
  20.  --                    - AVI video : video capture 
  21.  -- 
  22.  ------------------------------------------------------------------------------ 
  23.  -- Change log: 
  24.  -- 
  25.  -- 19 - Jan - 2010 (GdM) : using workaround to the slow attribute I/O issue (GNAT, OA); 
  26.  --                      buffered input; improvements on BMP 
  27.  -- 
  28.  -- 26 - May - 2008 (GdM) : added support for TGA images with RLE encoding 
  29.  -- 
  30.  -- 27 - Jan - 2008 (RK) :  added 'Image' record and a function to get greyscale pixels from an Image. 
  31.  -- 
  32.  -- 10 - May - 2007 (GdM) : screenshot and video capture 
  33.  -- 
  34.  -- 13 - Oct - 2006 (GdM) : new blending_hint out parameter, indicates possible 
  35.  --                      blending/transparency 
  36.  -- 
  37.  -- 30 - Apr - 2006 (GdM) : - added multi - format loaders 
  38.  --                    - dimensions not power of two allowed, but 
  39.  --                      discouraged in the docs. 
  40.  --                      - > removed TGA_BAD_DIMENSION 
  41.  
  42. with Ada.Streams.Stream_IO; 
  43. with Ada.Unchecked_Deallocation; 
  44.  
  45. package GL.IO is 
  46.  
  47.   File_Not_Found  : exception; 
  48.  
  49.   type Supported_format is (BMP, TGA); 
  50.  
  51.   type Byte_Array is array (Integer range <>) of aliased GL.Ubyte; 
  52.   type Byte_Array_Ptr is access all Byte_Array; 
  53.  
  54.   procedure Free is 
  55.     new Ada.Unchecked_Deallocation (Byte_Array, Byte_Array_Ptr); 
  56.  
  57.   type Byte_Grid is array (Integer range <>, Integer range <>) of aliased GL.Ubyte; 
  58.  
  59.   type Image is 
  60.     record 
  61.       blending_hint     : Boolean;        -- has the image blending / transparency /alpha ? 
  62.       tex_Format        : GL.TexFormatEnm; 
  63.       tex_pixel_Format  : GL.TexPixelFormatEnm; 
  64.       size              : Integer; 
  65.       Width, 
  66.       Height            : Integer; 
  67.       Data              : Byte_Array_Ptr; 
  68.     end record; 
  69.  
  70.   function To_TGA_Image (Filename : String) return Image; 
  71.  
  72.   function To_TGA_Image (S : Ada.Streams.Stream_IO.Stream_Access) return Image; 
  73.  
  74.   function to_greyscale_Pixels (the_Image : Image) return Byte_Grid; 
  75.  
  76.   -- Multi - format loader: 
  77.  
  78.    procedure Load (name          :     String;            -- file name 
  79.                    format        :     Supported_format;  -- expected file format 
  80.                    ID            :     Integer;           -- ID is the texture identifier to bind to 
  81.                    blending_hint : out Boolean);          -- has blending / transparency /alpha ? 
  82.  
  83.    procedure Load (s             :     Ada.Streams.Stream_IO.Stream_Access; -- input data stream (e.g. UnZip.Streams) 
  84.                    format        :     Supported_format;  -- expected file format 
  85.                    ID            :     Integer;           -- ID is the texture identifier to bind to 
  86.                    blending_hint : out Boolean);          -- has blending / transparency /alpha ? 
  87.  
  88.   -- Loaders specific to different formats: 
  89.  
  90.   ---------------------- 
  91.   -- BMP format Input -- 
  92.   ---------------------- 
  93.  
  94.    procedure Load_BMP (Name          :     String;   -- File name 
  95.                        Id            :     Integer;  -- Id is the texture identifier to bind to 
  96.                        blending_hint : out Boolean); -- has the image blending / transparency /alpha ? 
  97.  
  98.   procedure Load_BMP (S             :     Ada.Streams.Stream_IO.Stream_Access;  -- Input data stream 
  99.                       Id            :     Integer;  -- Id is the texture identifier to bind to 
  100.                       blending_hint : out Boolean); -- has the image blending / transparency /alpha ? 
  101.  
  102.   Unsupported_BMP_format, 
  103.   Not_BMP_format, 
  104.   BMP_Unsupported_Bits_per_Pixel, 
  105.   Unsupported_compression :      exception; 
  106.  
  107.   ---------------------- 
  108.   -- TGA format Input -- 
  109.   ---------------------- 
  110.  
  111.    procedure Load_TGA (Name          :     String;   -- File name 
  112.                        Id            :     Integer;  -- Id is the texture identifier to bind to 
  113.                        blending_hint : out Boolean); -- has the image blending / transparency /alpha ? 
  114.  
  115.    procedure Load_TGA (S             :     Ada.Streams.Stream_IO.Stream_Access; -- Input data stream 
  116.                        Id            :     Integer;  -- Id is the texture identifier to bind to 
  117.                        blending_hint : out Boolean); -- has the image blending / transparency /alpha ? 
  118.  
  119.   TGA_Unsupported_Image_Type      : exception;   -- color mapped or compressed image 
  120.   TGA_Unsupported_Bits_per_pixel  : exception;   -- image bits is not 8, 24 or 32 
  121.   TGA_Bad_Data                    : exception;   -- image data could not be loaded 
  122.  
  123.   --------------------------------------------------------------------------- 
  124.   -- Image ("screenshot") of the current, active viewport (RGB BMP format) -- 
  125.   --------------------------------------------------------------------------- 
  126.  
  127.   procedure Screenshot (Name : String); 
  128.  
  129.   -------------------------------------------------- 
  130.   -- Video capture (RGB uncompressed, AVI format) -- 
  131.   -------------------------------------------------- 
  132.  
  133.   procedure Start_Capture (AVI_Name : String; frame_rate : Positive); 
  134.  
  135.   procedure Capture_Frame; -- captures the current, active viewport. 
  136.  
  137.   procedure Stop_Capture; 
  138.  
  139.   -------------------------------------------------------------------------- 
  140.   -- An object - oriented stream buffering, initially for reading images to -- 
  141.   -- the GL system, but that may be useful elsewhere, hence its presence  -- 
  142.   -- in this package's specification                                      -- 
  143.   -------------------------------------------------------------------------- 
  144.   -- 
  145.   type Input_buffer is private; 
  146.  
  147.   procedure Attach_Stream (b   : out Input_buffer; 
  148.                            stm :     Ada.Streams.Stream_IO.Stream_Access); 
  149.  
  150.   procedure Get_Byte (b : in out Input_buffer; Return_Byte : out Ubyte); 
  151.   pragma Inline (Get_Byte); 
  152.  
  153. private 
  154.  
  155.   type Input_buffer is record 
  156.     data        : Byte_Array (1 .. 1024); 
  157.     stm         : Ada.Streams.Stream_IO.Stream_Access; 
  158.     InBufIdx    : Positive;   --  Points to next char in buffer to be read 
  159.     MaxInBufIdx : Natural;    --  Count of valid chars in input buffer 
  160.     InputEoF    : Boolean;    --  End of file indicator 
  161.   end record; 
  162.  
  163. end GL.IO;