program demo(input,output);
%include 'inc:util.pas'
%include 'cs4:iffinc.pas'

{J. Butler Jan 87}

{Trivial IFF utilities demo - takes nominated file and superimposes cross hairs}
var
a: array[0..512*768-1] of 0..255;
iffin, iffout: iffhdr_fm;
i,k,rc,xdiv2,ydiv2:integer;

begin
iff_setup;
rc := iff_open_file(cliparam, iffin, iff_read);
{If we opened the file successfully...}
if rc=0 then begin
   rc := iff_read_header(iffin);   {read in the header}
   if rc=0 then begin              {If we did so successfully...}
      iff_show_header(iffin, 1);   {display it}
      
      rc := iff_read_image(iffin, addr(a[0]));   {then read the image}
      
      if rc=0 then begin           {if that went OK...}
         {Plant cross-hairs across the picture}
         xdiv2 := iffin.wid div 2; ydiv2 := iffin.ht div 2;
         for i:=0 to iffin.ht-1 do
            a[i * iffin.wid + xdiv2] := 0;
         for i:=0 to iffin.wid-1 do
            a[ydiv2 * iffin.wid + i] := 0
      end else writeln('IFF read image: ',rc)
   end else writeln('IFF read header: ',iff_error[rc]);
   iff_close_file(iffin)
end else writeln('IFF open file: ',iff_error[rc]);

if rc=0 then begin

{Now write the modified picture back to file "junk.iff"}
rc := iff_open_file('junk', iffout, iff_write);
if rc=0 then begin                  {if we opened it OK...}
   {Set up the new header (remember OPENING it will have zapped IFFOUT)}
   iffout.title := 'demo image with crosshairs';
   iffout.wid := iffin.wid; iffout.ht := iffin.ht;
   rc := iff_write_header(iffout);
   if rc=0 then                     {we wrote header successfully, so..}
      rc := iff_write_image(iffout, addr(a[0]))   {write the image}
   else writeln(iff_error[rc]);
   iff_close_file(iffout)           {and close down}
end else writeln(iff_error[rc])

end

end.
         
   
