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

  1. /* Copyright (C) 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. /* gdevpcx.c */
  20. /* PCX file format devices for Ghostscript */
  21. #include "gdevprn.h"
  22. #include "gdevpccm.h"
  23. #include "gxlum.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.  * Default X and Y resolution.
  32.  */
  33. #define X_DPI 72
  34. #define Y_DPI 72
  35.  
  36. /* Monochrome. */
  37.  
  38. private dev_proc_print_page(pcxmono_print_page);
  39.  
  40. /* Use the default RGB->color map, so we get black=0, white=1. */
  41. private gx_device_procs pcxmono_procs =
  42.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  43.     gx_default_map_rgb_color, gx_default_map_color_rgb);
  44. gx_device_printer far_data gs_pcxmono_device =
  45.   prn_device(pcxmono_procs, "pcxmono",
  46.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  47.     X_DPI, Y_DPI,
  48.     0,0,0,0,            /* margins */
  49.     1, pcxmono_print_page);
  50.  
  51. /* Chunky 8-bit gray scale. */
  52.  
  53. private dev_proc_print_page(pcx256_print_page);
  54.  
  55. private gx_device_procs pcxgray_procs =
  56.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  57.     gx_default_gray_map_rgb_color, gx_default_gray_map_color_rgb);
  58. gx_device_printer far_data gs_pcxgray_device =
  59.   prn_device(pcxgray_procs, "pcxgray",
  60.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  61.     X_DPI, Y_DPI,
  62.     0,0,0,0,            /* margins */
  63.     8, pcx256_print_page);
  64.  
  65. /* 4-bit planar (EGA/VGA-style) color. */
  66.  
  67. private dev_proc_print_page(pcx16_print_page);
  68.  
  69. private gx_device_procs pcx16_procs =
  70.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  71.     pc_4bit_map_rgb_color, pc_4bit_map_color_rgb);
  72. gx_device_printer far_data gs_pcx16_device =
  73.   prn_device(pcx16_procs, "pcx16",
  74.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  75.     X_DPI, Y_DPI,
  76.     0,0,0,0,            /* margins */
  77.     4, pcx16_print_page);
  78.  
  79. /* Chunky 8-bit (SuperVGA-style) color. */
  80. /* (Uses a fixed palette of 3,3,2 bits.) */
  81.  
  82. private gx_device_procs pcx256_procs =
  83.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  84.     pc_8bit_map_rgb_color, pc_8bit_map_color_rgb);
  85. gx_device_printer far_data gs_pcx256_device =
  86.   prn_device(pcx256_procs, "pcx256",
  87.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  88.     X_DPI, Y_DPI,
  89.     0,0,0,0,            /* margins */
  90.     8, pcx256_print_page);
  91.  
  92. /* 24-bit color, 3 8-bit planes. */
  93.  
  94. private dev_proc_print_page(pcx24b_print_page);
  95.  
  96. private gx_device_procs pcx24b_procs =
  97.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  98.     gx_default_rgb_map_rgb_color, gx_default_rgb_map_color_rgb);
  99. gx_device_printer far_data gs_pcx24b_device =
  100.   prn_device(pcx24b_procs, "pcx24b",
  101.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  102.     X_DPI, Y_DPI,
  103.     0,0,0,0,            /* margins */
  104.     24, pcx24b_print_page);
  105.  
  106. /* ------ Private definitions ------ */
  107.  
  108. /* All two-byte quantities are stored LSB-first! */
  109. #if arch_is_big_endian
  110. #  define assign_ushort(a,v) a = ((v) >> 8) + ((v) << 8)
  111. #else
  112. #  define assign_ushort(a,v) a = (v)
  113. #endif
  114.  
  115. typedef struct pcx_header_s {
  116.     byte     manuf;        /* always 0x0a */
  117.     byte    version;    /* version info = 0,2,3,5 */
  118.     byte    encoding;    /* 1=RLE */
  119.     byte    bpp;        /* bits per pixel */
  120.     ushort    x1;        /* picture dimensions */
  121.     ushort    y1;
  122.     ushort    x2;
  123.     ushort    y2;
  124.     ushort    hres;        /* horz. resolution */
  125.     ushort    vres;        /* vert. resolution */
  126.     byte    palette[16*3];    /* color palette */
  127.     byte    reserved;
  128.     byte    nplanes;    /* number of color planes */
  129.     ushort    bpl;        /* number of bytes per line (uncompressed) */
  130.     ushort    palinfo;    /* palette info 1=color, 2=grey */
  131.     byte    xtra[58];    /* fill out header to 128 bytes */
  132. } pcx_header;
  133.  
  134. /* 
  135. ** version info for PCX is as follows 
  136. **
  137. ** 0 == 2.5
  138. ** 2 == 2.8 w/palette info
  139. ** 3 == 2.8 without palette info
  140. ** 5 == 3.0 (includes palette)
  141. **
  142. */
  143.  
  144. /* Forward declarations */
  145. private void pcx_write_rle(P3(const byte *, const byte *, FILE *));
  146. private int pcx_write_page(P4(gx_device_printer *, FILE *, pcx_header _ss *, int));
  147.  
  148. /* Write a monochrome PCX page. */
  149. private int
  150. pcxmono_print_page(gx_device_printer *pdev, FILE *file)
  151. {    pcx_header header;
  152.     header.bpp = 1;
  153.     header.nplanes = 1;
  154.     /* Set the first two entries of the short palette. */
  155.     memcpy((byte *)header.palette, "\000\000\000\377\377\377", 6);
  156.     return pcx_write_page(pdev, file, &header, 0);
  157. }
  158.  
  159. /* Write an "old" PCX page. */
  160. static const byte ega_palette[16*3] = {
  161.   0x00,0x00,0x00,  0x00,0x00,0xaa,  0x00,0xaa,0x00,  0x00,0xaa,0xaa,
  162.   0xaa,0x00,0x00,  0xaa,0x00,0xaa,  0xaa,0xaa,0x00,  0xaa,0xaa,0xaa,
  163.   0x55,0x55,0x55,  0x55,0x55,0xff,  0x55,0xff,0x55,  0x55,0xff,0xff,
  164.   0xff,0x55,0x55,  0xff,0x55,0xff,  0xff,0xff,0x55,  0xff,0xff,0xff
  165. };
  166. private int
  167. pcx16_print_page(gx_device_printer *pdev, FILE *file)
  168. {    pcx_header header;
  169.     header.bpp = 1;
  170.     header.nplanes = 4;
  171.     /* Fill the EGA palette appropriately. */
  172.     memcpy((byte *)header.palette, ega_palette, sizeof(ega_palette));
  173.     return pcx_write_page(pdev, file, &header, 1);
  174. }
  175.  
  176. /* Write a "new" PCX page. */
  177. private int
  178. pcx256_print_page(gx_device_printer *pdev, FILE *file)
  179. {    pcx_header header;
  180.     int code;
  181.     header.bpp = 8;
  182.     header.nplanes = 1;
  183.     /* Clear the EGA palette */
  184.     memset((byte *)header.palette, 0, sizeof(header.palette));
  185.     code = pcx_write_page(pdev, file, &header, 0);
  186.     if ( code >= 0 )
  187.     {    /* Write out the palette. */
  188.         fputc(0x0c, file);
  189.         code = pc_write_palette((gx_device *)pdev, 256, file);
  190.     }
  191.     return code;
  192. }
  193.  
  194. /* Write a 24-bit color PCX page. */
  195.  
  196. private int
  197. pcx24b_print_page(gx_device_printer *pdev, FILE *file)
  198. {    pcx_header header;
  199.     header.bpp = 8;
  200.     header.nplanes = 3;
  201.     /* Clear the EGA palette */
  202.     memset((byte *)header.palette, 0, sizeof(header.palette));
  203.     return pcx_write_page(pdev, file, &header, 1);
  204. }
  205.  
  206. /* Write out a page in PCX format. */
  207. /* This routine is used for all formats. */
  208. private int
  209. pcx_write_page(gx_device_printer *pdev, FILE *file, pcx_header _ss *phdr,
  210.   int planar)
  211. {    int raster = gdev_prn_raster(pdev);
  212.     uint rsize = round_up((pdev->width * phdr->bpp + 7) >> 3, 2);    /* PCX format requires even */
  213.     int height = pdev->height;
  214.     int depth = pdev->color_info.depth;
  215.     uint lsize = raster + rsize;
  216.     byte *line = (byte *)gs_malloc(lsize, 1, "pcx file buffer");
  217.     byte *plane = line + raster;
  218.     int y;
  219.     int code = 0;            /* return code */
  220.     if ( line == 0 )        /* can't allocate line buffer */
  221.         return_error(gs_error_VMerror);
  222.  
  223.     /* Set up the header struct. */
  224.  
  225.     phdr->manuf = 10;
  226.     phdr->version = 5;
  227.     phdr->encoding = 1;    /* 1 for rle 8-bit encoding */
  228.     /* bpp was set by the caller */
  229.     phdr->x1 = 0;
  230.     phdr->y1 = 0;
  231.     assign_ushort(phdr->x2, pdev->width-1);
  232.     assign_ushort(phdr->y2, height-1);
  233.     assign_ushort(phdr->hres, (int)pdev->x_pixels_per_inch);
  234.     assign_ushort(phdr->vres, (int)pdev->y_pixels_per_inch);
  235.     phdr->reserved = 0;
  236.     /* nplanes was set by the caller */
  237.     assign_ushort(phdr->bpl, (planar || depth == 1 ? rsize : raster));
  238.     assign_ushort(phdr->palinfo, (gx_device_has_color(pdev) ? 1 : 2));
  239.     /* Zero out the unused area. */
  240.     memset(&phdr->xtra[0], 0, sizeof(phdr->xtra));
  241.  
  242.     /* Write the header. */
  243.  
  244.     if ( fwrite((const char *)phdr, 1, 128, file) < 128 )
  245.     {    code = gs_error_ioerror;
  246.         goto pcx_done;
  247.     }
  248.  
  249.     /* Dump the contents of the image. */
  250.     for ( y = 0; y < height; y++ )
  251.     {    byte *row;
  252.         byte *end;
  253.         int code = gdev_prn_get_bits(pdev, y, line, &row);
  254.         if ( code < 0 ) break;
  255.         end = row + raster;
  256.         if ( !planar )
  257.         {    /* Just write the bits. */
  258.             end += raster & 1;    /* round to even */
  259.             pcx_write_rle(row, end, file);
  260.         }
  261.         else
  262.           switch ( depth )
  263.         {
  264.  
  265.         case 8:
  266.         {    register int shift;
  267.  
  268.             if ( !gx_device_has_color(pdev) )
  269.                {    /* Can't map gray scale */
  270.                 code = gs_error_undefinedresult;
  271.                 goto pcx_done;
  272.                }
  273.  
  274.             for ( shift = 0; shift < 4; shift++ )
  275.             {    register byte *from, *to;
  276.                 register int bmask = 1 << shift;
  277.                 for ( from = row, to = plane;
  278.                       from < end; from += 8
  279.                     )
  280.                 {    *to++ =
  281.                       ((((uint)from[0] & bmask) << 7) +
  282.                        (((uint)from[1] & bmask) << 6) +
  283.                        (((uint)from[2] & bmask) << 5) +
  284.                        (((uint)from[3] & bmask) << 4) +
  285.                        (((uint)from[4] & bmask) << 3) +
  286.                        (((uint)from[5] & bmask) << 2) +
  287.                        (((uint)from[6] & bmask) << 1) +
  288.                        (((uint)from[7] & bmask)))
  289.                       >> shift;
  290.                 }
  291.                 pcx_write_rle(plane, plane + rsize, file);
  292.             }
  293.         }
  294.             break;
  295.  
  296.         case 24:
  297.         {    byte *crow = row;
  298.             for ( ; crow != row + 3; crow++ )
  299.             {    register byte *from, *to;
  300.                 for ( from = crow, to = plane;
  301.                       from < end; to += 2, from += 6
  302.                     )
  303.                   *to = *from,
  304.                   to[1] = from[3];
  305.                 if ( raster & 1 )
  306.                   to[-1] = to[-2];    /* as good as any */
  307.                 pcx_write_rle(plane, plane + rsize, file);
  308.             }
  309.         }
  310.             break;
  311.  
  312.         default:
  313.               code = gs_error_undefinedresult;
  314.               goto pcx_done;
  315.  
  316.         }
  317.     }
  318.  
  319. pcx_done:
  320.     gs_free((char *)line, lsize, 1, "pcx file buffer");
  321.  
  322.     return code;
  323. }
  324.  
  325. /* ------ Internal routines ------ */
  326.  
  327. /* Write one line in PCX run-length-encoded format. */
  328. private void
  329. pcx_write_rle(const byte *from, const byte *end, FILE *file)
  330. {    while ( from < end )
  331.     {    byte data = *from++;
  332.         if ( data != *from || from == end )
  333.           {    if ( data >= 0xc0 )
  334.               putc(0xc1, file);
  335.           }
  336.         else
  337.           {    const byte *next = (end - from > 62 ? from + 62 : end);
  338.             const byte *start = from;
  339.             while ( (from < next) && (*from == data) )
  340.               from++;
  341.             putc(from - start + 0xc1, file);
  342.           }
  343.         putc(data, file);
  344.     }
  345. }
  346.