-- $Date: 2004/01/02 09:54:39 $
-- $Revision: 1.2 $
-- $Author: jcrocholl $

with Interfaces; use Interfaces;
with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;

package PBM is

   -- Raised if the magic number is not P4.
   Expect_P4 : exception;

   -- Read a PBM header from a stream.
   procedure Read_Header
     (Stream : in Stream_Access-- Read from this stream.
      Width  : out Positive;     -- Width of image in pixels.
      Height : out Positive);    -- Height of image in pixels.

   -- Write a PBM header to a stream.
   procedure Write_Header
     (Stream : in Stream_Access-- Write to this stream.
      Width  : in Positive;      -- Width of image in pixels.
      Height : in Positive);     -- Height of image in pixels.

   -- A buffer for one row of black/white pixels.
   type Bit_Buffer is private;

   -- Create a bit buffer with a capacity of 8 * Byte_Count pixels.
   function Create
     (Byte_Count : in Positive-- The number of bytes in the buffer.
     return Bit_Buffer;         -- The newly created bit buffer.

   -- Free the bit buffer's memory and set the instance variable to
   -- null.
   procedure Deallocate
     (This : in out Bit_Buffer); -- Free this bit buffer.

   -- Restart reading from the left of the bit buffer.
   procedure Reset
     (This : in Bit_Buffer); -- Reset this bit buffer.

   -- Set all bits in this bit buffer to False.
   procedure Clear
     (This : in Bit_Buffer); -- Clear this bit buffer.

   -- Fill the bit buffer with data from a stream.
   procedure Read_Row
     (From : in Stream_Access-- Read from this stream.
      This : in Bit_Buffer);   -- Read data into this bit buffer.

   -- Read the next bit from the bit buffer and advance the bit pointer.
   function Read_Pixel
     (This : in Bit_Buffer-- Read from this bit buffer.
     return Boolean;        -- The resulting bit value.

private

   -- Storage unit.
   subtype Byte is Unsigned_8;

   -- A buffer for bit data.
   type Byte_Array is array (Positive range <>) of Byte;

   -- Record for bit data and pointers.
   type Bit_Buffer_Record(Byte_Count : Positive) is record
      Bytes      : Byte_Array(1 .. Byte_Count);
      Temp       : Byte;    -- The byte that is currently being processed.
      Temp_Bits  : Natural-- The number of unread bits in Temp.
      Temp_Index : Natural-- The position of Temp in Bytes.
   end record;

   -- Internal definition of Bit_Buffer.
   type Bit_Buffer is access Bit_Buffer_Record;

end PBM;