Guest User

Eric Graham's Juggler RT3.C

a guest
Apr 9th, 2015
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.77 KB | None | 0 0
  1.  
  2. /*      RT3.C
  3.         Copyright 1987 Eric Graham
  4.         All rights reserved.
  5.         This file may not be copied, modified or uploaded to a bulletin
  6.         board system, except as provided below.
  7.         Permission is granted to make a reasonable number of backup copies,
  8.         in order that it may be used to generate executable code for use
  9.         on a single computer system.
  10.         Permission is granted to modify this code and use the modified code
  11.         for non commercial use by the original purchaser of this software,
  12.         and provided that this notice is included in the modified version.
  13. */
  14.  
  15. #include "exec/types.h"
  16. #include "exec/exec.h"
  17. #include "intuition/intuition.h"
  18.  
  19.  
  20.  
  21. static int threshhold=4;
  22. static int nallocr=2;
  23. static int creg[16][3]=         /* color registers */
  24.         {{0,0,0},               /* pre-allocate black and white */
  25.          {15,15,15},
  26.          {0,0,0},
  27.          {0,0,0},
  28.          {0,0,0},
  29.          {0,0,0},
  30.          {0,0,0},
  31.          {0,0,0},
  32.          {0,0,0},
  33.          {0,0,0},
  34.          {0,0,0},
  35.          {0,0,0},
  36.          {0,0,0},
  37.          {0,0,0},
  38.          {0,0,0},
  39.          {0,0,0}};
  40.  
  41.  
  42.  
  43.  static struct NewScreen newscreen={
  44.    0,0,         /* Leftedge, topedge */
  45.    320,200,     /* Width, height */
  46.    6,           /* Depth */
  47.    0,0,         /* pens */
  48.    HAM,         /* ViewModes */
  49.    CUSTOMSCREEN,
  50.    NULL,        /* Font */
  51.    NULL,        /* title */
  52.    NULL,        /* gadgets */
  53.    NULL};       /* bitmap */
  54.  
  55.  
  56.  static struct NewWindow newwindow={
  57.    0,0,              /* LeftEdge, TopEdge   */
  58.    320,200,          /* Width, Height       */
  59.    0,1,              /* DetailPen, BlockPen */
  60.    0,
  61.                      /* IDCMPFlags          */
  62.    BORDERLESS | ACTIVATE,
  63.                      /* Flags               */
  64.    NULL,             /* FirstGadget         */
  65.    NULL,             /* CheckMark           */
  66.    NULL,             /* Title               */
  67.    NULL,             /* Screen              */
  68.    NULL,             /* BitMap              */
  69.    0,0,              /* MinWidth, MinHeight */
  70.    0,0,              /* MaxWidth, MaxHeight */
  71.    CUSTOMSCREEN      /* Type                */
  72.    };
  73.  
  74.  struct RastPort *rastport;
  75.  
  76.  static struct Screen *screen;
  77.  static struct Window *window;
  78.  struct ViewPort *viewport;
  79.  
  80.  struct IntuitionBase *IntuitionBase;
  81.  struct GfxBase *GfxBase;
  82.  struct DosBase *DosBase;
  83.  struct Window *OpenWindow();
  84.  struct Screen *OpenScreen();
  85.  struct ViewPort *ViewPortAddress();
  86.  struct IOStdReq *CreateStdIO();
  87.  struct Port *CreatePort();
  88.  
  89.  
  90.  
  91.  
  92.         void
  93. initsc(width,height)    /* set up the screen and window */
  94. int width,height;
  95. {int i;
  96.  static UWORD pointer[]={0,0,0,0,0,0};
  97.  
  98.  if ((GfxBase=(struct GfxBase *)
  99.       OpenLibrary("graphics.library",0)) == NULL)
  100.          cleanup("GfxBase");
  101.  
  102.  if ((IntuitionBase=(struct IntuitionBase *)
  103.       OpenLibrary("intuition.library",0)) == NULL)
  104.          cleanup("IntuitionBase");
  105.  
  106.  if ((DosBase=(struct DosBase *)
  107.       OpenLibrary("dos.library",0)) == NULL)
  108.          cleanup("DosBase");
  109.  
  110.  screen=OpenScreen(&newscreen);
  111.  newwindow.Screen=screen;
  112.  newwindow.Width=width;
  113.  newwindow.MinWidth=width;
  114.  newwindow.MaxWidth=width;
  115.  newwindow.Height=height;
  116.  newwindow.MinHeight=height;
  117.  newwindow.MaxHeight=height;
  118.  window=OpenWindow(&newwindow);
  119.  if (!window) cleanup("OpenWindow");
  120.  rastport=window->RPort;
  121.  
  122.  /* viewport=ViewPortAddress(window); */
  123.  viewport=&(screen->ViewPort);
  124.  for (i=0; i<32; ++i)
  125.        SetRGB4(viewport,i, 0,0,0);
  126.  SetRGB4(viewport,1, 15,15,15);
  127.  
  128.  SetPointer(window,pointer,1,8,0,0);
  129. }
  130.  
  131.  
  132.  
  133. cleanup(s)
  134. char *s;
  135. {
  136.  if (window) CloseWindow(window);
  137.  if (screen) CloseScreen(screen);
  138.  if (DosBase) CloseLibrary(DosBase);
  139.  if (IntuitionBase) CloseLibrary(IntuitionBase);
  140.  if (GfxBase) CloseLibrary(GfxBase);
  141.  if (s) puts(s);
  142.  exit(!!s);
  143. }
  144.  
  145.  
  146.  
  147. ham2(i,j,pix)
  148. int i,j;
  149. int pix[3];
  150. {int k;
  151.  static int prevpix[3],map[3]={0x20,0x30,0x10};
  152.  int dif,dif2,id,maxdif,pen;
  153.  
  154.  if (!i)        /* first pixel on a line, use a register */
  155.         {pen=nearestp(pix,&dif2);
  156.          reg:
  157.          for (k=0; k<3; ++k) prevpix[k]=creg[pen][k];
  158.         }
  159.  else
  160.         {dif=coldist(pix,prevpix);      /* what change from last pixel*/
  161.          if (dif)
  162.                 {pen=nearestp(pix,&dif2); /* which register is nearest */
  163.                  if (dif2 < dif) goto reg;
  164.                 }
  165.          id=maxdif=0;
  166.          for (k=0; k<3; ++k)
  167.                 {dif=pix[k]-prevpix[k];
  168.                  if (dif < 0) dif=-dif;
  169.                  if (dif > maxdif) {maxdif=dif; id=k;}
  170.                 }
  171.          pen=map[id]+pix[id]; prevpix[id]=pix[id];      /* use HAM */
  172.         }
  173.  SetAPen(rastport,pen); WritePixel(rastport,i,j);
  174. }
  175.  
  176.  
  177. nearestp(c,dist)
  178. int *c,*dist;
  179. /*      Return the pen nearest to the color c.  Allocate it maybe. */
  180. {int i,mindist,nearest,d;
  181.  
  182.  mindist=32000;
  183.  
  184.  for (i=0; i<nallocr; ++i)
  185.         {d=coldist2(c,creg[i]);
  186.          if (d < mindist)
  187.                 {mindist=d; nearest=i;}
  188.         }
  189.  if (mindist > threshhold && nallocr < 16)
  190.         {for (i=0; i<3; ++i)
  191.                 creg[nallocr][i]=c[i];
  192.          SetRGB4(viewport,nallocr, c[0],c[1],c[2]);
  193.          nearest=nallocr++;
  194.          mindist=0;
  195.         }
  196.  *dist=mindist;
  197.  return nearest;
  198. }
  199.  
  200.  
  201. coldist(a,b)    /* The 'distance' between two colors after we correct */
  202. int *a,*b;      /* the worst color */
  203. {int k,r,d,m;
  204.  
  205.  for (r=k=m=0; k<3; ++k)
  206.         {d=a[k]-b[k];
  207.          if (d < 0) d=-d;
  208.          r+=d;
  209.          if (d > m) m=d;
  210.         }
  211.  return r-m;
  212. }
  213.  
  214.  
  215. coldist2(a,b)   /* the 'distance' between two colors */
  216. int *a,*b;
  217. {int k,r,d;
  218.  
  219.  for (r=k=0; k<3; ++k)
  220.         {d=a[k]-b[k];
  221.          if (d < 0) d=-d;
  222.          r+=d;
  223.         }
  224.  return r;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment