/* Math Art Subroutines 03/13/92 */
/* altcolor()       */
/* setresol(lr,sa)  */
/* pplot(a,b,col)   */
/* getpix(a,b)      */
/* dispdir()        */
/* viewima()        */
/* waitkey()        */
/* errtrap(err)     */
/* textcols()       */
/* gr7plus()  (6F01)*/
/* drawcurs(x,y)    */

int creg,esc,hue,intens,cbyte,key;
int px1,px2,err,col,i;
int p1,p2,p3,p4,p5,p6,p7,p8,lores;
char *tempi,*tempi1,*start;
char fspec[20],inpstr[20];

/* Alter Colors */
altcolor() $(
 creg=0;esc=0;while(esc==0) $(
  poke(752,1);
  if(creg==0)i=712;else i=707+creg;
  cbyte=peek(i)&254;
  hue=cbyte/16;intens=hue*16;
  intens=(cbyte-intens)&14;
  printf("\f\n C  Col:%-2d  H  Hue:%-2d  L  Lum:%d",creg,hue,intens);
  printf("\nSave  Restore");
  key=toupper(getkey());
  if(key=='R')defaultc();
  else if(key=='S') $(
   if(lores) $(
    dpoke(0x6F0,dpeek(708));
    poke(0x6F2,peek(710));
    poke(0x6F3,peek(712));
   $) else $(
    dpoke(0x6F4,dpeek(709));
    poke(0x6F6,peek(712));
   $)
  $)
  else if(key=='C') $(
   creg++;if(creg>3)creg=0;
  $)
  else if(key=='H') $(
   hue++;if(hue>15)hue=0;
  $)
  else if(key=='L') $(
   intens+=2;if(intens>14)intens=0;
  $)
  else esc=1;
  if(key=='H'||key=='L') $(
   cbyte=hue*16;cbyte+=intens;
   if(creg==0)i=712;else i=707+creg;
   poke(i,cbyte);
  $)
 $)
$)
/* set resolution and start */
setresol(lr,sa)
int lr; char *sa;
$(
 lores=lr;start=sa;
$)
/* pixel plot routine */
pplot(px,py,pc)
int px,py,pc;
$(
 if(lores==0) $(
  color(pc);plot(px,py);
 $) else $(
/* gr7plus plot routine */
  px1=px*2;px2=px1+1;
  if(pc==0) $(
   color(0);plot(px1,py);plot(px2,py);
  $) else if(pc==1) $(
   color(0);plot(px1,py);
   color(1);plot(px2,py);
  $) else if(pc==2) $(
   color(1);plot(px1,py);
   color(0);plot(px2,py);
  $) else if(pc==3) $(
   color(1);plot(px1,py);plot(px2,py);
  $)
 $)
$)
/* get pixel color (locate) */
getpix(a,b)
int a,b;
$(
/* Mode 7+ pixel test */
 if(lores) $(
  tempi=start;tempi+=b*40;
  tempi+=a/4;tempi=peek(tempi);
  tempi1=a/4;tempi1=tempi1*4;
  tempi1=a-tempi1;
  if(tempi1==0)tempi>>=6;
  if(tempi1==1)tempi>>=4;
  if(tempi1==2)tempi>>=2;
  tempi&=3;return(tempi);
 $)
/* Mode 8 pixel test */
 else $(
  tempi=start;tempi+=b*40;
  tempi+=a/8;tempi=peek(tempi);
  tempi1=a/8;tempi1=tempi1*8;
  tempi1=a-tempi1;
  if(tempi1==0)tempi>>=7;
  if(tempi1==1)tempi>>=6;
  if(tempi1==2)tempi>>=5;
  if(tempi1==3)tempi>>=4;
  if(tempi1==4)tempi>>=3;
  if(tempi1==5)tempi>>=2;
  if(tempi1==6)tempi>>=1;
  tempi&=1;return(tempi);
 $)
$)
/* display .PIC files */
dispdir() $(
 printf("\f");tempi=0;esc=0;
 strcpy(fspec,"*.PIC");
 normalize(fspec,"");
 err=open(1,6,0,fspec);
 if(err>=0) $(
  while(esc==0) $(
   err=cgets(inpstr,1);
   if(err>0) $(
    if(strlen(inpstr)==17) $(
     move(inpstr+2,fspec,8);
     poke(fspec+8,0);
     printf("%s ",fspec);
     tempi+=1;if(tempi>3) $(
      tempi=0;printf("\n");
     $)
    $)
    else $(
     move(inpstr,fspec,4);
     poke(fspec+4,0);
     printf("%sFREE",fspec);
    $)
    key=peek(764);poke(764,255);
    if(key==33)getkey();
   $)
   else esc=1;
  $)
 $)
 close(1);
 getkey();
$)
/* view the image */
viewima() $(
 graphics(8+48);if(lores)gr7plus();
 setcols();waitkey();
$)
waitkey() $(
 while(peek(764)==255)poke(77,0);
 poke(764,255);
$)
/* text screen colors */
textcols() $(
 poke(709,12);poke(710,0);poke(712,0);
$)
/* error!!! */
errtrap(errnum)
int errnum;
$(
 errnum=abs(errnum);
 graphics(8+32);setcols();poke(752,1);
 printf("\f\nError # %d\n",errnum);
 getkey();
$)
/* gr7plus mode ML routine */
gr7plus()
  asm 0x6F01;
/* draw cursor for magnify */
drawcurs(x,y)
int x,y;
$(
 p1=getpix(x-1,y);p2=getpix(x+1,y);
 p3=getpix(x,y-1);p4=getpix(x,y+1);
 p5=getpix(x-2,y);p6=getpix(x+2,y);
 p7=getpix(x,y-2);p8=getpix(x,y+2);
 col=0;tempi=2;if(lores)tempi=4;
 for(;col<tempi;++col) $(
  pplot(x-1,y,col);pplot(x+1,y,col);
  pplot(x,y-1,col);pplot(x,y+1,col);
  pplot(x-2,y,col);pplot(x+2,y,col);
  pplot(x,y-2,col);pplot(x,y+2,col);
/* delay curs in hires */
  if(lores==0) $(
   tempi1=0;while(tempi1<200)tempi1++;
  $)
 $)
 pplot(x-1,y,p1);pplot(x+1,y,p2);
 pplot(x,y-1,p3);pplot(x,y+1,p4);
 pplot(x-2,y,p5);pplot(x+2,y,p6);
 pplot(x,y-2,p7);pplot(x,y+2,p8);
$)
/* end of plotsubs */