home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / printing / ghostscrip / source / specific / c / gdevpcfb < prev    next >
Encoding:
Text File  |  1991-10-25  |  22.8 KB  |  889 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gdevpcfb.c */
  21. /* IBM PC-compatible display driver for Ghostscript */
  22. #include <dos.h>
  23. typedef union REGS registers;
  24. /* This is fundamentally an EGA driver with some parameters */
  25. /* that allow it to drive larger displays. */
  26. #include "memory_.h"
  27. #include "gs.h"
  28. #include "gsmatrix.h"            /* for gxdevice.h */
  29. #include "gxbitmap.h"
  30. #include "gxdevice.h"
  31.  
  32. #define USE_ASM 1            /* use assembly language */
  33.  
  34. /* Define the short (integer) version of "transparent" color. */
  35. /* ****** Depends on gx_no_color_index being all 1's. ******/
  36. #define no_color ((int)gx_no_color_index)
  37.  
  38. /* For testing, the EGA may be defined as a monochrome, 8-color, or */
  39. /* 16-color device. */
  40. #define ega_bits_of_color 2        /* 0, 1, or 2 */
  41.  
  42. /* Range of r-g-b values */
  43. #define rgb_max (ega_bits_of_color == 1 ? 1 : 3)
  44.  
  45. /* Procedures */
  46.  
  47.     /* See gxdevice.h for the definitions of the procedures. */
  48.  
  49. dev_proc_open_device(ega_open);
  50. dev_proc_close_device(ega_close);
  51. dev_proc_map_rgb_color(ega_map_rgb_color);
  52. dev_proc_map_color_rgb(ega_map_color_rgb);
  53. dev_proc_fill_rectangle(ega_fill_rectangle);
  54. dev_proc_tile_rectangle(ega_tile_rectangle);
  55. int ega_write_dot(P4(gx_device *, int, int, gx_color_index));
  56. dev_proc_copy_mono(ega_copy_mono);
  57. dev_proc_copy_color(ega_copy_color);
  58.  
  59. /* Type for frame buffer pointers. */
  60. /*** Intimately tied to the 80x86 (x<2) addressing architecture. ***/
  61. typedef byte far *fb_ptr;
  62.  
  63. /* The device descriptor */
  64. typedef struct gx_device_ega_s gx_device_ega;
  65. struct gx_device_ega_s {
  66.     gx_device_common;
  67.     int raster;            /* frame buffer bytes per line */
  68.     int fb_seg_mult;        /* multiplier for segment part */
  69.                     /* of frame buffer pointer */
  70.     int fb_byte_mult;        /* multiplier for word part ditto */
  71. #define mk_fb_ptr(x, y)\
  72.   (fb_dev->fb_byte_mult == 0 ?\
  73.    (fb_ptr)MK_FP(regen + (y) * (fb_dev->fb_seg_mult), (x) >> 3) :\
  74.    (fb_ptr)MK_FP(regen + ((y) >> 4) * (fb_dev->fb_seg_mult),\
  75.          (((y) & 15) * fb_dev->fb_byte_mult) + ((x) >> 3)))
  76. /* Define the largest height that can be processed */
  77. /* within a single 64K segment.  If fb_dev->height > max_rop_height, */
  78. /* we may have to break up operations into pieces. */
  79.     unsigned max_rop_height;
  80.     int video_mode;
  81. };
  82.  
  83. /* Macro for casting gx_device argument */
  84. #define fb_dev ((gx_device_ega *)dev)
  85.  
  86. /* Procedure record */
  87. private gx_device_procs ega_procs = {
  88.     ega_open,
  89.     gx_default_get_initial_matrix,
  90.     gx_default_sync_output,
  91.     gx_default_output_page,
  92.     ega_close,
  93.     ega_map_rgb_color,
  94.     ega_map_color_rgb,
  95.     ega_fill_rectangle,
  96.     ega_tile_rectangle,
  97.     ega_copy_mono,
  98.     ega_copy_color,
  99.     gx_default_draw_line,
  100.     gx_default_fill_trapezoid,
  101.     gx_default_tile_trapezoid
  102. };
  103.  
  104. /* Macro for creating instances */
  105. /* The initial parameters map an appropriate fraction of */
  106. /* the screen to an 8.5" x 11" coordinate space. */
  107. /* This may or may not be what is desired! */
  108. #define ega_device(dev_name, fb_raster, screen_height, aspect_ratio, video_mode)\
  109.    {    sizeof(gx_device_ega),\
  110.     &ega_procs,\
  111.     dev_name,\
  112.     fb_raster * 8, screen_height,\
  113.       (screen_height * (aspect_ratio)) / 11.0,    /* x density */\
  114.       screen_height / 11.0,        /* y density */\
  115.     no_margins,\
  116.     (ega_bits_of_color ? 1 : 0),    /* has_color */\
  117.     rgb_max,\
  118.     4,            /* bits per color pixel */\
  119.     0,            /* not opened yet */\
  120.     fb_raster,\
  121.     (fb_raster & 15 ? fb_raster : fb_raster >> 4),\
  122.     (fb_raster & 15 ? fb_raster : 0),\
  123.     ((unsigned)(0xffff - fb_raster) / fb_raster),\
  124.     video_mode\
  125.    }
  126.  
  127. /* All the known instances */
  128.         /* EGA */
  129. gx_device_ega gs_ega_device =
  130.     ega_device("ega", 80, 350, 48.0/35.0, 0x10);
  131.         /* VGA */
  132. gx_device_ega gs_vga_device =
  133.     ega_device("vga", 80, 480, 1.0, 0x12);
  134.         /* EIZO MDB-10 */
  135. gx_device_ega gs_mdb10_device =
  136.     ega_device("mdb10", 128, 768, 1.0, 0x37);
  137.  
  138. /* Define the color spectrum */
  139. #define black 0
  140. #define blue 1
  141. #define green 2
  142. #define cyan 3
  143. #define red 4
  144. #define magenta 5
  145. #define brown 6
  146. #define white 7
  147. #define dgray 8                /* dark gray is not very usable */
  148. #define lblue 9
  149. #define lgreen 10
  150. #define lcyan 11
  151. #define lred 12
  152. #define lmagenta 13
  153. #define yellow 14
  154. #define bwhite 15
  155.  
  156. /* Forward declarations */
  157. private int ega_get_mode();
  158. private void ega_set_mode(int);
  159.  
  160. /* Save the EGA mode */
  161. private int ega_save_mode = -1;
  162.  
  163. /* Reinitialize the EGA for text mode */
  164. int
  165. ega_close(gx_device *dev)
  166. {    if ( ega_save_mode >= 0 ) ega_set_mode(ega_save_mode);
  167.     return 0;
  168. }
  169.  
  170. /* Map a r-g-b color to an EGA color code. */
  171. /* r, g, b are 0..3. */
  172. gx_color_index
  173. ega_map_rgb_color(gx_device *dev, ushort r, ushort g, ushort b)
  174. {
  175. #ifdef DEBUG
  176.     if ( r > rgb_max || g > rgb_max || b > rgb_max )
  177.        {    eprintf3("Bad rgb values! %ud, %ud, %ud\n", r, g, b);
  178.         exit(1);
  179.        }
  180. #endif
  181. #if ega_bits_of_color == 0
  182.     static char mono_color[4] =
  183.         { black, dgray, white, bwhite };
  184.     return (gx_color_index)mono_color[((r * 3) + (g * 6) + b) >> 3];
  185. #else
  186. # if ega_bits_of_color == 1
  187.     return (gx_color_index)
  188.         ((r << 2) + (g << 1) + b +
  189.          ((r | b) << 3));
  190. # else
  191.     return (gx_color_index)
  192.         (((r & 2) << 1) + (g & 2) + ((b & 2) >> 1) +
  193.          (((r & b | g) & 1) << 3));
  194. # endif
  195. #endif
  196. }
  197.  
  198. /* Map a color code to r-g-b.  Surprisingly enough, this is algorithmic. */
  199. int
  200. ega_map_color_rgb(gx_device *dev, gx_color_index color, ushort prgb[3])
  201. {
  202. #define icolor (int)color
  203. #if rgb_max > 1
  204.     int aux = (icolor >> 3) & 1;
  205.     prgb[0] = ((icolor >> 1) & 2) + aux;
  206.     prgb[1] = (icolor & 2) + aux;
  207.     prgb[2] = ((icolor << 1) & 2) + aux;
  208. #else
  209.     prgb[0] = icolor >> 2;
  210.     prgb[1] = (icolor >> 1) & 1;
  211.     prgb[2] = icolor & 1;
  212. #endif
  213.     return 0;
  214. #undef icolor
  215. }
  216.  
  217. /* Macro for validating rectangle parameters x, y, w, h */
  218. #define validate_rect()\
  219.   if ( w <= 0 || h <= 0 ) return 0;\
  220.   if ( x < 0 || y < 0 || x + w > dev->width || y + h > dev->height ) return -1
  221.  
  222. /* ------ Internal routines ------ */
  223.  
  224. /* Read the device mode */
  225. private int
  226. ega_get_mode()
  227. {    registers regs;
  228.     regs.h.ah = 0xf;
  229.     int86(0x10, ®s, ®s);
  230.     return regs.h.al;
  231. }
  232.  
  233. /* Set the device mode */
  234. private void
  235. ega_set_mode(int mode)
  236. {    registers regs;
  237.     regs.h.ah = 0;
  238.     regs.h.al = mode;
  239.     int86(0x10, ®s, ®s);
  240. }
  241.  
  242. /* Structure for operation parameters. */
  243. /* Note that this structure is known to assembly code. */
  244. /* Not all parameters are used for every operation. */
  245. typedef struct rop_params_s {
  246.     fb_ptr dest;            /* pointer to frame buffer */
  247.     int draster;            /* raster of frame buffer */
  248.     byte far *src;            /* pointer to source data */
  249.     int sraster;            /* source raster */
  250.     int width;            /* width in bytes */
  251.     int height;            /* height in scan lines */
  252.     int shift;            /* amount to right shift source */
  253.     int invert;            /* 0 or -1 to invert source */
  254.     int data;            /* data for fill */
  255. } rop_params;
  256. typedef rop_params _ss *rop_ptr;
  257.  
  258. /* Assembly language routines */
  259.  
  260. #if USE_ASM
  261. void memsetcol(P1(rop_ptr)); /* dest, draster, height, data */
  262. #else
  263. #define memsetcol cmemsetcol
  264. private void
  265. cmemsetcol(rop_ptr rop)
  266. {    byte far *addr = rop->dest;
  267.     int yc = rop->height;
  268.     while ( yc-- )
  269.      { byte discard = *addr;
  270.        *addr = rop->data;
  271.        addr += rop->draster;
  272.      }
  273. }
  274. #endif
  275.  
  276. #if USE_ASM
  277. void memsetrect(P1(rop_ptr)); /* dest, draster, width, height, data */
  278. #else
  279. #define memsetrect cmemsetrect
  280. private void
  281. cmemsetrect(rop_ptr rop)
  282. {    byte far *addr = rop->dest;
  283.     int yc = rop->height;
  284.     while ( yc-- )
  285.      { int cnt = rop->width;
  286.        while ( cnt-- ) *addr++ = rop->data;
  287.        addr += rop->draster - rop->width;
  288.      }
  289. }
  290. #endif
  291.  
  292. #if USE_ASM
  293. void memrwcol(P1(rop_ptr)); /* dest, draster, src, sraster, height, shift, invert */
  294. #else
  295. #define memrwcol cmemrwcol
  296. private void
  297. cmemrwcol(rop_ptr rop)
  298. {    byte far *dp = rop->dest, *sp = rop->src;
  299.     int yc = rop->height;
  300.     int shift = rop->shift;
  301.     while ( yc-- )
  302.      { byte discard = *dp;
  303.        *dp = ((*sp >> shift) + (*sp << (8 - shift))) ^ rop->invert;
  304.        dp += rop->draster, sp += rop->sraster;
  305.      }
  306. }
  307. #endif
  308.  
  309. #if USE_ASM
  310. void memrwcol2(P1(rop_ptr)); /* dest, draster, src, sraster, height, shift, invert */
  311. #else
  312. #define memrwcol2 cmemrwcol2
  313. private void
  314. cmemrwcol2(rop_ptr rop)
  315. {    byte far *dp = rop->dest, *sp = rop->src;
  316.     int yc = rop->height;
  317.     int shift = rop->shift;
  318.     while ( yc-- )
  319.      { byte discard = *dp;
  320.        *dp = ((sp[1] >> shift) + (*sp << (8 - shift))) ^ rop->invert;
  321.        dp += rop->draster, sp += rop->sraster;
  322.      }
  323. }
  324. #endif
  325.  
  326. /* Forward definitions */
  327. void fill_rectangle(P4(rop_ptr, int, int, int));
  328. void fill_row(P3(byte far *, int, int));
  329.  
  330. /* Define the device port and register numbers, and the regen map base */
  331. #define out2(port, index, data)\
  332.   (outportb(port, index), outportb((port)+1, data))
  333. #define seq_addr 0x3c4
  334. #define s_map 2
  335. #define set_s_map(mask) out2(seq_addr, s_map, mask)
  336. #define graph_addr 0x3ce
  337. #define g_const 0            /* set/reset */
  338. #define set_g_const(color) out2(graph_addr, g_const, color)
  339. #define g_const_map 1            /* enable set/reset */
  340. #define set_g_const_map(map) out2(graph_addr, g_const_map, map)
  341. #define g_function 3
  342. #define set_g_function(func) out2(graph_addr, g_function, func)
  343. #  define gf_WRITE 0
  344. #  define gf_AND 8
  345. #  define gf_OR 0x10
  346. #  define gf_XOR 0x18
  347. #define g_mode 5
  348. #define set_g_mode(mode) out2(graph_addr, g_mode, mode)
  349. #  define gm_DATA 0
  350. #  define gm_FILL 2
  351. #define g_mask 8
  352. #define set_g_mask(mask) out2(graph_addr, g_mask, mask)
  353. #define regen 0xa000
  354.  
  355. /* Clean up after writing */
  356. #define dot_end()\
  357.   set_g_mask(0xff)            /* all bits on */
  358.  
  359. /* Initialize the EGA for graphics mode */
  360. int
  361. ega_open(gx_device *dev)
  362. {    if ( ega_save_mode < 0 ) ega_save_mode = ega_get_mode();
  363.     ega_set_mode(fb_dev->video_mode);
  364.     set_s_map(-1);            /* enable all maps */
  365.     return 0;
  366. }
  367.  
  368. /* Write a dot using the EGA color codes. */
  369. /* This doesn't have to be efficient. */
  370. int
  371. ega_write_dot(gx_device *dev, int x, int y, gx_color_index color)
  372. {    byte data = (byte)color;
  373.     return ega_copy_color(dev, &data, 1, 1, x, y, 1, 1);
  374. }
  375.  
  376. /* Macro for testing bit-inclusion */
  377. #define bit_included_in(x,y) !((x)&~(y))
  378.  
  379. /* Copy a monochrome bitmap.  The colors are given explicitly. */
  380. /* Color = gx_no_color_index means transparent (no effect on the image). */
  381. int
  382. ega_copy_mono(gx_device *dev, byte *base, int sourcex, int raster,
  383.   int x, int y, int w, int h, gx_color_index izero, gx_color_index ione)
  384. {    rop_params params;
  385. #define czero (int)izero
  386. #define cone (int)ione
  387.     int dleft, count;
  388.     byte mask, rmask;
  389.     fb_ptr save_dest;
  390.     int other_color = -1;
  391.     validate_rect();
  392.     while ( h > fb_dev->max_rop_height )
  393.        {    unsigned mrh = fb_dev->max_rop_height;
  394.         int code = ega_copy_mono(dev, base, sourcex, raster,
  395.                 x, y, w, mrh, izero, ione);
  396.         if ( code != 0 ) return code;
  397.         base = (byte *)((byte huge *)base +
  398.                 raster * (long)mrh);
  399.         y += mrh;
  400.         h -= mrh;
  401.        }
  402.     params.dest = mk_fb_ptr(x, y);
  403.     params.draster = fb_dev->raster;
  404.     params.src = base + (sourcex >> 3);
  405.     params.sraster = raster;
  406.     params.height = h;
  407.     params.shift = (x - sourcex) & 7;
  408.     /* Analyze the 16 possible cases: each of izero and ione may be */
  409.     /* 0, 0xf, transparent, or some other color. */
  410.     switch ( czero )
  411.        {
  412.     case no_color:
  413.         switch ( cone )
  414.            {
  415.         default:        /* (T, other) */
  416.             /* Must do 2 passes */
  417.             other_color = cone;
  418.             save_dest = params.dest;
  419.             /* falls through */
  420.         case 0:            /* (T, 0) */
  421.             set_g_function(gf_AND);
  422.             params.invert = -1;
  423.             break;
  424.         case 0xf:        /* (T, 0xf) */
  425.             set_g_function(gf_OR);
  426.             params.invert = 0;
  427.             break;
  428.         case no_color:        /* (T, T) */
  429.             return 0;    /* nothing to do */
  430.            }
  431.         break;
  432.     case 0:
  433.         params.invert = 0;
  434.         switch ( cone )
  435.            {
  436.         default:        /* (0, other) */
  437.             set_g_const(0);
  438.             set_g_const_map(cone ^ 0xf);
  439.             /* falls through */
  440.         case 0xf:        /* (0, 0xf) */
  441.             break;
  442.         case no_color:        /* (0, T) */
  443.             set_g_function(gf_AND);
  444.             break;
  445.            }
  446.         break;
  447.     case 0xf:
  448.         params.invert = -1;
  449.         switch ( cone )
  450.            {
  451.         case 0:            /* (0xf, 0) */
  452.             break;
  453.         default:        /* (0xf, other) */
  454.             set_g_const(0xf);
  455.             set_g_const_map(cone);
  456.             break;
  457.         case no_color:        /* (0xf, T) */
  458.             set_g_function(gf_OR);
  459.             /* falls through */
  460.            }
  461.         break;
  462.     default:
  463.         switch ( cone )
  464.            {
  465.         default:        /* (other, not T) */
  466.             if ( bit_included_in(czero, cone) )
  467.                {    set_g_const(czero);
  468.                 set_g_const_map(czero ^ cone ^ 0xf);
  469.                 params.invert = 0;
  470.                 break;
  471.                }
  472.             else if ( bit_included_in(cone, czero) )
  473.                {    set_g_const(cone);
  474.                 set_g_const_map(cone ^ czero ^ 0xf);
  475.                 params.invert = -1;
  476.                 break;
  477.                }
  478.             /* No way around it, fill with one color first. */
  479.             save_dest = params.dest;
  480.             fill_rectangle((rop_ptr)¶ms, x & 7, w, cone);
  481.             params.dest = save_dest;
  482.             set_g_function(gf_XOR);
  483.             set_s_map(czero ^ cone);
  484.             other_color = -2;    /* must reset s_map at end */
  485.             params.invert = -1;
  486.             break;
  487.         case no_color:        /* (other, T) */
  488.             /* Must do 2 passes */
  489.             other_color = czero;
  490.             save_dest = params.dest;
  491.             set_g_function(gf_AND);
  492.             params.invert = 0;
  493.             break;
  494.            }
  495.         break;
  496.        }
  497.     /* Actually copy the bits. */
  498.     dleft = 8 - (x & 7);
  499.     mask = 0xff >> (8 - dleft);
  500.     count = w - dleft;
  501.     if ( count < 0 )
  502.         mask -= mask >> w,
  503.         rmask = 0;
  504.     else
  505.         rmask = 0xff00 >> (count & 7);
  506.     /* params: dest, src, sraster, height, shift, invert */
  507.     /* Smashes params.src, params.dest, count. */
  508. copy:    set_g_mask(mask);
  509.     if ( params.shift == 0 )    /* optimize the aligned case */
  510.        {    /* Do left column */
  511.         memrwcol((rop_ptr)¶ms);
  512.         /* Do center */
  513.         if ( (count -= 8) >= 0 )
  514.            {    set_g_mask(0xff);
  515.             do
  516.                {    params.src++, params.dest++;
  517.                 memrwcol((rop_ptr)¶ms);
  518.                }
  519.             while ( (count -= 8) >= 0 );
  520.            }
  521.         /* Do right column */
  522.         if ( rmask )
  523.            {    params.src++, params.dest++;
  524.             set_g_mask(rmask);
  525.             memrwcol((rop_ptr)¶ms);
  526.            }
  527.        }
  528.     else
  529.        {    /* Do left column */
  530.         int sleft = 8 - (sourcex & 7);
  531.         if ( sleft >= dleft )
  532.            {    /* Source fits in one byte */
  533.             memrwcol((rop_ptr)¶ms);
  534.            }
  535.         else if ( w <= sleft )
  536.            {    /* Source fits in one byte, thin case */
  537.             memrwcol((rop_ptr)¶ms);
  538.             goto fin;
  539.            }
  540.         else
  541.            {    memrwcol2((rop_ptr)¶ms);
  542.             params.src++;
  543.            }
  544.         /* Do center */
  545.         if ( (count -= 8) >= 0 )
  546.            {    set_g_mask(0xff);
  547.             do
  548.                {    params.dest++;
  549.                 memrwcol2((rop_ptr)¶ms);
  550.                 params.src++;
  551.                }
  552.             while ( (count -= 8) >= 0 );
  553.            }
  554.         /* Do right column */
  555.         if ( rmask )
  556.            {    set_g_mask(rmask);
  557.             params.dest++;
  558.             if ( count + 8 <= params.shift )
  559.                 memrwcol((rop_ptr)¶ms);
  560.             else
  561.                 memrwcol2((rop_ptr)¶ms);
  562.            }
  563.        }
  564. fin:    if ( other_color != -1 )
  565.        {    if ( other_color >= 0 )
  566.            {    /* Do the second pass on (T, other) or (other, T). */
  567.             count = w - dleft;
  568.             params.src = base + (sourcex >> 3);
  569.             params.dest = save_dest;
  570.             params.invert ^= -1;
  571.             set_s_map(other_color);
  572.             set_g_function(gf_OR);
  573.             other_color = -2;
  574.             goto copy;
  575.            }
  576.         else
  577.            {    /* Finished second pass, restore s_map */
  578.             set_s_map(-1);
  579.            }
  580.        }
  581.     set_g_function(gf_WRITE);
  582.     set_g_const_map(0);
  583.     dot_end();
  584.     return 0;
  585. #undef czero
  586. #undef cone
  587. }
  588.  
  589. /* Copy a color pixelmap.  This is just like a bitmap, */
  590. /* except that each pixel takes 4 bits instead of 1. */
  591. int
  592. ega_copy_color(gx_device *dev, byte *base, int sourcex, int raster,
  593.   int x, int y, int w, int h)
  594. {    byte *line = base + (sourcex >> 1);
  595.     unsigned lmask = 0x80 >> (x & 7);
  596.     int bitx = sourcex & 1;
  597.     int ex = w + bitx;
  598.     fb_ptr fbline;
  599.     validate_rect();
  600.     while ( h > fb_dev->max_rop_height )
  601.        {    unsigned mrh = fb_dev->max_rop_height;
  602.         int code = ega_copy_color(dev, base, sourcex, raster,
  603.                 x, y, w, mrh);
  604.         if ( code != 0 ) return code;
  605.         base = (byte *)((byte huge *)base +
  606.                 raster * (long)mrh);
  607.         y += mrh;
  608.         h -= mrh;
  609.        }
  610.     fbline = mk_fb_ptr(x, y);
  611.     set_g_mode(gm_FILL);
  612.     outportb(graph_addr, g_mask);    /* first half of set_g_mask */
  613.     while ( --h >= 0 )
  614.     {    byte *bptr = line;
  615.         int px = bitx;
  616.         /* Turbo C will put an unsigned in a register, */
  617.         /* but not a byte! */
  618.         register unsigned mask = lmask;
  619.         fb_ptr fbptr = fbline;
  620.         do
  621.            {    byte discard = *fbptr;    /* latch frame buffer data */
  622.             outportb(graph_addr+1, mask);    /* 2nd half of set_g_mask */
  623.             *fbptr = (px & 1 ? *bptr++ : *bptr >> 4);
  624.             if ( (mask >>= 1) == 0 )
  625.                 mask = 0x80, fbptr++;
  626.            }
  627.         while ( ++px < ex );
  628.         line += raster;
  629.         fbline += fb_dev->raster;
  630.     }
  631.     set_g_mode(gm_DATA);
  632.     dot_end();
  633.     return 0;
  634. }
  635.  
  636. /* Fill a rectangle. */
  637. int
  638. ega_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  639.   gx_color_index color)
  640. {    rop_params params;
  641.     validate_rect();
  642.     while ( h > fb_dev->max_rop_height )
  643.        {    unsigned mrh = fb_dev->max_rop_height;
  644.         int code = ega_fill_rectangle(dev,
  645.             x, y, w, mrh, color);
  646.         if ( code != 0 ) return code;
  647.         y += mrh;
  648.         h -= mrh;
  649.        }
  650.     params.dest = mk_fb_ptr(x, y);
  651.     if ( h == 1 )
  652.        {    set_g_const((int)color);
  653.         fill_row(params.dest, x & 7, w);
  654.        }
  655.     else
  656.        {    params.draster = fb_dev->raster;
  657.         params.height = h;
  658.         fill_rectangle((rop_ptr)¶ms, x & 7, w, (int)color);
  659.        }
  660.     dot_end();
  661.     return 0;
  662. }
  663.  
  664. /* Tile a rectangle.  Note that the two colors must both be supplied, */
  665. /* i.e. neither one can be gx_no_color_index (transparent): */
  666. /* a transparent color means that the tile is colored, not a mask. */
  667. int
  668. ega_tile_rectangle(gx_device *dev, gx_bitmap *tile,
  669.   int x, int y, int w, int h, gx_color_index czero, gx_color_index cone,
  670.   int px, int py)
  671. #define zero (int)czero
  672. #define one (int)cone
  673. {    rop_params params;
  674.     int xmod, width_bytes;
  675.     int tile_height = tile->height;
  676.     int xbit;
  677.     int lcount;
  678.     int mask, rmask;
  679.     byte narrow;
  680.     byte again;
  681.     int const_bits, maps;
  682.     int ymod, yleft;
  683.     while ( h > fb_dev->max_rop_height )
  684.        {    int mod_height =
  685.             fb_dev->max_rop_height / tile_height * tile_height;
  686.         int code = ega_tile_rectangle(dev, tile,
  687.             x, y, w, mod_height, czero, cone, px, py);
  688.         if ( code != 0 ) return code;
  689.         y += mod_height;
  690.         h -= mod_height;
  691.        }
  692.     if ( tile->width & 7 ) return -1;    /* can't do it */
  693.     if ( one == -1 || zero == -1 ) return -1;    /* can't do colors */
  694.     if ( px || py ) return -1;    /* can't handle phase */
  695.     /* Following is similar to aligned case of copy_mono */    
  696.     validate_rect();
  697.     params.dest = mk_fb_ptr(x, y);
  698.     params.draster = fb_dev->raster;
  699.     params.sraster = tile->raster;
  700.     params.shift = 0;
  701.     xbit = x & 7;
  702.     /* Set up the graphics registers */
  703.     const_bits = (zero ^ one) ^ 0xf;
  704.     if ( const_bits )
  705.        {    set_g_const(zero);    /* either color will do */
  706.         set_g_const_map(const_bits);
  707.        }
  708.     if ( (maps = zero & ~one) != 0 )
  709.        {    set_s_map(maps += const_bits);
  710.         params.invert = -1;
  711.         again = one & ~zero;
  712.        }
  713.     else
  714.        {    maps = one & ~zero;
  715.         set_s_map(maps += const_bits);
  716.         params.invert = 0;
  717.         again = 0;
  718.        }
  719.     xmod = (x % tile->width) >> 3;
  720.     width_bytes = tile->width >> 3;
  721.     mask = 0xff >> xbit;
  722.     if ( w + xbit <= 8 )
  723.         mask -= mask >> w,
  724.         rmask = 0,
  725.         narrow = 1;
  726.     else
  727.        {    rmask = (0xff00 >> ((w + x) & 7)) & 0xff;
  728.         if ( xbit )    w += xbit - 8;
  729.         else        mask = 0, --xmod, --params.dest;
  730.         narrow = 0;
  731.        }
  732.     ymod = y % tile_height;
  733. tile:    yleft = tile_height - ymod;
  734.     params.src = tile->data + ymod * params.sraster + xmod;
  735.     lcount = h;
  736.     if ( narrow )            /* Optimize narrow case */
  737.        {    set_g_mask(mask);
  738.         if ( lcount > yleft )
  739.            {    params.height = yleft;
  740.             memrwcol((rop_ptr)¶ms);
  741.             params.dest += yleft * fb_dev->raster;
  742.             params.src = tile->data + xmod;
  743.             params.height = tile_height;
  744.             lcount -= yleft;
  745.             while ( lcount >= tile_height )
  746.                {    memrwcol((rop_ptr)¶ms);
  747.                 params.dest += tile_height * fb_dev->raster;
  748.                 lcount -= tile_height;
  749.                }
  750.            }
  751.         if ( lcount )
  752.            {    params.height = lcount;
  753.             memrwcol((rop_ptr)¶ms);
  754.            }
  755.        }
  756.     else
  757.        {    fb_ptr line = params.dest;
  758.         while ( 1 )
  759.            {    int xoff = xmod;
  760.             int count = w;
  761.             params.height = (lcount > yleft ? yleft : lcount);
  762.             /* Do first byte, if not a full byte. */
  763.             if ( mask )
  764.                {    set_g_mask(mask);
  765.                 memrwcol((rop_ptr)¶ms);
  766.                }
  767.             /* Do full bytes */
  768.             if ( (count -= 8) >= 0 )
  769.                {    set_g_mask(0xff);
  770.                 do
  771.                    {    if ( ++xoff == width_bytes )
  772.                         xoff = 0,
  773.                         params.src -= width_bytes;
  774.                     ++params.src, ++params.dest;
  775.                     memrwcol((rop_ptr)¶ms);
  776.                    }
  777.                 while ( (count -= 8) >= 0 );
  778.                }
  779.             /* Do last byte */
  780.             if ( rmask )
  781.                {    if ( ++xoff == width_bytes )
  782.                     xoff = 0,
  783.                     params.src -= width_bytes;
  784.                 set_g_mask(rmask);
  785.                 ++params.src, ++params.dest;
  786.                 memrwcol((rop_ptr)¶ms);
  787.                }
  788.             if ( (lcount -= params.height) == 0 ) break;
  789.             params.dest = line += params.height * fb_dev->raster;
  790.             params.src = tile->data + xmod;
  791.             yleft = tile_height;
  792.            }
  793.        }
  794.     /* Now do the second color if needed */
  795.     if ( again )
  796.        {    maps = again + const_bits;
  797.         set_s_map(maps);
  798.         again = 0;
  799.         params.dest = mk_fb_ptr(x, y);
  800.         if ( mask == 0 ) params.dest--;
  801.         params.invert = 0;
  802.         goto tile;
  803.        }
  804.     if ( maps != 0xf )
  805.         set_s_map(-1);
  806.     if ( const_bits )
  807.         set_g_const_map(0);
  808.     dot_end();
  809.     return 0;
  810. }
  811.  
  812. /* ------ Internal routines ------ */
  813.  
  814. /* Mask table for rectangle fill. */
  815. static byte rmask_tab[9] =
  816.    {    0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
  817.    };
  818.  
  819. /* Fill a rectangle specified by pointer into frame buffer, */
  820. /* starting bit within byte, width, and height. */
  821. /* Smashes rop->dest. */
  822. private void
  823. fill_rectangle(register rop_ptr rop, int bit, int w, int color)
  824.   /* rop: dest, draster, height */
  825. {    set_g_const(color);
  826.     set_g_const_map(0xf);
  827.     if ( bit + w <= 8 )
  828.        {    /* Less than one byte */
  829.         set_g_mask(rmask_tab[w] >> bit);
  830.         memsetcol(rop);
  831.        }
  832.     else
  833.        {    byte right_mask;
  834.         if ( bit )
  835.            {    set_g_mask(0xff >> bit);
  836.             memsetcol(rop);
  837.             rop->dest++;
  838.             w += bit - 8;
  839.            }
  840.         if ( w >= 8 )
  841.            {    int byte_count = w >> 3;
  842.             set_g_mask(0xff);    /* all bits */
  843.             rop->width = byte_count;
  844.             memsetrect(rop);
  845.             rop->dest += byte_count;
  846.            }
  847.         if ( (right_mask = rmask_tab[w & 7]) != 0 )
  848.            {    set_g_mask(right_mask);
  849.             memsetcol(rop);
  850.            }
  851.        }
  852.     set_g_const_map(0);
  853. }
  854.  
  855. /* Fill a single row specified by pointer into frame buffer, */
  856. /* starting bit within byte, and width. */
  857. /* Caller has already set g_const. */
  858. #define r_m_w(ptr) (*(ptr))++        /* read & write, data irrelevant */
  859. private void
  860. fill_row(byte far *dest, int bit, int w)
  861.   /* rop: dest */
  862. {    set_g_const_map(0xf);
  863.     if ( bit + w <= 8 )
  864.        {    /* Less than one byte */
  865.         set_g_mask(rmask_tab[w] >> bit);
  866.         r_m_w(dest);
  867.        }
  868.     else
  869.        {    byte right_mask;
  870.         if ( bit )
  871.            {    set_g_mask(0xff >> bit);
  872.             r_m_w(dest);
  873.             dest++;
  874.             w += bit - 8;
  875.            }
  876.         if ( w >= 8 )
  877.            {    int byte_count = w >> 3;
  878.             set_g_mask(0xff);    /* all bits */
  879.             memset(dest, 0, byte_count);    /* data irrelevant */
  880.             dest += byte_count;
  881.            }
  882.         if ( (right_mask = rmask_tab[w & 7]) != 0 )
  883.            {    set_g_mask(right_mask);
  884.             r_m_w(dest);
  885.            }
  886.        }
  887.     set_g_const_map(0);
  888. }
  889.