%include "inc:util.imp"
%include "iffinc.imp"

!J. Butler Jan 87

!Takes list of files and merges them into one.
%begin
%string (255) infile, infiles, outfile
%integerarray ad(0:15)
%record (iffhdr fm) iffin, iffout
%owninteger i,k,rc,base,offset,images,wid,ht

%routine readhead(%string (255) file, %record (iffhdr fm) %name h)
   %integer rc
   rc = iff open file(file, h, iff read)
                                     !If we opened the file successfully...
   %if rc=0 %start
      rc = iff read header(h)   ;!read in the header
      %if rc=0 %start               ;!If we did so successfully...
         iff show header(h, 1)  ;!display it
      %finishelse printline("IFF read header: ".iff error(rc))
   %finishelse printline("IFF open file: ".iff error(rc))
%end

Printline("Parameters?") %and %stop %unless cli param -> infiles.("/").outfile

base=0; images=0
wid=-1; ht=-1
%while infiles -> infile.(",").infiles %cycle
   readhead(infile, iffin)
   ad(images) = heapget(iffin_ht*iffin_wid)
   rc = iff read image(iffin, ad(images))  ;!then read the actual image
   printline("IFF read image: ".itos(rc,-1)) %if rc#0
   images=images+1
   iff close file(iffin)
%repeat
readhead(infiles, iffin)
ad(images) = heapget(iffin_ht*iffin_wid)
rc = iff read image(iffin, ad(images))
images=images+1
iff close file(iffin)

rc = iff open file(outfile, iffout, iff write)

%if rc=0 %start ;!if we opened it OK..
   !Set up the new header (remember OPENING it will have zapped IFFOUT)
   iffout_title = iffin_title." with crosshairs"
   iffout_wid = iffin_wid; iffout_ht = iffin_ht
   iffout_stereo=images-1
   rc = iff write header(iffout)
   %if rc=0 %start  ;!we wrote it OK, so..
      %for i=0, 1, images-1 %cycle
         rc = iff write image(iffout, ad(i))  ;!write the image..
         heapput(ad(i))
      %repeat
   %finishelse printline(iff error(rc))
   iff close file(iffout) ;!and close down
%finishelse printline(iff error(rc))

%endofprogram
         
   
