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

!Little utility turns IFF file image upside-down
!J. Butler Mar 87

%begin
%record (iffhdr fm) iffin, iffout
%owninteger rc,a,deg,b
%string (255) param,degs

%routine iff turn(%integer deg,
   %record (iffhdr fm) %name in, %integer a,
   %record (iffhdr fm) %name out, %integername b)
   !Turn image about an arbitrary angle (integer degrees)
   !Note this will generally increase dimensions of array
%end

%routine iff flip(%record (iffhdr fm) %name iffhdr, %integer ad)
   !Reflect image about a central horizontal axis.
   %integer from,to
   from=ad+iffhdr_wid ;to=ad+iffhdr_ht*iffhdr_wid
%label l1,l2
    *MOVEA.L iffhdr,A0
    *move.l  12(a0),d3     ;!d3 = iffhdr_wid
    *move.l  8(a0),d2
    *LSR.L   #1,d2
    *SUBQ.L  #1,d2         ;!d2 = iffhdr_ht>>1-1
    *MOVEA.L to,a2
L1:
    *move.l  d3,d1
    *sub.l   #1,d1         ;!d1 = iffhdr_wid-1
    *MOVEA.L from,a1
l2:
    *move.b  -(a1),d0      ;!exchange bytes at pointers
    *move.b  -(a2),(a1)
    *move.b  d0,(a2)
    *dbra    d1, l2        ;!decrement counter (d1) and loop iffhdr_wid times
    *ADD.L   d3,from
    *dbra    d2, l1
%end

%constinteger ibyte=0, iword=1, iboolean=2,    icompress= 16_C0
param = cli param
degs="180" %unless param -> param.("-").degs
deg = stoi(degs)
a=0
rc = iff readin(param, iffin, a)
!If we opened the file successfully...
printline("IFF connect: ".iff error(rc)) %and %stop %if rc#0

iff show header(iffin, 1)  ;!display it
iffout=iffin
iffin_mapaddr=0
%if deg=180 %start
   iff flip(iffin, a)
   rc = iff writeout(param, iffout, a)
%else
   iff turn(deg, iffin, a, iffout, b)
   rc = iff writeout(param, iffout, b)
   heapput(b)
%finish
heapput(a)
printline(iff error(rc)) %if rc#0
%endofprogram
