!****************************************************************
!*                                                              *
!*      CONVERT:  Convert from a variety of formats to IFF'     *
!*                                                              *
!*                  Version 1.2   16 Feb 1987                   *
!*                                                              *
!****************************************************************

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

%begin
%bytearray b(0:768*512)
%record (iffhdr fm) iffin, iffout
%constinteger ibyte=0, iword=1, iboolean=2,    icompress= 16_C0
%string (255) param, infile, outfile
%integer c,ct,ina,inl,i,j,k,rl,lastc,p,rowbase,inbase,inptr,offset,mode,max

%integer rc
%shortarray colmap(0:255)

%routine gettext(%string (31) promptext, %string (*) %name text)
   %string (255) yn
   %cycle
      prompt(promptext.":"); readline(text)
      prompt("OK? Y/N:"); readline(yn)
   %repeatuntil charno(yn,1) & 16_5F = 'Y'
   prompt(":")
%end

%shortintegerfn colword(%integer Red, Green, Blue)
   %result=Red+Green<<5+Blue<<10
%end

param=cliparam
printstring("Parameters?") %and %return %unless %c
param -> infile.(",").outfile %or param -> infile.("/").outfile

ct=0
rl=1

connectfile(infile, 0, ina, inl)
printstring("File is ");write(inl,-1); printstring(" ("); phex(inl)
printstring(") bytes long. Bytes 0-3 =")
%for i=0,1,3 %cycle; space; phex2(byteinteger(ina+i)); %repeat
newline
i = int(sqrt(inl))
write(i,-1); printstring(" * "); write(i,-1); printstring(" = "); write(i*i,-1)
newline

!*   wid  ht   map   len   image scans

!B   read read -     -     vert
!W                         horiz down
!L   read read [0]   256   horiz up
!P   [0]  [2]  [4]   508   vert
!F   [6]  [4]              horiz down
!I   read read -     -     horiz down

printline("Format is ".%c
"(B)itmap/(L)UCAS/(P).Reid/(W)INSOM-IFF/(F)unny WINSOM/WINSOM .(I)MG?")
prompt("b/l/p/w/i:")
%cycle; readsymbol(i); mode=i&16_5f
%repeatuntil %c
mode='W' %or mode='B' %or mode='L' %or mode='P' %or mode = 'F' %or %c
mode = 'I'
skipsymbol %while nextsymbol #nl; skipsymbol

iffin=0
%if mode='P' %start
   iffin_wid=shortinteger(ina) ;!Width
   iffin_ht=shortinteger(ina+2) ;!Height
   iffin_mapaddr = ina+4;!Colour map
   iffin_maplen = 256; iffin_mapwid = 16
   offset=512

%elseif mode='W' ;!True WINSOM output (IFF). Incorporates JHB extensions
   
   rc=iff read header(iffin)
   offset = iffin_hlen * 2 ;!Convert to bytes

   %if iffin_mapaddr # 0 %start
      printline("Funny map length ".itos(iffin_maplen,-1)) %and %c
      iffin_maplen=256 %if iffin_maplen#256 %and iffin_maplen#0
      printline("Funny map width  ".itos(iffin_mapwid,-1)) %and %c
      iffin_mapwid=16  %if iffin_mapwid#16 %and iffin_mapwid#0
      iffin_mapaddr = ina+512
      %for i=0,1,iffin_maplen-1 %cycle
         -> notsame %if byteinteger(iffin_mapaddr+i)#colmap(i)
      %repeat
      printline("Map is actually a grey scale"); iffin_mapaddr=0
notsame:

   %else
      printline("No colour map - grey scale assumed")
   %finish

   printstring(iffin_title); newline
   printstring(iffin_date."  ".iffin_time); newline
   
%elseif mode = 'F'
   iffin_wid = byteinteger(ina+7)<<8+byteinteger(ina+6)
   iffin_ht = byteinteger(ina+5)<<8+byteinteger(ina+4)
   offset = 8 {?}
%else
   prompt("wid:"); read(iffin_wid)
   prompt("ht:"); read(iffin_ht)
   offset=0
   %if mode = 'L' %start
      iffin_mapaddr = ina ;!Colour map
      iffin_maplen=256; iffin_mapwid=16
      offset=512
   %elseif mode = 'I'
   %else
      %for i = 1,1,7 %cycle
         k = 32*(i-1)
         colmap(k+j)=colword((i&1)*j, (i&2)>>1*j, (i&4)>>2*j) %for j = 0,1,31
         iffin_mapaddr = addr(colmap(0))
      %repeat
   %finish
%finish

printstring("File is "); write(iffin_wid, 3)
printstring(" * "); write(iffin_ht, 3); printstring(" pixels")
newline
gettext("Description", iffin_title) %if iffin_title=""

rc = iff open file(outfile, iffout, iff write)
iffout_datatype = ibyte ! icompress
iffout_ht = iffin_ht; iffout_wid=iffin_wid
iffout_maplen=iffin_maplen; iffout_mapwid=iffin_mapwid
iffout_mapaddr=iffin_mapaddr; iffin_mapaddr=0
rc=iff write header(iffout)
                                    !    Image. Rasters down from top left

inbase=ina+offset
inptr=0
max=iffout_ht*iffout_wid
p=0
%if mode = 'L' %start ;!Scans horizontal rasters upwards
   rowbase=max-iffout_wid; inptr=0
   %for i=offset,1,inl-1 %cycle
      b(p) = byteinteger(inbase+rowbase+inptr); inptr=inptr+1; p=p+1
      %if inptr>=max %then inptr=0 %and rowbase=rowbase-iffout_wid
   %repeat

%elseif mode = 'W' %or mode='F' %or mode = 'I';!Scans horizontal rasters downwards
   printline("Inconsistent") %and %stop %if offset >= inl
   %for i=offset,1,inl-1 %cycle
      b(p) = byteinteger(inbase+inptr); inptr=inptr+1; p=p+1
   %repeat

%else ;!Scans vertical rasters left->right
   !WRONG
   inptr=max
   %for i=offset,1,inl-1 %cycle
      b(p) = byteinteger(inbase+inptr); inptr=inptr-iffout_wid; p=p+1
      %if inptr<=0 %then inptr=inptr+max+1
   %repeat

%finish
rc = iff write image(iffout, addr(b(0)))

iff close file(iffout)
%end

%endoffile
