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 / GDEVPCX.C < prev    next >
C/C++ Source or Header  |  1992-09-07  |  8KB  |  300 lines

  1. /* Copyright (C) 1992 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. /* gdevpcx.c */
  21. /* PCX file format devices for Ghostscript */
  22. #include "gdevprn.h"
  23. #include "gdevpccm.h"
  24.  
  25. /* Thanks to Phil Conrad for donating the original version */
  26. /* of these drivers to Aladdin Enterprises. */
  27.  
  28. /* ------ The device descriptors ------ */
  29.  
  30. /*
  31.  * Standard U.S. page width and height.  A4 paper is 8.4" x 11.7".
  32.  */
  33. #define WIDTH_10THS 85
  34. #define HEIGHT_10THS 110
  35.  
  36. /*
  37.  * Default X and Y resolution.
  38.  */
  39. #define X_DPI 72
  40. #define Y_DPI 72
  41.  
  42. /* Monochrome. */
  43.  
  44. private dev_proc_print_page(pcxmono_print_page);
  45.  
  46. gx_device_printer gs_pcxmono_device =
  47.   prn_device(prn_std_procs, "pcxmono",
  48.     WIDTH_10THS, HEIGHT_10THS,
  49.     X_DPI, Y_DPI,
  50.     0,0,0,0,            /* margins */
  51.     1, pcxmono_print_page);
  52.  
  53. /* 4-bit planar (EGA/VGA-style) color. */
  54.  
  55. private dev_proc_print_page(pcx16_print_page);
  56.  
  57. private gx_device_procs pcx16_procs =
  58.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  59.     pc_4bit_map_rgb_color, pc_4bit_map_color_rgb);
  60. gx_device_printer gs_pcx16_device =
  61.   prn_device(pcx16_procs, "pcx16",
  62.     WIDTH_10THS, HEIGHT_10THS,
  63.     X_DPI, Y_DPI,
  64.     0,0,0,0,            /* margins */
  65.     4, pcx16_print_page);
  66.  
  67. /* Chunky 8-bit (SuperVGA-style) color. */
  68. /* (Uses a fixed palette of 3,3,2 bits.) */
  69.  
  70. private dev_proc_print_page(pcx256_print_page);
  71.  
  72. private gx_device_procs pcx256_procs =
  73.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  74.     pc_8bit_map_rgb_color, pc_8bit_map_color_rgb);
  75. gx_device_printer gs_pcx256_device =
  76.   prn_device(pcx256_procs, "pcx256",
  77.     WIDTH_10THS, HEIGHT_10THS,
  78.     X_DPI, Y_DPI,
  79.     0,0,0,0,            /* margins */
  80.     8, pcx256_print_page);
  81.  
  82. /* ------ Private definitions ------ */
  83.  
  84. /* All two-byte quantities are stored LSB-first! */
  85. #if arch_is_big_endian
  86. #  define assign_ushort(a,v) a = ((v) >> 8) + ((v) << 8)
  87. #else
  88. #  define assign_ushort(a,v) a = (v)
  89. #endif
  90.  
  91. typedef struct pcx_header_s {
  92.     byte     manuf;        /* always 0x0a */
  93.     byte    version;    /* version info = 0,2,3,5 */
  94.     byte    encoding;    /* 1=RLE */
  95.     byte    bpp;        /* bits per pixel */
  96.     ushort    x1;        /* picture dimensions */
  97.     ushort    y1;
  98.     ushort    x2;
  99.     ushort    y2;
  100.     ushort    hres;        /* horz. resolution */
  101.     ushort    vres;        /* vert. resolution */
  102.     byte    palette[16*3];    /* color palette */
  103.     byte    vmode;        /* video mode for graphics board */
  104.     byte    nplanes;    /* number of color planes */
  105.     ushort    bpl;        /* number of bytes per line (uncompresses) */
  106.     ushort    palinfo;    /* palette info 1=color, 2=grey */
  107.     ushort    shres;        /* scanner horz. resolution */
  108.     ushort    svres;        /* scanner vert. resolution */
  109.     byte    xtra[58];    /* fill out header to 128 bytes */
  110. } pcx_header;
  111.  
  112. /* 
  113. ** version info for PCX is as follows 
  114. **
  115. ** 0 == 2.5
  116. ** 2 == 2.8 w/palette info
  117. ** 3 == 2.8 without palette info
  118. ** 5 == 3.0
  119. **
  120. */
  121.  
  122. /* Forward declarations */
  123. private void pcx_write_rle(P3(const byte *, const byte *, FILE *));
  124. private int pcx_write_page(P4(gx_device_printer *, FILE *, pcx_header _ss *, int));
  125.  
  126. /* Write an "old" PCX page. */
  127. static const byte ega_palette[16*3] = {
  128.   0x00,0x00,0x00,  0x00,0x00,0xaa,  0x00,0xaa,0x00,  0x00,0xaa,0xaa,
  129.   0xaa,0x00,0x00,  0xaa,0x00,0xaa,  0xaa,0xaa,0x00,  0xaa,0xaa,0xaa,
  130.   0x55,0x55,0x55,  0x55,0x55,0xff,  0x55,0xff,0x55,  0x55,0xff,0xff,
  131.   0xff,0x55,0x55,  0xff,0x55,0xff,  0xff,0xff,0x55,  0xff,0xff,0xff
  132. };
  133. private int
  134. pcx16_print_page(gx_device_printer *pdev, FILE *file)
  135. {    pcx_header header;
  136.     header.version = 2;
  137.     header.bpp = 1;
  138.     header.nplanes = 4;
  139.     /* Fill the EGA palette appropriately. */
  140.     memcpy((byte *)header.palette, ega_palette, sizeof(ega_palette));
  141.     return pcx_write_page(pdev, file, &header, 1);
  142. }
  143.  
  144. /* Write a "new" PCX page. */
  145. private int
  146. pcx256_print_page(gx_device_printer *pdev, FILE *file)
  147. {    pcx_header header;
  148.     int code;
  149.     header.version = 5;
  150.     header.bpp = 8;
  151.     header.nplanes = 1;
  152.     /* Clear the EGA palette */
  153.     memset((byte *)header.palette, 0, sizeof(header.palette));
  154.     code = pcx_write_page(pdev, file, &header, 0);
  155.     if ( code >= 0 )
  156.     {    /* Write out the palette. */
  157.         fputc(0x0c, file);
  158.         code = pc_write_palette((gx_device *)pdev, 256, file);
  159.     }
  160.     return code;
  161. }
  162.  
  163. /* Write a monochrome PCX page. */
  164. private int
  165. pcxmono_print_page(gx_device_printer *pdev, FILE *file)
  166. {    pcx_header header;
  167.     header.version = 2;
  168.     header.bpp = 1;
  169.     header.nplanes = 1;
  170.     /* Clear the EGA palette */
  171.     memset((byte *)header.palette, 0, sizeof(header.palette));
  172.     return pcx_write_page(pdev, file, &header, 0);
  173. }
  174.  
  175. /* Write out a page in PCX format. */
  176. /* This routine is used for all three formats (monochrome, planar */
  177. /* "8-bit" actually 4-bit color, and chunky 8-bit color.) */
  178. private int
  179. pcx_write_page(gx_device_printer *pdev, FILE *file, pcx_header _ss *phdr,
  180.   int planar)
  181. {    int raster = gdev_prn_raster(pdev);
  182.     int height = pdev->height;
  183.     int depth = pdev->color_info.depth;
  184.     uint rsize = (pdev->width + 7) >> 3;
  185.     byte *row = (byte *)gs_malloc(raster + rsize, 1, "pcx file buffer");
  186.     byte *end = row + raster;
  187.     byte *plane = end;
  188.     int y;
  189.     int code = 0;            /* return code */
  190.     if ( row == 0 )            /* can't allocate row buffer */
  191.         return_error(gs_error_VMerror);
  192.  
  193.     /* setup the header struct */
  194.  
  195.     phdr->manuf = 10;
  196.     /* version and bpp were set by the caller */
  197.     phdr->encoding = 1;    /* 1 for rle 8-bit encoding */
  198.     phdr->x1 = 0;
  199.     phdr->y1 = 0;
  200.     assign_ushort(phdr->x2, pdev->width-1);
  201.     assign_ushort(phdr->y2, height-1);
  202.     assign_ushort(phdr->hres, (int)pdev->x_pixels_per_inch);
  203.     assign_ushort(phdr->vres, (int)pdev->y_pixels_per_inch);
  204.     phdr->vmode = 0;
  205.     /* nplanes was set by the caller */
  206.     assign_ushort(phdr->bpl, (planar && depth > 1 ? rsize : raster));
  207.     assign_ushort(phdr->palinfo, (gx_device_has_color(pdev) ? 1 : 2));
  208.  
  209.     /* Write the header. */
  210.  
  211.     if ( fwrite((const char *)phdr, 1, 128, file) < 128 )
  212.        {    code = gs_error_ioerror;
  213.         goto pcx_done;
  214.        }
  215.  
  216.     /* Dump the contents of the image. */
  217.     for ( y = 0; y < height; y++ )
  218.        {    gdev_prn_copy_scan_lines(pdev, y, row, raster);
  219.         switch ( depth )
  220.            {
  221.         case 1:
  222.            {    /* PCX assumes 0=black, 1=white. */
  223.             register byte *bp;
  224.             for ( bp = row; bp < end; bp++ )
  225.                 *bp = ~*bp;
  226.             pcx_write_rle(row, end, file);
  227.            }
  228.             break;
  229.         case 8:
  230.            {    register int shift;
  231.  
  232.             if ( !gx_device_has_color(pdev) )
  233.                {    /* Can't map gray scale */
  234.                 code = gs_error_undefinedresult;
  235.                 goto pcx_done;
  236.                }
  237.  
  238.             if ( !planar )
  239.                {    /* Just write the bits */
  240.                 pcx_write_rle(row, end, file);
  241.                 break;
  242.                }
  243.  
  244.             for ( shift = 0; shift < 4; shift++ )
  245.             {    register byte *from, *to;
  246.                 register int bmask = 1 << shift;
  247.                 for ( from = row, to = plane;
  248.                       from < end; from += 8
  249.                     )
  250.                 {    *to++ =
  251.                       ((((uint)from[0] & bmask) << 7) +
  252.                        (((uint)from[1] & bmask) << 6) +
  253.                        (((uint)from[2] & bmask) << 5) +
  254.                        (((uint)from[3] & bmask) << 4) +
  255.                        (((uint)from[4] & bmask) << 3) +
  256.                        (((uint)from[5] & bmask) << 2) +
  257.                        (((uint)from[6] & bmask) << 1) +
  258.                        (((uint)from[7] & bmask)))
  259.                       >> shift;
  260.                 }
  261.                 pcx_write_rle(plane, to, file);
  262.             }
  263.            }
  264.             break;
  265.  
  266.         default:
  267.               code = gs_error_undefinedresult;
  268.               goto pcx_done;
  269.  
  270.            }
  271.        }
  272.  
  273. pcx_done:
  274.     gs_free((char *)row, raster + rsize, 1, "pcx file buffer");
  275.  
  276.     return code;
  277. }
  278.  
  279. /* ------ Internal routines ------ */
  280.  
  281. /* Write one line in PCX run-length-encoded format. */
  282. private void
  283. pcx_write_rle(const byte *from, const byte *end, FILE *file)
  284. {    while ( from < end )
  285.     {    byte data = *from++;
  286.         int count = 1;
  287.         while ( (from < end) && (*from == data) )
  288.             count++, from++;
  289.         while ( count > 63 )
  290.         {    putc(0xff, file);
  291.             putc(data, file);
  292.             count -= 63;
  293.         }
  294.         if ( count != 1 || data >= 0xc0 )
  295.         {    putc(count | 0xc0, file);
  296.         }
  297.         putc(data, file);
  298.     }
  299. }
  300.