%include "iff:iffinc.imp"
%include "inc:util.imp"
%begin
%halfarray cm(0:255)
%record (iffhdr fm) iffh
%integer i,j,k,p,rc
%routine Mix Colour (%short Col, Red, Green, Blue)
   CM(Col)=Red+Green<<5+Blue<<10
%end

iffh=0
rc = iff open file("junk", iffh, iff write)
iffh_ht = 518
iffh_wid=312
iffh_datatype = 16_c0
iffh_maplen=256
iffh_mapwid=16
iffh_mapaddr=addr(cm(0))
     CM(0)  = 0
     CM(1)  = 31
     CM(2)  = 31<<5
     CM(3)  = 31<<5+31
     CM(4)  = 31<<10
     CM(5) = 31<<10+31
     CM(6) = 31<<10+31<<5
     CM(7) = 31<<10+31<<5+31
     CM(8) = 31<<10+31<<5+31
     CM(9) = 31<<10+31<<5
     CM(10) = 31<<10+31
     CM(11) = 31<<10
     CM(12) = 31<<5+31
     CM(13) = 31<<5
     CM(14) = 31
     CM(15) = 0
%for i = 1,1,7 %cycle
   j = i<<5
   Mix Colour (j+k,(i&1)*k,((i>>1)&1)*k,((i>>2)&1)*k) %for k = 0,1,31
%repeat
Mix Colour (30,6,5,11)
rc = iff write header(iffh)
openinput(1, "grid.dat"); selectinput(1)
k=heapget(iffh_ht*iffh_wid)
p=k+iffh_ht*iffh_wid - iffh_wid
%for i=0,1,iffh_ht-1 %cycle
   %for j=0,1,iffh_wid-1 %cycle
   readsymbol(byteinteger(p)); p=p+1
   %repeat
   p=p-2*iffh_wid
%repeat
rc = iff write image(iffh, k)
iff close file(iffh)
%endofprogram

