home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GDEVCDJ.C < prev    next >
C/C++ Source or Header  |  1994-07-30  |  52KB  |  1,571 lines

  1. /* Copyright (C) 1991, 1992, 1993, 1994 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. /* gdevcdj.c */
  20. /* H-P colour printer drivers for Ghostscript */
  21. #include "std.h"        /* to stop stdlib.h redefining types */
  22. #include <stdlib.h>        /* for rand() */
  23. #include "gdevprn.h"
  24. #include "gdevpcl.h"
  25. #include "gsparam.h"
  26. #include "gsstate.h"
  27.  
  28. /***
  29.  *** This file contains multiple drivers.  The main body of code, and all
  30.  *** but the DesignJet driver, were contributed by George Cameron;
  31.  *** please contact g.cameron@biomed.abdn.ac.uk if you have questions.
  32.  *     1 - cdj500:    HP DeskJet 500C
  33.  *     2 - cdj550:    HP DeskJet 550C
  34.  *     3 - pjxl300:    HP PaintJet XL300
  35.  *     4 - pj:        HP PaintJet
  36.  *     5 - pjxl:    HP PaintJet XL
  37.  *     6 - declj250:    DEC LJ250
  38.  *** The DesignJet 650C driver was contributed by Koert Zeilstra;
  39.  *** please contact koert@digex.net if you have questions.
  40.  *     7 - dnj650c      HP DesignJet 650C
  41.  ***/
  42.  
  43. /*
  44.  * All of these drivers have 8-bit (monochrome), 16-bit and 24-bit
  45.  *     (colour) and for the DJ 550C 32-bit, (colour, cmyk mode)
  46.  *     options in addition to the usual 1-bit and 3-bit modes
  47.  * It is also possible to set various printer-specific parameters
  48.  *     from the gs command line, eg.
  49.  *
  50.  *  gs -sDEVICE=cdj550 -dBitsPerPixel=16 -dDepletion=1 -dShingling=2 tiger.ps
  51.  *
  52.  * Please consult the appropriate section in the devices.doc file for
  53.  * further details on all these drivers.
  54.  */
  55.  
  56. #define DESKJET_PRINT_LIMIT  0.04    /* 'real' top margin? */
  57. #define PAINTJET_PRINT_LIMIT 0.0    /* This is a guess.. */
  58.  
  59. /* Margins are left, bottom, right, top. */
  60. #define DESKJET_MARGINS_LETTER   0.25, 0.50, 0.25, 0.167
  61. #define DESKJET_MARGINS_A4       0.125, 0.50, 0.143, 0.167
  62. /* The PaintJet and DesignJet seem to have the same margins */
  63. /* regardless of paper size. */
  64. #define PAINTJET_MARGINS         0.167, 0.167, 0.167, 0.167
  65. #define DESIGNJET_MARGINS        0.167, 0.167, 0.167, 0.167
  66.  
  67. /* Default page size is US-Letter or A4 (other sizes from command line) */
  68. #ifndef A4
  69. #  define WIDTH_10THS            85
  70. #  define HEIGHT_10THS           110
  71. #else
  72. #  define WIDTH_10THS            83      /* 210mm */
  73. #  define HEIGHT_10THS           117     /* 297mm */
  74. #endif
  75.  
  76. /* Define bits-per-pixel for generic drivers - default is 24-bit mode */
  77. #ifndef BITSPERPIXEL
  78. #  define BITSPERPIXEL 24
  79. #endif
  80.  
  81. #define W sizeof(word)
  82. #define I sizeof(int)
  83.  
  84. /* Printer types */
  85. #define DJ500C   0
  86. #define DJ550C   1
  87. #define PJXL300  2
  88. #define PJ180    3
  89. #define PJXL180  4
  90. #define DECLJ250 5
  91. #define DNJ650C  6
  92.  
  93. /* No. of ink jets (used to minimise head movements) */
  94. #define HEAD_ROWS_MONO 50
  95. #define HEAD_ROWS_COLOUR 16
  96.  
  97. /* Colour mapping procedures */
  98. private dev_proc_map_rgb_color (gdev_pcl_map_rgb_color);
  99. private dev_proc_map_color_rgb (gdev_pcl_map_color_rgb);
  100.  
  101. /* Print-page, parameters and miscellaneous procedures */
  102. private dev_proc_open_device(dj500c_open);
  103. private dev_proc_open_device(dj550c_open);
  104. private dev_proc_open_device(dnj650c_open);
  105. private dev_proc_open_device(pj_open);
  106. private dev_proc_open_device(pjxl_open);
  107. private dev_proc_open_device(pjxl300_open);
  108. private dev_proc_print_page(dj500c_print_page);
  109. private dev_proc_print_page(dj550c_print_page);
  110. private dev_proc_print_page(dnj650c_print_page);
  111. private dev_proc_print_page(pj_print_page);
  112. private dev_proc_print_page(pjxl_print_page);
  113. private dev_proc_print_page(pjxl300_print_page);
  114. private dev_proc_print_page(declj250_print_page);
  115. private dev_proc_get_params(cdj_get_params);
  116. private dev_proc_get_params(pj_get_params);
  117. private dev_proc_get_params(pjxl_get_params);
  118. private dev_proc_put_params(cdj_put_params);
  119. private dev_proc_put_params(pj_put_params);
  120. private dev_proc_put_params(pjxl_put_params);
  121.  
  122. /* The device descriptors */
  123. typedef struct gx_device_cdj_s gx_device_cdj;
  124. struct gx_device_cdj_s {
  125.     gx_device_common;
  126.     gx_prn_device_common;
  127.     int correction;           /* Black correction parameter */
  128.     int shingling;          /* Interlaced, multi-pass printing */
  129.     int depletion;          /* 'Intelligent' dot-removal */
  130. };
  131.  
  132. typedef struct gx_device_pjxl_s gx_device_pjxl;
  133. struct gx_device_pjxl_s {
  134.     gx_device_common;
  135.     gx_prn_device_common;
  136.     uint correction;          /* Black correction parameter */
  137.     int printqual;            /* Mechanical print quality */
  138.     int rendertype;           /* Driver or printer dithering control */
  139. };
  140.  
  141. typedef struct gx_device_hp_s gx_device_hp;
  142. struct gx_device_hp_s {
  143.     gx_device_common;
  144.     gx_prn_device_common;
  145.     uint correction;          /* Black correction parameter
  146.                    * (used only by DJ500C) */
  147. };
  148.  
  149. typedef struct gx_device_hp_s gx_device_pj;
  150.  
  151. #define hp_device ((gx_device_hp *)pdev)
  152. #define cdj       ((gx_device_cdj *)pdev)
  153. #define pjxl      ((gx_device_pjxl *)pdev)
  154. #define pj    ((gx_device_pj *)pdev)
  155.  
  156. #define prn_hp_colour_device(procs, dev_name, x_dpi, y_dpi, bpp, print_page)\
  157.     prn_device_body(gx_device_printer, procs, dev_name,\
  158.     WIDTH_10THS, HEIGHT_10THS, x_dpi, y_dpi, 0, 0, 0, 0, 0,\
  159.     bpp, 0, 0, 0, 0, print_page)
  160.  
  161. #define cdj_device(procs, dev_name, x_dpi, y_dpi, bpp, print_page, correction, shingling, depletion)\
  162. { prn_hp_colour_device(procs, dev_name, x_dpi, y_dpi, bpp, print_page),\
  163.     correction,\
  164.     shingling,\
  165.     depletion\
  166. }
  167.  
  168. #define pjxl_device(procs, dev_name, x_dpi, y_dpi, bpp, print_page, printqual, rendertype)\
  169. { prn_hp_colour_device(procs, dev_name, x_dpi, y_dpi, bpp, print_page), 0, \
  170.     printqual,\
  171.     rendertype\
  172. }
  173.  
  174. #define pj_device(procs, dev_name, x_dpi, y_dpi, bpp, print_page)\
  175. { prn_hp_colour_device(procs, dev_name, x_dpi, y_dpi, bpp, print_page), 0\
  176. }
  177.  
  178. #define hp_colour_procs(proc_colour_open, proc_get_params, proc_put_params) {\
  179.     proc_colour_open,\
  180.     gdev_pcl_get_initial_matrix,\
  181.     gx_default_sync_output,\
  182.     gdev_prn_output_page,\
  183.     gdev_prn_close,\
  184.     gdev_pcl_map_rgb_color,\
  185.     gdev_pcl_map_color_rgb,\
  186.     NULL,    /* fill_rectangle */\
  187.     NULL,    /* tile_rectangle */\
  188.     NULL,    /* copy_mono */\
  189.     NULL,    /* copy_color */\
  190.     NULL,    /* draw_line */\
  191.     gx_default_get_bits,\
  192.     proc_get_params,\
  193.     proc_put_params\
  194. }
  195.  
  196. private gx_device_procs cdj500_procs =
  197. hp_colour_procs(dj500c_open, cdj_get_params, cdj_put_params);
  198.  
  199. private gx_device_procs cdj550_procs =
  200. hp_colour_procs(dj550c_open, cdj_get_params, cdj_put_params);
  201.  
  202. private gx_device_procs dnj650c_procs =
  203. hp_colour_procs(dnj650c_open, cdj_get_params, cdj_put_params);
  204.  
  205. private gx_device_procs pj_procs =
  206. hp_colour_procs(pj_open, pj_get_params, pj_put_params);
  207.  
  208. private gx_device_procs pjxl_procs =
  209. hp_colour_procs(pjxl_open, pjxl_get_params, pjxl_put_params);
  210.  
  211. private gx_device_procs pjxl300_procs =
  212. hp_colour_procs(pjxl300_open, pjxl_get_params, pjxl_put_params);
  213.  
  214. gx_device_cdj far_data gs_cdjmono_device =
  215. cdj_device(cdj500_procs, "cdjmono", 300, 300, 1,
  216.        dj500c_print_page, 4, 0, 1);
  217.  
  218. gx_device_cdj far_data gs_cdeskjet_device =
  219. cdj_device(cdj500_procs, "cdeskjet", 300, 300, 3,
  220.        dj500c_print_page, 4, 2, 1);
  221.  
  222. gx_device_cdj far_data gs_cdjcolor_device =
  223. cdj_device(cdj500_procs, "cdjcolor", 300, 300, 24,
  224.        dj500c_print_page, 4, 2, 1);
  225.  
  226. gx_device_cdj far_data gs_cdj500_device =
  227. cdj_device(cdj500_procs, "cdj500", 300, 300, BITSPERPIXEL,
  228.        dj500c_print_page, 4, 2, 1);
  229.  
  230. gx_device_cdj far_data gs_cdj550_device =
  231. cdj_device(cdj550_procs, "cdj550", 300, 300, BITSPERPIXEL,
  232.        dj550c_print_page, 0, 2, 1);
  233.  
  234. gx_device_cdj far_data gs_dnj650c_device =
  235. cdj_device(dnj650c_procs, "dnj650c", 300, 300, BITSPERPIXEL,
  236.        dnj650c_print_page, 0, 2, 1);
  237.  
  238. gx_device_pj far_data gs_declj250_device =
  239. pj_device(pj_procs, "declj250", 180, 180, BITSPERPIXEL,
  240.        declj250_print_page);
  241.  
  242. gx_device_pj far_data gs_pj_device =
  243. pj_device(pj_procs, "pj", 180, 180, BITSPERPIXEL,
  244.        pj_print_page);
  245.  
  246. gx_device_pjxl far_data gs_pjxl_device =
  247. pjxl_device(pjxl_procs, "pjxl", 180, 180, BITSPERPIXEL,
  248.        pjxl_print_page, 0, 0);
  249.  
  250. gx_device_pjxl far_data gs_pjxl300_device =
  251. pjxl_device(pjxl300_procs, "pjxl300", 300, 300, BITSPERPIXEL,
  252.        pjxl300_print_page, 0, 0);
  253.  
  254. /* Forward references */
  255. private int gdev_pcl_mode1compress(P3(const byte *, const byte *, byte *));
  256. private int gdev_pcl_mode9compress(P4(int, const byte *, const byte *, byte *));
  257. private int hp_colour_open(P2(gx_device *, int));
  258. private int hp_colour_print_page(P3(gx_device_printer *, FILE *, int));
  259. private int near put_param_int(P6(gs_param_list *, gs_param_name, int *, int, int, int));
  260. private uint gdev_prn_rasterwidth(P2(const gx_device_printer *, int));
  261. private void set_bpp(P2(gx_device *, int));
  262. private void expand_line(P4(word *, int, int, int));
  263.  
  264. /* Open the printer and set up the margins. */
  265. private int
  266. dj500c_open(gx_device *pdev)
  267. { return hp_colour_open(pdev, DJ500C);
  268. }
  269.  
  270. private int
  271. dj550c_open(gx_device *pdev)
  272. { return hp_colour_open(pdev, DJ550C);
  273. }
  274.  
  275. private int
  276. dnj650c_open(gx_device *pdev)
  277. { return hp_colour_open(pdev, DNJ650C);
  278. }
  279.  
  280. private int
  281. pjxl300_open(gx_device *pdev)
  282. { return hp_colour_open(pdev, PJXL300);
  283. }
  284.  
  285. private int
  286. pj_open(gx_device *pdev)
  287. { return hp_colour_open(pdev, PJ180);
  288. }
  289.  
  290. private int
  291. pjxl_open(gx_device *pdev)
  292. { return hp_colour_open(pdev, PJXL180);
  293. }
  294.  
  295. private int
  296. hp_colour_open(gx_device *pdev, int ptype)
  297. {    /* Change the margins if necessary. */
  298.   static const float dj_a4[4] = { DESKJET_MARGINS_A4 };
  299.   static const float dj_letter[4] = { DESKJET_MARGINS_LETTER };
  300.   static const float pj_all[4] = { PAINTJET_MARGINS };
  301.   static const float dnj_all[4] = { DESIGNJET_MARGINS };
  302.   const float _ds *m;
  303.  
  304.   /* Set up colour params if put_params has not already done so */
  305.   if (pdev->color_info.num_components == 0)
  306.     set_bpp(pdev, pdev->color_info.depth);
  307.  
  308.   switch (ptype) {
  309.   case DJ500C:
  310.   case DJ550C:
  311.     m = (gdev_pcl_paper_size(pdev) == PAPER_SIZE_A4 ? dj_a4 :
  312.      dj_letter);
  313.     break;
  314.   case DNJ650C:
  315.     m = dnj_all;
  316.     break;
  317.   case PJ180:
  318.   case PJXL300:
  319.   case PJXL180:
  320.     m = pj_all;
  321.     break;
  322.   }
  323.   pdev->l_margin = m[0];
  324.   pdev->b_margin = m[1];
  325.   pdev->r_margin = m[2];
  326.   pdev->t_margin = m[3];
  327.   return gdev_prn_open(pdev);
  328. }
  329.  
  330. /* Added parameters for DeskJet 5xxC */
  331.  
  332. /* Get parameters.  In addition to the standard and printer 
  333.  * parameters, we supply shingling and depletion parameters,
  334.  * and control over the bits-per-pixel used in output rendering */
  335. private int
  336. cdj_get_params(gx_device *pdev, gs_param_list *plist)
  337. {    int code = gdev_prn_get_params(pdev, plist);
  338.     code < 0 ||
  339.     (code = param_write_int(plist, "BlackCorrect", &cdj->correction)) < 0 ||
  340.     (code = param_write_int(plist, "Shingling", &cdj->shingling)) < 0 ||
  341.     (code = param_write_int(plist, "Depletion", &cdj->depletion)) < 0 ||
  342.     (code = param_write_int(plist, "BitsPerPixel", &cdj->color_info.depth)) < 0;
  343.     return code;
  344. }
  345.  
  346. /* Put parameters. */
  347. private int
  348. cdj_put_params(gx_device *pdev, gs_param_list *plist)
  349. {    int old_bpp = cdj->color_info.depth;
  350.     int bpp = 0;
  351.     int code = 0;
  352.  
  353.     code = gdev_prn_put_params(pdev, plist);
  354.     code = put_param_int(plist, "BlackCorrect", &cdj->correction, 0, 9, code);
  355.     code = put_param_int(plist, "Shingling", &cdj->shingling, 0, 2, code);
  356.     code = put_param_int(plist, "Depletion", &cdj->depletion, 1, 3, code);
  357.     code = put_param_int(plist, "BitsPerPixel", &bpp, 1, 32, code);
  358.  
  359.     if ( code < 0 )
  360.       return code;
  361.  
  362.     if (bpp != 0) {
  363.       set_bpp(pdev, bpp);
  364.       
  365.       /* Close the device; gs_putdeviceparams will reopen it. */
  366.       if ( bpp != old_bpp && pdev->is_open )
  367.         { int ccode = gs_closedevice(pdev);
  368.           if ( ccode < 0 ) return ccode;
  369.         }
  370.     }
  371.     
  372.     return code;
  373. }
  374.  
  375. /* Added parameters for PaintJet XL and PaintJet XL300 */
  376.  
  377. /* Get parameters.  In addition to the standard and printer
  378.  * parameters, we supply print_quality and render_type 
  379.  * parameters, together with bpp control. */
  380. private int
  381. pjxl_get_params(gx_device *pdev, gs_param_list *plist)
  382. {    int code = gdev_prn_get_params(pdev, plist);
  383.     code < 0 ||
  384.     (code = param_write_int(plist, "PrintQuality", &pjxl->printqual)) < 0 ||
  385.     (code = param_write_int(plist, "RenderType", &pjxl->rendertype)) < 0 ||
  386.     (code = param_write_int(plist, "BitsPerPixel", &pjxl->color_info.depth)) < 0;
  387.     return code;
  388. }
  389.  
  390. /* Put parameters. */
  391. private int
  392. pjxl_put_params(gx_device *pdev, gs_param_list *plist)
  393. {    int old_bpp = pjxl->color_info.depth;
  394.     int bpp = 0;
  395.     int code = 0;
  396.  
  397.     code = gdev_prn_put_params(pdev, plist);
  398.     code = put_param_int(plist, "PrintQuality", &pjxl->printqual, -1, 1, code);
  399.     code = put_param_int(plist, "RenderType", &pjxl->rendertype, 0, 10, code);
  400.     code = put_param_int(plist, "BitsPerPixel", &bpp, 1, 32, code);
  401.  
  402.     if ( code < 0 )
  403.       return code;
  404.  
  405.     if (pjxl->rendertype > 0) {
  406.       /* If printer is doing the dithering, we must have a
  407.        * true-colour mode, ie. 16 or 24 bits per pixel */
  408.       if ((bpp == 0 && old_bpp < 16) || (bpp > 0 && bpp < 16))
  409.         bpp = 24;
  410.     }
  411.  
  412.     if (bpp != 0) {
  413.       set_bpp(pdev, bpp);
  414.       
  415.       /* Close the device; gs_putdeviceparams will reopen it. */
  416.       if ( bpp != old_bpp && pdev->is_open )
  417.         { int ccode = gs_closedevice(pdev);
  418.           if ( ccode < 0 ) return ccode;
  419.         }
  420.     }
  421.     
  422.     return code;
  423. }
  424.  
  425. /* Added parameters for PaintJet */
  426.  
  427. /* Get parameters.  In addition to the standard and printer */
  428. /* parameters, we allow control of the bits-per-pixel */
  429. private int
  430. pj_get_params(gx_device *pdev, gs_param_list *plist)
  431. {    int code = gdev_prn_get_params(pdev, plist);
  432.     code < 0 ||
  433.     (code = param_write_int(plist, "BitsPerPixel", &pj->color_info.depth)) < 0;
  434.     return code;
  435. }
  436.  
  437. /* Put parameters. */
  438. private int
  439. pj_put_params(gx_device *pdev, gs_param_list *plist)
  440. {    int old_bpp = pj->color_info.depth;
  441.     int bpp = 0;
  442.     int code = 0;
  443.  
  444.     code = gdev_prn_put_params(pdev, plist);
  445.     code = put_param_int(plist, "BitsPerPixel", &bpp, 1, 32, code);
  446.  
  447.     if ( code < 0 )
  448.       return code;
  449.  
  450.     if (bpp != 0) {
  451.       set_bpp(pdev, bpp);
  452.       
  453.       /* Close the device; gs_putdeviceparams will reopen it. */
  454.       if ( bpp != old_bpp && pdev->is_open )
  455.         { int ccode = gs_closedevice(pdev);
  456.           if ( ccode < 0 ) return ccode;
  457.         }
  458.     }
  459.     
  460.     return code;
  461. }
  462.  
  463. /* ------ Internal routines ------ */
  464.  
  465. /* The DeskJet500C can compress (mode 9) */
  466. private int
  467. dj500c_print_page(gx_device_printer * pdev, FILE * prn_stream)
  468. {
  469.   return hp_colour_print_page(pdev, prn_stream, DJ500C);
  470. }
  471.  
  472. /* The DeskJet550C can compress (mode 9) */
  473. private int
  474. dj550c_print_page(gx_device_printer * pdev, FILE * prn_stream)
  475. {
  476.   return hp_colour_print_page(pdev, prn_stream, DJ550C);
  477. }
  478.  
  479. /* The DesignJet650C can compress (mode 1) */
  480. private int
  481. dnj650c_print_page(gx_device_printer * pdev, FILE * prn_stream)
  482. {
  483.   return hp_colour_print_page(pdev, prn_stream, DNJ650C);
  484. }
  485.  
  486. /* The PJXL300 can compress (modes 2 & 3) */
  487. private int
  488. pjxl300_print_page(gx_device_printer * pdev, FILE * prn_stream)
  489. { int ret_code;
  490.   /* Ensure we're operating in PCL mode */
  491.   fputs("\033%-12345X@PJL enter language = PCL\n", prn_stream);
  492.   ret_code = hp_colour_print_page(pdev, prn_stream, PJXL300);
  493.   /* Reenter switch-configured language */
  494.   fputs("\033%-12345X", prn_stream);
  495.   return ret_code;
  496. }
  497.  
  498. /* The PaintJet XL can compress (modes 2 & 3) */
  499. private int
  500. pjxl_print_page(gx_device_printer * pdev, FILE * prn_stream)
  501. {
  502.   return hp_colour_print_page(pdev, prn_stream, PJXL180);
  503. }
  504.  
  505. /* The PaintJet can compress (mode 1) */
  506. private int
  507. pj_print_page(gx_device_printer * pdev, FILE * prn_stream)
  508. {
  509.   return hp_colour_print_page(pdev, prn_stream, PJ180);
  510. }
  511.  
  512. /* The LJ250 can compress (mode 1) */
  513. private int
  514. declj250_print_page(gx_device_printer * pdev, FILE * prn_stream)
  515. { int ret_code;
  516.   fputs("\033%8", prn_stream);    /* Enter PCL emulation mode */
  517.   ret_code = hp_colour_print_page(pdev, prn_stream, DECLJ250);
  518.   fputs("\033%@", prn_stream);    /* Exit PCL emulation mode */
  519.   return ret_code;
  520. }
  521.  
  522. /* MACROS FOR DITHERING (we use macros for compact source and faster code) */
  523. /* Floyd-Steinberg dithering. Often results in a dramatic improvement in
  524.  * subjective image quality, but can also produce dramatic increases in
  525.  * amount of printer data generated and actual printing time!! Mode 9 2D
  526.  * compression is still useful for fairly flat colour or blank areas but its
  527.  * compression is much less effective in areas where the dithering has
  528.  * effectively randomised the dot distribution. */
  529.  
  530. #define SHIFT ((I * 8) - 13)
  531. #define RSHIFT ((I * 8) - 16)
  532. #define RANDOM (((rand() << RSHIFT) % (MAXVALUE / 2))  - MAXVALUE / 4);
  533. #define MINVALUE  0
  534. #define MAXVALUE  (255 << SHIFT)
  535. #define THRESHOLD (128 << SHIFT)
  536. #define C 8
  537.  
  538. #define FSdither(inP, out, errP, Err, Bit, Offset, Element)\
  539.     oldErr = Err;\
  540.     Err = (errP[Element] + ((Err * 7 + C) >> 4) + ((int)inP[Element] << SHIFT));\
  541.     if (Err > THRESHOLD) {\
  542.       out |= Bit;\
  543.       Err -= MAXVALUE;\
  544.     }\
  545.     errP[Element + Offset] += ((Err * 3 + C) >> 4);\
  546.     errP[Element] = ((Err * 5 + oldErr + C) >> 4);
  547.  
  548. /* Here we rely on compiler optimisation to remove lines of the form
  549.  * (if (1 >= 4) {...}, ie. the constant boolean expressions */
  550.  
  551. #define FSDline(scan, i, j, plane_size, cErr, mErr, yErr, kErr, cP, mP, yP, kP, n)\
  552. {\
  553.     if (scan == 0) {       /* going_up */\
  554.       for (i = 0; i < plane_size; i++) {\
  555.     byte c, y, m, k, bitmask;\
  556.     int oldErr;\
  557.     bitmask = 0x80;\
  558.     for (c = m = y = k = 0; bitmask != 0; bitmask >>= 1) {\
  559.       if (n >= 4) {\
  560.         if (*dp) {\
  561.           FSdither(dp, k, ep, kErr, bitmask, -n, 0);\
  562.           cErr = mErr = yErr = 0;\
  563.         } else {\
  564.           FSdither(dp, c, ep, cErr, bitmask, -n, n - 3);\
  565.           FSdither(dp, m, ep, mErr, bitmask, -n, n - 2);\
  566.           FSdither(dp, y, ep, yErr, bitmask, -n, n - 1);\
  567.         }\
  568.       } else {\
  569.         if (n >= 3) {\
  570.           FSdither(dp, c, ep, cErr, bitmask, -n, n - 3);\
  571.           FSdither(dp, m, ep, mErr, bitmask, -n, n - 2);\
  572.         }\
  573.         FSdither(dp, y, ep, yErr, bitmask, -n, n - 1);\
  574.       }\
  575.       dp += n, ep += n;\
  576.     }\
  577.     if (n >= 4)\
  578.       *kP++ = k;\
  579.     if (n >= 3) {\
  580.       *cP++ = c;\
  581.           *mP++ = m;\
  582.     }\
  583.     *yP++ = y;\
  584.       }\
  585.     } else {        /* going_down */\
  586.       for (i = 0; i < plane_size; i++) {\
  587.     byte c, y, m, k, bitmask;\
  588.     int oldErr;\
  589.     bitmask = 0x01;\
  590.     for (c = m = y = k = 0; bitmask != 0; bitmask <<= 1) {\
  591.           dp -= n, ep -= n;\
  592.       if (n >= 4) {\
  593.             if (*(dp)) {\
  594.           cErr = mErr = yErr = 0;\
  595.           FSdither(dp, k, ep, kErr, bitmask, n, 0);\
  596.         } else {\
  597.           FSdither(dp, y, ep, yErr, bitmask, n, n - 1);\
  598.           FSdither(dp, m, ep, mErr, bitmask, n, n - 2);\
  599.           FSdither(dp, c, ep, cErr, bitmask, n, n - 3);\
  600.         }\
  601.       } else {\
  602.         FSdither(dp, y, ep, yErr, bitmask, n, n - 1);\
  603.         if (n >= 3) {\
  604.           FSdither(dp, m, ep, mErr, bitmask, n, n - 2);\
  605.           FSdither(dp, c, ep, cErr, bitmask, n, n - 3);\
  606.         }\
  607.       }\
  608.     }\
  609.     *--yP = y;\
  610.     if (n >= 3)\
  611.       { *--mP = m;\
  612.         *--cP = c;\
  613.       }\
  614.     if (n >= 4)\
  615.       *--kP = k;\
  616.       }\
  617.     }\
  618. }
  619. /* END MACROS FOR DITHERING */
  620.  
  621. /* Some convenient shorthand .. */
  622. #define x_dpi        (pdev->x_pixels_per_inch)
  623. #define y_dpi        (pdev->y_pixels_per_inch)
  624. #define CONFIG_16BIT "\033*v6W\000\003\000\005\006\005"
  625. #define CONFIG_24BIT "\033*v6W\000\003\000\010\010\010"
  626.  
  627. /* To calculate buffer size as next greater multiple of both parameter and W */
  628. #define calc_buffsize(a, b) (((((a) + ((b) * W) - 1) / ((b) * W))) * W)
  629.  
  630. /* Send the page to the printer.  Compress each scan line. */
  631. private int
  632. hp_colour_print_page(gx_device_printer * pdev, FILE * prn_stream, int ptype)
  633. {
  634.   uint raster_width = gdev_prn_rasterwidth(pdev, 1);
  635. /*  int line_size = gdev_prn_rasterwidth(pdev, 0); */
  636.   int line_size = gdev_prn_raster(pdev);
  637.   int line_size_words = (line_size + W - 1) / W;
  638.   int paper_size = gdev_pcl_paper_size((gx_device *)pdev);
  639.   int num_comps = pdev->color_info.num_components;
  640.   int bits_per_pixel = pdev->color_info.depth;
  641.   int storage_bpp = bits_per_pixel;
  642.   int expanded_bpp = bits_per_pixel;
  643.   int plane_size, databuff_size;
  644.   int combined_escapes = 1;
  645.   int errbuff_size = 0;
  646.   int outbuff_size = 0;
  647.   int compression = 0;
  648.   int scan = 0;
  649.   int *errors[2];
  650.   const char *cid_string;
  651.   byte *data[4], *plane_data[4][4], *out_data;
  652.   byte *out_row, *out_row_alt;
  653.   word *storage;
  654.   uint storage_size_words;
  655.  
  656.   /* Tricks and cheats ... */
  657.   switch (ptype) {
  658.   case DJ550C:
  659.     if (num_comps == 3)
  660.       num_comps = 4;                      /* 4-component printing */
  661.     break;
  662.   case PJXL300:
  663.   case PJXL180:
  664.     if (pjxl->rendertype > 0) {
  665.       if (bits_per_pixel < 16)
  666.     pjxl->rendertype = 0;
  667.       else {
  668.     /* Control codes for CID sequence */
  669.     cid_string = (bits_per_pixel == 16) ? CONFIG_16BIT : CONFIG_24BIT;
  670.     /* Pretend we're a monobit device so we send the data out unchanged */
  671.     bits_per_pixel = storage_bpp = expanded_bpp = 1;
  672.     num_comps = 1;
  673.       }
  674.     }
  675.     break;
  676.   }
  677.  
  678.   if (storage_bpp == 8 && num_comps >= 3)
  679.     bits_per_pixel = expanded_bpp = 3;    /* Only 3 bits of each byte used */
  680.  
  681.   plane_size = calc_buffsize(line_size, storage_bpp);
  682.  
  683.   if (bits_per_pixel == 1) {        /* Data printed direct from i/p */
  684.     databuff_size = 0;            /* so no data buffer required, */
  685.     outbuff_size = plane_size * 4;    /* but need separate output buffers */
  686.   }
  687.   
  688.   if (bits_per_pixel > 4) {        /* Error buffer for FS dithering */
  689.     storage_bpp = expanded_bpp = 
  690.       num_comps * 8;            /* 8, 24 or 32 bits */
  691.     errbuff_size =            /* 4n extra values for line ends */
  692.       calc_buffsize((plane_size * expanded_bpp + num_comps * 4) * I, 1);
  693.   }
  694.  
  695.   databuff_size = plane_size * expanded_bpp;
  696.  
  697.   storage_size_words = ((plane_size + plane_size) * num_comps +
  698.             databuff_size + errbuff_size + outbuff_size) / W;
  699.  
  700.   storage = (ulong *) gs_malloc(storage_size_words, W, "hp_colour_print_page");
  701.  
  702.   /*
  703.    * The principal data pointers are stored as pairs of values, with
  704.    * the selection being made by the 'scan' variable. The function of the
  705.    * scan variable is overloaded, as it controls both the alternating
  706.    * raster scan direction used in the Floyd-Steinberg dithering and also
  707.    * the buffer alternation required for line-difference compression.
  708.    *
  709.    * Thus, the number of pointers required is as follows:
  710.    * 
  711.    *   errors:      2  (scan direction only)
  712.    *   data:        4  (scan direction and alternating buffers)
  713.    *   plane_data:  4  (scan direction and alternating buffers)
  714.    */
  715.  
  716.   if (storage == 0)        /* can't allocate working area */
  717.     return_error(gs_error_VMerror);
  718.   else {
  719.     int i;
  720.     byte *p = out_data = out_row = (byte *)storage;    
  721.     data[0] = data[1] = data[2] = p;
  722.     data[3] = p + databuff_size;
  723.     out_row_alt = out_row + plane_size * 2;
  724.     if (bits_per_pixel > 1) {
  725.       p += databuff_size;
  726.     }
  727.     if (bits_per_pixel > 4) {
  728.       errors[0] = (int *)p + num_comps * 2;
  729.       errors[1] = errors[0] + databuff_size;
  730.       p += errbuff_size;
  731.     }
  732.     for (i = 0; i < num_comps; i++) {
  733.       plane_data[0][i] = plane_data[2][i] = p;
  734.       p += plane_size;
  735.     }
  736.     for (i = 0; i < num_comps; i++) {
  737.       plane_data[1][i] = p;
  738.       plane_data[3][i] = p + plane_size;
  739.       p += plane_size;
  740.     }
  741.     if (bits_per_pixel == 1) {
  742.       out_data = out_row = p;      /* size is outbuff_size * 4 */
  743.       out_row_alt = out_row + plane_size * 2;
  744.       data[1] += databuff_size;   /* coincides with plane_data pointers */
  745.       data[3] += databuff_size;
  746.     }
  747.   }
  748.   
  749.   /* Clear temp storage */
  750.   memset(storage, 0, storage_size_words * W);
  751.   
  752.   /* Initialize printer. */
  753.   fputs("\033*rbC", prn_stream);                   /* End raster graphics */
  754.   fprintf(prn_stream, "\033*t%dR", (int)x_dpi);       /* Set resolution */
  755.  
  756. #define DOFFSET (pdev->t_margin - DESKJET_PRINT_LIMIT)    /* Print position */
  757. #define POFFSET (pdev->t_margin - PAINTJET_PRINT_LIMIT)
  758.   switch (ptype) {
  759.   case DJ500C:
  760.   case DJ550C:
  761.     /* Page size, orientation, top margin & perforation skip */
  762.     fprintf(prn_stream, "\033&l%daolE", paper_size);
  763.     /* Set depletion and shingling levels */
  764.     fprintf(prn_stream, "\033*o%dd%dQ", cdj->depletion, cdj->shingling);
  765.     /* Move to top left of printed area */
  766.     fprintf(prn_stream, "\033*p%dY", (int)(300 * DOFFSET));
  767.     /* Set number of planes ((-)1 is mono, (-)3 is (cmy)rgb, -4 is cmyk),
  768.      * and raster width, then start raster graphics */
  769.     fprintf(prn_stream, "\033*r%ds-%du0A", raster_width, num_comps);
  770.     /* Select data compression */
  771.     compression = 9;
  772.     break;
  773.   case DNJ650C:
  774.     fprintf (prn_stream, "\033%%0B"); /* Enter HPGL/2 mode */
  775.     fprintf (prn_stream, "BP5,1"); /* Turn off autorotation */
  776.     fprintf (prn_stream, "PS%d,%d",
  777.          (int)((pdev->height/pdev->y_pixels_per_inch)*1016),
  778.          (int)((pdev->width/pdev->x_pixels_per_inch)*1016)); /* Set length/width of page */
  779.     fprintf (prn_stream, "PU"); /* Pen up */
  780.     fprintf (prn_stream, "PA%d,%d", 0, 0); /* Move pen to upper-left */
  781.     fprintf (prn_stream, "\033%%1A"); /* Enter HP-RTL mode */
  782.     fprintf (prn_stream, "\033&a1N"); /* No negative motion - allow plotting
  783.                         while receiving */
  784.     { static const char temp[] = {
  785.         033, '*', 'v', '6', 'W',
  786.     000 /* color model */,
  787.     000 /* pixel encoding mode */,
  788.     003 /* number of bits per index */,
  789.     010 /* bits red */,
  790.     010 /* bits green */,
  791.     010 /* bits blue */
  792.       };
  793.       fwrite (temp, 1, sizeof(temp), prn_stream);
  794.     }
  795.  
  796.     /* Set raster width */
  797.     fprintf(prn_stream, "\033*r%dS", raster_width);
  798.     /* Start raster graphics */
  799.     fprintf(prn_stream, "\033*r1A");
  800.  
  801.     /* Select data compression */
  802.     compression = 1;
  803.     /* No combined escapes for raster transfers */
  804.     combined_escapes = 0;
  805.     break;
  806.   case PJXL300:
  807.     /* Page size, orientation, top margin & perforation skip */
  808.     fprintf(prn_stream, "\033&l%daolE", paper_size);
  809.     /* Set no-negative-motion mode, for faster (unbuffered) printing */
  810.     fprintf(prn_stream, "\033&a1N");
  811.     /* Set print quality */
  812.     fprintf(prn_stream, "\033*o%dQ", pjxl->printqual);
  813.     /* Move to top left of printed area */
  814.     fprintf(prn_stream, "\033*p%dY", (int)(300 * POFFSET));
  815.     /* Configure colour setup */
  816.     if (pjxl->rendertype > 0) {
  817.       /* Set render type */
  818.       fprintf(prn_stream, "\033*t%dJ", pjxl->rendertype);
  819.       /* Configure image data */
  820.       fputs(cid_string, prn_stream);
  821.       /* Set raster width, then start raster graphics */
  822.       fprintf(prn_stream, "\033*r%ds1A", raster_width);
  823.     } else {
  824.       /* Set number of planes (1 is mono, 3 is rgb),
  825.        * and raster width, then start raster graphics */
  826.       fprintf(prn_stream, "\033*r%ds-%du0A", raster_width, num_comps);
  827.     }
  828.     /* No combined escapes for raster transfers */
  829.     combined_escapes = 0;
  830.     break;
  831.   case PJXL180:
  832.     /* Page size, orientation, top margin & perforation skip */
  833.     fprintf(prn_stream, "\033&l%daolE", paper_size);
  834.     /* Set print quality */
  835.     fprintf(prn_stream, "\033*o%dQ", pjxl->printqual);
  836.     /* Move to top left of printed area */
  837.     fprintf(prn_stream, "\033*p%dY", (int)(180 * POFFSET));
  838.     /* Configure colour setup */
  839.     if (pjxl->rendertype > 0) {
  840.       /* Set render type */
  841.       fprintf(prn_stream, "\033*t%dJ", pjxl->rendertype);
  842.       /* Configure image data */
  843.       fputs(cid_string, prn_stream);
  844.       /* Set raster width, then start raster graphics */
  845.       fprintf(prn_stream, "\033*r%ds1A", raster_width);
  846.     } else {
  847.       /* Set number of planes (1 is mono, 3 is rgb),
  848.        * and raster width, then start raster graphics */
  849.       fprintf(prn_stream, "\033*r%ds%du0A", raster_width, num_comps);
  850.     }
  851.     break;
  852.   case PJ180:
  853.   case DECLJ250:
  854.     /* Disable perforation skip */
  855.     fprintf(prn_stream, "\033&lL");
  856.     /* Move to top left of printed area */
  857.     fprintf(prn_stream, "\033&a%dV", (int)(720 * POFFSET));
  858.     /* Set number of planes (1 is mono, 3 is rgb),
  859.      * and raster width, then start raster graphics */
  860.     fprintf(prn_stream, "\033*r%ds%du0A", raster_width, num_comps);
  861.     if (ptype == DECLJ250) {
  862.       /* No combined escapes for raster transfers */
  863.       combined_escapes = 0;
  864.       /* From here on, we're a standard Paintjet .. */
  865.       ptype = PJ180;
  866.     }
  867.     /* Select data compression */
  868.     compression = 1;
  869.     break;
  870.   }
  871.  
  872.   /* Unfortunately, the Paintjet XL300 PCL interpreter introduces a
  873.    * version of the PCL language which is different to all earlier HP
  874.    * colour and mono inkjets, in that it loses the very useful ability
  875.    * to use combined escape sequences with the raster transfer
  876.    * commands. In this respect, it is incompatible even with the older
  877.    * 180 dpi PaintJet and PaintJet XL printers!  Another regrettable
  878.    * omission is that 'mode 9' compression is not supported, as this
  879.    * mode can give both computational and PCL file size advantages. */
  880.  
  881.   if (combined_escapes) {
  882.     /* From now on, all escape commands start with \033*b, so we
  883.      * combine them (if the printer supports this). */
  884.     fputs("\033*b", prn_stream);
  885.      /* Set compression if the mode has been defined. */
  886.     if (compression)
  887.       fprintf(prn_stream, "%dm", compression);
  888.   } else
  889.     if (compression)
  890.       fprintf(prn_stream, "\033*b%dM", compression);
  891.  
  892.   /* Send each scan line in turn */
  893.   {
  894.     int lend = pdev->height - (pdev->t_margin + pdev->b_margin) * y_dpi;
  895.     int cErr, mErr, yErr, kErr;
  896.     int this_pass, lnum, i;
  897.     int num_blank_lines = 0;
  898.     int start_rows = (num_comps == 1) ?
  899.       HEAD_ROWS_MONO - 1 : HEAD_ROWS_COLOUR - 1;
  900.     word rmask = ~(word) 0 << ((-pdev->width * storage_bpp) & (W * 8 - 1));
  901.  
  902.     cErr = mErr = yErr = kErr = 0;
  903.  
  904.     if (bits_per_pixel > 4) { /* Randomly seed initial error buffer */
  905.       int *ep = errors[0];
  906.       for (i = 0; i < databuff_size; i++) {
  907.     *ep++ = RANDOM;
  908.       }
  909.     }
  910.  
  911.     /* Inhibit blank line printing for RGB-only printers, since in
  912.      * this case 'blank' means black!  Also disabled for XL300 due to
  913.      * an obscure bug in the printer's firmware */
  914.     if (ptype == PJ180 || ptype == PJXL180 || ptype == PJXL300)
  915.       start_rows = -1;
  916.  
  917.     this_pass = start_rows;
  918.     for (lnum = 0; lnum < lend; lnum++) {
  919.       word *data_words = (word *)data[scan];
  920.       register word *end_data = data_words + line_size_words;
  921.  
  922.       gdev_prn_copy_scan_lines(pdev, lnum, data[scan], line_size);
  923.  
  924.       /* Mask off 1-bits beyond the line width. */
  925.       end_data[-1] &= rmask;
  926.  
  927.       /* Remove trailing 0s. */
  928.       while (end_data > data_words && end_data[-1] == 0)
  929.     end_data--;
  930.       if (ptype != DNJ650C)    /* DesignJet can't skip blank lines ? ? */
  931.     if (end_data == data_words) {    /* Blank line */
  932.       num_blank_lines++;
  933.       continue;
  934.     }
  935.       /* Skip blank lines if any */
  936.       if (num_blank_lines > 0) {
  937.     if (num_blank_lines < this_pass) {
  938.       /* Moving down from current position
  939.        * causes head motion on the DeskJets, so
  940.        * if the number of lines is within the
  941.        * current pass of the print head, we're
  942.        * better off printing blanks. */
  943.       this_pass -= num_blank_lines;
  944.       if (combined_escapes) {
  945.         fputc('y', prn_stream);   /* Clear current and seed rows */
  946.         for (; num_blank_lines; num_blank_lines--)
  947.           fputc('w', prn_stream);
  948.       } else {
  949. #if 0
  950. /**************** The following code has been proposed ****************/
  951. /**************** as a replacement: ****************/
  952.         fputs("\033*b1Y", prn_stream);   /* Clear current and seed rows */
  953.         if ( num_blank_lines > 1 )
  954.           fprintf(prn_stream, "\033*b%dY", num_blank_lines - 1);
  955.         num_blank_lines = 0;
  956. #else
  957.         fputs("\033*bY", prn_stream);   /* Clear current and seed rows */
  958.         if (ptype == DNJ650C) {
  959.           fprintf (prn_stream, "\033*b%dY", num_blank_lines);
  960.           num_blank_lines = 0;
  961.         }
  962.         else {
  963.           for (; num_blank_lines; num_blank_lines--)
  964.             fputs("\033*bW", prn_stream);
  965.         }
  966. #endif
  967.       }
  968.     } else {
  969.       if (combined_escapes)
  970.         fprintf(prn_stream, "%dy", num_blank_lines);
  971.       else
  972.         fprintf(prn_stream, "\033*b%dY", num_blank_lines);
  973.     }
  974.     memset(plane_data[1 - scan][0], 0, plane_size * num_comps);
  975.     num_blank_lines = 0;
  976.     this_pass = start_rows;
  977.       }
  978.       {            /* Printing non-blank lines */
  979.     register byte *kP = plane_data[scan + 2][3];
  980.     register byte *cP = plane_data[scan + 2][2];
  981.     register byte *mP = plane_data[scan + 2][1];
  982.     register byte *yP = plane_data[scan + 2][0];
  983.     register byte *dp = data[scan + 2];
  984.     register int *ep = errors[scan];
  985.     int zero_row_count;
  986.     int i, j;
  987.     byte *odp;
  988.  
  989.     if (this_pass)
  990.       this_pass--;
  991.     else
  992.       this_pass = start_rows;
  993.  
  994.     if (expanded_bpp > bits_per_pixel)   /* Expand line if required */
  995.       expand_line(data_words, line_size, bits_per_pixel, expanded_bpp);
  996.  
  997.     /* In colour modes, we have some bit-shuffling to do before
  998.      * we can print the data; in FS mode we also have the
  999.      * dithering to take care of. */
  1000.     switch (expanded_bpp) {    /* Can be 1, 3, 8, 24 or 32 */
  1001.     case 3:
  1002.       /* Transpose the data to get pixel planes. */
  1003.       for (i = 0, odp = plane_data[scan][0]; i < databuff_size;
  1004.            i += 8, odp++) {    /* The following is for 16-bit
  1005.                  * machines */
  1006. #define spread3(c)\
  1007.     { 0, c, c*0x100, c*0x101, c*0x10000L, c*0x10001L, c*0x10100L, c*0x10101L }
  1008.         static ulong spr40[8] = spread3(0x40);
  1009.         static ulong spr08[8] = spread3(8);
  1010.         static ulong spr02[8] = spread3(2);
  1011.         register byte *dp = data[scan] + i;
  1012.         register ulong pword =
  1013.         (spr40[dp[0]] << 1) +
  1014.         (spr40[dp[1]]) +
  1015.         (spr40[dp[2]] >> 1) +
  1016.         (spr08[dp[3]] << 1) +
  1017.         (spr08[dp[4]]) +
  1018.         (spr08[dp[5]] >> 1) +
  1019.         (spr02[dp[6]]) +
  1020.         (spr02[dp[7]] >> 1);
  1021.         odp[0] = (byte) (pword >> 16);
  1022.         odp[plane_size] = (byte) (pword >> 8);
  1023.         odp[plane_size * 2] = (byte) (pword);
  1024.       }
  1025.       break;
  1026.  
  1027.     case 8:
  1028.       FSDline(scan, i, j, plane_size, cErr, mErr, yErr, kErr,
  1029.           cP, mP, yP, kP, 1);
  1030.       break;
  1031.     case 24:
  1032.       FSDline(scan, i, j, plane_size, cErr, mErr, yErr, kErr,
  1033.           cP, mP, yP, kP, 3);
  1034.       break;
  1035.     case 32:
  1036.       FSDline(scan, i, j, plane_size, cErr, mErr, yErr, kErr,
  1037.           cP, mP, yP, kP, 4);
  1038.       break;
  1039.  
  1040.     } /* switch(expanded_bpp) */
  1041.  
  1042.     /* Make sure all black is in the k plane */
  1043.     if (num_comps == 4) {
  1044.       register word *kp = (word *)plane_data[scan][3];
  1045.       register word *cp = (word *)plane_data[scan][2];
  1046.       register word *mp = (word *)plane_data[scan][1];
  1047.       register word *yp = (word *)plane_data[scan][0];
  1048.       if (bits_per_pixel > 4) {  /* Done as 4 planes */
  1049.         for (i = 0; i < plane_size / W; i++) {
  1050.           word bits = *cp & *mp & *yp;
  1051.           *kp++ |= bits;
  1052.           bits = ~bits;
  1053.           *cp++ &= bits;
  1054.           *mp++ &= bits;
  1055.           *yp++ &= bits;
  1056.         }
  1057.       } else {  /* This has really been done as 3 planes */
  1058.         for (i = 0; i < plane_size / W; i++) {
  1059.           word bits = *cp & *mp & *yp;
  1060.           *kp++ = bits;
  1061.           bits = ~bits;
  1062.           *cp++ &= bits;
  1063.           *mp++ &= bits;
  1064.           *yp++ &= bits;
  1065.         }
  1066.       }
  1067.     }
  1068.  
  1069.     /* Transfer raster graphics
  1070.      * in the order (K), C, M, Y. */
  1071.     for (zero_row_count = 0, i = num_comps - 1; i >= 0; i--) {
  1072.       int output_plane = 1;
  1073.       int out_count;
  1074.       
  1075.       switch (ptype) {
  1076.       case DJ500C:    /* Always compress using mode 9 */
  1077.       case DJ550C:
  1078.         out_count = gdev_pcl_mode9compress(plane_size,
  1079.                            plane_data[scan][i],
  1080.                            plane_data[1 - scan][i],
  1081.                            out_data);
  1082.  
  1083.         /* This optimisation allows early termination of the
  1084.          * row, but this doesn't work correctly in an alternating
  1085.          * mode 2 / mode 3 regime, so we only use it with mode 9
  1086.          * compression */
  1087.            if (out_count == 0)
  1088.              { output_plane = 0;      /* No further output for this plane */
  1089.                if (i == 0)
  1090.                  fputc('w', prn_stream);
  1091.                else
  1092.                  zero_row_count++;
  1093.              }
  1094.            else
  1095.              { for (; zero_row_count; zero_row_count--)
  1096.                  fputc('v', prn_stream);
  1097.              }
  1098.         break;
  1099.       case PJ180:
  1100.         if (num_comps > 1)
  1101.           { word *wp = (word *)plane_data[scan][i];
  1102.         for (j = 0; j < plane_size / W; j++, wp++)
  1103.           *wp = ~*wp;
  1104.           }
  1105.         out_count = gdev_pcl_mode1compress((const byte *)
  1106.                            plane_data[scan][i],
  1107.                            (const byte *)
  1108.                            plane_data[scan][i] + plane_size - 1,
  1109.                            out_data);
  1110.         break;
  1111.       case PJXL180:    /* Need to invert data as CMY not supported */
  1112.         if (num_comps > 1)
  1113.           { word *wp = (word *)plane_data[scan][i];
  1114.         for (j = 0; j < plane_size / W; j++, wp++)
  1115.           *wp = ~*wp;
  1116.           }
  1117.         /* fall through .. */
  1118.       case PJXL300:     /* Compression modes 2 and 3 are both 
  1119.                  * available.  Try both and see which one
  1120.                  * produces the least output data. */
  1121.         { const byte *plane = plane_data[scan][i];
  1122.           byte *prev_plane = plane_data[1 - scan][i];
  1123.           const word *row = (word *)plane;
  1124.           const word *end_row = row + plane_size/W;
  1125.           int count2 = gdev_pcl_mode2compress(row, end_row, out_row_alt);
  1126.           int count3 = gdev_pcl_mode3compress(plane_size, plane, prev_plane, out_row);
  1127.           int penalty = combined_escapes ? strlen("#m") : strlen("\033*b#M");
  1128.           int penalty2 = (compression == 2 ? 0 : penalty);
  1129.           int penalty3 = (compression == 3 ? 0 : penalty);
  1130.           
  1131.           if (count3 + penalty3 < count2 + penalty2)
  1132.         { if ( compression != 3 ) {
  1133.             if (combined_escapes)
  1134.               fputs("3m", prn_stream);
  1135.             else
  1136.               fputs("\033*b3M", prn_stream);
  1137.             compression = 3;
  1138.           }
  1139.           out_data = out_row;
  1140.           out_count = count3;
  1141.         }
  1142.           else
  1143.         { if ( compression != 2 ) {
  1144.             if (combined_escapes)
  1145.               fputs("2m", prn_stream);
  1146.             else
  1147.               fputs("\033*b2M", prn_stream);
  1148.             compression = 2;
  1149.           }
  1150.           out_data = out_row_alt;
  1151.           out_count = count2;
  1152.         }
  1153.         }
  1154.         break;
  1155.       }
  1156.       if (output_plane) {
  1157.         if (combined_escapes)
  1158.           fprintf(prn_stream, "%d%c", out_count, "wvvv"[i]);
  1159.         else
  1160.           fprintf(prn_stream, "\033*b%d%c", out_count, "WVVV"[i]);
  1161.         fwrite(out_data, sizeof(byte), out_count, prn_stream);
  1162.       }
  1163.       
  1164.     } /* Transfer Raster Graphics ... */
  1165.     scan = 1 - scan;          /* toggle scan direction */
  1166.       }      /* Printing non-blank lines */
  1167.     }     /* for lnum ... */
  1168.   }       /* send each scan line in turn */
  1169.  
  1170.   if (combined_escapes)
  1171.     fputs("0M", prn_stream);
  1172.  
  1173.   /* end raster graphics */
  1174.   fputs("\033*rbC", prn_stream);
  1175.  
  1176.   /* eject page */
  1177.   if (ptype == PJ180)
  1178.     fputc('\f', prn_stream);
  1179.   else if (ptype == DNJ650C)
  1180.     fputs ("\033*rC\033%%0BPG;", prn_stream);
  1181.   else
  1182.     fputs("\033&l0H", prn_stream);
  1183.  
  1184.   /* free temporary storage */
  1185.   gs_free((char *) storage, storage_size_words, W, "hp_colour_print_page");
  1186.  
  1187.   return 0;
  1188. }
  1189.  
  1190. /*
  1191.  * Mode 9 2D compression for the HP DeskJet 5xxC. This mode can give
  1192.  * very good compression ratios, especially if there are areas of flat
  1193.  * colour (or blank areas), and so is 'highly recommended' for colour
  1194.  * printing in particular because of the very large amounts of data which
  1195.  * can be generated
  1196.  */
  1197. private int
  1198. gdev_pcl_mode9compress(int bytecount, const byte * current, const byte * previous, byte * compressed)
  1199. {
  1200.   register const byte *cur = current;
  1201.   register const byte *prev = previous;
  1202.   register byte *out = compressed;
  1203.   const byte *end = current + bytecount;
  1204.  
  1205.   while (cur < end) {        /* Detect a run of unchanged bytes. */
  1206.     const byte *run = cur;
  1207.     register const byte *diff;
  1208.     int offset;
  1209.     while (cur < end && *cur == *prev) {
  1210.       cur++, prev++;
  1211.     }
  1212.     if (cur == end)
  1213.       break;            /* rest of row is unchanged */
  1214.     /* Detect a run of changed bytes. */
  1215.     /* We know that *cur != *prev. */
  1216.     diff = cur;
  1217.     do {
  1218.       prev++;
  1219.       cur++;
  1220.     }
  1221.     while (cur < end && *cur != *prev);
  1222.     /* Now [run..diff) are unchanged, and */
  1223.     /* [diff..cur) are changed. */
  1224.     offset = diff - run;
  1225.     {
  1226.       const byte *stop_test = cur - 4;
  1227.       int dissimilar, similar;
  1228.  
  1229.       while (diff < cur) {
  1230.     const byte *compr = diff;
  1231.     const byte *next;    /* end of run */
  1232.     byte value;
  1233.     while (diff <= stop_test &&
  1234.            ((value = *diff) != diff[1] ||
  1235.         value != diff[2] ||
  1236.         value != diff[3]))
  1237.       diff++;
  1238.  
  1239.     /* Find out how long the run is */
  1240.     if (diff > stop_test)    /* no run */
  1241.       next = diff = cur;
  1242.     else {
  1243.       next = diff + 4;
  1244.       while (next < cur && *next == value)
  1245.         next++;
  1246.     }
  1247.  
  1248. #define MAXOFFSETU 15
  1249. #define MAXCOUNTU 7
  1250.     /* output 'dissimilar' bytes, uncompressed */
  1251.     if ((dissimilar = diff - compr)) {
  1252.       int temp, i;
  1253.  
  1254.       if ((temp = --dissimilar) > MAXCOUNTU)
  1255.         temp = MAXCOUNTU;
  1256.       if (offset < MAXOFFSETU)
  1257.         *out++ = (offset << 3) | (byte) temp;
  1258.       else {
  1259.         *out++ = (MAXOFFSETU << 3) | (byte) temp;
  1260.         offset -= MAXOFFSETU;
  1261.         while (offset >= 255) {
  1262.           *out++ = 255;
  1263.           offset -= 255;
  1264.         }
  1265.         *out++ = offset;
  1266.       }
  1267.       if (temp == MAXCOUNTU) {
  1268.         temp = dissimilar - MAXCOUNTU;
  1269.         while (temp >= 255) {
  1270.           *out++ = 255;
  1271.           temp -= 255;
  1272.         }
  1273.         *out++ = (byte) temp;
  1274.       }
  1275.       for (i = 0; i <= dissimilar; i++)
  1276.         *out++ = *compr++;
  1277.       offset = 0;
  1278.     }            /* end uncompressed */
  1279. #define MAXOFFSETC 3
  1280. #define MAXCOUNTC 31
  1281.     /* output 'similar' bytes, run-length encoded */
  1282.     if ((similar = next - diff)) {
  1283.       int temp;
  1284.  
  1285.       if ((temp = (similar -= 2)) > MAXCOUNTC)
  1286.         temp = MAXCOUNTC;
  1287.       if (offset < MAXOFFSETC)
  1288.         *out++ = 0x80 | (offset << 5) | (byte) temp;
  1289.       else {
  1290.         *out++ = 0x80 | (MAXOFFSETC << 5) | (byte) temp;
  1291.         offset -= MAXOFFSETC;
  1292.         while (offset >= 255) {
  1293.           *out++ = 255;
  1294.           offset -= 255;
  1295.         }
  1296.         *out++ = offset;
  1297.       }
  1298.       if (temp == MAXCOUNTC) {
  1299.         temp = similar - MAXCOUNTC;
  1300.         while (temp >= 255) {
  1301.           *out++ = 255;
  1302.           temp -= 255;
  1303.         }
  1304.         *out++ = (byte) temp;
  1305.       }
  1306.       *out++ = value;
  1307.       offset = 0;
  1308.     }            /* end compressed */
  1309.     diff = next;
  1310.       }
  1311.     }
  1312.   }
  1313.   return out - compressed;
  1314. }
  1315.  
  1316. /*
  1317.  * Row compression for the H-P PaintJet.
  1318.  * Compresses data from row up to end_row, storing the result
  1319.  * starting at compressed.  Returns the number of bytes stored.
  1320.  * The compressed format consists of a byte N followed by a
  1321.  * data byte that is to be repeated N+1 times.
  1322.  * In the worst case, the `compressed' representation is
  1323.  * twice as large as the input.
  1324.  * We complement the bytes at the same time, because
  1325.  * we accumulated the image in complemented form.
  1326.  */
  1327. private int
  1328. gdev_pcl_mode1compress(const byte *row, const byte *end_row, byte *compressed)
  1329. {    register const byte *in = row;
  1330.     register byte *out = compressed;
  1331.     while ( in < end_row )
  1332.            {    byte test = *in++;
  1333.         const byte *run = in;
  1334.         while ( in < end_row && *in == test ) in++;
  1335.         /* Note that in - run + 1 is the repetition count. */
  1336.         while ( in - run > 255 )
  1337.                    {    *out++ = 255;
  1338.             *out++ = test;
  1339.             run += 256;
  1340.                    }
  1341.         *out++ = in - run;
  1342.         *out++ = test;
  1343.            }
  1344.     return out - compressed;
  1345. }
  1346.  
  1347. /*
  1348.  * Map a r-g-b color to a color index.
  1349.  * We complement the colours, since we're using cmy anyway, and
  1350.  * because the buffering routines expect white to be zero.
  1351.  * Includes colour balancing, following HP recommendations, to try
  1352.  * and correct the greenish cast resulting from an equal mix of the
  1353.  * c, m, y, inks by reducing the cyan component to give a truer black.
  1354.  */
  1355. private gx_color_index
  1356. gdev_pcl_map_rgb_color(gx_device *pdev, gx_color_value r,
  1357.                  gx_color_value g, gx_color_value b)
  1358. {
  1359.   if (gx_color_value_to_byte(r & g & b) == 0xff)
  1360.     return (gx_color_index)0;         /* white */
  1361.   else {
  1362.     int correction = hp_device->correction;
  1363.     gx_color_value c = gx_max_color_value - r;
  1364.     gx_color_value m = gx_max_color_value - g;
  1365.     gx_color_value y = gx_max_color_value - b;
  1366.     
  1367.     /* Colour correction for better blacks when using the colour ink
  1368.      * cartridge (on the DeskJet 500C only). We reduce the cyan component
  1369.      * by some fraction (eg. 4/5) to correct the slightly greenish cast
  1370.      * resulting from an equal mix of the three inks */
  1371.     if (correction) {
  1372.       ulong maxval, minval, range;
  1373.       
  1374.       maxval = c >= m ? (c >= y ? c : y) : (m >= y ? m : y);
  1375.       if (maxval > 0) {
  1376.     minval = c <= m ? (c <= y ? c : y) : (m <= y? m : y);
  1377.     range = maxval - minval;
  1378.     
  1379. #define shift (gx_color_value_bits - 12)
  1380.     c = ((c >> shift) * (range + (maxval * correction))) /
  1381.       ((maxval * (correction + 1)) >> shift);
  1382.       }
  1383.     }
  1384.     
  1385.     switch (pdev->color_info.depth) {
  1386.     case 1:
  1387.       return ((c | m | y) > gx_max_color_value / 2 ?
  1388.           (gx_color_index)1 : (gx_color_index)0);
  1389.     case 8:
  1390.       if (pdev->color_info.num_components >= 3)
  1391. #define gx_color_value_to_1bit(cv) ((cv) >> (gx_color_value_bits - 1))
  1392.     return (gx_color_value_to_1bit(c) +
  1393.         (gx_color_value_to_1bit(m) << 1) +
  1394.         (gx_color_value_to_1bit(y) << 2));
  1395.       else
  1396. #define red_weight 306
  1397. #define green_weight 601
  1398. #define blue_weight 117
  1399.     return ((((ulong)c * red_weight +
  1400.           (ulong)m * green_weight +
  1401.           (ulong)y * blue_weight)
  1402.          >> (gx_color_value_bits + 2)));
  1403.     case 16:
  1404. #define gx_color_value_to_5bits(cv) ((cv) >> (gx_color_value_bits - 5))
  1405. #define gx_color_value_to_6bits(cv) ((cv) >> (gx_color_value_bits - 6))
  1406.       return (gx_color_value_to_5bits(y) +
  1407.           (gx_color_value_to_6bits(m) << 5) +
  1408.           (gx_color_value_to_5bits(c) << 11));
  1409.     case 24:
  1410.       return (gx_color_value_to_byte(y) +
  1411.           (gx_color_value_to_byte(m) << 8) +
  1412.           ((ulong)gx_color_value_to_byte(c) << 16));
  1413.     case 32:
  1414.       { return ((c == m && c == y) ? ((ulong)gx_color_value_to_byte(c) << 24)
  1415.       : (gx_color_value_to_byte(y) +
  1416.          (gx_color_value_to_byte(m) << 8) +
  1417.          ((ulong)gx_color_value_to_byte(c) << 16)));
  1418.       }
  1419.     }
  1420.   }
  1421.   return (gx_color_index)0;   /* This never happens */
  1422. }
  1423.     
  1424. /* Map a color index to a r-g-b color. */
  1425. private int
  1426. gdev_pcl_map_color_rgb(gx_device *pdev, gx_color_index color,
  1427.                 gx_color_value prgb[3])
  1428. {
  1429.   /* For the moment, we simply ignore any black correction */
  1430.   switch (pdev->color_info.depth) {
  1431.   case 1:
  1432.     prgb[0] = prgb[1] = prgb[2] = -((gx_color_value)color ^ 1);
  1433.     break;
  1434.   case 8:
  1435.       if (pdev->color_info.num_components >= 3)
  1436.     { gx_color_value c = (gx_color_value)color ^ 7;
  1437.       prgb[0] = -(c & 1);
  1438.       prgb[1] = -((c >> 1) & 1);
  1439.       prgb[2] = -(c >> 2);
  1440.     }
  1441.       else
  1442.     { gx_color_value value = (gx_color_value)color ^ 0xff;
  1443.       prgb[0] = prgb[1] = prgb[2] = (value << 8) + value;
  1444.     }
  1445.     break;
  1446.   case 16:
  1447.     { gx_color_value c = (gx_color_value)color ^ 0xffff;
  1448.       ushort value = c >> 11;
  1449.       prgb[0] = ((value << 11) + (value << 6) + (value << 1) +
  1450.          (value >> 4)) >> (16 - gx_color_value_bits);
  1451.       value = (c >> 6) & 0x3f;
  1452.       prgb[1] = ((value << 10) + (value << 4) + (value >> 2))
  1453.     >> (16 - gx_color_value_bits);
  1454.       value = c & 0x1f;
  1455.       prgb[2] = ((value << 11) + (value << 6) + (value << 1) +
  1456.          (value >> 4)) >> (16 - gx_color_value_bits);
  1457.     }
  1458.     break;
  1459.   case 24:
  1460.     { gx_color_value c = (gx_color_value)color ^ 0xffffff;
  1461.       prgb[0] = gx_color_value_from_byte(c >> 16);
  1462.       prgb[1] = gx_color_value_from_byte((c >> 8) & 0xff);
  1463.       prgb[2] = gx_color_value_from_byte(c & 0xff);
  1464.     }
  1465.     break;
  1466.   case 32:
  1467. #define  gx_maxcol gx_color_value_from_byte(gx_color_value_to_byte(gx_max_color_value))
  1468.     { gx_color_value w = gx_maxcol - gx_color_value_from_byte(color >> 24);
  1469.       prgb[0] = w - gx_color_value_from_byte((color >> 16) & 0xff);
  1470.       prgb[1] = w - gx_color_value_from_byte((color >> 8) & 0xff);
  1471.       prgb[2] = w - gx_color_value_from_byte(color & 0xff);
  1472.     }
  1473.     break;
  1474.   }
  1475.   return 0;
  1476. }
  1477.  
  1478. /*
  1479.  * Convert and expand scanlines:
  1480.  *
  1481.  *       (a)    16 -> 24 bit   (1-stage)
  1482.  *       (b)    16 -> 32 bit   (2-stage)
  1483.  *   or  (c)    24 -> 32 bit   (1-stage)
  1484.  */
  1485. private void
  1486. expand_line(word *line, int linesize, int bpp, int ebpp)
  1487. {
  1488.   int endline = linesize;
  1489.   byte *start = (byte *)line;
  1490.   register byte *in, *out;
  1491.  
  1492.   if (bpp == 16)              /* 16 to 24 (cmy) if required */
  1493.     { register byte b0, b1;
  1494.       endline = ((endline + 1) / 2);
  1495.       in = start + endline * 2;
  1496.       out = start + (endline *= 3);
  1497.       
  1498.       while (in > start)
  1499.     { b0 = *--in;
  1500.       b1 = *--in;
  1501.       *--out = (b0 << 3) + ((b0 >> 2) & 0x7);
  1502.       *--out = (b1 << 5) + ((b0 >> 3)  & 0x1c) + ((b1 >> 1) & 0x3);
  1503.       *--out = (b1 & 0xf8) + (b1 >> 5);
  1504.     }
  1505.     }
  1506.  
  1507.   if (ebpp == 32)             /* 24 (cmy) to 32 (cmyk) if required */
  1508.     { register byte c, m, y;
  1509.       endline = ((endline + 2) / 3);
  1510.       in = start + endline * 3;
  1511.       out = start + endline * 4;
  1512.  
  1513.       while (in > start)
  1514.     { y = *--in;
  1515.       m = *--in;
  1516.       c = *--in;
  1517.       if (c == y && c == m) {
  1518.         *--out = 0, *--out = 0, *--out = 0;
  1519.         *--out = c;
  1520.       } else {
  1521.         *--out = y, *--out = m, *--out = c;
  1522.         *--out = 0;
  1523.       }
  1524.     }
  1525.     }
  1526. }
  1527.  
  1528. private int near
  1529. put_param_int(gs_param_list *plist, gs_param_name pname, int *pvalue,
  1530.   int minval, int maxval, int ecode)
  1531. {    int code, value;
  1532.     switch ( code = param_read_int(plist, pname, &value) )
  1533.     {
  1534.     default:
  1535.         return code;
  1536.     case 1:
  1537.         return ecode;
  1538.     case 0:
  1539.         if ( value < minval || value > maxval )
  1540.             param_return_error(plist, pname, gs_error_rangecheck);
  1541.         *pvalue = value;
  1542.         return (ecode < 0 ? ecode : 1);
  1543.     }
  1544. }    
  1545.  
  1546. private void
  1547. set_bpp(gx_device *pdev, int bits_per_pixel)
  1548. { gx_device_color_info *ci = &pdev->color_info;
  1549.   /* Only valid bits-per-pixel are 1, 3, 8, 16, 24, 32 */
  1550.   int bpp = bits_per_pixel < 3 ? 1 : bits_per_pixel < 8 ? 3 : 
  1551.     (bits_per_pixel >> 3) << 3;
  1552.   ci->num_components = ((bpp == 1) || (bpp == 8) ? 1 : 3);
  1553.   ci->depth = ((bpp > 1) && (bpp < 8) ? 8 : bpp);
  1554.   ci->max_gray = (bpp >= 8 ? 255 : 1);
  1555.   ci->max_rgb = (bpp >= 8 ? 255 : bpp > 1 ? 1 : 0);
  1556.   ci->dither_gray = (bpp >= 8 ? 5 : 2);
  1557.   ci->dither_rgb = (bpp >= 8 ? 5 : bpp > 1 ? 2 : 0);
  1558. }
  1559.  
  1560. /* This returns either the number of pixels in a scan line, or the number
  1561.  * of bytes required to store the line, both clipped to the page margins */
  1562. private uint
  1563. gdev_prn_rasterwidth(const gx_device_printer *pdev, int pixelcount)
  1564. {
  1565.   ulong raster_width =
  1566.     pdev->width - pdev->x_pixels_per_inch * (pdev->l_margin + pdev->r_margin);
  1567.   return (pixelcount ?
  1568.           (uint)raster_width :
  1569.           (uint)((raster_width * pdev->color_info.depth + 7) >> 3));
  1570. }
  1571.