home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevpcx.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  13.7 KB  |  463 lines

  1. /* Copyright (C) 1992, 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevpcx.c,v 1.2 2000/09/19 19:00:17 lpd Exp $ */
  20. /* PCX file format drivers */
  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 const 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. const gx_device_printer 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 const 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. const gx_device_printer gs_pcxgray_device =
  59. {prn_device_body(gx_device_printer, pcxgray_procs, "pcxgray",
  60.          DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  61.          X_DPI, Y_DPI,
  62.          0, 0, 0, 0,    /* margins */
  63.          1, 8, 255, 0, 256, 0, pcx256_print_page)
  64. };
  65.  
  66. /* 4-bit planar (EGA/VGA-style) color. */
  67.  
  68. private dev_proc_print_page(pcx16_print_page);
  69.  
  70. private const gx_device_procs pcx16_procs =
  71. prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  72.         pc_4bit_map_rgb_color, pc_4bit_map_color_rgb);
  73. const gx_device_printer gs_pcx16_device =
  74. {prn_device_body(gx_device_printer, pcx16_procs, "pcx16",
  75.          DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  76.          X_DPI, Y_DPI,
  77.          0, 0, 0, 0,    /* margins */
  78.          3, 4, 3, 2, 4, 3, pcx16_print_page)
  79. };
  80.  
  81. /* Chunky 8-bit (SuperVGA-style) color. */
  82. /* (Uses a fixed palette of 3,3,2 bits.) */
  83.  
  84. private const gx_device_procs pcx256_procs =
  85. prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  86.         pc_8bit_map_rgb_color, pc_8bit_map_color_rgb);
  87. const gx_device_printer gs_pcx256_device =
  88. {prn_device_body(gx_device_printer, pcx256_procs, "pcx256",
  89.          DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  90.          X_DPI, Y_DPI,
  91.          0, 0, 0, 0,    /* margins */
  92.          3, 8, 6, 6, 7, 7, pcx256_print_page)
  93. };
  94.  
  95. /* 24-bit color, 3 8-bit planes. */
  96.  
  97. private dev_proc_print_page(pcx24b_print_page);
  98.  
  99. private const gx_device_procs pcx24b_procs =
  100. prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  101.         gx_default_rgb_map_rgb_color, gx_default_rgb_map_color_rgb);
  102. const gx_device_printer gs_pcx24b_device =
  103. prn_device(pcx24b_procs, "pcx24b",
  104.        DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  105.        X_DPI, Y_DPI,
  106.        0, 0, 0, 0,        /* margins */
  107.        24, pcx24b_print_page);
  108.  
  109. /* 4-bit chunky CMYK color. */
  110.  
  111. private dev_proc_print_page(pcxcmyk_print_page);
  112.  
  113. private const gx_device_procs pcxcmyk_procs =
  114. {
  115.     gdev_prn_open,
  116.     NULL,            /* get_initial_matrix */
  117.     NULL,            /* sync_output */
  118.     gdev_prn_output_page,
  119.     gdev_prn_close,
  120.     NULL,            /* map_rgb_color */
  121.     cmyk_1bit_map_color_rgb,
  122.     NULL,            /* fill_rectangle */
  123.     NULL,            /* tile_rectangle */
  124.     NULL,            /* copy_mono */
  125.     NULL,            /* copy_color */
  126.     NULL,            /* draw_line */
  127.     NULL,            /* get_bits */
  128.     gdev_prn_get_params,
  129.     gdev_prn_put_params,
  130.     cmyk_1bit_map_cmyk_color,
  131.     NULL,            /* get_xfont_procs */
  132.     NULL,            /* get_xfont_device */
  133.     NULL,            /* map_rgb_alpha_color */
  134.     gx_page_device_get_page_device
  135. };
  136. const gx_device_printer gs_pcxcmyk_device =
  137. {prn_device_body(gx_device_printer, pcxcmyk_procs, "pcxcmyk",
  138.          DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  139.          X_DPI, Y_DPI,
  140.          0, 0, 0, 0,    /* margins */
  141.          4, 4, 1, 1, 2, 2, pcxcmyk_print_page)
  142. };
  143.  
  144. /* ------ Private definitions ------ */
  145.  
  146. /* All two-byte quantities are stored LSB-first! */
  147. #if arch_is_big_endian
  148. #  define assign_ushort(a,v) a = ((v) >> 8) + ((v) << 8)
  149. #else
  150. #  define assign_ushort(a,v) a = (v)
  151. #endif
  152.  
  153. typedef struct pcx_header_s {
  154.     byte manuf;            /* always 0x0a */
  155.     byte version;
  156. #define version_2_5            0
  157. #define version_2_8_with_palette    2
  158. #define version_2_8_without_palette    3
  159. #define version_3_0 /* with palette */    5
  160.     byte encoding;        /* 1=RLE */
  161.     byte bpp;            /* bits per pixel per plane */
  162.     ushort x1;            /* X of upper left corner */
  163.     ushort y1;            /* Y of upper left corner */
  164.     ushort x2;            /* x1 + width - 1 */
  165.     ushort y2;            /* y1 + height - 1 */
  166.     ushort hres;        /* horz. resolution (dots per inch) */
  167.     ushort vres;        /* vert. resolution (dots per inch) */
  168.     byte palette[16 * 3];    /* color palette */
  169.     byte reserved;
  170.     byte nplanes;        /* number of color planes */
  171.     ushort bpl;            /* number of bytes per line (uncompressed) */
  172.     ushort palinfo;
  173. #define palinfo_color    1
  174. #define palinfo_gray    2
  175.     byte xtra[58];        /* fill out header to 128 bytes */
  176. } pcx_header;
  177.  
  178. /* Define the prototype header. */
  179. private const pcx_header pcx_header_prototype =
  180. {
  181.     10,                /* manuf */
  182.     0,                /* version (variable) */
  183.     1,                /* encoding */
  184.     0,                /* bpp (variable) */
  185.     00, 00,            /* x1, y1 */
  186.     00, 00,            /* x2, y2 (variable) */
  187.     00, 00,            /* hres, vres (variable) */
  188.     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    /* palette (variable) */
  189.      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  190.      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  191.      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  192.     0,                /* reserved */
  193.     0,                /* nplanes (variable) */
  194.     00,                /* bpl (variable) */
  195.     00,                /* palinfo (variable) */
  196.     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    /* xtra */
  197.      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  198.      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  199.      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  200. };
  201.  
  202. /*
  203.  * Define the DCX header.  We don't actually use this yet.
  204.  * All quantities are stored little-endian!
  205.  bytes 0-3: ID = 987654321
  206.  bytes 4-7: file offset of page 1
  207.  [... up to 1023 entries ...]
  208.  bytes N-N+3: 0 to mark end of page list
  209.  * This is followed by the pages in order, each of which is a PCX file.
  210.  */
  211. #define dcx_magic 987654321
  212. #define dcx_max_pages 1023
  213.  
  214. /* Forward declarations */
  215. private void pcx_write_rle(P4(const byte *, const byte *, int, FILE *));
  216. private int pcx_write_page(P4(gx_device_printer *, FILE *, pcx_header *, bool));
  217.  
  218. /* Write a monochrome PCX page. */
  219. private int
  220. pcxmono_print_page(gx_device_printer * pdev, FILE * file)
  221. {
  222.     pcx_header header;
  223.  
  224.     header = pcx_header_prototype;
  225.     header.version = version_2_8_with_palette;
  226.     header.bpp = 1;
  227.     header.nplanes = 1;
  228.     assign_ushort(header.palinfo, palinfo_gray);
  229.     /* Set the first two entries of the short palette. */
  230.     memcpy((byte *) header.palette, "\000\000\000\377\377\377", 6);
  231.     return pcx_write_page(pdev, file, &header, false);
  232. }
  233.  
  234. /* Write an "old" PCX page. */
  235. static const byte pcx_ega_palette[16 * 3] =
  236. {
  237.     0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0x00, 0xaa, 0xaa,
  238.     0xaa, 0x00, 0x00, 0xaa, 0x00, 0xaa, 0xaa, 0xaa, 0x00, 0xaa, 0xaa, 0xaa,
  239.     0x55, 0x55, 0x55, 0x55, 0x55, 0xff, 0x55, 0xff, 0x55, 0x55, 0xff, 0xff,
  240.     0xff, 0x55, 0x55, 0xff, 0x55, 0xff, 0xff, 0xff, 0x55, 0xff, 0xff, 0xff
  241. };
  242. private int
  243. pcx16_print_page(gx_device_printer * pdev, FILE * file)
  244. {
  245.     pcx_header header;
  246.  
  247.     header = pcx_header_prototype;
  248.     header.version = version_2_8_with_palette;
  249.     header.bpp = 1;
  250.     header.nplanes = 4;
  251.     /* Fill the EGA palette appropriately. */
  252.     memcpy((byte *) header.palette, pcx_ega_palette,
  253.        sizeof(pcx_ega_palette));
  254.     return pcx_write_page(pdev, file, &header, true);
  255. }
  256.  
  257. /* Write a "new" PCX page. */
  258. private int
  259. pcx256_print_page(gx_device_printer * pdev, FILE * file)
  260. {
  261.     pcx_header header;
  262.     int code;
  263.  
  264.     header = pcx_header_prototype;
  265.     header.version = version_3_0;
  266.     header.bpp = 8;
  267.     header.nplanes = 1;
  268.     assign_ushort(header.palinfo,
  269.           (pdev->color_info.num_components > 1 ?
  270.            palinfo_color : palinfo_gray));
  271.     code = pcx_write_page(pdev, file, &header, false);
  272.     if (code >= 0) {        /* Write out the palette. */
  273.     fputc(0x0c, file);
  274.     code = pc_write_palette((gx_device *) pdev, 256, file);
  275.     }
  276.     return code;
  277. }
  278.  
  279. /* Write a 24-bit color PCX page. */
  280. private int
  281. pcx24b_print_page(gx_device_printer * pdev, FILE * file)
  282. {
  283.     pcx_header header;
  284.  
  285.     header = pcx_header_prototype;
  286.     header.version = version_3_0;
  287.     header.bpp = 8;
  288.     header.nplanes = 3;
  289.     assign_ushort(header.palinfo, palinfo_color);
  290.     return pcx_write_page(pdev, file, &header, true);
  291. }
  292.  
  293. /* Write a 4-bit chunky CMYK color PCX page. */
  294. static const byte pcx_cmyk_palette[16 * 3] =
  295. {
  296.     0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x0f, 0x0f, 0x00,
  297.     0xff, 0x00, 0xff, 0x0f, 0x00, 0x0f, 0xff, 0x00, 0x00, 0x0f, 0x00, 0x00,
  298.     0x00, 0xff, 0xff, 0x00, 0x0f, 0x0f, 0x00, 0xff, 0x00, 0x00, 0x0f, 0x00,
  299.     0x00, 0x00, 0xff, 0x00, 0x00, 0x0f, 0x1f, 0x1f, 0x1f, 0x0f, 0x0f, 0x0f,
  300. };
  301. private int
  302. pcxcmyk_print_page(gx_device_printer * pdev, FILE * file)
  303. {
  304.     pcx_header header;
  305.  
  306.     header = pcx_header_prototype;
  307.     header.version = 2;
  308.     header.bpp = 4;
  309.     header.nplanes = 1;
  310.     /* Fill the palette appropriately. */
  311.     memcpy((byte *) header.palette, pcx_cmyk_palette,
  312.        sizeof(pcx_cmyk_palette));
  313.     return pcx_write_page(pdev, file, &header, false);
  314. }
  315.  
  316. /* Write out a page in PCX format. */
  317. /* This routine is used for all formats. */
  318. /* The caller has set header->bpp, nplanes, and palette. */
  319. private int
  320. pcx_write_page(gx_device_printer * pdev, FILE * file, pcx_header * phdr,
  321.            bool planar)
  322. {
  323.     int raster = gdev_prn_raster(pdev);
  324.     uint rsize = ROUND_UP((pdev->width * phdr->bpp + 7) >> 3, 2);    /* PCX format requires even */
  325.     int height = pdev->height;
  326.     int depth = pdev->color_info.depth;
  327.     uint lsize = raster + rsize;
  328.     byte *line = gs_alloc_bytes(pdev->memory, lsize, "pcx file buffer");
  329.     byte *plane = line + raster;
  330.     int y;
  331.     int code = 0;        /* return code */
  332.  
  333.     if (line == 0)        /* can't allocate line buffer */
  334.     return_error(gs_error_VMerror);
  335.  
  336.     /* Fill in the other variable entries in the header struct. */
  337.  
  338.     assign_ushort(phdr->x2, pdev->width - 1);
  339.     assign_ushort(phdr->y2, height - 1);
  340.     assign_ushort(phdr->hres, (int)pdev->x_pixels_per_inch);
  341.     assign_ushort(phdr->vres, (int)pdev->y_pixels_per_inch);
  342.     assign_ushort(phdr->bpl, (planar || depth == 1 ? rsize :
  343.                   raster + (raster & 1)));
  344.  
  345.     /* Write the header. */
  346.  
  347.     if (fwrite((const char *)phdr, 1, 128, file) < 128) {
  348.     code = gs_error_ioerror;
  349.     goto pcx_done;
  350.     }
  351.     /* Write the contents of the image. */
  352.     for (y = 0; y < height; y++) {
  353.     byte *row;
  354.     byte *end;
  355.  
  356.     code = gdev_prn_get_bits(pdev, y, line, &row);
  357.     if (code < 0)
  358.         break;
  359.     end = row + raster;
  360.     if (!planar) {        /* Just write the bits. */
  361.         if (raster & 1) {    /* Round to even, with predictable padding. */
  362.         *end = end[-1];
  363.         ++end;
  364.         }
  365.         pcx_write_rle(row, end, 1, file);
  366.     } else
  367.         switch (depth) {
  368.  
  369.         case 4:
  370.             {
  371.             byte *pend = plane + rsize;
  372.             int shift;
  373.  
  374.             for (shift = 0; shift < 4; shift++) {
  375.                 register byte *from, *to;
  376.                 register int bright = 1 << shift;
  377.                 register int bleft = bright << 4;
  378.  
  379.                 for (from = row, to = plane;
  380.                  from < end; from += 4
  381.                 ) {
  382.                 *to++ =
  383.                     (from[0] & bleft ? 0x80 : 0) |
  384.                     (from[0] & bright ? 0x40 : 0) |
  385.                     (from[1] & bleft ? 0x20 : 0) |
  386.                     (from[1] & bright ? 0x10 : 0) |
  387.                     (from[2] & bleft ? 0x08 : 0) |
  388.                     (from[2] & bright ? 0x04 : 0) |
  389.                     (from[3] & bleft ? 0x02 : 0) |
  390.                     (from[3] & bright ? 0x01 : 0);
  391.                 }
  392.                 /* We might be one byte short of rsize. */
  393.                 if (to < pend)
  394.                 *to = to[-1];
  395.                 pcx_write_rle(plane, pend, 1, file);
  396.             }
  397.             }
  398.             break;
  399.  
  400.         case 24:
  401.             {
  402.             int pnum;
  403.  
  404.             for (pnum = 0; pnum < 3; ++pnum) {
  405.                 pcx_write_rle(row + pnum, row + raster, 3, file);
  406.                 if (pdev->width & 1)
  407.                 fputc(0, file);        /* pad to even */
  408.             }
  409.             }
  410.             break;
  411.  
  412.         default:
  413.             code = gs_note_error(gs_error_rangecheck);
  414.             goto pcx_done;
  415.  
  416.         }
  417.     }
  418.  
  419.   pcx_done:
  420.     gs_free_object(pdev->memory, line, "pcx file buffer");
  421.  
  422.     return code;
  423. }
  424.  
  425. /* ------ Internal routines ------ */
  426.  
  427. /* Write one line in PCX run-length-encoded format. */
  428. private void
  429. pcx_write_rle(const byte * from, const byte * end, int step, FILE * file)
  430. {                /*
  431.                  * The PCX format theoretically allows encoding runs of 63
  432.                  * identical bytes, but some readers can't handle repetition
  433.                  * counts greater than 15.
  434.                  */
  435. #define MAX_RUN_COUNT 15
  436.     int max_run = step * MAX_RUN_COUNT;
  437.  
  438.     while (from < end) {
  439.     byte data = *from;
  440.  
  441.     from += step;
  442.     if (data != *from || from == end) {
  443.         if (data >= 0xc0)
  444.         putc(0xc1, file);
  445.     } else {
  446.         const byte *start = from;
  447.  
  448.         while ((from < end) && (*from == data))
  449.         from += step;
  450.         /* Now (from - start) / step + 1 is the run length. */
  451.         while (from - start >= max_run) {
  452.         putc(0xc0 + MAX_RUN_COUNT, file);
  453.         putc(data, file);
  454.         start += max_run;
  455.         }
  456.         if (from > start || data >= 0xc0)
  457.         putc((from - start) / step + 0xc1, file);
  458.     }
  459.     putc(data, file);
  460.     }
  461. #undef MAX_RUN_COUNT
  462. }
  463.