!****************************************************************
!*                                                              *
!*      IFFPIX:     Displays a "gallery" of IFF files in a      *
!*                  directory                                   *
!*                                                              *
!*                  Version 1.1    6 Apr 1988                   *
!*                                                              *
!****************************************************************

%include "inc:util.imp"
%include "iff:iffinc.imp"
%include "inc:fs.imp"
%include "inc:fsutil.imp"
%include "level1:graphinc.imp"
%begin
%integer xoff, yoff, xsam, ysam
%string (255) dir, s
%on 3, 9 %start; ->eof; %finish

%routine iff sample(%record (iffhdr fm) %name iffh, %integer ad, xsam, ysam)
   %integer i, j, xmax, ymax, from, to
   xmax = iffh_wid//xsam; xmax=xmax+1 %if xmax*xsam # iffh_wid
   ymax = iffh_ht//ysam;  ymax=ymax+1 %if ymax*ysam # iffh_ht
   to = ad
   %for i=0, 1, ymax-1 %cycle
      from = ad+i*iffh_wid*ysam
      %for j=0, 1, xmax-1 %cycle
         byteinteger(to) = byteinteger(from); to=to+1; from=from+xsam
      %repeat
   %repeat
%end

%routine grey map
   %integer i, c
   %half %array CM (0:255)
   %for i=0,1,255 %cycle; c = i>>3; CM(i) = (c<<5 + c)<<5 + c; %repeat
   Update Colour Map (cm(0))
%end

%routine iff 2 to 8(%record (iffhdr fm) %name iffh, %integer from, to)
   !Expand packed booleans to array of 0's and 255's. Nicked from DISP.
   %integer i, max, k, mask
   %label l1,l2,l5,l6
   max = (iffh_ht * iffh_wid)>>3
      *move.l  from,a0
      *move.l  to,A1
      *move.l  max,d3
      *subq.l  #1,max

l1:   *move.l  #128,d1
      *moveq   #7,d2

l2:   *move.b  0(a0),d0
      *and.l   d1,d0
      *bne     l6
      *move.b  #255,d0
      *bra     l5

l6:   *clr.b   d0

l5:   *move.b  d0,(a1)+
      *lsr.l   #1,d1
      *dbra   d2, l2
      *addq.l  #1,a0
      *dbra   d3, l1

%end

%routine do file(%string (255) file, %integer xoff, yoff)
   %record (iffhdr fm) iffh
   %integer rc, ad, wid, ht, base
   %string (255) s
   %on 0,3,9 %start; %return; %finish

   printstring(file)
   ad=0; iffh=0
   rc = iff readin(file, iffh, ad)
   %return %if rc#0

%if iffh_datatype & 7 # 0 %start ;!Non-byte images.  Reformat to bytes.
   %if iffh_datatype & 7 = 1 %start ;!16-bit image
!!      iff 16 to 8(iffh, ad, ad)
   %elseif iffh_datatype & 7 = 2 ;!Packed booleans
      !This EXPANDS the image so we can't do it in-place.
      base = heapget(iffh_wid * iffh_ht)
      iff 2 to 8(iffh, ad, base)
      heapput(ad); ad=base
   %elseif iffh_datatype & 7 = 4
!!      iff 32 to 8(iffh, ad, ad)
   %elseif iffh_datatype & 7 = 5
!!      iff real to 8(iffh, ad, ad)
   %finish
%finish

   %if iffh_mapaddr#0 %then update colour map(halfinteger(iffh_mapaddr)) %c
   %else grey map
   %if iffh_wid<=128 %and iffh_ht<=128 %start
      wid = iffh_wid; ht=iffh_ht
      xsam=1; ysam=1
   %else
      xsam = (iffh_wid-1)//128+1; ysam = (iffh_ht-1)//128+1
      %if ysam>xsam %then xsam=ysam %else ysam=xsam
      iff sample(iffh, ad, xsam, ysam)
      !Correct sizes.  Adjust for "fence-post" condition.
      wid = iffh_wid//xsam; wid=wid+1 %if wid*xsam # iffh_wid
      ht = iffh_ht//ysam; ht=ht+1 %if ht*ysam # iffh_ht
   %finish
   spaces(12-length(file)) %if length(file)<12
   space; write(iffh_ht, -1); printstring(" * "); write(iffh_wid,-1)
   printstring(" Scale /"); write(xsam, -1); newline
   iffh_wid=wid; iffh_ht=ht
   iff flip(iffh, ad)
   clear %if xoff=0 %and yoff=512-144 ;!New screenful
   xoff=xoff+8
   %while file -> s.(":").file %cycle; %repeat
   textat(xoff,yoff); colour(255); showstring(file)
   showstring(" (1/".itos(iffh_stereo+1,-1).")") %if iffh_stereo#0
   yoff=yoff+12
   col fill(xoff, yoff, xoff+wid-1, yoff+ht-1, byteinteger(ad))
   heapput(ad); heapput(iffh_mapaddr) %if iffh_mapaddr#0
%end

clear
dir = cli param
dir = current directory %if dir=""
dir=dir.":" %unless charno(dir, length(dir)) = ':'
openinput(1, dir); selectinput(1)

xoff=0; yoff=512-144
%cycle
   readline(s); to upper(s)
   %if s -> s.(".IFF") %start
      do file(dir.s, xoff, yoff)
      xoff = xoff + 144; xoff = 0 %and yoff = yoff - 144 %if xoff > 688-128
      %if yoff<0 %start; xoff=0; yoff=512-144; %finish
  %finish
%repeat

eof:
close input
%endofprogram
