-- $Date: 2003/12/30 04:54:08 $
-- $Revision: 1.4 $
-- $Author: jcrocholl $

package body PNM is

   -------------------
   -- Internal reading
   -------------------

   -- Read one character from a stream.
   function Read_Character
     (Stream : in Stream_Access-- Read from this stream.
     return Character            -- The character.
   is
      Result : Character;
   begin
      Character'Read(Stream, Result);
      return Result;
   end Read_Character;

   -- Read a PNM file type's magic number from a stream.
   function Read_Magic
     (Stream : in Stream_Access-- Read from this stream.
     return String2              -- The magic number.
   is
      Result : String2;
   begin
      String2'Read(Stream, Result);
      return Result;
   end Read_Magic;

   -- Convert a character from '0' to '9' to a natural number.
   function Character_To_Natural
     (C : in Character-- Convert this character.
     return Natural is  -- Result of conversion.
   begin
      return Character'Pos(C) - Character'Pos('0');
   end Character_To_Natural;

   -- Read a natural number from a stream. Ignore leading white space
   -- and comments.
   function Read_Natural
     (Stream : in Stream_Access-- Read from this stream.
     return Natural              -- The natural number.
   is
      Result : Natural;
      C      : Character;
   begin
      loop
         C := Read_Character(Stream);
         if C = '#' then
            while C /= ASCII.LF loop
               C := Read_Character(Stream);
            end loop;
         end if;
         exit when C >= '0' and C <= '9';
      end loop;
      Result := Character_To_Natural(C);
      loop
         C := Read_Character(Stream);
         exit when C < '0' or C > '9';
         Result := Result * 10 + Character_To_Natural(C);
      end loop;
      return Result;
   end Read_Natural;

   -------------------
   -- Internal writing
   -------------------

   -- Write a PNM file type's magic number to a stream.
   procedure Write_Magic
     (Stream : in Stream_Access-- Write to this stream.
      Magic  : in String2) is    -- Write this magic number.
   begin
      String2'Write(Stream, Magic);
   end Write_Magic;

   -- Write one white space and a natural number to a stream.
   procedure Write_Natural
     (Stream : in Stream_Access-- Write to this stream.
      N      : in Natural) is    -- Write this number.
   begin
      String'Write(Stream, Natural'Image(N));
   end Write_Natural;

   ----------------------
   -- Headers with Maxval
   ----------------------

   -- Read a PNM file header, with Maxval (for PGM and PPM).
   procedure Read_Header
     (Stream : in Stream_Access-- Read from this stream.
      Magic  : out String2;      -- Magic number for PNM file type.
      Width  : out Positive;     -- Width of the image in pixels.
      Height : out Positive;     -- Height of the image in pixels.
      Maxval : out Positive) is  -- Maximum pixel component value.
   begin
      Magic := Read_Magic(Stream);
      Width := Read_Natural(Stream);
      Height := Read_Natural(Stream);
      Maxval := Read_Natural(Stream);
   end Read_Header;

   -- Write a PNM file header, with Maxval (for PGM and PPM).
   procedure Write_Header
     (Stream : in Stream_Access-- Write to this stream.
      Magic  : in String2;       -- Magic number for PNM file type.
      Width  : in Positive;      -- Width of the image in pixels.
      Height : in Positive;      -- Height of the image in pixels.
      Maxval : in Positive) is   -- Maximum pixel component value.
   begin
      Write_Magic(Stream, Magic);
      Write_Natural(Stream, Width);
      Write_Natural(Stream, Height);
      Write_Natural(Stream, Maxval);
      Character'Write(Stream, Character'Val(10));
   end Write_Header;

   -------------------------
   -- Headers without Maxval
   -------------------------

   -- Read a PNM file header, without Maxval (for PBM).
   procedure Read_Header
     (Stream : in Stream_Access-- Read from this stream.
      Magic  : out String2;      -- Magic number for PNM file type.
      Width  : out Positive;     -- Width of the image in pixels.
      Height : out Positive) is  -- Height of the image in pixels.
   begin
      Magic := Read_Magic(Stream);
      Width := Read_Natural(Stream);
      Height := Read_Natural(Stream);
   end Read_Header;

   -- Write a PNM file header, without Maxval (for PBM).
   procedure Write_Header
     (Stream : in Stream_Access-- Write to this stream.
      Magic  : in String2;       -- Magic number for PNM file type.
      Width  : in Positive;      -- Width of the image in pixels.
      Height : in Positive) is   -- Height of the image in pixels.
   begin
      Write_Magic(Stream, Magic);
      Write_Natural(Stream, Width);
      Write_Natural(Stream, Height);
      Character'Write(Stream, Character'Val(10));
   end Write_Header;

end PNM;