home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 015.lha / tracer_source / dith.c < prev    next >
C/C++ Source or Header  |  1986-11-10  |  4KB  |  167 lines

  1. #include <graphics/gfxbase.h>
  2. #include <graphics/display.h>
  3. #include <intuition/intuition.h>
  4. #include <stdio.h>
  5.  
  6. struct    GfxBase        *GfxBase;    /* Export the library pointers */
  7. struct    IntuitionBase    *IntuitionBase;
  8. struct    RastPort    *rp;        /* Graphics structures           */
  9. struct    ViewPort    *vp;
  10. struct    Window        *w;        /* Intuition structures        */ 
  11. struct    Screen        *screen;
  12.  
  13. struct    NewScreen    ns = {
  14.     0, 0,                /* start position                */
  15.     640, 400, 4,            /* width, height, depth          */
  16.     15, 0,                /* detail pen, block pen         */
  17.     HIRES|INTERLACE,        /* Hires ViewMode                */
  18.     CUSTOMSCREEN,            /* screen type                   */
  19.     NULL,                /* font to use                   */
  20.     " Dither Tester ",        /* default title for screen      */
  21.     NULL                /* pointer to additional gadgets */
  22. };
  23.  
  24. struct NewWindow nw = {
  25.     0, 11,                /* start position                */
  26.     640, 386,            /* width, height                 */
  27.     15, 0,                /* detail pen, block pen         */
  28.     NULL,                /* IDCMP flags                   */
  29.     BORDERLESS,            /* window flags                  */
  30.     NULL,                /* pointer to first user gadget  */
  31.     NULL,                /* pointer to user checkmark     */
  32.     NULL,                /* window title                  */
  33.     NULL,                /* pointer to screen (set below) */
  34.     NULL,                /* pointer to superbitmap        */
  35.     0, 0, 640, 386,            /* ignored since not sizeable    */
  36.     CUSTOMSCREEN            /* type of screen desired        */
  37. };
  38.  
  39. FILE    *in;
  40.  
  41. main(argc, argv)
  42. int    argc;
  43. char    **argv;
  44. {
  45.     char    wait[10];
  46.  
  47.     if ( !(GfxBase=(struct GfxBase *)OpenLibrary("graphics.library") ) ) {
  48.         cleanup();
  49.         exit(103);
  50.     }
  51.  
  52.     if ( !( IntuitionBase
  53.         =(struct IntuitionBase *)OpenLibrary("intuition.library") )
  54.      ) {
  55.         cleanup();
  56.         exit(103);
  57.     }
  58.     if ( !(screen = (struct Screen *)OpenScreen(&ns) ) ) { 
  59.         cleanup();
  60.         exit(103);
  61.     }
  62.  
  63.     nw.Screen = screen;                /* Open window in our new screen */
  64.     if ( ! (w = (struct Window *)OpenWindow(&nw) ) ) {
  65.         cleanup();
  66.         exit(103);
  67.     }
  68.  
  69.     vp = &screen->ViewPort;             /* Set colors in screen's VP    */
  70.     rp = w->RPort;                      /* Render into the window's RP  */
  71.  
  72.     /*  Set the color registers */
  73.     SetRGB4(vp, 0, 00, 00, 00);
  74.     SetRGB4(vp, 1, 01, 00, 01);    
  75.     SetRGB4(vp, 2, 02, 01, 02);   
  76.     SetRGB4(vp, 3, 03, 02, 03);
  77.     SetRGB4(vp, 4, 04, 03, 04);
  78.     SetRGB4(vp, 5, 05, 04, 05);
  79.     SetRGB4(vp, 6, 06, 05, 06);
  80.     SetRGB4(vp, 7, 07, 06, 07);
  81.     SetRGB4(vp, 8, 08, 07, 08);
  82.     SetRGB4(vp, 9, 09, 08, 09);
  83.     SetRGB4(vp,10, 10, 09, 10);
  84.     SetRGB4(vp,11, 11, 10, 11);
  85.     SetRGB4(vp,12, 12, 11, 12);
  86.     SetRGB4(vp,13, 13, 12, 13);
  87.     SetRGB4(vp,14, 14, 13, 14);
  88.     SetRGB4(vp,15, 15, 14, 15);
  89.  
  90.     show();
  91.     
  92.     gets(wait);
  93.  
  94.     cleanup();
  95. }
  96.  
  97. cleanup()
  98. {
  99.     if (w)
  100.         CloseWindow(w);
  101.  
  102.     if (screen)
  103.         CloseScreen(screen);
  104.  
  105.     if (IntuitionBase)
  106.         CloseLibrary(IntuitionBase);
  107.  
  108.     if (GfxBase)
  109.         CloseLibrary(GfxBase);
  110. }
  111.  
  112. show()
  113. {
  114.     int    x, y, color=0;
  115.  
  116.     for(y=0; y<=0x0F; y++)
  117.         for(x=0; x<=0x0F; x++)
  118.             box( (x*17)+30, (y*17)+30, color++);
  119. }
  120.  
  121. box(x, y, color)
  122. int    x, y, color;
  123. {
  124.     int    i, j;
  125.     
  126.     for (i=0; i<=0x0F; i++)
  127.         for (j=0; j<=0x0F; j++) {
  128.             SetAPen(rp, dither(x+i, x+j, color) );
  129.             WritePixel(rp, x+i, y+j);
  130.         }
  131. }
  132.  
  133. #define dithsize 7
  134.  
  135. /* 8 x 8 dithering matrix */
  136. static    int    matrix[dithsize+1][dithsize+1] = {
  137.     {   0, 128,  32, 160,   8, 136,  40, 168 },
  138.     { 192,  64, 224,  96, 200,  72, 232, 104 },
  139.     {  48, 176,  16, 144,  56, 184,  24, 152 },
  140.     { 240, 112, 208,  80, 248, 120, 216,  88 },
  141.     {  12, 140,  44, 174,   4, 132,  36, 164 },
  142.     { 204,  76, 236, 108, 196,  68, 228, 100 },
  143.     {  60, 188,  28, 156,  52, 180,  20, 148 },
  144.     { 252, 124, 210,  92, 244, 116, 212,  84 }
  145. };
  146.  
  147. /*
  148.     dithering by area access to matrix by using
  149.     the MOD function on the x and y coordinates
  150. */
  151. dither(x, y, level) /* dithering function */
  152. register    int    x, y, level;
  153. {
  154.     register    int    base, dith;
  155.  
  156.     base = level >> 4;
  157.     if (base == 0x0F)
  158.         return(base);
  159.  
  160.     dith = (level & 0x0F) << 4;
  161.  
  162.     if ( dith > matrix [x%dithsize] [y%dithsize] )
  163.         return(base+1);
  164.  
  165.     return(base);
  166. }
  167.