home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / GDEVPE.C < prev    next >
C/C++ Source or Header  |  1992-09-07  |  8KB  |  373 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. /* gdevpe.c  Private Eye display driver for Ghostscript
  21.    Hacked by Fran Taylor, Reflection Technology Inc. */
  22.  
  23. #include "memory_.h"
  24. #include "gx.h"
  25. #include "gxdevice.h"
  26.  
  27. char *getenv(char *name);
  28.  
  29. typedef struct gx_device_pe_s {
  30.     gx_device_common;
  31.     byte *fbaddr;
  32.     unsigned regs;
  33. } gx_device_pe;
  34. #define pedev ((gx_device_pe *)dev)
  35.  
  36. typedef struct {
  37.     ushort reg, val;
  38. } regval;
  39.  
  40. #define XSIZE 720
  41. #define YSIZE 280
  42. #define BPL 90
  43. #define XPPI 160.0
  44. #define YPPI 96.0
  45. #define DEFAULT_ADDRESS ((byte *) 0xb8000000)
  46. #define DEFAULT_REGISTERS 0x3d0
  47.  
  48. dev_proc_open_device(pe_open);
  49. dev_proc_close_device(pe_close);
  50. dev_proc_fill_rectangle(pe_fill_rectangle);
  51. dev_proc_copy_mono(pe_copy_mono);
  52.  
  53. private gx_device_procs pe_procs =
  54. {    pe_open,
  55.     gx_default_get_initial_matrix,
  56.     gx_default_sync_output,
  57.     gx_default_output_page,
  58.     pe_close,
  59.     gx_default_map_rgb_color,
  60.     gx_default_map_color_rgb,
  61.     pe_fill_rectangle,
  62.     gx_default_tile_rectangle,
  63.     pe_copy_mono,
  64.     gx_default_copy_color,
  65.     gx_default_draw_line,
  66.     gx_default_get_bits,
  67.     gx_default_get_props,
  68.     gx_default_put_props
  69. };
  70.  
  71. gx_device_pe gs_pe_device = 
  72. {    sizeof(gx_device_pe),
  73.     &pe_procs,
  74.     "Private Eye",
  75.     XSIZE, YSIZE,
  76.     XPPI, YPPI,
  77.     no_margins,
  78.     dci_black_and_white,
  79.     0,
  80.     DEFAULT_ADDRESS, DEFAULT_REGISTERS
  81. };
  82.  
  83. static regval peinit[] = {{0x04, 0x1e}, {0x05, 0x00},
  84.               {0x04, 0x0c}, {0x05, 0x21},
  85.               {0x04, 0x0d}, {0x05, 0x98},
  86.               {0x08, 0x00}, {0x08, 0x1e},
  87.               {0x04, 0x1e}, {0x05, 0x01}};
  88.  
  89. static regval pedone[] = {{0x04, 0x1e}, {0x05, 0x10},
  90.               {0x04, 0x0a}, {0x05, 0x00},
  91.               {0x04, 0x0b}, {0x05, 0x07},
  92.               {0x04, 0x0c}, {0x05, 0x00},
  93.               {0x04, 0x0d}, {0x05, 0x00},
  94.               {0x04, 0x0e}, {0x05, 0x00},
  95.               {0x04, 0x0f}, {0x05, 0x00},
  96.               {0x08, 0x00}, {0x08, 0x29}};
  97.  
  98. int pe_open(gx_device *dev)
  99. {
  100.     char *str;
  101.     int i;
  102.  
  103.     if ((str = getenv("PEFBADDR")) != 0)
  104.     {
  105.         if (!sscanf(str, "%lx", &(pedev->fbaddr)))
  106.         {
  107.             eprintf("Private Eye: PEFBADDR environment string format error\n");
  108.             exit(1);
  109.         }
  110.     }
  111.  
  112.     if ((str = getenv("PEREGS")) != 0)
  113.     {
  114.         if (!sscanf(str, "%x", &(pedev->regs)))
  115.         {
  116.             eprintf("Private Eye: PEREGS environment string format error\n");
  117.             exit(1);
  118.         }
  119.     }
  120.  
  121.     for (i = 0; i < 10; i++)
  122.         outportb(pedev->regs + peinit[i].reg, peinit[i].val);
  123.  
  124.     return 0;
  125. }
  126.  
  127. int pe_close(gx_device *dev)
  128. {
  129.     int i;
  130.  
  131.     /* restore the screen */
  132.     for (i = 0; i < 16; i++)
  133.         outportb(pedev->regs + pedone[i].reg, pedone[i].val);
  134.  
  135.     /* clear the frame buffer */
  136.     memset(pedev->fbaddr, 0, 4000);
  137.  
  138.     return 0;
  139. }
  140.  
  141. int pe_fill_rectangle(gx_device *dev, int x1, int y1, int w, int h,
  142.                       gx_color_index color)
  143. {
  144.     int x2, y2, xlen;
  145.     byte led, red, d;
  146.     byte *ptr;
  147.  
  148.     /* cull */
  149.  
  150.     if ((w <= 0) || (h <= 0) || (x1 > XSIZE) || (y1 > YSIZE))
  151.         return 0;
  152.  
  153.     x2 = x1 + w - 1;
  154.     y2 = y1 + h - 1;
  155.  
  156.     /* cull some more */
  157.  
  158.     if ((x2 < 0) || (y2 < 0))
  159.         return 0;
  160.  
  161.     /* clip */
  162.  
  163.     if (x1 < 0) x1 = 0;
  164.     if (x2 > XSIZE-1) x2 = XSIZE-1;
  165.     if (y1 < 0) y1 = 0;
  166.     if (y2 > YSIZE-1) y2 = YSIZE-1;
  167.  
  168.     w = x2 - x1 + 1;
  169.     h = y2 - y1 + 1;
  170.     xlen = (x2 >> 3) - (x1 >> 3) - 1;
  171.     led = 0xff >> (x1 & 7);
  172.     red = 0xff << (7 - (x2 & 7));
  173.  
  174.     ptr = pedev->fbaddr + (y1 * BPL) + (x1 >> 3);
  175.  
  176.     if (color)
  177.     {
  178.         /* here to set pixels */
  179.         
  180.         if (xlen == -1)
  181.         {
  182.             /* special for rectangles that fit in a byte */
  183.             
  184.             d = led & red;
  185.             for(; h >= 0; h--, ptr += BPL)
  186.                 *ptr |= d;
  187.             return 0;
  188.         }
  189.         
  190.         /* normal fill */
  191.         
  192.         for(; h >= 0; h--, ptr += BPL)
  193.         {    register int x = xlen;
  194.             register byte *p = ptr;
  195.             *p++ |= led;
  196.             while ( x-- ) *p++ = 0xff;
  197.             *p |= red;
  198.         }
  199.     }
  200.  
  201.     /* here to clear pixels */
  202.  
  203.     led = ~led;
  204.     red = ~red;
  205.  
  206.     if (xlen == -1)
  207.     {
  208.         /* special for rectangles that fit in a byte */
  209.         
  210.         d = led | red;
  211.         for(; h >= 0; h--, ptr += BPL)
  212.             *ptr &= d;
  213.         return 0;
  214.     }
  215.  
  216.     /* normal fill */
  217.         
  218.     for(; h >= 0; h--, ptr += BPL)
  219.     {    register int x = xlen;
  220.         register byte *p = ptr;
  221.         *p++ &= led;
  222.         while ( x-- ) *p++ = 0x00;
  223.         *p &= red;
  224.     }
  225.     return 0;
  226. }
  227.  
  228. int pe_copy_mono(gx_device *dev,
  229.          const byte *base, int sourcex, int raster, gx_bitmap_id id,
  230.                  int x, int y, int w, int h, 
  231.          gx_color_index zero, gx_color_index one)
  232. {
  233.     const byte *line;
  234.     int sleft, dleft;
  235.     int mask, rmask;
  236.     int invert, zmask, omask;
  237.     byte *dest;
  238.     int offset;
  239.  
  240. #define izero (int)zero
  241. #define ione (int)one
  242.  
  243. if ( ione == izero )        /* vacuous case */
  244.         return pe_fill_rectangle(dev, x, y, w, h, zero);
  245.  
  246.     /* clip */
  247.  
  248.     if ((x > XSIZE) || (y > YSIZE) || ((x + w) < 0) || ((y + h) < 0))
  249.         return 0;
  250.  
  251.     offset = x >> 3;
  252.     dest = pedev->fbaddr + (y * BPL) + offset;
  253.     line = base + (sourcex >> 3);
  254.     sleft = 8 - (sourcex & 7);
  255.     dleft = 8 - (x & 7);
  256.     mask = 0xff >> (8 - dleft);
  257.     if ( w < dleft )
  258.         mask -= mask >> w;
  259.     else
  260.         rmask = 0xff00 >> ((w - dleft) & 7);
  261.  
  262.     /* Macros for writing partial bytes. */
  263.     /* bits has already been inverted by xor'ing with invert. */
  264.  
  265. #define write_byte_masked(ptr, bits, mask)\
  266.   *ptr = ((bits | ~mask | zmask) & *ptr | (bits & mask & omask))
  267.  
  268. #define write_byte(ptr, bits)\
  269.   *ptr = ((bits | zmask) & *ptr | (bits & omask))
  270.  
  271. /*    if ( dev->invert )
  272.     {
  273.         if ( izero != (int)gx_no_color_index ) zero ^= 1;
  274.         if ( ione != (int)gx_no_color_index ) one ^= 1;
  275.     } */
  276.     invert = (izero == 1 || ione == 0 ? -1 : 0);
  277.     zmask = (izero == 0 || ione == 0 ? 0 : -1);
  278.     omask = (izero == 1 || ione == 1 ? -1 : 0);
  279.  
  280. #undef izero
  281. #undef ione
  282.  
  283.     if (sleft == dleft)        /* optimize the aligned case */
  284.     {
  285.         w -= dleft;
  286.         while ( --h >= 0 )
  287.         {
  288.             register const byte *bptr = line;
  289.             int count = w;
  290.             register byte *optr = dest;
  291.             register int bits = *bptr ^ invert;    /* first partial byte */
  292.             
  293.             write_byte_masked(optr, bits, mask);
  294.             
  295.             /* Do full bytes. */
  296.             
  297.             while ((count -= 8) >= 0)
  298.             {
  299.                 bits = *++bptr ^ invert;
  300.                 ++optr;
  301.                 write_byte(optr, bits);
  302.             }
  303.             
  304.             /* Do last byte */
  305.             
  306.             if (count > -8)
  307.             {
  308.                 bits = *++bptr ^ invert;
  309.                 ++optr;
  310.                 write_byte_masked(optr, bits, rmask);
  311.             }
  312.             dest += BPL;
  313.             line += raster;
  314.         }
  315.     }
  316.     else
  317.     {
  318.         int skew = (sleft - dleft) & 7;
  319.         int cskew = 8 - skew;
  320.         
  321.         while (--h >= 0)
  322.         {
  323.             const byte *bptr = line;
  324.             int count = w;
  325.             byte *optr = dest;
  326.             register int bits;
  327.             
  328.             /* Do the first partial byte */
  329.             
  330.             if (sleft >= dleft)
  331.             {
  332.                 bits = *bptr >> skew;
  333.             }    
  334.             else /* ( sleft < dleft ) */
  335.             {
  336.                 bits = *bptr++ << cskew;
  337.                 if (count > sleft)
  338.                     bits += *bptr >> skew;
  339.             }
  340.             bits ^= invert;
  341.             write_byte_masked(optr, bits, mask);
  342.             count -= dleft;
  343.             optr++;
  344.             
  345.             /* Do full bytes. */
  346.             
  347.             while ( count >= 8 )
  348.             {
  349.                 bits = *bptr++ << cskew;
  350.                 bits += *bptr >> skew;
  351.                 bits ^= invert;
  352.                 write_byte(optr, bits);
  353.                 count -= 8;
  354.                 optr++;
  355.             }
  356.             
  357.             /* Do last byte */
  358.             
  359.             if (count > 0)
  360.             {
  361.                 bits = *bptr++ << cskew;
  362.                  if (count > skew)
  363.                     bits += *bptr >> skew;
  364.                 bits ^= invert;
  365.                 write_byte_masked(optr, bits, rmask);
  366.             }
  367.             dest += BPL;
  368.             line += raster;
  369.         }
  370.     }
  371.     return 0;
  372. }
  373.