Hosted by
|
package body PNM is
function Read_Character
(Stream : in Stream_Access)
return Character
is
Result : Character;
begin
Character'Read(Stream, Result);
return Result;
end Read_Character;
function Read_Magic
(Stream : in Stream_Access)
return String2
is
Result : String2;
begin
String2'Read(Stream, Result);
return Result;
end Read_Magic;
function Character_To_Natural
(C : in Character)
return Natural is
begin
return Character'Pos(C) - Character'Pos('0');
end Character_To_Natural;
function Read_Natural
(Stream : in Stream_Access)
return Natural
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;
procedure Write_Magic
(Stream : in Stream_Access;
Magic : in String2) is
begin
String2'Write(Stream, Magic);
end Write_Magic;
procedure Write_Natural
(Stream : in Stream_Access;
N : in Natural) is
begin
String'Write(Stream, Natural'Image(N));
end Write_Natural;
procedure Read_Header
(Stream : in Stream_Access;
Magic : out String2;
Width : out Positive;
Height : out Positive;
Maxval : out Positive) is
begin
Magic := Read_Magic(Stream);
Width := Read_Natural(Stream);
Height := Read_Natural(Stream);
Maxval := Read_Natural(Stream);
end Read_Header;
procedure Write_Header
(Stream : in Stream_Access;
Magic : in String2;
Width : in Positive;
Height : in Positive;
Maxval : in Positive) is
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;
procedure Read_Header
(Stream : in Stream_Access;
Magic : out String2;
Width : out Positive;
Height : out Positive) is
begin
Magic := Read_Magic(Stream);
Width := Read_Natural(Stream);
Height := Read_Natural(Stream);
end Read_Header;
procedure Write_Header
(Stream : in Stream_Access;
Magic : in String2;
Width : in Positive;
Height : in Positive) is
begin
Write_Magic(Stream, Magic);
Write_Natural(Stream, Width);
Write_Natural(Stream, Height);
Character'Write(Stream, Character'Val(10));
end Write_Header;
end PNM;
|