home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GDEVPCFB.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  22KB  |  837 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gdevpcfb.c */
  20. /* IBM PC EGA and VGA display drivers for Ghostscript */
  21. /* This is fundamentally an EGA driver with some parameters */
  22. /* that allow it to drive larger displays. */
  23. #include "memory_.h"
  24. #include "gx.h"
  25. #include "gserrors.h"
  26. #include "gxdevice.h"
  27. #include "gdevpccm.h"
  28. #include "gdevpcfb.h"
  29.  
  30. /* Macro for casting gx_device argument */
  31. #define fb_dev ((gx_device_ega *)dev)
  32.  
  33. /* Procedure record */
  34. private dev_proc_map_rgb_color(ega0_map_rgb_color);
  35. private dev_proc_map_rgb_color(ega1_map_rgb_color);
  36. #define ega2_map_rgb_color pc_4bit_map_rgb_color
  37. private dev_proc_map_color_rgb(ega01_map_color_rgb);
  38. #define ega2_map_color_rgb pc_4bit_map_color_rgb
  39. private gx_device_procs ega_procs = {
  40.     ega_open,
  41.     NULL,            /* get_initial_matrix */
  42.     NULL,            /* sync_output */
  43.     NULL,            /* output_page */
  44.     ega_close,
  45. #if ega_bits_of_color == 0
  46.     ega0_map_rgb_color,
  47.     ega01_map_color_rgb,
  48. #else
  49. # if ega_bits_of_color == 1
  50.     ega1_map_rgb_color,
  51.     ega01_map_color_rgb,
  52. # else
  53.     ega2_map_rgb_color,
  54.     ega2_map_color_rgb,
  55. # endif
  56. #endif
  57.     ega_fill_rectangle,
  58.     ega_tile_rectangle,
  59.     ega_copy_mono,
  60.     ega_copy_color,
  61.     NULL,            /* draw_line */
  62.     ega_get_bits
  63. };
  64.  
  65. /* All the known instances */
  66.         /* EGA */
  67. gx_device_ega gs_ega_device =
  68.     ega_device("ega", ega_procs, 80, 350, 48.0/35.0, 0x10);
  69.         /* VGA */
  70. gx_device_ega gs_vga_device =
  71.     ega_device("vga", ega_procs, 80, 480, 1.0, 0x12);
  72.         /* ATI Wonder SuperVGA, 800x600, 16-color mode */
  73. gx_device_ega gs_atiw16_device =
  74.     ega_device("atiw16", ega_procs, 100, 600, 1.0, 0x54);
  75.         /* Trident SuperVGA, 800x600, 16-color mode */
  76. gx_device_ega gs_tvga16_device =
  77.     ega_device("tvga16", ega_procs, 100, 600, 1.0, 0x5b);
  78.         /* Tseng Labs SuperVGA, 800x600, 16-color mode */
  79. gx_device_ega gs_tseng16_device =
  80.     ega_device("tseng16", ega_procs, 100, 600, 1.0, 0x29);
  81.  
  82. /* Save the EGA mode */
  83. private int ega_save_mode = -1;
  84.  
  85. /* Initialize the EGA for graphics mode */
  86. int
  87. ega_open(gx_device *dev)
  88. {    /* Adjust the device resolution. */
  89.     /* This is a hack, pending refactoring of the put_params machinery. */
  90.     switch ( fb_dev->video_mode )
  91.     {
  92.     case 0x10:    /* EGA */
  93.         gx_device_adjust_resolution(dev, 640, 350, 1); break;
  94.     case 0x12:    /* VGA */
  95.         gx_device_adjust_resolution(dev, 640, 480, 1); break;
  96.     case 0x54:    /* ATI Wonder */
  97.     case 0x5b:    /* Trident */
  98.     case 0x29:    /* Tseng */
  99.         gx_device_adjust_resolution(dev, 800, 600, 1); break;
  100.     }
  101.     if ( ega_save_mode < 0 )
  102.         ega_save_mode = ega_get_mode();
  103.     /* Do implementation-specific initialization */
  104.     ega_set_signals(dev);
  105.     ega_set_mode(fb_dev->video_mode);
  106.     set_s_map(-1);            /* enable all maps */
  107.     return 0;
  108. }
  109.  
  110. /* Reinitialize the EGA for text mode */
  111. int
  112. ega_close(gx_device *dev)
  113. {    if ( ega_save_mode >= 0 )
  114.         ega_set_mode(ega_save_mode);
  115.     return 0;
  116. }
  117.  
  118. /* Map a r-g-b color to an EGA color code. */
  119. #define Nb gx_color_value_bits
  120. private gx_color_index
  121. ega0_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  122.   gx_color_value b)
  123. {    return pc_4bit_map_rgb_color(dev, r, r, r);
  124. }
  125. private gx_color_index
  126. ega1_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  127.   gx_color_value b)
  128. {
  129. #define cvtop (gx_color_value)(1 << (Nb - 1))
  130.     return pc_4bit_map_rgb_color(dev, r & cvtop, g & cvtop, b & cvtop);
  131. }
  132. #undef Nb
  133.  
  134. /* Map a color code to r-g-b. */
  135. #define icolor (int)color
  136. private int
  137. ega01_map_color_rgb(gx_device *dev, gx_color_index color,
  138.   gx_color_value prgb[3])
  139. {
  140. #define one (gx_max_color_value / 2 + 1)
  141.     prgb[0] = (icolor & 4 ? one : 0);
  142.     prgb[1] = (icolor & 2 ? one : 0);
  143.     prgb[2] = (icolor & 1 ? one : 0);
  144.     return 0;
  145. #undef one
  146. }
  147. #undef icolor
  148.  
  149. /* ------ Internal routines ------ */
  150.  
  151. /* Structure for operation parameters. */
  152. /* Note that this structure is known to assembly code. */
  153. /* Not all parameters are used for every operation. */
  154. typedef struct rop_params_s {
  155.     fb_ptr dest;            /* pointer to frame buffer */
  156.     int draster;            /* raster of frame buffer */
  157.     const byte *src;        /* pointer to source data */
  158.     int sraster;            /* source raster */
  159.     int width;            /* width in bytes */
  160.     int height;            /* height in scan lines */
  161.     int shift;            /* amount to right shift source */
  162.     int invert;            /* 0 or -1 to invert source */
  163.     int data;            /* data for fill */
  164. } rop_params;
  165. typedef rop_params _ss *rop_ptr;
  166.  
  167. /* Assembly language routines */
  168.  
  169. #if USE_ASM
  170. void memsetcol(P1(rop_ptr)); /* dest, draster, height, data */
  171. #else
  172. #define memsetcol cmemsetcol
  173. private void
  174. cmemsetcol(rop_ptr rop)
  175. {    byte *addr = rop->dest;
  176.     int yc = rop->height;
  177.     byte data = rop->data;
  178.     int draster = rop->draster;
  179.     while ( yc-- )
  180.      { byte_discard(*addr);
  181.        *addr = data;
  182.        addr += draster;
  183.      }
  184. }
  185. #endif
  186.  
  187. #if USE_ASM
  188. void memsetrect(P1(rop_ptr)); /* dest, draster, width, height, data */
  189. #else
  190. #define memsetrect cmemsetrect
  191. private void
  192. cmemsetrect(rop_ptr rop)
  193. {    int yc = rop->height;
  194.     int width = rop->width;
  195.     if ( yc <= 0 || width <= 0 ) return;
  196.        {    byte *addr = rop->dest;
  197.         byte data = rop->data;
  198.         if ( width > 5 )    /* use memset */
  199.            {    int skip = rop->draster;
  200.             do
  201.                {    memset(addr, data, width);
  202.                 addr += skip;
  203.                }
  204.             while ( --yc );
  205.            }
  206.         else            /* avoid the fixed overhead */
  207.            {    int skip = rop->draster - width;
  208.             do
  209.                {    int cnt = width;
  210.                 do { *addr++ = data; } while ( --cnt );
  211.                 addr += skip;
  212.                }
  213.             while ( --yc );
  214.            }
  215.        }
  216. }
  217. #endif
  218.  
  219. #if USE_ASM
  220. void memrwcol(P1(rop_ptr)); /* dest, draster, src, sraster, height, shift, invert */
  221. #  define memrwcol0(rop) memrwcol(rop)    /* same except shift = 0 */
  222. #else
  223. #  define memrwcol cmemrwcol
  224. #  define memrwcol0 cmemrwcol0
  225. private void
  226. cmemrwcol(rop_ptr rop)
  227. {    byte *dp = rop->dest;
  228.     const byte *sp = rop->src;
  229.     int yc = rop->height;
  230.     int shift = rop->shift;
  231.     byte invert = rop->invert;
  232.     int sraster = rop->sraster, draster = rop->draster;
  233.     while ( yc-- )
  234.      { byte_discard(*dp);
  235.        *dp = ((*sp >> shift) + (*sp << (8 - shift))) ^ invert;
  236.        dp += draster, sp += sraster;
  237.      }
  238. }
  239. private void
  240. cmemrwcol0(rop_ptr rop)
  241. {    byte *dp = rop->dest;
  242.     const byte *sp = rop->src;
  243.     int yc = rop->height;
  244.     byte invert = rop->invert;
  245.     int sraster = rop->sraster, draster = rop->draster;
  246.     if ( yc > 0 ) do
  247.      { byte_discard(*dp);
  248.        *dp = *sp ^ invert;
  249.        dp += draster, sp += sraster;
  250.      }
  251.     while ( --yc );
  252. }
  253. #endif
  254.  
  255. #if USE_ASM
  256. void memrwcol2(P1(rop_ptr)); /* dest, draster, src, sraster, height, shift, invert */
  257. #else
  258. #define memrwcol2 cmemrwcol2
  259. private void
  260. cmemrwcol2(rop_ptr rop)
  261. {    byte *dp = rop->dest;
  262.     const byte *sp = rop->src;
  263.     int yc = rop->height;
  264.     int shift = rop->shift;
  265.     byte invert = rop->invert;
  266.     int sraster = rop->sraster, draster = rop->draster;
  267.     while ( yc-- )
  268.      { byte_discard(*dp);
  269.        *dp = ((sp[1] >> shift) + (*sp << (8 - shift))) ^ invert;
  270.        dp += draster, sp += sraster;
  271.      }
  272. }
  273. #endif
  274.  
  275. /* Forward definitions */
  276. int ega_write_dot(P4(gx_device *, int, int, gx_color_index));
  277. private void near fill_rectangle(P4(rop_ptr, int, int, int));
  278. private void near fill_row_only(P4(byte *, int, int, int));
  279.  
  280. /* Clean up after writing */
  281. #define dot_end()\
  282.   set_g_mask(0xff)            /* all bits on */
  283.  
  284. /* Write a dot using the EGA color codes. */
  285. /* This doesn't have to be efficient. */
  286. int
  287. ega_write_dot(gx_device *dev, int x, int y, gx_color_index color)
  288. {    byte data[4];
  289.     data[0] = (byte)color;
  290.     return ega_copy_color(dev, data, 1, 4, gx_no_bitmap_id, x, y, 1, 1);
  291. }
  292.  
  293. /* Macro for testing bit-inclusion */
  294. #define bit_included_in(x,y) !((x)&~(y))
  295.  
  296. /* Copy a monochrome bitmap.  The colors are given explicitly. */
  297. /* Color = gx_no_color_index means transparent (no effect on the image). */
  298. int
  299. ega_copy_mono(gx_device *dev,
  300.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  301.   int x, int y, int w, int h, gx_color_index izero, gx_color_index ione)
  302. {    rop_params params;
  303. #define czero (int)izero
  304. #define cone (int)ione
  305.     int dleft, count;
  306.     byte mask, rmask;
  307.     fb_ptr save_dest;
  308.     int other_color = -1;
  309.     fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
  310.     params.dest = mk_fb_ptr(x, y);
  311.     params.draster = fb_dev->raster;
  312.     params.src = base + (sourcex >> 3);
  313.     params.sraster = raster;
  314.     params.height = h;
  315.     params.shift = (x - sourcex) & 7;
  316.     /* Analyze the 16 possible cases: each of izero and ione may be */
  317.     /* 0, 0xf, transparent, or some other color. */
  318.     switch ( czero )
  319.        {
  320.     case no_color:
  321.         switch ( cone )
  322.            {
  323.         default:        /* (T, other) */
  324.             /* Must do 2 passes */
  325.             other_color = cone;
  326.             save_dest = params.dest;
  327.             /* falls through */
  328.         case 0:            /* (T, 0) */
  329.             set_g_function(gf_AND);
  330.             params.invert = -1;
  331.             break;
  332.         case 0xf:        /* (T, 0xf) */
  333.             set_g_function(gf_OR);
  334.             params.invert = 0;
  335.             break;
  336.         case no_color:        /* (T, T) */
  337.             return 0;    /* nothing to do */
  338.            }
  339.         break;
  340.     case 0:
  341.         params.invert = 0;
  342.         switch ( cone )
  343.            {
  344.         default:        /* (0, other) */
  345.             set_g_const(0);
  346.             set_g_const_map(cone ^ 0xf);
  347.             /* falls through */
  348.         case 0xf:        /* (0, 0xf) */
  349.             break;
  350.         case no_color:        /* (0, T) */
  351.             set_g_function(gf_AND);
  352.             break;
  353.            }
  354.         break;
  355.     case 0xf:
  356.         params.invert = -1;
  357.         switch ( cone )
  358.            {
  359.         case 0:            /* (0xf, 0) */
  360.             break;
  361.         default:        /* (0xf, other) */
  362.             set_g_const(0xf);
  363.             set_g_const_map(cone);
  364.             break;
  365.         case no_color:        /* (0xf, T) */
  366.             set_g_function(gf_OR);
  367.             /* falls through */
  368.            }
  369.         break;
  370.     default:
  371.         switch ( cone )
  372.            {
  373.         default:        /* (other, not T) */
  374.             if ( bit_included_in(czero, cone) )
  375.                {    set_g_const(czero);
  376.                 set_g_const_map(czero ^ cone ^ 0xf);
  377.                 params.invert = 0;
  378.                 break;
  379.                }
  380.             else if ( bit_included_in(cone, czero) )
  381.                {    set_g_const(cone);
  382.                 set_g_const_map(cone ^ czero ^ 0xf);
  383.                 params.invert = -1;
  384.                 break;
  385.                }
  386.             /* No way around it, fill with one color first. */
  387.             save_dest = params.dest;
  388.             fill_rectangle((rop_ptr)¶ms, x & 7, w, cone);
  389.             params.dest = save_dest;
  390.             set_g_function(gf_XOR);
  391.             set_s_map(czero ^ cone);
  392.             other_color = -2;    /* must reset s_map at end */
  393.             params.invert = -1;
  394.             break;
  395.         case no_color:        /* (other, T) */
  396.             /* Must do 2 passes */
  397.             other_color = czero;
  398.             save_dest = params.dest;
  399.             set_g_function(gf_AND);
  400.             params.invert = 0;
  401.             break;
  402.            }
  403.         break;
  404.        }
  405.     /* Actually copy the bits. */
  406.     dleft = 8 - (x & 7);
  407.     mask = 0xff >> (8 - dleft);
  408.     count = w - dleft;
  409.     if ( count < 0 )
  410.         mask -= mask >> w,
  411.         rmask = 0;
  412.     else
  413.         rmask = 0xff00 >> (count & 7);
  414.     /* params: dest, src, sraster, height, shift, invert */
  415.     /* Smashes params.src, params.dest, count. */
  416. copy:    set_g_mask(mask);
  417.     if ( params.shift == 0 )    /* optimize the aligned case */
  418.        {    /* Do left column */
  419.         memrwcol0((rop_ptr)¶ms);
  420.         /* Do center */
  421.         if ( (count -= 8) >= 0 )
  422.            {    out_g_mask(0xff);
  423.             do
  424.                {    params.src++, params.dest++;
  425.                 memrwcol0((rop_ptr)¶ms);
  426.                }
  427.             while ( (count -= 8) >= 0 );
  428.            }
  429.         /* Do right column */
  430.         if ( rmask )
  431.            {    params.src++, params.dest++;
  432.             out_g_mask(rmask);
  433.             memrwcol0((rop_ptr)¶ms);
  434.            }
  435.        }
  436.     else
  437.        {    /* Do left column */
  438.         int sleft = 8 - (sourcex & 7);
  439.         if ( sleft >= dleft )
  440.            {    /* Source fits in one byte */
  441.             memrwcol((rop_ptr)¶ms);
  442.            }
  443.         else if ( w <= sleft )
  444.            {    /* Source fits in one byte, thin case */
  445.             memrwcol((rop_ptr)¶ms);
  446.             goto fin;
  447.            }
  448.         else
  449.            {    memrwcol2((rop_ptr)¶ms);
  450.             params.src++;
  451.            }
  452.         /* Do center */
  453.         if ( (count -= 8) >= 0 )
  454.            {    out_g_mask(0xff);
  455.             do
  456.                {    params.dest++;
  457.                 memrwcol2((rop_ptr)¶ms);
  458.                 params.src++;
  459.                }
  460.             while ( (count -= 8) >= 0 );
  461.            }
  462.         /* Do right column */
  463.         if ( rmask )
  464.            {    out_g_mask(rmask);
  465.             params.dest++;
  466.             if ( count + 8 <= params.shift )
  467.                 memrwcol((rop_ptr)¶ms);
  468.             else
  469.                 memrwcol2((rop_ptr)¶ms);
  470.            }
  471.        }
  472. fin:    if ( other_color != -1 )
  473.        {    if ( other_color >= 0 )
  474.            {    /* Do the second pass on (T, other) or (other, T). */
  475.             count = w - dleft;
  476.             params.src = base + (sourcex >> 3);
  477.             params.dest = save_dest;
  478.             params.invert ^= -1;
  479.             set_s_map(other_color);
  480.             set_g_function(gf_OR);
  481.             other_color = -2;
  482.             goto copy;
  483.            }
  484.         else
  485.            {    /* Finished second pass, restore s_map */
  486.             set_s_map(-1);
  487.            }
  488.        }
  489.     set_g_function(gf_WRITE);
  490.     set_g_const_map(0);
  491.     dot_end();
  492.     return 0;
  493. #undef czero
  494. #undef cone
  495. }
  496.  
  497. /* Copy a color pixelmap.  This is just like a bitmap, */
  498. /* except that each pixel takes 4 bits instead of 1. */
  499. int
  500. ega_copy_color(gx_device *dev,
  501.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  502.   int x, int y, int w, int h)
  503. {    const byte *line = base + (sourcex >> 1);
  504.     unsigned mask = 0x80 >> (x & 7);
  505.     int px = sourcex & 1;
  506.     fb_ptr fb_line;
  507.     int fb_raster = fb_dev->raster;
  508.     fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
  509.     fb_line = mk_fb_ptr(x, y);
  510.     set_g_mode(gm_FILL);
  511.     select_g_mask();
  512.     for ( ; ; px++ )
  513.     {    const byte *bptr = line;
  514.         fb_ptr fbptr = fb_line;
  515.         int py = h;
  516.         out_g_mask(mask);
  517.         if ( px & 1 )
  518.         {    do
  519.                {    byte_discard(*fbptr);    /* latch frame buffer data */
  520.                 *fbptr = *bptr;
  521.                 bptr += raster;
  522.                 fbptr += fb_raster;
  523.                }
  524.             while ( --py );
  525.             line++;
  526.         }
  527.         else
  528.         {    do
  529.                {    byte_discard(*fbptr);    /* latch frame buffer data */
  530.                 *fbptr = *bptr >> 4;
  531.                 bptr += raster;
  532.                 fbptr += fb_raster;
  533.                }
  534.             while ( --py );
  535.         }
  536.         if ( !--w )
  537.             break;
  538.         if ( (mask >>= 1) == 0 )
  539.             mask = 0x80, fb_line++;
  540.     }
  541.     set_g_mode(gm_DATA);
  542.     dot_end();
  543.     return 0;
  544. }
  545.  
  546. /* Fill a rectangle. */
  547. int
  548. ega_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  549.   gx_color_index color)
  550. {    rop_params params;
  551.     fit_fill(dev, x, y, w, h);
  552.     params.dest = mk_fb_ptr(x, y);
  553.     if ( h == 1 )
  554.         fill_row_only(params.dest, x & 7, w, (int)color);
  555.     else
  556.        {    params.draster = fb_dev->raster;
  557.         params.height = h;
  558.         fill_rectangle((rop_ptr)¶ms, x & 7, w, (int)color);
  559.         dot_end();
  560.        }
  561.     return 0;
  562. }
  563.  
  564. /* Tile a rectangle.  Note that the two colors must both be supplied, */
  565. /* i.e. neither one can be gx_no_color_index (transparent): */
  566. /* a transparent color means that the tile is colored, not a mask. */
  567. int
  568. ega_tile_rectangle(gx_device *dev, const gx_tile_bitmap *tile,
  569.   int x, int y, int w, int h, gx_color_index czero, gx_color_index cone,
  570.   int px, int py)
  571. #define zero (int)czero
  572. #define one (int)cone
  573. {    rop_params params;
  574.     int xmod, width_bytes;
  575.     int tile_height = tile->size.y;
  576.     int xbit;
  577.     int lcount;
  578.     int mask, rmask;
  579.     byte narrow;
  580.     byte again;
  581.     int const_bits, maps;
  582.     int ymod, yleft;
  583.     fit_fill(dev, x, y, w, h);
  584.     /* We only handle the easiest cases directly. */
  585.     if ( (tile->size.x & 7) || one == -1 || zero == -1 || px || py )
  586.         return gx_default_tile_rectangle(dev, tile, x, y, w, h,
  587.             czero, cone, px, py);
  588.     /* Following is similar to aligned case of copy_mono */    
  589.     params.dest = mk_fb_ptr(x, y);
  590.     params.draster = fb_dev->raster;
  591.     params.sraster = tile->raster;
  592.     params.shift = 0;
  593.     xbit = x & 7;
  594.     /* Set up the graphics registers */
  595.     const_bits = (zero ^ one) ^ 0xf;
  596.     if ( const_bits )
  597.        {    set_g_const(zero);    /* either color will do */
  598.         set_g_const_map(const_bits);
  599.        }
  600.     if ( (maps = zero & ~one) != 0 )
  601.        {    set_s_map(maps += const_bits);
  602.         params.invert = -1;
  603.         again = one & ~zero;
  604.        }
  605.     else
  606.        {    maps = one & ~zero;
  607.         set_s_map(maps += const_bits);
  608.         params.invert = 0;
  609.         again = 0;
  610.        }
  611.     xmod = (x % tile->size.x) >> 3;
  612.     width_bytes = tile->size.x >> 3;
  613.     mask = 0xff >> xbit;
  614.     if ( w + xbit <= 8 )
  615.         mask -= mask >> w,
  616.         rmask = 0,
  617.         narrow = 1;
  618.     else
  619.        {    rmask = (0xff00 >> ((w + x) & 7)) & 0xff;
  620.         if ( xbit )    w += xbit - 8;
  621.         else        mask = 0, --xmod, --params.dest;
  622.         narrow = 0;
  623.        }
  624.     ymod = y % tile_height;
  625. tile:    yleft = tile_height - ymod;
  626.     params.src = tile->data + ymod * params.sraster + xmod;
  627.     lcount = h;
  628.     if ( narrow )            /* Optimize narrow case */
  629.        {    set_g_mask(mask);
  630.         if ( lcount > yleft )
  631.            {    params.height = yleft;
  632.             memrwcol0((rop_ptr)¶ms);
  633.             params.dest += yleft * params.draster;
  634.             params.src = tile->data + xmod;
  635.             params.height = tile_height;
  636.             lcount -= yleft;
  637.             while ( lcount >= tile_height )
  638.                {    memrwcol0((rop_ptr)¶ms);
  639.                 params.dest += tile_height * params.draster;
  640.                 lcount -= tile_height;
  641.                }
  642.            }
  643.         if ( lcount )
  644.            {    params.height = lcount;
  645.             memrwcol0((rop_ptr)¶ms);
  646.            }
  647.        }
  648.     else
  649.        {    fb_ptr line = params.dest;
  650.         int xpos = width_bytes - xmod;
  651.         while ( 1 )
  652.            {    int xleft = xpos;
  653.             int count = w;
  654.             params.height = (lcount > yleft ? yleft : lcount);
  655.             /* Do first byte, if not a full byte. */
  656.             if ( mask )
  657.                {    set_g_mask(mask);
  658.                 memrwcol0((rop_ptr)¶ms);
  659.                }
  660.             /* Do full bytes */
  661.             if ( (count -= 8) >= 0 )
  662.                {    set_g_mask(0xff);
  663.                 do
  664.                    {    if ( !--xleft )
  665.                         xleft = width_bytes,
  666.                         params.src -= width_bytes;
  667.                     ++params.src, ++params.dest;
  668.                     memrwcol0((rop_ptr)¶ms);
  669.                    }
  670.                 while ( (count -= 8) >= 0 );
  671.                }
  672.             /* Do last byte */
  673.             if ( rmask )
  674.                {    if ( !--xleft )
  675.                     xleft = width_bytes,
  676.                     params.src -= width_bytes;
  677.                 set_g_mask(rmask);
  678.                 ++params.src, ++params.dest;
  679.                 memrwcol0((rop_ptr)¶ms);
  680.                }
  681.             if ( (lcount -= params.height) == 0 ) break;
  682.             params.dest = line += params.height * params.draster;
  683.             params.src = tile->data + xmod;
  684.             yleft = tile_height;
  685.            }
  686.        }
  687.     /* Now do the second color if needed */
  688.     if ( again )
  689.        {    maps = again + const_bits;
  690.         set_s_map(maps);
  691.         again = 0;
  692.         params.dest = mk_fb_ptr(x, y);
  693.         if ( mask == 0 ) params.dest--;
  694.         params.invert = 0;
  695.         goto tile;
  696.        }
  697.     if ( maps != 0xf )
  698.         set_s_map(-1);
  699.     if ( const_bits )
  700.         set_g_const_map(0);
  701.     dot_end();
  702.     return 0;
  703. }
  704.  
  705. /* Read scan lines back from the frame buffer. */
  706. int
  707. ega_get_bits(gx_device *dev, int y, byte *data, byte **actual_data)
  708. {    uint bytes_per_row = dev->width >> 1;
  709.     int plane;
  710.     ushort *dest = (ushort *)data;
  711.     if ( y < 0 || y >= dev->height )
  712.         return gs_error_rangecheck;
  713.     memset(data, 0, bytes_per_row);
  714.     for ( plane = 0; plane < 4; plane++ )
  715.        {    fb_ptr src = mk_fb_ptr(0, y);
  716.         int i;
  717.         /* Plane 0 is the least significant plane. */
  718.         /* We know we're on a little-endian machine.... */
  719.         static const ushort spread4[16] =
  720.            {    0x0, 0x800, 0x8000, 0x8800,
  721.             0x8, 0x808, 0x8008, 0x8808,
  722.             0x80, 0x880, 0x8080, 0x8880,
  723.             0x88, 0x888, 0x8088, 0x8888
  724.            };
  725.         set_g_read_plane(plane);
  726.         for ( i = 0; i < dev->width; i += 8, src++, dest += 2 )
  727.            {    byte b = *src;
  728.             dest[0] = (dest[0] >> 1) + spread4[b >> 4];
  729.             dest[1] = (dest[1] >> 1) + spread4[b & 0xf];
  730.            }
  731.        }
  732.     if ( actual_data != 0 )
  733.         *actual_data = data;
  734.     return 0;
  735. }
  736.  
  737. /* ------ Internal routines ------ */
  738.  
  739. /* Mask table for rectangle fill. */
  740. static const byte rmask_tab[9] =
  741.    {    0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
  742.    };
  743.  
  744. /* Fill a rectangle specified by pointer into frame buffer, */
  745. /* starting bit within byte, width, and height. */
  746. /* Smashes rop->dest. */
  747. private void near
  748. fill_rectangle(register rop_ptr rop, int bit, int w, int color)
  749.   /* rop: dest, draster, height */
  750. {    set_g_const(color);
  751.     set_g_const_map(0xf);
  752.     select_g_mask();
  753.     if ( bit + w <= 8 )
  754.        {    /* Less than one byte */
  755.         out_g_mask(rmask_tab[w] >> bit);
  756.         memsetcol(rop);
  757.        }
  758.     else
  759.        {    byte right_mask;
  760.         if ( bit )
  761.            {    out_g_mask(0xff >> bit);
  762.             memsetcol(rop);
  763.             rop->dest++;
  764.             w += bit - 8;
  765.            }
  766.         if ( w >= 8 )
  767.            {    out_g_mask(0xff);    /* all bits */
  768.             rop->width = w >> 3;
  769.             memsetrect(rop);
  770.             rop->dest += rop->width;
  771.             w &= 7;
  772.            }
  773.         if ( (right_mask = rmask_tab[w]) != 0 )
  774.            {    out_g_mask(right_mask);
  775.             memsetcol(rop);
  776.            }
  777.        }
  778.     set_g_const_map(0);
  779. }
  780.  
  781. /* Fill a single row specified by pointer into frame buffer, */
  782. /* starting bit within byte, and width; clean up afterwards. */
  783. #define r_m_w(ptr) (*(ptr))++        /* read & write, data irrelevant */
  784. private void near
  785. fill_row_only(byte *dest, int bit, int w, int color)
  786.   /* rop: dest */
  787. {    if ( bit + w <= 8 )
  788.        {    /* Less than one byte. */
  789.         /* Optimize filling with black or white. */
  790.         switch ( color )
  791.         {
  792.         case 0:
  793.             set_g_mask(rmask_tab[w] >> bit);
  794.             *dest &= color;        /* read, then write 0s; */
  795.                 /* some compilers optimize &= 0 to a store. */
  796.             out_g_mask(0xff);        /* dot_end */
  797.             break;
  798.         case 0xf:
  799.             set_g_mask(rmask_tab[w] >> bit);
  800.             *dest |= 0xff;        /* read, then write 1s; */
  801.                 /* some compilers optimize &= 0 to a store. */
  802.             out_g_mask(0xff);        /* dot_end */
  803.             break;
  804.         default:
  805.             set_g_const(color);
  806.             set_g_const_map(0xf);
  807.             set_g_mask(rmask_tab[w] >> bit);
  808.             r_m_w(dest);
  809.             out_g_mask(0xff);        /* dot_end */
  810.             set_g_const_map(0);
  811.         }
  812.        }
  813.     else
  814.        {    byte right_mask;
  815.         int byte_count;
  816.         set_g_const(color);
  817.         set_g_const_map(0xf);
  818.         select_g_mask();
  819.         if ( bit )
  820.            {    out_g_mask(0xff >> bit);
  821.             r_m_w(dest);
  822.             dest++;
  823.             w += bit - 8;
  824.            }
  825.         byte_count = w >> 3;
  826.         if ( (right_mask = rmask_tab[w & 7]) != 0 )
  827.            {    out_g_mask(right_mask);
  828.             r_m_w(dest + byte_count);
  829.            }
  830.         out_g_mask(0xff);
  831.         if ( byte_count )
  832.            {    memset(dest, 0, byte_count);    /* data irrelevant */
  833.            }
  834.         set_g_const_map(0);
  835.        }
  836. }
  837.