home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / SVGALIB / SVGALIB1.TAR / svgalib / gl / cbitmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-15  |  3.9 KB  |  182 lines

  1. /* Framebuffer Graphics Libary for Linux, Copyright 1993 Harm Hanemaayer */
  2. /* cbitmap.c    Compiled bitmaps */
  3.  
  4.  
  5. #include <stdlib.h>
  6. #include <vga.h>
  7. #include "inlstring.h"        /* include inline string operations */
  8.  
  9. #include "vgagl.h"
  10. #include "def.h"
  11.  
  12.  
  13.  
  14. void gl_compileboxmask( int w, int h, void *_dp1, void *_dp2 ) {
  15. /* Compiled format: <bytes_to_skip (0-254)><number_of_pixels (0-255)> */
  16. /*            <pixel_data>[<end_of_line(255)>]... */
  17.     uchar *dp1 = _dp1;
  18.     uchar *dp2 = _dp2;
  19.     int i;
  20.     for (i = 0; i < h; i++) {
  21.         int x = 0;
  22.         while (x < w) {
  23.             int count;
  24.             /* count zeroes */
  25.             count = 0;
  26.             while (*(dp1 + count) == 0 && count < 254 && x < w) {
  27.                 count++;
  28.                 x++;
  29.             }
  30.             dp1 += count;
  31.             if (x < w) {
  32.                 *dp2++ = count;
  33.                 /* count nonzeroes */
  34.                 count = 0;
  35.                 while (*(dp1 + count) != 0 && count < 255 && x < w) {
  36.                     *(dp2 + count + 1) = *(dp1 + count);
  37.                     count++;
  38.                     x++;
  39.                 }
  40.                 *dp2 = count;
  41.                 dp2 += count + 1;
  42.                 dp1 += count;
  43.             }
  44.         }
  45.         *dp2++ = 0xff;
  46.     }        
  47. }
  48.  
  49. int gl_compiledboxmasksize( int w, int h, void *_dp1 ) {
  50. /* Compiled format: <bytes_to_skip (0-254)><number_of_pixels (0-255)> */
  51. /*            <pixel_data>[<end_of_line(255)>]... */
  52.     uchar *dp1 = _dp1;
  53.     int size = 0;
  54.     int i;
  55.     for (i = 0; i < h; i++) {
  56.         int x = 0;
  57.         while (x < w) {
  58.             int count;
  59.             /* count zeroes */
  60.             count = 0;
  61.             while (*(dp1 + count) == 0 && count < 254 && x < w) {
  62.                 count++;
  63.                 x++;
  64.             }
  65.             size++;
  66.             dp1 += count;
  67.             /* count nonzeroes */
  68.             if (x < w) {
  69.                 count = 0;
  70.                 while (*(dp1 + count) != 0 && count < 255 && x < w) {
  71.                     count++;
  72.                     x++;
  73.                 }
  74.                 size += count + 1;
  75.                 dp1 += count;
  76.             }
  77.         }
  78.         size++;
  79.     }
  80.     return size;        
  81. }
  82.  
  83. static void gl_putboxmaskcompiledclip( int nx, int ny, int nw, int nh, int _x,
  84.     int _y, int w, int h, void *_dp ) {
  85. /* Special case costly clipping */
  86.     uchar *dp = _dp;
  87.     uchar *vp, *vpline;
  88.     int y;
  89.     vpline = VBUF + _y * BYTEWIDTH + _x;
  90.     for (y = _y; y < ny + nh; y++) {
  91.         int x = _x;
  92.         vp = vpline;
  93.         for (;;) {
  94.             int count = *dp++;
  95.             if (count == 0xff)
  96.                 break;    /* end of line */
  97.             vp += count;
  98.             x += count;
  99.             count = *dp++;
  100.             /* __memcpy gives severe bug here */
  101.             if (y >= ny)
  102.                 if (x >= nx)
  103.                     if (x + count > __clipx2 + 1) {
  104.                         if (x <= __clipx2) 
  105.                             __memcpyb(vp, dp, __clipx2 - x + 1);
  106.                     }
  107.                     else
  108.                         __memcpyb(vp, dp, count);
  109.                 else
  110.                     if (x + count > __clipx1)
  111.                         if (x + count > __clipx2 + 1)
  112.                             __memcpyb(vp + __clipx1 - x,
  113.                                 dp + __clipx1 - x,
  114.                                 __clipx2 - __clipx1 + 1);
  115.                         else
  116.                             __memcpy(vp + __clipx1 - x,
  117.                                 dp + __clipx1 - x,
  118.                                 count - __clipx1 + x);
  119.                 x += count;
  120.                 vp += count;
  121.                 dp += count;
  122.         }
  123.         vpline += BYTEWIDTH;
  124.     }
  125. }
  126.  
  127. #define ADJUSTBITMAPBOX() \
  128.     nw = w; nh = h; nx = x; ny = y;                \
  129.     if (nx + nw < __clipx1 || nx > __clipx2)        \
  130.         return;                        \
  131.     if (ny + nh < __clipy1 || ny > __clipy2)        \
  132.         return;                        \
  133.     if (nx < __clipx1) {        /* left adjust */    \
  134.         nw += nx - __clipx1;                \
  135.         nx = __clipx1;                    \
  136.     }                            \
  137.     if (ny < __clipy1) {        /* top adjust */    \
  138.         nh += ny - __clipy1;                \
  139.         ny = __clipy1;                    \
  140.     }                            \
  141.     if (nx + nw > __clipx2)        /* right adjust */    \
  142.         nw = __clipx2 - nx + 1;                \
  143.     if (ny + nh > __clipy2)        /* bottom adjust */    \
  144.         nh = __clipy2 - ny + 1;                \
  145.  
  146.  
  147. void gl_putboxmaskcompiled( int x, int y, int w, int h, void *_dp ) {
  148. /* no clipping */
  149.     uchar *dp = _dp;
  150.     uchar *vp, *vpline;
  151.     int i;
  152.     if (MODETYPE != CONTEXT_LINEAR && MODETYPE != CONTEXT_VIRTUAL) {
  153.         printf("vgagl: putboxmaskcompiled only supported in linear framebuffer\n");
  154.         return;
  155.     }
  156.     if (__clip) {
  157.         int nx, ny, nw, nh;
  158.         ADJUSTBITMAPBOX();
  159.         if (nw != w || nh != h) {
  160.             gl_putboxmaskcompiledclip(nx, ny, nw, nh, x, y, w, h, 
  161.                 dp);
  162.             return;
  163.         }
  164.     }
  165.     vpline= VBUF + y * BYTEWIDTH + x;
  166.     for (i = 0; i < h; i++) {
  167.         vp = vpline;
  168.         for (;;) {
  169.             int count = *dp++;
  170.             if (count == 0xff)
  171.                 break;    /* end of line */
  172.             vp += count;
  173.             count = *dp++;
  174.             /* __memcpy gives severe bug here */
  175.             __memcpyb(vp, dp, count);
  176.             vp += count;
  177.             dp += count;
  178.         }
  179.         vpline += BYTEWIDTH;
  180.     } 
  181. }
  182.