home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d157 / animballs.lha / AnimBalls / hamstuff.c < prev    next >
C/C++ Source or Header  |  1988-10-02  |  4KB  |  137 lines

  1. /* hamstuff.c   - routines to be given a set of colors. These will be plotted
  2.         in ham mode. This is based on hamrtn.c */
  3.  
  4. #include <exec/types.h>
  5. #include <stdio.h>
  6. #include <intuition/intuition.h>
  7.  
  8. #include "globals.h"
  9.  
  10. extern UWORD colortable[];
  11. extern UBYTE near[];
  12. extern UWORD nearmc[];
  13.  
  14. /* near is 4096 long and is configured as a 16*16*16 array        */
  15. /* each byte gives the distance to the nearest colortable entry        */
  16. /* the high order nible is the index into the color table and the    */
  17. /* low order nible is the distance.                    */
  18. /* color[16] - colormap containing rgb values fairly evenly */
  19. /*             spread throughout 16x16x16 space.            */
  20. /* near[4096] - table of distance. For each r,g,b           */
  21. /*              address = (r<<8 | g<<4 | b) & 0xfff         */
  22. /*              near>>4 gives the nearest color (index) and */
  23. /*              near & 0x0f gives the distance.             */
  24. /* nearmc[4096] - table of distance minus color             */
  25. /*       This contains the nearest color ignoring a color   */
  26. /*       (nearmc >> 8) & 0xf is for red,                    */
  27. /*       (nearmc >> 4) & 0xf is for green,                  */
  28. /*       and nearmc & 0xf is for blue                       */
  29.  
  30. #define INDEXNEAR(r,g,b) near[(((r)<<8) | ((g)<<4) | (b))&0xfff]
  31.  
  32. #define PLOTABS(x,y,color)     {mywritepixel(&ibitmap,x,y,color); \
  33.                 i = colortable[color];    \
  34.                 pr = i >> 8;        \
  35.                 pg = (i >> 4) & 0x0f;    \
  36.                 pb = i & 0x0f;         \
  37.                 if (!exact)        \
  38.                   mywritepixel(&ibitmap,ox,oy,near[oc]>>4);}
  39.  
  40.  
  41. #define RED    0x20
  42. #define GREEN    0x30
  43. #define BLUE    0x10
  44.  
  45. extern struct RastPort *rp;
  46.  
  47. static short pr, pg, pb;
  48. static int ox, oy, oc;
  49.  
  50. void
  51. setcolor(xpos,ypos,yepos,r,b,g,exact)
  52. int xpos,ypos,yepos,r,b,g,exact;
  53. {
  54.     short d1, dr, dg, db;
  55.     int i;
  56.  
  57.     r &= 0x0f;
  58.     b &= 0x0f;
  59.     g &= 0x0f;
  60.  
  61.     if (bw) mywritepixel(&ibitmap,xpos,ypos,(r+b+g)/3);
  62.     else {
  63.     d1 = INDEXNEAR(r,g,b);
  64.     if (((d1 & 0x0f) == 0) || (exact)) {    /* equals existing color */
  65.         PLOTABS(xpos,ypos,(d1>>4));
  66.         if (exact) mywritepixel(&ibitmap,xpos,yepos,d1>>4);
  67.     } else {
  68.         /* calculate nearest dist from prev */
  69.         dr = r - pr;
  70.         if (dr < 0) dr = -dr;
  71.         dg = g - pg;
  72.         if (dg < 0) dg = -dg;
  73.         db = b - pb;
  74.         if (db < 0) db = -db;
  75.             if (dr > dg) {
  76.             if (dr > db) {
  77.             /* dr is max */
  78.             if ((d1 & 0x0f) < (dg+db)) {
  79.                 PLOTABS(xpos,ypos,(d1>>4));
  80.             } else {
  81.                 mywritepixel(&ibitmap,xpos,ypos,RED | r);
  82.                 pr = r;
  83.                 mywritepixel(&ibitmap,ox,oy,(nearmc[oc]>>8));
  84.             }
  85.             } else {    /* if (dr > db) . . . */
  86.             /* db is max */
  87.             if ((d1 & 0x0f) < (dg+dr)) {
  88.                 PLOTABS(xpos,ypos,(d1>>4));
  89.             } else {
  90.                 mywritepixel(&ibitmap,xpos,ypos, BLUE | b);
  91.                 pb = b;
  92.                 mywritepixel(&ibitmap,ox,oy,(nearmc[oc] & 0xf));
  93.             }
  94.             }
  95.         } else {    /* if (dr > dg) . . . */
  96.             if (dg > db) {
  97.             /* dg is max */
  98.             if ((d1 & 0x0f) < (dr+db)) {
  99.                 PLOTABS(xpos,ypos,(d1>>4));
  100.             } else {
  101.                 mywritepixel(&ibitmap,xpos,ypos,GREEN | g);
  102.                 pg = g;
  103.                 mywritepixel(&ibitmap,ox,oy,
  104.                         (nearmc[oc]>>4)& 0xf);
  105.             }
  106.             } else {    /* if (dg > db) . . . */
  107.             /* db is max */
  108.             if ((d1 & 0x0f) < (dg+dr)) {
  109.                 PLOTABS(xpos,ypos,(d1>>4));
  110.             } else {
  111.                 mywritepixel(&ibitmap,xpos,ypos,BLUE | b);
  112.                 pb = b;
  113.                 mywritepixel(&ibitmap,ox,oy,(nearmc[oc] & 0xf));
  114.             }
  115.             }
  116.         }    /* end if (dr > dg) */
  117.     }        /* end else for if ((d1 & 0x0f) == 0) */
  118.     oc = r<<8 | g<<4 | b;
  119.     ox = xpos;
  120.     oy = yepos;
  121.     }
  122. }                /* end setcolor */
  123.  
  124. void
  125. dolast()
  126. {
  127.     if (!bw) mywritepixel(&ibitmap,ox,oy,near[oc]>>4);
  128. }
  129.  
  130. int
  131. match(r,g,b)
  132. int r,g,b;
  133. {
  134.     if (bw) return(0.5 + ((float)(r + g + b)) / 3.0);
  135.     else return(INDEXNEAR(r,g,b) >> 4);
  136. }
  137.