home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / plug-ins / print / print-escp2.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-22  |  123.3 KB  |  4,365 lines

  1. /*
  2.  * "$Id: print-escp2.c,v 1.19 2000/10/21 13:27:30 mitch Exp $"
  3.  *
  4.  *   Print plug-in EPSON ESC/P2 driver for the GIMP.
  5.  *
  6.  *   Copyright 1997-2000 Michael Sweet (mike@easysw.com) and
  7.  *    Robert Krawitz (rlk@alum.mit.edu)
  8.  *
  9.  *   This program is free software; you can redistribute it and/or modify it
  10.  *   under the terms of the GNU General Public License as published by the Free
  11.  *   Software Foundation; either version 2 of the License, or (at your option)
  12.  *   any later version.
  13.  *
  14.  *   This program is distributed in the hope that it will be useful, but
  15.  *   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16.  *   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  17.  *   for more details.
  18.  *
  19.  *   You should have received a copy of the GNU General Public License
  20.  *   along with this program; if not, write to the Free Software
  21.  *   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22.  */
  23.  
  24. /*
  25.  * This file must include only standard C header files.  The core code must
  26.  * compile on generic platforms that don't support glib, gimp, gtk, etc.
  27.  */
  28.  
  29. #ifndef WEAVETEST
  30. #include "print.h"
  31. #endif
  32.  
  33. #ifndef __GNUC__
  34. #  define inline
  35. #endif /* !__GNUC__ */
  36.  
  37. /*#include <endian.h>*/
  38.  
  39. typedef enum {
  40.   COLOR_MONOCHROME,
  41.   COLOR_CMYK,
  42.   COLOR_CCMMYK
  43. } colormode_t;
  44.  
  45. /*
  46.  * Mapping between color and linear index.  The colors are
  47.  * black, magenta, cyan, yellow, light magenta, light cyan
  48.  */
  49.  
  50. static const int color_indices[16] = { 0, 1, 2, -1,
  51.                        3, -1, -1, -1,
  52.                        -1, 4, 5, -1,
  53.                        -1, -1, -1, -1 };
  54. static const int colors[6] = { 0, 1, 2, 4, 1, 2 };
  55. static const int densities[6] = { 0, 0, 0, 0, 1, 1 };
  56.  
  57. #ifndef WEAVETEST
  58.  
  59. static void
  60. escp2_write_microweave(FILE *, const unsigned char *,
  61.                const unsigned char *, const unsigned char *,
  62.                const unsigned char *, const unsigned char *,
  63.                const unsigned char *, int, int, int, int, int,
  64.                int, int);
  65. static void *initialize_weave(int jets, int separation, int oversample,
  66.                   int horizontal, int vertical,
  67.                   colormode_t colormode, int width, int linewidth,
  68.                   int lineheight, int vertical_row_separation,
  69.                   int first_line, int phys_lines, int strategy);
  70. static void escp2_flush_all(void *, int model, int width, int hoffset,
  71.                 int ydpi, int xdpi, int physical_xdpi, FILE *prn);
  72. static void
  73. escp2_write_weave(void *, FILE *, int, int, int, int, int, int, int,
  74.           const unsigned char *c, const unsigned char *m,
  75.           const unsigned char *y, const unsigned char *k,
  76.           const unsigned char *C, const unsigned char *M);
  77. static void escp2_init_microweave(int);
  78. static void escp2_free_microweave(void);
  79.  
  80. static void destroy_weave(void *);
  81.  
  82. /*
  83.  * We really need to get away from this silly static nonsense...
  84.  */
  85. #define PHYSICAL_BPI 720
  86. #define MAX_OVERSAMPLED 4
  87. #define MAX_BPP 2
  88. #define BITS_PER_BYTE 8
  89. #define COMPBUFWIDTH (PHYSICAL_BPI * MAX_OVERSAMPLED * MAX_BPP * \
  90.     MAX_CARRIAGE_WIDTH / BITS_PER_BYTE)
  91.  
  92. /*
  93.  * Printer capabilities.
  94.  *
  95.  * Various classes of printer capabilities are represented by bitmasks.
  96.  */
  97.  
  98. typedef unsigned long long model_cap_t;
  99. typedef unsigned long long model_featureset_t;
  100.  
  101. /*
  102.  * For each printer, we can select from a variety of dot sizes.
  103.  * For single dot size printers, the available sizes are usually 0,
  104.  * which is the "default", and some subset of 1-4.  For simple variable
  105.  * dot size printers (with only one kind of variable dot size), the
  106.  * variable dot size is specified as 0x10.  For newer printers, there
  107.  * is a choice of variable dot sizes available, 0x10, 0x11, and 0x12 in
  108.  * order of increasing size.
  109.  *
  110.  * Normally, we want to specify the smallest dot size that lets us achieve
  111.  * a density of less than .8 or thereabouts (above that we start to get
  112.  * some dither artifacts).  This needs to be tested for each printer and
  113.  * resolution.
  114.  *
  115.  * An entry of -1 in a slot means that this resolution is not available.
  116.  */
  117.  
  118. typedef struct escp2_dot_sizes
  119. {
  120.   int dot_180;
  121.   int dot_360_microweave;
  122.   int dot_360;
  123.   int dot_720_microweave;
  124.   int dot_720;
  125.   int dot_1440_microweave;
  126.   int dot_1440;
  127. } escp2_dot_size_t;
  128.  
  129. /*
  130.  * Specify the base density for each available resolution.
  131.  * This obviously depends upon the dot size.  Experience suggests that
  132.  * variable dot size mode (0x10) on the 870 requires the density
  133.  * derived from the printer base and the resolution to be multiplied
  134.  * by 3.3.  Using dot size 0x11 requires the density to be multiplied
  135.  * by 2.2.
  136.  */
  137.  
  138. typedef struct escp2_densities
  139. {
  140.   double d_180_180;
  141.   double d_360_360_micro;
  142.   double d_360_360;
  143.   double d_720_720_micro;
  144.   double d_720_720;
  145.   double d_1440_720_micro;
  146.   double d_1440_720;
  147.   double d_1440_1440;
  148.   double d_1440_2880;
  149. } escp2_densities_t;
  150.  
  151.  
  152. /*
  153.  * Definition of the multi-level inks available to a given printer.
  154.  * Each printer may use a different kind of ink droplet for variable
  155.  * and single drop size for each supported horizontal resolution and
  156.  * type of ink (4 or 6 color).
  157.  *
  158.  * Recall that 6 color ink is treated as simply another kind of
  159.  * multi-level ink, but the driver offers the user a choice of 4 and
  160.  * 6 color ink, so we need to define appropriate inksets for both
  161.  * kinds of ink.
  162.  *
  163.  * Stuff like the MIS 4 and 6 "color" monochrome inks doesn't fit into
  164.  * this model very nicely, so we'll either have to special case it
  165.  * or find some way of handling it in here.
  166.  */
  167.  
  168. typedef struct escp2_variable_ink
  169. {
  170.   simple_dither_range_t *range;
  171.   int count;
  172.   double density;
  173. } escp2_variable_ink_t;
  174.  
  175. typedef struct escp2_variable_inkset
  176. {
  177.   escp2_variable_ink_t *c;
  178.   escp2_variable_ink_t *m;
  179.   escp2_variable_ink_t *y;
  180.   escp2_variable_ink_t *k;
  181. } escp2_variable_inkset_t;
  182.  
  183. typedef struct escp2_variable_inklist
  184. {
  185.   escp2_variable_inkset_t *s_180_4;
  186.   escp2_variable_inkset_t *s_360_4;
  187.   escp2_variable_inkset_t *s_720_4;
  188.   escp2_variable_inkset_t *s_1440_4;
  189.   escp2_variable_inkset_t *s_180_6;
  190.   escp2_variable_inkset_t *s_360_6;
  191.   escp2_variable_inkset_t *s_720_6;
  192.   escp2_variable_inkset_t *s_1440_6;
  193.   escp2_variable_inkset_t *v_180_4;
  194.   escp2_variable_inkset_t *v_360_4;
  195.   escp2_variable_inkset_t *v_720_4;
  196.   escp2_variable_inkset_t *v_1440_4;
  197.   escp2_variable_inkset_t *v_180_6;
  198.   escp2_variable_inkset_t *v_360_6;
  199.   escp2_variable_inkset_t *v_720_6;
  200.   escp2_variable_inkset_t *v_1440_6;
  201. } escp2_variable_inklist_t;
  202.  
  203. static simple_dither_range_t photo_dither_ranges[] =
  204. {
  205.   { 0.33, 0x1, 0, 1 },
  206.   { 1.0,  0x1, 1, 1 }
  207. };
  208.  
  209. static escp2_variable_ink_t photo_ink =
  210. {
  211.   photo_dither_ranges,
  212.   sizeof(photo_dither_ranges) / sizeof(simple_dither_range_t),
  213.   .75
  214. };
  215.  
  216.  
  217. static simple_dither_range_t photo_6pl_dither_ranges[] =
  218. {
  219.   { 0.15,  0x1, 0, 1 },
  220.   { 0.227, 0x2, 0, 2 },
  221. /*  { 0.333, 0x3, 0, 3 }, */
  222.   { 0.45,  0x1, 1, 1 },
  223.   { 0.68,  0x2, 1, 2 },
  224.   { 1.0,   0x3, 1, 3 }
  225. };
  226.  
  227. static escp2_variable_ink_t photo_6pl_ink =
  228. {
  229.   photo_6pl_dither_ranges,
  230.   sizeof(photo_6pl_dither_ranges) / sizeof(simple_dither_range_t),
  231.   1.0
  232. };
  233.  
  234.  
  235. static simple_dither_range_t photo_pigment_dither_ranges[] =
  236. { /* MRS: Not calibrated! */
  237.   { 0.15,  0x1, 0, 1 },
  238.   { 0.227, 0x2, 0, 2 },
  239.   { 0.5,   0x1, 1, 1 },
  240.   { 1.0,   0x2, 1, 2 }
  241. };
  242.  
  243. static escp2_variable_ink_t photo_pigment_ink =
  244. {
  245.   photo_pigment_dither_ranges,
  246.   sizeof(photo_pigment_dither_ranges) / sizeof(simple_dither_range_t),
  247.   1.0
  248. };
  249.  
  250.  
  251. static simple_dither_range_t photo_4pl_dither_ranges[] =
  252. {
  253.   { 0.15,  0x1, 0, 1 },
  254.   { 0.227, 0x2, 0, 2 },
  255. /*  { 0.333, 0x3, 0, 3 }, */
  256.   { 0.45,  0x1, 1, 1 },
  257.   { 0.68,  0x2, 1, 2 },
  258.   { 1.0,   0x3, 1, 3 }
  259. };
  260.  
  261. static escp2_variable_ink_t photo_4pl_ink =
  262. {
  263.   photo_4pl_dither_ranges,
  264.   sizeof(photo_4pl_dither_ranges) / sizeof(simple_dither_range_t),
  265.   1.0
  266. };
  267.  
  268.  
  269. static simple_dither_range_t standard_6pl_dither_ranges[] =
  270. {
  271.   { 0.45,  0x1, 1, 1 },
  272.   { 0.68,  0x2, 1, 2 },
  273.   { 1.0,   0x3, 1, 3 }
  274. };
  275.  
  276. static escp2_variable_ink_t standard_6pl_ink =
  277. {
  278.   standard_6pl_dither_ranges,
  279.   sizeof(standard_6pl_dither_ranges) / sizeof(simple_dither_range_t),
  280.   1.0
  281. };
  282.  
  283.  
  284. static simple_dither_range_t standard_pigment_dither_ranges[] =
  285. { /* MRS: Not calibrated! */
  286.   { 0.55,  0x1, 1, 1 },
  287.   { 1.0,   0x2, 1, 2 }
  288. };
  289.  
  290. static escp2_variable_ink_t standard_pigment_ink =
  291. {
  292.   standard_pigment_dither_ranges,
  293.   sizeof(standard_pigment_dither_ranges) / sizeof(simple_dither_range_t),
  294.   1.0
  295. };
  296.  
  297.  
  298. static simple_dither_range_t standard_4pl_dither_ranges[] =
  299. {
  300.   { 0.45,  0x1, 1, 1 },
  301.   { 0.68,  0x2, 1, 2 },
  302.   { 1.0,   0x3, 1, 3 }
  303. };
  304.  
  305. static escp2_variable_ink_t standard_4pl_ink =
  306. {
  307.   standard_4pl_dither_ranges,
  308.   sizeof(standard_4pl_dither_ranges) / sizeof(simple_dither_range_t),
  309.   1.0
  310. };
  311.  
  312.  
  313. static simple_dither_range_t standard_3pl_dither_ranges[] =
  314. {
  315.   { 0.225, 0x1, 1, 1 },
  316.   { 0.68,  0x2, 1, 2 },
  317.   { 1.0,   0x3, 1, 3 }
  318. };
  319.  
  320. static escp2_variable_ink_t standard_3pl_ink =
  321. {
  322.   standard_3pl_dither_ranges,
  323.   sizeof(standard_3pl_dither_ranges) / sizeof(simple_dither_range_t),
  324.   1.0
  325. };
  326.  
  327.  
  328. static simple_dither_range_t photo_multishot_dither_ranges[] =
  329. {
  330.   { 0.1097, 0x1, 0, 1 },
  331.   { 0.227,  0x2, 0, 2 },
  332. /*  { 0.333, 0x3, 0, 3 }, */
  333.   { 0.28,   0x1, 1, 1 },
  334.   { 0.58,   0x2, 1, 2 },
  335.   { 0.85,   0x3, 1, 3 },
  336.   { 1.0,    0x3, 1, 3 }
  337. };
  338.  
  339. static escp2_variable_ink_t photo_multishot_ink =
  340. {
  341.   photo_multishot_dither_ranges,
  342.   sizeof(photo_multishot_dither_ranges) / sizeof(simple_dither_range_t),
  343.   1.0
  344. };
  345.  
  346.  
  347. static simple_dither_range_t standard_multishot_dither_ranges[] =
  348. {
  349.   { 0.28,  0x1, 1, 1 },
  350.   { 0.58,  0x2, 1, 2 },
  351.   { 0.85,  0x3, 1, 3 },
  352.   { 1.0,   0x3, 1, 3 }
  353. };
  354.  
  355. static escp2_variable_ink_t standard_multishot_ink =
  356. {
  357.   standard_multishot_dither_ranges,
  358.   sizeof(standard_multishot_dither_ranges) / sizeof(simple_dither_range_t),
  359.   1.0
  360. };
  361.  
  362.  
  363. static escp2_variable_inkset_t standard_inks =
  364. {
  365.   NULL,
  366.   NULL,
  367.   NULL,
  368.   NULL
  369. };
  370.  
  371. static escp2_variable_inkset_t photo_inks =
  372. {
  373.   &photo_ink,
  374.   &photo_ink,
  375.   NULL,
  376.   NULL
  377. };
  378.  
  379. static escp2_variable_inkset_t escp2_6pl_standard_inks =
  380. {
  381.   &standard_6pl_ink,
  382.   &standard_6pl_ink,
  383.   &standard_6pl_ink,
  384.   &standard_6pl_ink
  385. };
  386.  
  387. static escp2_variable_inkset_t escp2_6pl_photo_inks =
  388. {
  389.   &photo_6pl_ink,
  390.   &photo_6pl_ink,
  391.   &standard_6pl_ink,
  392.   &standard_6pl_ink
  393. };
  394.  
  395. static escp2_variable_inkset_t escp2_pigment_standard_inks =
  396. {
  397.   &standard_pigment_ink,
  398.   &standard_pigment_ink,
  399.   &standard_pigment_ink,
  400.   &standard_pigment_ink
  401. };
  402.  
  403. static escp2_variable_inkset_t escp2_pigment_photo_inks =
  404. {
  405.   &photo_pigment_ink,
  406.   &photo_pigment_ink,
  407.   &standard_pigment_ink,
  408.   &standard_pigment_ink
  409. };
  410.  
  411. static escp2_variable_inkset_t escp2_4pl_standard_inks =
  412. {
  413.   &standard_4pl_ink,
  414.   &standard_4pl_ink,
  415.   &standard_4pl_ink,
  416.   &standard_4pl_ink
  417. };
  418.  
  419. static escp2_variable_inkset_t escp2_4pl_photo_inks =
  420. {
  421.   &photo_4pl_ink,
  422.   &photo_4pl_ink,
  423.   &standard_4pl_ink,
  424.   &standard_4pl_ink
  425. };
  426.  
  427. static escp2_variable_inkset_t escp2_3pl_standard_inks =
  428. {
  429.   &standard_3pl_ink,
  430.   &standard_3pl_ink,
  431.   &standard_3pl_ink,
  432.   &standard_3pl_ink
  433. };
  434.  
  435. static escp2_variable_inkset_t escp2_multishot_standard_inks =
  436. {
  437.   &standard_multishot_ink,
  438.   &standard_multishot_ink,
  439.   &standard_multishot_ink,
  440.   &standard_multishot_ink
  441. };
  442.  
  443. static escp2_variable_inkset_t escp2_multishot_photo_inks =
  444. {
  445.   &photo_multishot_ink,
  446.   &photo_multishot_ink,
  447.   &standard_multishot_ink,
  448.   &standard_multishot_ink
  449. };
  450.  
  451.  
  452. static escp2_variable_inklist_t simple_4color_inks =
  453. {
  454.   &standard_inks,
  455.   &standard_inks,
  456.   &standard_inks,
  457.   &standard_inks,
  458.   NULL,
  459.   NULL,
  460.   NULL,
  461.   NULL,
  462.   NULL,
  463.   NULL,
  464.   NULL,
  465.   NULL,
  466.   NULL,
  467.   NULL,
  468.   NULL,
  469.   NULL
  470. };
  471.  
  472. static escp2_variable_inklist_t simple_6color_inks =
  473. {
  474.   &standard_inks,
  475.   &standard_inks,
  476.   &standard_inks,
  477.   &standard_inks,
  478.   &photo_inks,
  479.   &photo_inks,
  480.   &photo_inks,
  481.   &photo_inks,
  482.   NULL,
  483.   NULL,
  484.   NULL,
  485.   NULL,
  486.   NULL,
  487.   NULL,
  488.   NULL,
  489.   NULL
  490. };
  491.  
  492. static escp2_variable_inklist_t variable_6pl_4color_inks =
  493. {
  494.   &standard_inks,
  495.   &standard_inks,
  496.   &standard_inks,
  497.   &standard_inks,
  498.   NULL,
  499.   NULL,
  500.   NULL,
  501.   NULL,
  502.   &escp2_6pl_standard_inks,
  503.   &escp2_6pl_standard_inks,
  504.   &escp2_6pl_standard_inks,
  505.   &escp2_6pl_standard_inks,
  506.   NULL,
  507.   NULL,
  508.   NULL,
  509.   NULL
  510. };
  511.  
  512. static escp2_variable_inklist_t variable_6pl_6color_inks =
  513. {
  514.   &standard_inks,
  515.   &standard_inks,
  516.   &standard_inks,
  517.   &standard_inks,
  518.   &photo_inks,
  519.   &photo_inks,
  520.   &photo_inks,
  521.   &photo_inks,
  522.   &escp2_6pl_standard_inks,
  523.   &escp2_6pl_standard_inks,
  524.   &escp2_6pl_standard_inks,
  525.   &escp2_6pl_standard_inks,
  526.   &escp2_6pl_photo_inks,
  527.   &escp2_6pl_photo_inks,
  528.   &escp2_6pl_photo_inks,
  529.   &escp2_6pl_photo_inks
  530. };
  531.  
  532. static escp2_variable_inklist_t variable_pigment_inks =
  533. {
  534.   &standard_inks,
  535.   &standard_inks,
  536.   &standard_inks,
  537.   &standard_inks,
  538.   &photo_inks,
  539.   &photo_inks,
  540.   &photo_inks,
  541.   &photo_inks,
  542.   &escp2_pigment_standard_inks,
  543.   &escp2_pigment_standard_inks,
  544.   &escp2_pigment_standard_inks,
  545.   &escp2_pigment_standard_inks,
  546.   &escp2_pigment_photo_inks,
  547.   &escp2_pigment_photo_inks,
  548.   &escp2_pigment_photo_inks,
  549.   &escp2_pigment_photo_inks
  550. };
  551.  
  552. static escp2_variable_inklist_t variable_3pl_inks =
  553. {
  554.   &standard_inks,
  555.   &standard_inks,
  556.   &standard_inks,
  557.   &standard_inks,
  558.   NULL,
  559.   NULL,
  560.   NULL,
  561.   NULL,
  562.   &escp2_multishot_standard_inks,
  563.   &escp2_multishot_standard_inks,
  564.   &escp2_6pl_standard_inks,
  565.   &escp2_3pl_standard_inks,
  566.   NULL,
  567.   NULL,
  568.   NULL,
  569.   NULL
  570. };
  571.  
  572. static escp2_variable_inklist_t variable_4pl_4color_inks =
  573. {
  574.   &standard_inks,
  575.   &standard_inks,
  576.   &standard_inks,
  577.   &standard_inks,
  578.   NULL,
  579.   NULL,
  580.   NULL,
  581.   NULL,
  582.   &escp2_multishot_standard_inks,
  583.   &escp2_multishot_standard_inks,
  584.   &escp2_6pl_standard_inks,
  585.   &escp2_4pl_standard_inks,
  586.   NULL,
  587.   NULL,
  588.   NULL,
  589.   NULL
  590. };
  591.  
  592. static escp2_variable_inklist_t variable_4pl_6color_inks =
  593. {
  594.   &standard_inks,
  595.   &standard_inks,
  596.   &standard_inks,
  597.   &standard_inks,
  598.   &photo_inks,
  599.   &photo_inks,
  600.   &photo_inks,
  601.   &photo_inks,
  602.   &escp2_multishot_standard_inks,
  603.   &escp2_multishot_standard_inks,
  604.   &escp2_6pl_standard_inks,
  605.   &escp2_4pl_standard_inks,
  606.   &escp2_multishot_photo_inks,
  607.   &escp2_multishot_photo_inks,
  608.   &escp2_6pl_photo_inks,
  609.   &escp2_4pl_photo_inks
  610. };
  611.  
  612.  
  613. typedef struct escp2_printer
  614. {
  615.   model_cap_t    flags;        /* Bitmask of flags, see below */
  616.   int         nozzles;    /* Number of nozzles per color */
  617.   int        nozzle_separation; /* Separation between rows, in 1/720" */
  618.   int        black_nozzles;    /* Number of black nozzles (may be extra) */
  619.   int        black_nozzle_separation; /* Separation between rows */
  620.   int        xres;        /* Normal distance between dots in */
  621.                 /* softweave mode (inverse inches) */
  622.   int        enhanced_xres;    /* Distance between dots in highest */
  623.                 /* quality modes */
  624.   int        max_paper_width; /* Maximum paper width, in points*/
  625.   int        max_paper_height; /* Maximum paper height, in points */
  626.   int        left_margin;    /* Left margin, points */
  627.   int        right_margin;    /* Right margin, points */
  628.   int        top_margin;    /* Absolute top margin, points */
  629.   int        bottom_margin;    /* Absolute bottom margin, points */
  630.   int        separation_rows; /* Some printers require funky spacing */
  631.                 /* arguments in microweave mode. */
  632.   int        pseudo_separation_rows;/* Some printers require funky */
  633.                 /* spacing arguments in softweave mode */
  634.   escp2_dot_size_t dot_sizes;    /* Vector of dot sizes for resolutions */
  635.   escp2_densities_t densities;    /* List of densities for each printer */
  636.   escp2_variable_inklist_t *inks; /* Choices of inks for this printer */
  637. } escp2_printer_t;
  638.  
  639. #define MODEL_INIT_MASK        0xfull /* Is a special init sequence */
  640. #define MODEL_INIT_STANDARD    0x0ull /* required for this printer, and if */
  641. #define MODEL_INIT_900        0x1ull /* so, what */
  642.  
  643. #define MODEL_HASBLACK_MASK    0x10ull /* Can this printer print black ink */
  644. #define MODEL_HASBLACK_YES    0x00ull /* when it is also printing color? */
  645. #define MODEL_HASBLACK_NO    0x10ull /* Only the 1500 can't. */
  646.  
  647. #define MODEL_6COLOR_MASK    0x20ull /* Is this a 6-color printer? */
  648. #define MODEL_6COLOR_NO        0x00ull
  649. #define MODEL_6COLOR_YES    0x20ull
  650.  
  651. #define MODEL_1440DPI_MASK    0x40ull /* Can this printer do 1440x720 dpi? */
  652. #define MODEL_1440DPI_NO    0x00ull
  653. #define MODEL_1440DPI_YES    0x40ull
  654.  
  655. #define MODEL_GRAYMODE_MASK    0x80ull /* Does this printer support special */
  656. #define MODEL_GRAYMODE_NO    0x00ull /* fast black printing? */
  657. #define MODEL_GRAYMODE_YES    0x80ull
  658.  
  659. #define MODEL_720DPI_MODE_MASK    0x300ull /* Does this printer require old */
  660. #define MODEL_720DPI_DEFAULT    0x000ull /* or new setting for printing */
  661. #define MODEL_720DPI_600    0x100ull /* 720 dpi?  Only matters for */
  662.                          /* single dot size printers */
  663.  
  664. #define MODEL_VARIABLE_DOT_MASK    0xc00ull /* Does this printer support var */
  665. #define MODEL_VARIABLE_NORMAL    0x000ull /* dot size printing? The newest */
  666. #define MODEL_VARIABLE_4    0x400ull /* printers support multiple modes */
  667. #define MODEL_VARIABLE_MULTI    0x800ull /* of variable dot sizes. */
  668.  
  669. #define MODEL_COMMAND_MASK    0xf000ull /* What general command set does */
  670. #define MODEL_COMMAND_GENERIC    0x0000ull /* this printer use? */
  671. #define MODEL_COMMAND_1998    0x1000ull
  672. #define MODEL_COMMAND_1999    0x2000ull /* The 1999 series printers */
  673.  
  674. #define MODEL_INK_MASK        0x10000ull /* Does this printer support */
  675. #define MODEL_INK_NORMAL    0x00000ull /* different types of inks? */
  676. #define MODEL_INK_SELECTABLE    0x10000ull /* Only the Stylus Pro's do */
  677.  
  678. #define MODEL_ROLLFEED_MASK    0x20000ull /* Does this printer support */
  679. #define MODEL_ROLLFEED_NO    0x00000ull /* a roll feed? */
  680. #define MODEL_ROLLFEED_YES    0x20000ull
  681.  
  682. #define MODEL_ZEROMARGIN_MASK    0x40000ull /* Does this printer support */
  683. #define MODEL_ZEROMARGIN_NO    0x00000ull /* zero margin mode? */
  684. #define MODEL_ZEROMARGIN_YES    0x40000ull /* (print to the edge of the paper) */
  685.  
  686. #define INCH(x)        (72 * x)
  687.  
  688. static escp2_printer_t model_capabilities[] =
  689. {
  690.   /* FIRST GENERATION PRINTERS */
  691.   /* 0: Stylus Color */
  692.   {
  693.     (MODEL_INIT_STANDARD | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  694.      | MODEL_6COLOR_NO | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_NORMAL
  695.      | MODEL_COMMAND_GENERIC | MODEL_GRAYMODE_YES | MODEL_1440DPI_NO
  696.      | MODEL_ROLLFEED_NO | MODEL_ZEROMARGIN_NO),
  697.     1, 1, 1, 1, 720, 720, INCH(17 / 2), INCH(14), 14, 14, 9, 49, 1, 0,
  698.     { -2, -2, -1, -2, -1, -1, -1 },
  699.     { 2.0, 1.3, 1.3, .568, 0, 0, 0, 0, 0 },
  700.     &simple_4color_inks
  701.   },
  702.   /* 1: Stylus Color Pro/Pro XL/400/500 */
  703.   {
  704.     (MODEL_INIT_STANDARD | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  705.      | MODEL_6COLOR_NO | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_NORMAL
  706.      | MODEL_COMMAND_GENERIC | MODEL_GRAYMODE_NO | MODEL_1440DPI_NO
  707.      | MODEL_ROLLFEED_NO | MODEL_ZEROMARGIN_NO),
  708.     48, 6, 48, 6, 720, 720, INCH(17 / 2), INCH(14), 14, 14, 0, 30, 1, 0,
  709.     { -2, -2, -1, -2, -1, -1, -1 },
  710.     { 2.0, 1.3, 1.3, .631, 0, 0, 0, 0, 0 },
  711.     &simple_4color_inks
  712.   },
  713.   /* 2: Stylus Color 1500 */
  714.   {
  715.     (MODEL_INIT_STANDARD | MODEL_HASBLACK_NO | MODEL_INK_NORMAL
  716.      | MODEL_6COLOR_NO | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_NORMAL
  717.      | MODEL_COMMAND_GENERIC | MODEL_GRAYMODE_NO | MODEL_1440DPI_NO
  718.      | MODEL_ROLLFEED_YES | MODEL_ZEROMARGIN_NO),
  719.     1, 1, 1, 1, 720, 720, INCH(11), INCH(17), 14, 14, 9, 49, 1, 0,
  720.     { -2, -2, -1, -2, -1, -1, -1 },
  721.     { 2.0, 1.3, 1.3, .631, 0, 0, 0, 0, 0 },
  722.     &simple_4color_inks
  723.   },
  724.   /* 3: Stylus Color 600 */
  725.   {
  726.     (MODEL_INIT_STANDARD | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  727.      | MODEL_6COLOR_NO | MODEL_720DPI_600 | MODEL_VARIABLE_NORMAL
  728.      | MODEL_COMMAND_GENERIC | MODEL_GRAYMODE_YES | MODEL_1440DPI_YES
  729.      | MODEL_ROLLFEED_NO | MODEL_ZEROMARGIN_NO),
  730.     32, 8, 32, 8, 720, 360, INCH(17 / 2), INCH(14), 8, 9, 0, 30, 1, 0,
  731.     { 4, 4, -1, 2, 2, -1, 1 },
  732.     { 2.0, 1.3, 1.3, .775, .775, .55, .55, .275, .138 },
  733.     &simple_4color_inks
  734.   },
  735.   /* 4: Stylus Color 800 */
  736.   {
  737.     (MODEL_INIT_STANDARD | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  738.      | MODEL_6COLOR_NO | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_NORMAL
  739.      | MODEL_COMMAND_GENERIC | MODEL_GRAYMODE_YES | MODEL_1440DPI_YES
  740.      | MODEL_ROLLFEED_NO | MODEL_ZEROMARGIN_NO),
  741.     64, 4, 64, 4, 720, 360, INCH(17 / 2), INCH(14), 8, 9, 9, 40, 1, 4,
  742.     { 3, 3, -1, 1, 1, -1, 4 },
  743.     { 2.0, 1.3, 1.3, .775, .775, .55, .55, .275, .138 },
  744.     &simple_4color_inks
  745.   },
  746.   /* 5: Stylus Color 850 */
  747.   {
  748.     (MODEL_INIT_STANDARD | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  749.      | MODEL_6COLOR_NO | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_NORMAL
  750.      | MODEL_COMMAND_GENERIC | MODEL_GRAYMODE_YES | MODEL_1440DPI_YES
  751.      | MODEL_ROLLFEED_NO | MODEL_ZEROMARGIN_NO),
  752.     64, 4, 128, 2, 720, 360, INCH(17 / 2), INCH(14), 9, 9, 9, 40, 1, 4,
  753.     { 3, 3, -1, 1, 1, -1, 4 },
  754.     { 2.0, 1.3, 1.3, .775, .775, .55, .55, .275, .138 },
  755.     &simple_4color_inks
  756.   },
  757.   /* 6: Stylus Color 1520 */
  758.   {
  759.     (MODEL_INIT_STANDARD | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  760.      | MODEL_6COLOR_NO | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_NORMAL
  761.      | MODEL_COMMAND_GENERIC | MODEL_GRAYMODE_YES | MODEL_1440DPI_YES
  762.      | MODEL_ROLLFEED_YES | MODEL_ZEROMARGIN_NO),
  763.     64, 4, 64, 4, 720, 360, INCH(17), INCH(55), 8, 9, 9, 40, 1, 4,
  764.     { 3, 3, -1, 1, 1, -1, 4 },
  765.     { 2.0, 1.3, 1.3, .775, .775, .55, .55, .275, .138 },
  766.     &simple_4color_inks
  767.   },
  768.  
  769.   /* SECOND GENERATION PRINTERS */
  770.   /* 7: Stylus Photo 700 */
  771.   {
  772.     (MODEL_INIT_STANDARD | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  773.      | MODEL_6COLOR_YES | MODEL_720DPI_600 | MODEL_VARIABLE_NORMAL
  774.      | MODEL_COMMAND_1998 | MODEL_GRAYMODE_NO | MODEL_1440DPI_YES
  775.      | MODEL_ROLLFEED_NO | MODEL_ZEROMARGIN_NO),
  776.     32, 8, 32, 8, 720, 360, INCH(17 / 2), INCH(14), 9, 9, 0, 30, 1, 0,
  777.     { 3, 3, -1, -1, 1, -1, 4 },
  778.     { 2.0, 1.3, 1.3, .775, .775, .55, .55, .275, .138 },
  779.     &simple_6color_inks
  780.   },
  781.   /* 8: Stylus Photo EX */
  782.   {
  783.     (MODEL_INIT_STANDARD | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  784.      | MODEL_6COLOR_YES | MODEL_720DPI_600 | MODEL_VARIABLE_NORMAL
  785.      | MODEL_COMMAND_1998 | MODEL_GRAYMODE_NO | MODEL_1440DPI_YES
  786.      | MODEL_ROLLFEED_NO | MODEL_ZEROMARGIN_NO),
  787.     32, 8, 32, 8, 720, 360, INCH(11), INCH(17), 9, 9, 0, 30, 1, 0,
  788.     { 3, 3, -1, -1, 1, -1, 4 },
  789.     { 2.0, 1.3, 1.3, .775, .775, .55, .55, .275, .138 },
  790.     &simple_6color_inks
  791.   },
  792.   /* 9: Stylus Photo */
  793.   {
  794.     (MODEL_INIT_STANDARD | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  795.      | MODEL_6COLOR_YES | MODEL_720DPI_600 | MODEL_VARIABLE_NORMAL
  796.      | MODEL_COMMAND_1998 | MODEL_GRAYMODE_NO | MODEL_1440DPI_NO
  797.      | MODEL_ROLLFEED_NO | MODEL_ZEROMARGIN_NO),
  798.     32, 8, 32, 8, 720, 360, INCH(17 / 2), INCH(14), 9, 9, 0, 30, 1, 0,
  799.     { 3, 3, -1, -1, 1, -1, -1 },
  800.     { 2.0, 1.3, 1.3, .775, .775, 0, 0, 0, 0 },
  801.     &simple_6color_inks
  802.   },
  803.  
  804.   /* THIRD GENERATION PRINTERS */
  805.   /* 10: Stylus Color 440/460 */
  806.   /* Thorsten Schnier has confirmed that the separation is 8.  Why on */
  807.   /* earth anyone would use 21 nozzles when designing a print head is */
  808.   /* completely beyond me, but there you are... */
  809.   {
  810.     (MODEL_INIT_STANDARD | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  811.      | MODEL_6COLOR_NO | MODEL_720DPI_600 | MODEL_VARIABLE_NORMAL
  812.      | MODEL_COMMAND_1999 | MODEL_GRAYMODE_YES | MODEL_1440DPI_NO
  813.      | MODEL_ROLLFEED_NO | MODEL_ZEROMARGIN_NO),
  814.     21, 8, 21, 8, 720, 720, INCH(17 / 2), INCH(14), 9, 9, 0, 9, 1, 0,
  815.     { -1, 3, -1, 1, 1, -1, -1 },
  816.     { 3.0, 2.0, 2.0, .900, .900, 0, 0, 0, 0 },
  817.     &simple_4color_inks
  818.   },
  819.   /* 11: Stylus Color 640 */
  820.   {
  821.     (MODEL_INIT_STANDARD | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  822.      | MODEL_6COLOR_NO | MODEL_720DPI_600 | MODEL_VARIABLE_NORMAL
  823.      | MODEL_COMMAND_1999 | MODEL_GRAYMODE_YES | MODEL_1440DPI_YES
  824.      | MODEL_ROLLFEED_NO | MODEL_ZEROMARGIN_NO),
  825.     32, 8, 64, 4, 720, 720, INCH(17 / 2), INCH(14), 9, 9, 0, 9, 1, 0,
  826.     { -1, 3, -1, 1, 1, -1, 1 },
  827.     { 3.0, 2.0, 2.0, .900, .900, .45, .45, .225, .113 },
  828.     &simple_4color_inks
  829.   },
  830.   /* 12: Stylus Color 740 */
  831.   {
  832.     (MODEL_INIT_STANDARD | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  833.      | MODEL_6COLOR_NO | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_4
  834.      | MODEL_COMMAND_1999 | MODEL_GRAYMODE_YES | MODEL_1440DPI_YES
  835.      | MODEL_ROLLFEED_NO | MODEL_ZEROMARGIN_NO),
  836.     48, 6, 144, 2, 360, 360, INCH(17 / 2), INCH(14), 9, 9, 0, 9, 1, 0,
  837.     { -1, 4, 0x10, 3, 0x10, -1, 0x10 },
  838.     { 2.0, 1.3, 2.0, .646, .710, .323, .365, .1825, .0913 },
  839.     &variable_6pl_4color_inks
  840.   },
  841.   /* 13: Stylus Color 900 */
  842.   /* Still need to figure out density for 3 pl drops! */
  843.   {
  844.     (MODEL_INIT_900 | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  845.      | MODEL_6COLOR_NO | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_4
  846.      | MODEL_COMMAND_1999 | MODEL_GRAYMODE_YES | MODEL_1440DPI_YES
  847.      | MODEL_ROLLFEED_NO | MODEL_ZEROMARGIN_NO),
  848.     96, 4, 192, 2, 360, 180, INCH(17 / 2), INCH(44), 9, 9, 0, 9, 1, 0,
  849.     { -1, 1, 0x11, 1, 0x10, -1, 0x10 },
  850.     { 2.0, 1.3, 1.3, .646, .710, .323, .365, .1825, .0913 },
  851.     &variable_3pl_inks
  852.   },
  853.   /* 14: Stylus Photo 750 */
  854.   {
  855.     (MODEL_INIT_900 | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  856.      | MODEL_6COLOR_YES | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_4
  857.      | MODEL_COMMAND_1999 | MODEL_GRAYMODE_NO | MODEL_1440DPI_YES
  858.      | MODEL_ROLLFEED_NO | MODEL_ZEROMARGIN_NO),
  859.     48, 6, 48, 6, 360, 360, INCH(17 / 2), INCH(44), 9, 9, 0, 9, 1, 0,
  860.     { -1, 2, 0x10, 4, 0x10, -1, 0x10 },
  861.     { 2.0, 1.3, 1.3, .646, .710, .323, .365, .1825, .0913 },
  862.     &variable_6pl_6color_inks
  863.   },
  864.   /* 15: Stylus Photo 1200 */
  865.   {
  866.     (MODEL_INIT_900 | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  867.      | MODEL_6COLOR_YES | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_4
  868.      | MODEL_COMMAND_1999 | MODEL_GRAYMODE_NO | MODEL_1440DPI_YES
  869.      | MODEL_ROLLFEED_YES | MODEL_ZEROMARGIN_NO),
  870.     48, 6, 48, 6, 360, 360, INCH(13), INCH(44), 9, 9, 0, 9, 1, 0,
  871.     { -1, 2, 0x10, 4, 0x10, -1, 0x10 },
  872.     { 2.0, 1.3, 1.3, .646, .710, .323, .365, .1825, .0913 },
  873.     &variable_6pl_6color_inks
  874.   },
  875.   /* 16: Stylus Color 860 */
  876.   {
  877.     (MODEL_INIT_900 | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  878.      | MODEL_6COLOR_NO | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_MULTI
  879.      | MODEL_COMMAND_1999 | MODEL_GRAYMODE_YES | MODEL_1440DPI_YES
  880.      | MODEL_ROLLFEED_NO | MODEL_ZEROMARGIN_NO),
  881.     48, 6, 144, 2, 360, 360, INCH(17 / 2), INCH(14), 9, 9, 0, 9, 1, 0,
  882.     { -1, 0, 0x12, 0, 0x11, -1, 0x10 },
  883.     { 2.0, 1.3, 1.3, .431, .710, .216, .533, .2665, .1333 },
  884.     &variable_4pl_4color_inks
  885.   },
  886.   /* 17: Stylus Color 1160 */
  887.   {
  888.     (MODEL_INIT_900 | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  889.      | MODEL_6COLOR_NO | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_MULTI
  890.      | MODEL_COMMAND_1999 | MODEL_GRAYMODE_YES | MODEL_1440DPI_YES
  891.      | MODEL_ROLLFEED_NO | MODEL_ZEROMARGIN_NO),
  892.     48, 6, 144, 2, 360, 360, INCH(13), INCH(44), 9, 9, 0, 9, 1, 0,
  893.     { -1, 0, 0x12, 0, 0x11, -1, 0x10 },
  894.     { 2.0, 1.3, 1.3, .431, .710, .216, .533, .2665, .1333 },
  895.     &variable_4pl_4color_inks
  896.   },
  897.   /* 18: Stylus Color 660 */
  898.   {
  899.     (MODEL_INIT_900 | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  900.      | MODEL_6COLOR_NO | MODEL_720DPI_600 | MODEL_VARIABLE_NORMAL
  901.      | MODEL_COMMAND_1998 | MODEL_GRAYMODE_YES | MODEL_1440DPI_YES
  902.      | MODEL_ROLLFEED_NO | MODEL_ZEROMARGIN_NO),
  903.     32, 8, 64, 4, 720, 720, INCH(17 / 2), INCH(44), 9, 9, 0, 9, 1, 8,
  904.     { -1, 3, -1, 3, 0, -1, 0 },
  905.     { 3.0, 2.0, 2.0, .646, .646, .323, .323, .1615, .0808 },
  906.     &simple_4color_inks
  907.   },
  908.   /* 19: Stylus Color 760 */
  909.   {
  910.     (MODEL_INIT_900 | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  911.      | MODEL_6COLOR_NO | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_MULTI
  912.      | MODEL_COMMAND_1999 | MODEL_GRAYMODE_YES | MODEL_1440DPI_YES
  913.      | MODEL_ROLLFEED_NO | MODEL_ZEROMARGIN_NO),
  914.     48, 6, 144, 2, 360, 360, INCH(17 / 2), INCH(14), 9, 9, 0, 9, 1, 0,
  915.     { -1, 0, 0x12, 0, 0x11, -1, 0x10 },
  916.     { 2.0, 1.3, 1.3, .431, .710, .216, .533, .2665, .1333 },
  917.     &variable_4pl_4color_inks
  918.   },
  919.   /* 20: Stylus Photo 720 (Australia) */
  920.   {
  921.     (MODEL_INIT_900 | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  922.      | MODEL_6COLOR_YES | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_4
  923.      | MODEL_COMMAND_1999 | MODEL_GRAYMODE_NO | MODEL_1440DPI_YES
  924.      | MODEL_ROLLFEED_NO | MODEL_ZEROMARGIN_NO),
  925.     32, 8, 32, 8, 360, 360, INCH(17 / 2), INCH(14), 9, 9, 0, 9, 1, 0,
  926.     { -1, 2, 0x12, 4, 0x11, -1, 0x11 },
  927.     { 2.0, 1.3, 1.3, .646, .710, .323, .365, .1825, .0913 },
  928.     &variable_6pl_6color_inks
  929.   },
  930.   /* 21: Stylus Color 480 */
  931.   {
  932.     (MODEL_INIT_900 | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  933.      | MODEL_6COLOR_NO | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_4
  934.      | MODEL_COMMAND_1999 | MODEL_GRAYMODE_NO | MODEL_1440DPI_NO
  935.      | MODEL_ROLLFEED_NO | MODEL_ZEROMARGIN_NO),
  936.     15, 6, 15, 6, 360, 360, INCH(17 / 2), INCH(14), 9, 9, 0, 9, 1, 0,
  937.     { -1, -2, 0x13, -2, 0x10, -1, -1 },
  938.     { 2.0, 1.3, 1.3, .646, .710, .323, .365, .1825, .0913 },
  939.     &variable_6pl_4color_inks
  940.   },
  941.   /* 22: Stylus Photo 870 */
  942.   {
  943.     (MODEL_INIT_900 | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  944.      | MODEL_6COLOR_YES | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_MULTI
  945.      | MODEL_COMMAND_1999 | MODEL_GRAYMODE_NO | MODEL_1440DPI_YES
  946.      | MODEL_ROLLFEED_YES | MODEL_ZEROMARGIN_YES),
  947.     48, 6, 48, 6, 360, 360, INCH(17 / 2), INCH(44), 0, 0, 0, 9, 1, 0,
  948.     { -1, 4, 0x12, 2, 0x11, -1, 0x10 },
  949.     { 2.0, 1.3, 1.3, .431, .710, .216, .533, .2665, .1333 },
  950.     &variable_4pl_6color_inks
  951.   },
  952.   /* 23: Stylus Photo 1270 */
  953.   {
  954.     (MODEL_INIT_900 | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  955.      | MODEL_6COLOR_YES | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_MULTI
  956.      | MODEL_COMMAND_1999 | MODEL_GRAYMODE_NO | MODEL_1440DPI_YES
  957.      | MODEL_ROLLFEED_YES | MODEL_ZEROMARGIN_YES),
  958.     48, 6, 48, 6, 360, 360, INCH(13), INCH(44), 0, 0, 0, 9, 1, 0,
  959.     { -1, 4, 0x12, 2, 0x11, -1, 0x10 },
  960.     { 2.0, 1.3, 1.3, .431, .710, .216, .533, .2665, .1333 },
  961.     &variable_4pl_6color_inks
  962.   },
  963.   /* 24: Stylus Color 3000 */
  964.   {
  965.     (MODEL_INIT_STANDARD | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  966.      | MODEL_6COLOR_NO | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_NORMAL
  967.      | MODEL_COMMAND_GENERIC | MODEL_GRAYMODE_YES | MODEL_1440DPI_YES
  968.      | MODEL_ROLLFEED_YES | MODEL_ZEROMARGIN_NO),
  969.     64, 4, 64, 4, 720, 360, INCH(17), INCH(55), 8, 9, 9, 40, 1, 4,
  970.     { 3, 3, -1, 1, 1, -1, 4 },
  971.     { 2.0, 1.3, 1.3, .775, .775, .55, .55, .275, .138 },
  972.     &simple_4color_inks
  973.   },
  974.   /* 25: Stylus Color 670 */
  975.   {
  976.     (MODEL_INIT_900 | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  977.      | MODEL_6COLOR_NO | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_MULTI
  978.      | MODEL_COMMAND_1999 | MODEL_GRAYMODE_YES | MODEL_1440DPI_YES
  979.      | MODEL_ROLLFEED_NO | MODEL_ZEROMARGIN_NO),
  980.     32, 8, 64, 4, 360, 360, INCH(17 / 2), INCH(44), 9, 9, 0, 9, 1, 0,
  981.     { -1, 3, 0x12, 3, 0x11, -1, 0x11 },
  982.     { 2.0, 1.3, 1.3, .646, .710, .323, .365, .1825, .0913 },
  983.     &variable_6pl_4color_inks
  984.   },
  985.   /* 26: Stylus Photo 2000P */
  986.   {
  987.     (MODEL_INIT_900 | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  988.      | MODEL_6COLOR_YES | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_4
  989.      | MODEL_COMMAND_1999 | MODEL_GRAYMODE_NO | MODEL_1440DPI_YES
  990.      | MODEL_ROLLFEED_NO | MODEL_ZEROMARGIN_NO),
  991.     48, 6, 144, 2, 360, 360, INCH(17 / 2), INCH(44), 9, 9, 0, 9, 1, 0,
  992.     { -1, 2, 0x11, 4, 0x10, -1, 0x10 },
  993.     { 2.0, 1.3, 1.3, .775, .852, .388, .438, .219, .110 },
  994.     &variable_pigment_inks
  995.   },
  996.   /* 27: Stylus Pro 5000 */
  997.   {
  998.     (MODEL_INIT_900 | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  999.      | MODEL_6COLOR_YES | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_NORMAL
  1000.      | MODEL_COMMAND_1999 | MODEL_GRAYMODE_NO | MODEL_1440DPI_YES
  1001.      | MODEL_ROLLFEED_NO | MODEL_ZEROMARGIN_NO),
  1002.     64, 4, 64, 4, 360, 360, INCH(13), INCH(1200), 9, 9, 0, 9, 1, 0,
  1003.     { -1, 2, -1, 4, 0, 4, 0 },
  1004.     { 2.0, 1.3, 1.3, .646, .646, .323, .323, .1615, .0808 },
  1005.     &simple_6color_inks
  1006.   },
  1007.   /* 28: Stylus Pro 7000 */
  1008.   {
  1009.     (MODEL_INIT_900 | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  1010.      | MODEL_6COLOR_YES | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_NORMAL
  1011.      | MODEL_COMMAND_1999 | MODEL_GRAYMODE_NO | MODEL_1440DPI_YES
  1012.      | MODEL_ROLLFEED_YES | MODEL_ZEROMARGIN_NO),
  1013.     64, 4, 64, 4, 360, 360, INCH(24), INCH(1200), 9, 9, 0, 9, 1, 0,
  1014.     { -1, 2, -1, 4, 0, 4, 0 },
  1015.     { 2.0, 1.3, 1.3, .646, .646, .323, .323, .1615, .0808 },
  1016.     &simple_6color_inks
  1017.   },
  1018.   /* 29: Stylus Pro 7500 */
  1019.   {
  1020.     (MODEL_INIT_900 | MODEL_HASBLACK_YES | MODEL_INK_SELECTABLE
  1021.      | MODEL_6COLOR_YES | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_NORMAL
  1022.      | MODEL_COMMAND_1999 | MODEL_GRAYMODE_NO | MODEL_1440DPI_YES
  1023.      | MODEL_ROLLFEED_YES | MODEL_ZEROMARGIN_NO),
  1024.     64, 4, 64, 4, 360, 360, INCH(24), INCH(1200), 9, 9, 0, 9, 1, 0,
  1025.     { -1, 2, -1, 4, 0, 4, 0 },
  1026.     { 2.0, 1.3, 1.3, .646, .646, .323, .323, .1615, .0808 },
  1027.     &simple_6color_inks
  1028.   },
  1029.   /* 30: Stylus Pro 9000 */
  1030.   {
  1031.     (MODEL_INIT_900 | MODEL_HASBLACK_YES | MODEL_INK_NORMAL
  1032.      | MODEL_6COLOR_YES | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_NORMAL
  1033.      | MODEL_COMMAND_1999 | MODEL_GRAYMODE_NO | MODEL_1440DPI_YES
  1034.      | MODEL_ROLLFEED_YES | MODEL_ZEROMARGIN_NO),
  1035.     64, 4, 64, 4, 360, 360, INCH(44), INCH(1200), 9, 9, 0, 9, 1, 0,
  1036.     { -1, 2, -1, 4, 0, 4, 0 },
  1037.     { 2.0, 1.3, 1.3, .646, .646, .323, .323, .1615, .0808 },
  1038.     &simple_6color_inks
  1039.   },
  1040.   /* 31: Stylus Pro 9500 */
  1041.   {
  1042.     (MODEL_INIT_900 | MODEL_HASBLACK_YES | MODEL_INK_SELECTABLE
  1043.      | MODEL_6COLOR_YES | MODEL_720DPI_DEFAULT | MODEL_VARIABLE_NORMAL
  1044.      | MODEL_COMMAND_1999 | MODEL_GRAYMODE_NO | MODEL_1440DPI_YES
  1045.      | MODEL_ROLLFEED_YES | MODEL_ZEROMARGIN_NO),
  1046.     64, 4, 64, 4, 360, 360, INCH(44), INCH(1200), 9, 9, 0, 9, 1, 0,
  1047.     { -1, 2, -1, 4, 0, 4, 0 },
  1048.     { 2.0, 1.3, 1.3, .646, .646, .323, .323, .1615, .0808 },
  1049.     &simple_6color_inks
  1050.   },
  1051.  
  1052. };
  1053.  
  1054. typedef struct escp_init
  1055. {
  1056.   int model;
  1057.   int output_type;
  1058.   int ydpi;
  1059.   int xdpi;
  1060.   int use_softweave;
  1061.   int page_length;
  1062.   int page_width;
  1063.   int page_top;
  1064.   int page_bottom;
  1065.   int nozzles;
  1066.   int nozzle_separation;
  1067.   int horizontal_passes;
  1068.   int vertical_passes;
  1069.   int vertical_oversample;
  1070.   int bits;
  1071.   const char *paper_type;
  1072. } escp_init_t;
  1073.  
  1074. typedef struct {
  1075.   const char name[65];
  1076.   int hres;
  1077.   int vres;
  1078.   int softweave;
  1079.   int vertical_passes;
  1080.   int vertical_oversample;
  1081. } res_t;
  1082.  
  1083. static const res_t escp2_reslist[] = {
  1084.   { "180 DPI",                        180,  180,  0, 1, 1 },
  1085.   { "360 DPI",                        360,  360,  0, 1, 1 },
  1086.   { "360 DPI Softweave",              360,  360,  1, 1, 1 },
  1087.   { "360 DPI High Quality",           360,  360,  1, 2, 1 },
  1088.   { "720 DPI Microweave",             720,  720,  0, 1, 1 },
  1089.   { "720 DPI Softweave",              720,  720,  1, 1, 1 },
  1090.   { "720 DPI High Quality",           720,  720,  1, 2, 1 },
  1091.   { "720 DPI Highest Quality",        720,  720,  1, 4, 1 },
  1092.   { "1440 x 720 DPI Microweave",      1440, 720,  0, 1, 1 },
  1093.   { "1440 x 720 DPI Softweave",       1440, 720,  1, 1, 1 },
  1094.   { "1440 x 720 DPI Highest Quality", 1440, 720,  1, 2, 1 },
  1095.   { "1440 x 1440 DPI Emulated",       1440, 1440, 1, 1, 2 },
  1096.   { "1440 x 2880 DPI Emulated",       1440, 2880, 1, 1, 4 },
  1097.   { "", 0, 0, 0, 0, 0 }
  1098. };
  1099.  
  1100. typedef struct {
  1101.   const char name[65];
  1102.   int is_color;
  1103.   int variable_dot_size;
  1104.   int dot_size_bits;
  1105.   simple_dither_range_t *standard_dither;
  1106.   simple_dither_range_t *photo_dither;
  1107. } ink_t;
  1108.  
  1109. typedef struct {
  1110.   const char name[65];
  1111.   int paper_feed_sequence;
  1112.   int platen_gap;
  1113.   double base_density;
  1114.   double k_lower_scale;
  1115.   double k_upper;
  1116. } paper_t;
  1117.  
  1118. static const paper_t escp2_paper_list[] = {
  1119.   { "Plain Paper", 1, 0, .5, .25, .5 },
  1120.   { "Plain Paper Fast Load", 5, 0, .5, .25, .5 },
  1121.   { "Postcard", 2, 0, .6, .25, .6 },
  1122.   { "Glossy Film", 3, 0, 1.0, 1.0, .999 },
  1123.   { "Transparencies", 3, 0, 1.0, 1.0, .999 },
  1124.   { "Envelopes", 4, 0, .5, .25, .5 },
  1125.   { "Back Light Film", 6, 0, 1.0, 1.0, .999 },
  1126.   { "Matte Paper", 7, 0, 1.0, 1.0, .999 },
  1127.   { "Inkjet Paper", 7, 0, .78, .25, .6 },
  1128.   { "Photo Quality Inkjet Paper", 7, 0, 1, 1.0, .999 },
  1129.   { "Photo Paper", 8, 0, 1, 1.0, .999 },
  1130.   { "Premium Glossy Photo Paper", 8, 0, .9, 1.0, .999 },
  1131.   { "Photo Quality Glossy Paper", 6, 0, 1.0, 1.0, .999 },
  1132.   { "Other", 0, 0, .5, .25, .5 },
  1133. };
  1134.  
  1135. static const int paper_type_count = sizeof(escp2_paper_list) / sizeof(paper_t);
  1136.  
  1137.  
  1138. static const paper_t *
  1139. get_media_type(const char *name)
  1140. {
  1141.   int i;
  1142.   for (i = 0; i < paper_type_count; i++)
  1143.     {
  1144.       if (!strcmp(name, escp2_paper_list[i].name))
  1145.     return &(escp2_paper_list[i]);
  1146.     }
  1147.   return NULL;
  1148. }
  1149.  
  1150. static int
  1151. escp2_has_cap(int model, model_featureset_t featureset,
  1152.           model_featureset_t class)
  1153. {
  1154.   return ((model_capabilities[model].flags & featureset) == class);
  1155. }
  1156.  
  1157. static unsigned
  1158. escp2_nozzles(int model)
  1159. {
  1160.   return (model_capabilities[model].nozzles);
  1161. }
  1162.  
  1163. static unsigned
  1164. escp2_black_nozzles(int model)
  1165. {
  1166.   return (model_capabilities[model].black_nozzles);
  1167. }
  1168.  
  1169. static unsigned
  1170. escp2_nozzle_separation(int model)
  1171. {
  1172.   return (model_capabilities[model].nozzle_separation);
  1173. }
  1174.  
  1175. static unsigned
  1176. escp2_black_nozzle_separation(int model)
  1177. {
  1178.   return (model_capabilities[model].black_nozzle_separation);
  1179. }
  1180.  
  1181. static unsigned
  1182. escp2_separation_rows(int model)
  1183. {
  1184.   return (model_capabilities[model].separation_rows);
  1185. }
  1186.  
  1187. static unsigned
  1188. escp2_xres(int model)
  1189. {
  1190.   return (model_capabilities[model].xres);
  1191. }
  1192.  
  1193. static unsigned
  1194. escp2_enhanced_xres(int model)
  1195. {
  1196.   return (model_capabilities[model].enhanced_xres);
  1197. }
  1198.  
  1199. static int
  1200. escp2_ink_type(int model, int resolution, int microweave)
  1201. {
  1202.   if (microweave)
  1203.     {
  1204.       switch (resolution)
  1205.     {
  1206.     case 180:
  1207.       return model_capabilities[model].dot_sizes.dot_180;
  1208.     case 360:
  1209.       return model_capabilities[model].dot_sizes.dot_360_microweave;
  1210.     case 720:
  1211.       return model_capabilities[model].dot_sizes.dot_720_microweave;
  1212.     case 1440:
  1213.       return model_capabilities[model].dot_sizes.dot_1440_microweave;
  1214.     default:
  1215.       return -1;
  1216.     }
  1217.     }
  1218.   else
  1219.     {
  1220.       switch (resolution)
  1221.     {
  1222.     case 180:
  1223.       return model_capabilities[model].dot_sizes.dot_180;
  1224.     case 360:
  1225.       return model_capabilities[model].dot_sizes.dot_360;
  1226.     case 720:
  1227.       return model_capabilities[model].dot_sizes.dot_720;
  1228.     case 1440:
  1229.       return model_capabilities[model].dot_sizes.dot_1440;
  1230.     default:
  1231.       return -1;
  1232.     }
  1233.     }
  1234. }
  1235.  
  1236. static double
  1237. escp2_density(int model, int xdpi, int ydpi, int microweave)
  1238. {
  1239.   if (microweave)
  1240.     {
  1241.       switch (xdpi)
  1242.     {    
  1243.     case 180:
  1244.       return model_capabilities[model].densities.d_180_180;
  1245.     case 360:
  1246.       return model_capabilities[model].densities.d_360_360_micro;
  1247.     case 720:
  1248.       return model_capabilities[model].densities.d_720_720_micro;
  1249.     case 1440:
  1250.       return model_capabilities[model].densities.d_1440_720_micro;
  1251.     }
  1252.     }
  1253.   switch (xdpi)
  1254.     {
  1255.     case 180:
  1256.       return model_capabilities[model].densities.d_180_180;
  1257.     case 360:
  1258.       return model_capabilities[model].densities.d_360_360;
  1259.     case 720:
  1260.       return model_capabilities[model].densities.d_720_720;
  1261.     case 1440:
  1262.       switch (ydpi)
  1263.     {
  1264.     case 720:
  1265.       return model_capabilities[model].densities.d_1440_720;
  1266.     case 1440:
  1267.       return model_capabilities[model].densities.d_1440_1440;
  1268.     case 2880:
  1269.       return model_capabilities[model].densities.d_1440_2880;
  1270.     }
  1271.     }
  1272.   return 0;
  1273. }
  1274.  
  1275. static escp2_variable_inkset_t *
  1276. escp2_inks(int model, int xdpi, int colors, int bits)
  1277. {
  1278.   escp2_variable_inklist_t *inks = model_capabilities[model].inks;
  1279.   switch (bits)
  1280.     {
  1281.     case 1:
  1282.       switch (colors)
  1283.     {
  1284.     case 1:
  1285.     case 4:
  1286.       switch (xdpi)
  1287.         {
  1288.         case 180:
  1289.           return inks->s_180_4;
  1290.         case 360:
  1291.           return inks->s_360_4;
  1292.         case 720:
  1293.           return inks->s_720_4;
  1294.         case 1440:
  1295.           return inks->s_1440_4;
  1296.         }
  1297.     case 6:
  1298.       switch (xdpi)
  1299.         {
  1300.         case 180:
  1301.           return inks->s_180_6;
  1302.         case 360:
  1303.           return inks->s_360_6;
  1304.         case 720:
  1305.           return inks->s_720_6;
  1306.         case 1440:
  1307.           return inks->s_1440_6;
  1308.         }
  1309.     }
  1310.     case 2:
  1311.       switch (colors)
  1312.     {
  1313.     case 1:
  1314.     case 4:
  1315.       switch (xdpi)
  1316.         {
  1317.         case 180:
  1318.           return inks->v_180_4;
  1319.         case 360:
  1320.           return inks->v_360_4;
  1321.         case 720:
  1322.           return inks->v_720_4;
  1323.         case 1440:
  1324.           return inks->v_1440_4;
  1325.         }
  1326.     case 6:
  1327.       switch (xdpi)
  1328.         {
  1329.         case 180:
  1330.           return inks->v_180_6;
  1331.         case 360:
  1332.           return inks->v_360_6;
  1333.         case 720:
  1334.           return inks->v_720_6;
  1335.         case 1440:
  1336.           return inks->v_1440_6;
  1337.         }
  1338.     }
  1339.     }
  1340.   return NULL;
  1341. }
  1342.  
  1343. static unsigned
  1344. escp2_max_paper_width(int model)
  1345. {
  1346.   return (model_capabilities[model].max_paper_width);
  1347. }
  1348.  
  1349. static unsigned
  1350. escp2_max_paper_height(int model)
  1351. {
  1352.   return (model_capabilities[model].max_paper_height);
  1353. }
  1354.  
  1355. static unsigned
  1356. escp2_left_margin(int model)
  1357. {
  1358.   return (model_capabilities[model].left_margin);
  1359. }
  1360.  
  1361. static unsigned
  1362. escp2_right_margin(int model)
  1363. {
  1364.   return (model_capabilities[model].right_margin);
  1365. }
  1366.  
  1367. static unsigned
  1368. escp2_top_margin(int model)
  1369. {
  1370.   return (model_capabilities[model].top_margin);
  1371. }
  1372.  
  1373. static unsigned
  1374. escp2_bottom_margin(int model)
  1375. {
  1376.   return (model_capabilities[model].bottom_margin);
  1377. }
  1378.  
  1379. static int
  1380. escp2_pseudo_separation_rows(int model)
  1381. {
  1382.   return (model_capabilities[model].pseudo_separation_rows);
  1383. }
  1384.  
  1385. /*
  1386.  * 'escp2_parameters()' - Return the parameter values for the given parameter.
  1387.  */
  1388.  
  1389. char **                    /* O - Parameter values */
  1390. escp2_parameters(const printer_t *printer,    /* I - Printer model */
  1391.                  char *ppd_file,    /* I - PPD file (not used) */
  1392.                  char *name,        /* I - Name of parameter */
  1393.                  int  *count)        /* O - Number of values */
  1394. {
  1395.   int        i;
  1396.   int        model = printer->model;
  1397.   char        **valptrs;
  1398.  
  1399.   static const char *ink_types[] =
  1400.   {
  1401.     "Six Color Photo",
  1402.     "Four Color Standard"
  1403.   };
  1404.  
  1405.   if (count == NULL)
  1406.     return (NULL);
  1407.  
  1408.   *count = 0;
  1409.  
  1410.   if (name == NULL)
  1411.     return (NULL);
  1412.  
  1413.   if (strcmp(name, "PageSize") == 0)
  1414.     {
  1415.       unsigned int length_limit, width_limit;
  1416.       const papersize_t *papersizes = get_papersizes();
  1417.       valptrs = malloc(sizeof(char *) * known_papersizes());
  1418.       *count = 0;
  1419.       width_limit = escp2_max_paper_width(model);
  1420.       length_limit = escp2_max_paper_height(model);
  1421.       for (i = 0; i < known_papersizes(); i++)
  1422.     {
  1423.       if (strlen(papersizes[i].name) > 0 &&
  1424.           papersizes[i].width <= width_limit &&
  1425.           papersizes[i].length <= length_limit)
  1426.         {
  1427.           valptrs[*count] = malloc(strlen(papersizes[i].name) + 1);
  1428.           strcpy(valptrs[*count], papersizes[i].name);
  1429.           (*count)++;
  1430.         }
  1431.     }
  1432.       return (valptrs);
  1433.     }
  1434.   else if (strcmp(name, "Resolution") == 0)
  1435.     {
  1436.       const res_t *res = &(escp2_reslist[0]);
  1437.       valptrs = malloc(sizeof(char *) * sizeof(escp2_reslist) / sizeof(res_t));
  1438.       *count = 0;
  1439.       while(res->hres)
  1440.     {
  1441.       if (escp2_ink_type(model, res->hres, !res->softweave) != -1)
  1442.         {
  1443.           int nozzles = escp2_nozzles(model);
  1444.           int xdpi = res->hres;
  1445.           int physical_xdpi = xdpi > 720 ? escp2_enhanced_xres(model) :
  1446.         escp2_xres(model);
  1447.           int horizontal_passes = xdpi / physical_xdpi;
  1448.           int oversample = horizontal_passes * res->vertical_passes
  1449.                              * res->vertical_oversample;
  1450.           if (horizontal_passes < 1)
  1451.         horizontal_passes = 1;
  1452.           if (oversample < 1)
  1453.         oversample = 1;
  1454.           if (((horizontal_passes * res->vertical_passes) <= 8) &&
  1455.           (! res->softweave || (nozzles > 1 && nozzles > oversample)))
  1456.         {
  1457.           valptrs[*count] = malloc(strlen(res->name) + 1);
  1458.           strcpy(valptrs[*count], res->name);
  1459.           (*count)++;
  1460.         }
  1461.         }
  1462.       res++;
  1463.     }
  1464.       return (valptrs);
  1465.     }
  1466.   else if (strcmp(name, "InkType") == 0)
  1467.     {
  1468.       if (escp2_has_cap(model, MODEL_6COLOR_MASK, MODEL_6COLOR_NO))
  1469.     return NULL;
  1470.       else
  1471.     {
  1472.       int ninktypes = sizeof(ink_types) / sizeof(char *);
  1473.       valptrs = malloc(sizeof(char *) * ninktypes);
  1474.       for (i = 0; i < ninktypes; i++)
  1475.         {
  1476.           valptrs[i] = malloc(strlen(ink_types[i]) + 1);
  1477.           strcpy(valptrs[i], ink_types[i]);
  1478.         }
  1479.       *count = ninktypes;
  1480.       return valptrs;
  1481.     }
  1482.     }
  1483.   else if (strcmp(name, "MediaType") == 0)
  1484.     {
  1485.       int nmediatypes = paper_type_count;
  1486.       valptrs = malloc(sizeof(char *) * nmediatypes);
  1487.       for (i = 0; i < nmediatypes; i++)
  1488.     {
  1489.       valptrs[i] = malloc(strlen(escp2_paper_list[i].name) + 1);
  1490.       strcpy(valptrs[i], escp2_paper_list[i].name);
  1491.     }
  1492.       *count = nmediatypes;
  1493.       return valptrs;
  1494.     }
  1495.   else
  1496.     return (NULL);
  1497.  
  1498. }
  1499.  
  1500. /*
  1501.  * 'escp2_imageable_area()' - Return the imageable area of the page.
  1502.  */
  1503.  
  1504. void
  1505. escp2_imageable_area(const printer_t *printer,    /* I - Printer model */
  1506.              const vars_t *v,   /* I */
  1507.                      int  *left,    /* O - Left position in points */
  1508.                      int  *right,    /* O - Right position in points */
  1509.                      int  *bottom,    /* O - Bottom position in points */
  1510.                      int  *top)        /* O - Top position in points */
  1511. {
  1512.   int    width, length;            /* Size of page */
  1513.  
  1514.   default_media_size(printer, v, &width, &length);
  1515.   *left =    escp2_left_margin(printer->model);
  1516.   *right =    width - escp2_right_margin(printer->model);
  1517.   *top =    length - escp2_top_margin(printer->model);
  1518.   *bottom =    escp2_bottom_margin(printer->model);
  1519. }
  1520.  
  1521. void
  1522. escp2_limit(const printer_t *printer,    /* I - Printer model */
  1523.         const vars_t *v,          /* I */
  1524.         int  *width,        /* O - Left position in points */
  1525.         int  *length)        /* O - Top position in points */
  1526. {
  1527.   *width =    escp2_max_paper_width(printer->model);
  1528.   *length =    escp2_max_paper_height(printer->model);
  1529. }
  1530.  
  1531. const char *
  1532. escp2_default_resolution(const printer_t *printer)
  1533. {
  1534.   const res_t *res = &(escp2_reslist[0]);
  1535.   while (res->hres)
  1536.     {
  1537.       if (escp2_ink_type(printer->model, res->hres, !res->softweave) != -1)
  1538.     {
  1539.       int nozzles = escp2_nozzles(printer->model);
  1540.       int xdpi = res->hres;
  1541.       int physical_xdpi = xdpi > 720 ?
  1542.         escp2_enhanced_xres(printer->model) : escp2_xres(printer->model);
  1543.       int horizontal_passes = xdpi / physical_xdpi;
  1544.       int oversample = horizontal_passes * res->vertical_passes
  1545.         * res->vertical_oversample;
  1546.       if (horizontal_passes < 1)
  1547.         horizontal_passes = 1;
  1548.       if (oversample < 1)
  1549.         oversample = 1;
  1550.       if (((horizontal_passes * res->vertical_passes) <= 8) &&
  1551.           (! res->softweave || (nozzles > 1 && nozzles > oversample)))
  1552.         return res->name;
  1553.     }
  1554.       res++;
  1555.     }
  1556.   return NULL;
  1557. }
  1558.  
  1559. static void
  1560. escp2_reset_printer(FILE *prn, escp_init_t *init)
  1561. {
  1562.   /*
  1563.    * Hack that seems to be necessary for these silly things to recognize
  1564.    * the input.  It only needs to be done once per printer evidently, but
  1565.    * it needs to be done.
  1566.    */
  1567.   if (escp2_has_cap(init->model, MODEL_INIT_MASK, MODEL_INIT_900))
  1568.     fprintf(prn, "%c%c%c\033\001@EJL 1284.4\n@EJL     \n\033@", 0, 0, 0);
  1569.  
  1570.   fputs("\033@", prn);                 /* ESC/P2 reset */
  1571. }
  1572.  
  1573. static void
  1574. escp2_set_remote_sequence(FILE *prn, escp_init_t *init)
  1575. {
  1576.   /* Magic remote mode commands, whatever they do */
  1577.   if (escp2_has_cap(init->model, MODEL_COMMAND_MASK, MODEL_COMMAND_1999))
  1578.     {
  1579.       int feed_sequence = 0;
  1580.       const paper_t *p = get_media_type(init->paper_type);
  1581.       if (p)
  1582.     feed_sequence = p->paper_feed_sequence;
  1583.       fprintf(prn, /* Enter remote mode */
  1584.                    "\033(R\010%c%cREMOTE1"
  1585.                    /* Function unknown */
  1586.                    "PM\002%c%c%c"
  1587.                    /* Set mechanism sequence */
  1588.                "SN\003%c%c%c%c",
  1589.           0, 0,
  1590.           0, 0, 0,
  1591.           0, 0, 0, feed_sequence);
  1592.       if (escp2_has_cap(init->model, MODEL_ZEROMARGIN_MASK,
  1593.                                      MODEL_ZEROMARGIN_YES))
  1594.     fprintf(prn, /* Set zero-margin print mode */
  1595.                  "FP\003%c%c\260\377", 0, 0);
  1596.       fprintf(prn, /* Exit remote mode */
  1597.                    "\033%c%c%c", 0, 0, 0);
  1598.     }
  1599. }
  1600.  
  1601. static void
  1602. escp2_set_graphics_mode(FILE *prn, escp_init_t *init)
  1603. {
  1604.   fwrite("\033(G\001\000\001", 6, 1, prn);    /* Enter graphics mode */
  1605. }
  1606.  
  1607. static void
  1608. escp2_set_resolution(FILE *prn, escp_init_t *init)
  1609. {
  1610.   if (!(escp2_has_cap(init->model, MODEL_VARIABLE_DOT_MASK,
  1611.              MODEL_VARIABLE_NORMAL)) &&
  1612.       init->use_softweave)
  1613.     fprintf(prn, "\033(U\005%c%c%c%c%c%c", 0, 1440 / init->ydpi,
  1614.         1440 / init->ydpi,
  1615.         1440 / (init->ydpi * (init->horizontal_passes > 2 ? 2 : 1)),
  1616.         1440 % 256, 1440 / 256);
  1617.   else
  1618.     fprintf(prn, "\033(U\001%c%c", 0, 3600 / init->ydpi);
  1619. }
  1620.  
  1621. static void
  1622. escp2_set_color(FILE *prn, escp_init_t *init)
  1623. {
  1624.   if (escp2_has_cap(init->model, MODEL_GRAYMODE_MASK, MODEL_GRAYMODE_YES))
  1625.     fprintf(prn, "\033(K\002%c%c%c", 0, 0,
  1626.         (init->output_type == OUTPUT_GRAY ? 1 : 2));
  1627. }
  1628.  
  1629. static void
  1630. escp2_set_microweave(FILE *prn, escp_init_t *init)
  1631. {
  1632.   fprintf(prn, "\033(i\001%c%c", 0,
  1633.       (init->use_softweave || init->ydpi < 720) ? 0 : 1);
  1634. }
  1635.  
  1636. static void
  1637. escp2_set_printhead_speed(FILE *prn, escp_init_t *init)
  1638. {
  1639.   if (init->horizontal_passes * init->vertical_passes *
  1640.       init->vertical_oversample * escp2_xres(init->model) / 720 > 2)
  1641.     {
  1642.       fprintf(prn, "\033U%c", 1);
  1643.       if (init->xdpi > 720)        /* Slow mode if available */
  1644.     fprintf(prn, "\033(s%c%c%c", 1, 0, 2);
  1645.     }
  1646.   else
  1647.     fprintf(prn, "\033U%c", 0);
  1648. }
  1649.  
  1650. static void
  1651. escp2_set_dot_size(FILE *prn, escp_init_t *init)
  1652. {
  1653.   /* Dot size */
  1654.   int drop_size = escp2_ink_type(init->model, init->xdpi,
  1655.                  !init->use_softweave);
  1656.   if (drop_size >= 0)
  1657.     fprintf(prn, "\033(e\002%c%c%c", 0, 0, drop_size);
  1658. }
  1659.  
  1660. static void
  1661. escp2_set_page_length(FILE *prn, escp_init_t *init)
  1662. {
  1663.   int l = init->ydpi * init->page_length / 72;
  1664.   if (!(escp2_has_cap(init->model, MODEL_VARIABLE_DOT_MASK,
  1665.               MODEL_VARIABLE_NORMAL)) &&
  1666.       init->use_softweave)
  1667.     fprintf(prn, "\033(C\004%c%c%c%c%c", 0,
  1668.         l & 0xff, (l >> 8) & 0xff, (l >> 16) & 0xff, (l >> 24) & 0xff);
  1669.   else
  1670.     fprintf(prn, "\033(C\002%c%c%c", 0, l & 255, l >> 8);
  1671. }
  1672.  
  1673. static void
  1674. escp2_set_margins(FILE *prn, escp_init_t *init)
  1675. {
  1676.   int l = init->ydpi * (init->page_length - init->page_bottom) / 72;
  1677.   int t = init->ydpi * (init->page_length - init->page_top) / 72;
  1678.   if (!(escp2_has_cap(init->model, MODEL_VARIABLE_DOT_MASK,
  1679.               MODEL_VARIABLE_NORMAL)) &&
  1680.       init->use_softweave)
  1681.     {
  1682.       if (escp2_has_cap(init->model, MODEL_6COLOR_MASK, MODEL_6COLOR_YES))
  1683.     fprintf(prn, "\033(c\010%c%c%c%c%c%c%c%c%c", 0,
  1684.         t & 0xff, t >> 8, (t >> 16) & 0xff, (t >> 24) & 0xff,
  1685.         l & 0xff, l >> 8, (l >> 16) & 0xff, (l >> 24) & 0xff);
  1686.       else
  1687.     fprintf(prn, "\033(c\004%c%c%c%c%c", 0,
  1688.         t & 0xff, t >> 8, l & 0xff, l >> 8);
  1689.     }
  1690.   else
  1691.     fprintf(prn, "\033(c\004%c%c%c%c%c", 0,
  1692.         t & 0xff, t >> 8, l & 0xff, l >> 8);
  1693. }
  1694.  
  1695. static void
  1696. escp2_set_form_factor(FILE *prn, escp_init_t *init)
  1697. {
  1698.   int page_width = init->page_width * init->ydpi / 72;
  1699.   int page_length = init->page_length * init->ydpi / 72;
  1700.  
  1701.   if (escp2_has_cap(init->model, MODEL_ZEROMARGIN_MASK, MODEL_ZEROMARGIN_YES))
  1702.       /* Make the page 2/10" wider (probably ignored by the printer anyway) */
  1703.       page_width += 144 * 720 / init->xdpi;
  1704.  
  1705.   if (escp2_has_cap(init->model, MODEL_COMMAND_MASK, MODEL_COMMAND_1999))
  1706.     fprintf(prn, "\033(S\010%c%c%c%c%c%c%c%c%c", 0,
  1707.         ((page_width >> 0) & 0xff), ((page_width >> 8) & 0xff),
  1708.         ((page_width >> 16) & 0xff), ((page_width >> 24) & 0xff),
  1709.         ((page_length >> 0) & 0xff), ((page_length >> 8) & 0xff),
  1710.         ((page_length >> 16) & 0xff), ((page_length >> 24) & 0xff));
  1711. }
  1712.  
  1713. static void
  1714. escp2_set_printhead_resolution(FILE *prn, escp_init_t *init)
  1715. {
  1716.   if (!(escp2_has_cap(init->model, MODEL_VARIABLE_DOT_MASK,
  1717.               MODEL_VARIABLE_NORMAL)) &&
  1718.       init->use_softweave)
  1719.     {
  1720.       /* Magic resolution cookie */
  1721.       if (init->xdpi > 720)
  1722.     {
  1723.       if (init->output_type == OUTPUT_GRAY)
  1724.         fprintf(prn, "\033(D%c%c%c%c%c%c", 4, 0, 14400 % 256, 14400 / 256,
  1725.             escp2_black_nozzle_separation(init->model) * 14400 / 720,
  1726.             14400 / escp2_enhanced_xres(init->model));
  1727.       else
  1728.         fprintf(prn, "\033(D%c%c%c%c%c%c", 4, 0, 14400 % 256, 14400 / 256,
  1729.             escp2_nozzle_separation(init->model) * 14400 / 720,
  1730.             14400 / escp2_enhanced_xres(init->model));
  1731.     }
  1732.       else
  1733.     {
  1734.       if (init->output_type == OUTPUT_GRAY)
  1735.         fprintf(prn, "\033(D%c%c%c%c%c%c", 4, 0, 14400 % 256, 14400 / 256,
  1736.             escp2_black_nozzle_separation(init->model) * 14400 / 720,
  1737.             14400 / escp2_xres(init->model));
  1738.       else
  1739.         fprintf(prn, "\033(D%c%c%c%c%c%c", 4, 0, 14400 % 256, 14400 / 256,
  1740.             escp2_nozzle_separation(init->model) * 14400 / 720,
  1741.             14400 / escp2_xres(init->model));
  1742.     }
  1743.     }
  1744. }
  1745.  
  1746. static void
  1747. escp2_init_printer(FILE *prn, escp_init_t *init)
  1748. {
  1749.   if (init->ydpi > 720)
  1750.     init->ydpi = 720;
  1751.  
  1752.   escp2_reset_printer(prn, init);
  1753.   escp2_set_remote_sequence(prn, init);
  1754.   escp2_set_graphics_mode(prn, init);
  1755.   escp2_set_resolution(prn, init);
  1756.   escp2_set_color(prn, init);
  1757.   escp2_set_microweave(prn, init);
  1758.   escp2_set_printhead_speed(prn, init);
  1759.   escp2_set_dot_size(prn, init);
  1760.   escp2_set_page_length(prn, init);
  1761.   escp2_set_margins(prn, init);
  1762.   escp2_set_form_factor(prn, init);
  1763.   escp2_set_printhead_resolution(prn, init);
  1764. }
  1765.  
  1766. static void
  1767. escp2_deinit_printer(FILE *prn, escp_init_t *init)
  1768. {
  1769.   fputs(/* Eject page */
  1770.         "\014"
  1771.         /* ESC/P2 reset */
  1772.         "\033@", prn);
  1773.   if (escp2_has_cap(init->model, MODEL_COMMAND_MASK, MODEL_COMMAND_1999)) {
  1774.     fprintf(prn, /* Enter remote mode */
  1775.                  "\033(R\010%c%cREMOTE1"
  1776.                  /* Load settings from NVRAM */
  1777.                  "LD%c%c"
  1778.                  /* Exit remote mode */
  1779.                  "\033%c%c%c", 0, 0, 0, 0, 0, 0, 0);
  1780.   }
  1781. }
  1782.  
  1783. /*
  1784.  * 'escp2_print()' - Print an image to an EPSON printer.
  1785.  */
  1786. void
  1787. escp2_print(const printer_t *printer,        /* I - Model */
  1788.             int       copies,        /* I - Number of copies */
  1789.             FILE      *prn,        /* I - File to print to */
  1790.         Image     image,        /* I - Image to print */
  1791.         const vars_t    *v)
  1792. {
  1793.   unsigned char *cmap = v->cmap;
  1794.   int        model = printer->model;
  1795.   const char    *resolution = v->resolution;
  1796.   const char    *media_type = v->media_type;
  1797.   int         output_type = v->output_type;
  1798.   int        orientation = v->orientation;
  1799.   const char    *ink_type = v->ink_type;
  1800.   double    scaling = v->scaling;
  1801.   int        top = v->top;
  1802.   int        left = v->left;
  1803.   int        y;        /* Looping vars */
  1804.   int        xdpi, ydpi;    /* Resolution */
  1805.   int        physical_ydpi;
  1806.   int        physical_xdpi;
  1807.   int        n;        /* Output number */
  1808.   unsigned short *out;    /* Output pixels (16-bit) */
  1809.   unsigned char    *in,        /* Input pixels */
  1810.         *black,        /* Black bitmap data */
  1811.         *cyan,        /* Cyan bitmap data */
  1812.         *magenta,    /* Magenta bitmap data */
  1813.         *lcyan,        /* Light cyan bitmap data */
  1814.         *lmagenta,    /* Light magenta bitmap data */
  1815.         *yellow;    /* Yellow bitmap data */
  1816.   int        page_left,    /* Left margin of page */
  1817.         page_right,    /* Right margin of page */
  1818.         page_top,    /* Top of page */
  1819.         page_bottom,    /* Bottom of page */
  1820.         page_width,    /* Width of page */
  1821.         page_height,    /* Height of page */
  1822.         page_length,    /* True length of page */
  1823.         out_width,    /* Width of image on page */
  1824.         out_height,    /* Height of image on page */
  1825.         out_bpp,    /* Output bytes per pixel */
  1826.         length,        /* Length of raster data */
  1827.         errdiv,        /* Error dividend */
  1828.         errmod,        /* Error modulus */
  1829.         errval,        /* Current error value */
  1830.         errline,    /* Current raster line */
  1831.         errlast;    /* Last raster line loaded */
  1832.   convert_t    colorfunc = 0;    /* Color conversion function... */
  1833.   int           image_height,
  1834.                 image_width,
  1835.                 image_bpp;
  1836.   int        use_softweave = 0;
  1837.   int        nozzles = 1;
  1838.   int        nozzle_separation = 1;
  1839.   int        horizontal_passes = 1;
  1840.   int        vertical_passes = 1;
  1841.   int        vertical_oversample = 1;
  1842.   int        use_6color = 0;
  1843.   const res_t     *res;
  1844.   int        bits;
  1845.   void *    weave = NULL;
  1846.   void *    dither;
  1847.   colormode_t colormode = COLOR_CCMMYK;
  1848.   int        separation_rows = escp2_separation_rows(model);
  1849.   int        ink_spread;
  1850.   vars_t    nv;
  1851.   escp_init_t    init;
  1852.   escp2_variable_inkset_t *inks;
  1853.   const paper_t *pt;
  1854.   double k_upper, k_lower;
  1855.  
  1856.   memcpy(&nv, v, sizeof(vars_t));
  1857.  
  1858.   if (escp2_has_cap(model, MODEL_6COLOR_MASK, MODEL_6COLOR_YES) &&
  1859.       strcmp(ink_type, "Four Color Standard") != 0 &&
  1860.       nv.image_type != IMAGE_MONOCHROME)
  1861.     use_6color = 1;
  1862.  
  1863.   if (nv.image_type == IMAGE_MONOCHROME)
  1864.     {
  1865.       colormode = COLOR_MONOCHROME;
  1866.       output_type = OUTPUT_GRAY;
  1867.       bits = 1;
  1868.     }
  1869.   else if (output_type == OUTPUT_GRAY)
  1870.     colormode = COLOR_MONOCHROME;
  1871.   else if (use_6color)
  1872.     colormode = COLOR_CCMMYK;
  1873.   else
  1874.     colormode = COLOR_CMYK;
  1875.  
  1876.  /*
  1877.   * Setup a read-only pixel region for the entire image...
  1878.   */
  1879.  
  1880.   Image_init(image);
  1881.   image_height = Image_height(image);
  1882.   image_width = Image_width(image);
  1883.   image_bpp = Image_bpp(image);
  1884.  
  1885.  /*
  1886.   * Choose the correct color conversion function...
  1887.   */
  1888.  
  1889.   colorfunc = choose_colorfunc(output_type, image_bpp, cmap, &out_bpp, &nv);
  1890.  
  1891.  /*
  1892.   * Compute the output size...
  1893.   */
  1894.   escp2_imageable_area(printer, &nv, &page_left, &page_right,
  1895.                        &page_bottom, &page_top);
  1896.  
  1897.   compute_page_parameters(page_right, page_left, page_top, page_bottom,
  1898.               scaling, image_width, image_height, image,
  1899.               &orientation, &page_width, &page_height,
  1900.               &out_width, &out_height, &left, &top);
  1901.  
  1902.   /*
  1903.    * Recompute the image height and width.  If the image has been
  1904.    * rotated, these will change from previously.
  1905.    */
  1906.   image_height = Image_height(image);
  1907.   image_width = Image_width(image);
  1908.  
  1909.  /*
  1910.   * Figure out the output resolution...
  1911.   */
  1912.   for (res = &escp2_reslist[0];;res++)
  1913.     {
  1914.       if (!strcmp(resolution, res->name))
  1915.     {
  1916.       use_softweave = res->softweave;
  1917.       xdpi = res->hres;
  1918.       ydpi = res->vres;
  1919.       vertical_passes = res->vertical_passes;
  1920.       vertical_oversample = res->vertical_oversample;
  1921.       if (xdpi > 720)
  1922.         physical_xdpi = escp2_enhanced_xres(model);
  1923.       else
  1924.         physical_xdpi = escp2_xres(model);
  1925.       if (use_softweave)
  1926.         horizontal_passes = xdpi / physical_xdpi;
  1927.       else
  1928.         horizontal_passes = xdpi / 720;
  1929.       if (horizontal_passes == 0)
  1930.         horizontal_passes = 1;
  1931.       if (output_type == OUTPUT_GRAY)
  1932.         {
  1933.           nozzles = escp2_black_nozzles(model);
  1934.           if (nozzles == 0)
  1935.         {
  1936.           nozzle_separation = escp2_nozzle_separation(model);
  1937.           nozzles = escp2_nozzles(model);
  1938.         }
  1939.           else
  1940.           nozzle_separation = escp2_black_nozzle_separation(model);
  1941.         }
  1942.       else
  1943.         {
  1944.           nozzle_separation = escp2_nozzle_separation(model);
  1945.           nozzles = escp2_nozzles(model);
  1946.         }
  1947.       if (ydpi < 720)
  1948.         nozzle_separation = nozzle_separation * ydpi / 720;
  1949.       break;
  1950.     }
  1951.       else if (!strcmp(resolution, ""))
  1952.     {
  1953.       return;
  1954.     }
  1955.     }
  1956.   if (!(escp2_has_cap(model, MODEL_VARIABLE_DOT_MASK, MODEL_VARIABLE_NORMAL))
  1957.       && use_softweave)
  1958.     bits = 2;
  1959.   else
  1960.     bits = 1;
  1961.  
  1962.  /*
  1963.   * Let the user know what we're doing...
  1964.   */
  1965.  
  1966.   Image_progress_init(image);
  1967.  
  1968.  /*
  1969.   * Send ESC/P2 initialization commands...
  1970.   */
  1971.   default_media_size(printer, &nv, &n, &page_length);
  1972. #if 0
  1973.   /*
  1974.    * I believe that this code is no longer needed.  I think I put it in
  1975.    * a while back because our softweave pattern couldn't handle the top
  1976.    * and bottom of the page.  Lying to the printer about the length of
  1977.    * the page would allow us to print the entire image.  With our current
  1978.    * weave code able to use the entire width of the permitted region,
  1979.    * this hack shouldn't be needed any more.
  1980.    */
  1981.   page_length += (39 + (escp2_nozzles(model) * 2) *
  1982.           escp2_nozzle_separation(model)) / 10; /* Top and bottom */
  1983.  
  1984.   page_top += (39 + (escp2_nozzles(model) * 2) *
  1985.            escp2_nozzle_separation(model)) / 10;
  1986. #endif
  1987.   init.model = model;
  1988.   init.output_type = output_type;
  1989.   init.ydpi = ydpi;
  1990.   init.xdpi = xdpi;
  1991.   init.use_softweave = use_softweave;
  1992.   init.page_length = page_length;
  1993.   init.page_width = page_width;
  1994.   init.page_top = page_top;
  1995.   init.page_bottom = page_bottom;
  1996.   init.horizontal_passes = horizontal_passes;
  1997.   init.vertical_passes = vertical_passes;
  1998.   init.vertical_oversample = vertical_oversample;
  1999.   init.bits = bits;
  2000.   init.paper_type = media_type;
  2001.  
  2002.   escp2_init_printer(prn, &init);
  2003.  
  2004.  /*
  2005.   * Convert image size to printer resolution...
  2006.   */
  2007.  
  2008.   out_width  = xdpi * out_width / 72;
  2009.   out_height = ydpi * out_height / 72;
  2010.  
  2011.   physical_ydpi = ydpi;
  2012.   if (ydpi > 720)
  2013.     physical_ydpi = 720;
  2014.  
  2015.   left = physical_ydpi * left / 72;
  2016.  
  2017.  /*
  2018.   * Adjust for zero-margin printing...
  2019.   */
  2020.  
  2021.   if (escp2_has_cap(model, MODEL_ZEROMARGIN_MASK, MODEL_ZEROMARGIN_YES))
  2022.     {
  2023.      /*
  2024.       * In zero-margin mode, the origin is about 3/20" to the left of the
  2025.       * paper's left edge.
  2026.       */
  2027.       left += 92 * physical_ydpi / 720;
  2028.     }
  2029.  
  2030.  /*
  2031.   * Allocate memory for the raster data...
  2032.   */
  2033.  
  2034.   length = (out_width + 7) / 8;
  2035.  
  2036.   if (output_type == OUTPUT_GRAY)
  2037.   {
  2038.     black   = malloc(length * bits);
  2039.     cyan    = NULL;
  2040.     magenta = NULL;
  2041.     lcyan    = NULL;
  2042.     lmagenta = NULL;
  2043.     yellow  = NULL;
  2044.   }
  2045.   else
  2046.   {
  2047.     cyan    = malloc(length * bits);
  2048.     magenta = malloc(length * bits);
  2049.     yellow  = malloc(length * bits);
  2050.  
  2051.     if (escp2_has_cap(model, MODEL_HASBLACK_MASK, MODEL_HASBLACK_YES))
  2052.       black = malloc(length * bits);
  2053.     else
  2054.       black = NULL;
  2055.     if (use_6color) {
  2056.       lcyan = malloc(length * bits);
  2057.       lmagenta = malloc(length * bits);
  2058.     } else {
  2059.       lcyan = NULL;
  2060.       lmagenta = NULL;
  2061.     }
  2062.   }
  2063.  
  2064.   if (use_softweave)
  2065.     /* Epson printers are all 720 physical dpi */
  2066.     weave = initialize_weave(nozzles, nozzle_separation, horizontal_passes,
  2067.                  vertical_passes, vertical_oversample, colormode,
  2068.                  bits,
  2069.                  out_width * escp2_xres(model) / physical_ydpi,
  2070.                  out_height, separation_rows,
  2071.                  top * physical_ydpi / 72,
  2072.                  page_height * physical_ydpi / 72, use_softweave);
  2073.   else
  2074.     escp2_init_microweave(top * ydpi / 72);
  2075.  
  2076.   /*
  2077.    * Compute the LUT.  For now, it's 8 bit, but that may eventually
  2078.    * sometimes change.
  2079.    */
  2080.   pt = get_media_type(nv.media_type);
  2081.   if (pt)
  2082.     nv.density *= pt->base_density;
  2083.   else
  2084.     nv.density *= .5;        /* Can't find paper type? Assume plain */
  2085.   nv.density *= escp2_density(model, xdpi, ydpi, !use_softweave);
  2086.   if (nv.density > 1.0)
  2087.     nv.density = 1.0;
  2088.   compute_lut(256, &nv);
  2089.  
  2090.  /*
  2091.   * Output the page...
  2092.   */
  2093.  
  2094.   if (xdpi > ydpi)
  2095.     dither = init_dither(image_width, out_width, 1, xdpi / ydpi, &nv);
  2096.   else
  2097.     dither = init_dither(image_width, out_width, ydpi / xdpi, 1, &nv);
  2098.  
  2099.   dither_set_black_levels(dither, 1.0, 1.0, 1.0);
  2100.   if (use_6color)
  2101.     k_lower = .4 / bits + .1;
  2102.   else
  2103.     k_lower = .25 / bits;
  2104.   if (pt)
  2105.     {
  2106.       k_lower *= pt->k_lower_scale;
  2107.       k_upper = pt->k_upper;
  2108.     }
  2109.   else
  2110.     {
  2111.       k_lower *= .5;
  2112.       k_upper = .5;
  2113.     }
  2114.   dither_set_black_lower(dither, k_lower);
  2115.   dither_set_black_upper(dither, k_upper);
  2116.   if (bits == 2)
  2117.     {
  2118.       if (use_6color)
  2119.     dither_set_adaptive_divisor(dither, 8);
  2120.       else
  2121.     dither_set_adaptive_divisor(dither, 16);
  2122.     }  
  2123.   else
  2124.     dither_set_adaptive_divisor(dither, 4);
  2125.  
  2126.   inks = escp2_inks(model, xdpi, use_6color ? 6 : 4, bits);
  2127.   if (inks->c)
  2128.     dither_set_c_ranges(dither, inks->c->count, inks->c->range,
  2129.             inks->c->density * nv.density);
  2130.   if (inks->m)
  2131.     dither_set_m_ranges(dither, inks->m->count, inks->m->range,
  2132.             inks->m->density * nv.density);
  2133.   if (inks->y)
  2134.     dither_set_y_ranges(dither, inks->y->count, inks->y->range,
  2135.             inks->y->density * nv.density);
  2136.   if (inks->k)
  2137.     dither_set_k_ranges(dither, inks->k->count, inks->k->range,
  2138.             inks->k->density * nv.density);
  2139.  
  2140.   if (bits == 2)
  2141.     {
  2142.       if (use_6color)
  2143.     dither_set_transition(dither, .7);
  2144.       else
  2145.     dither_set_transition(dither, .5);
  2146.     }
  2147.   if (!strcmp(nv.dither_algorithm, "Ordered"))
  2148.     dither_set_transition(dither, 1);
  2149.  
  2150.   switch (nv.image_type)
  2151.     {
  2152.     case IMAGE_LINE_ART:
  2153.       dither_set_ink_spread(dither, 19);
  2154.       break;
  2155.     case IMAGE_SOLID_TONE:
  2156.       dither_set_ink_spread(dither, 15);
  2157.       break;
  2158.     case IMAGE_CONTINUOUS:
  2159.       ink_spread = 13;
  2160.       if (ydpi > 720)
  2161.     ink_spread++;
  2162.       if (bits > 1)
  2163.     ink_spread++;
  2164.       dither_set_ink_spread(dither, ink_spread);
  2165.       break;
  2166.     }
  2167.   dither_set_density(dither, nv.density);
  2168.  
  2169.   in  = malloc(image_width * image_bpp);
  2170.   out = malloc(image_width * out_bpp * 2);
  2171.  
  2172.   errdiv  = image_height / out_height;
  2173.   errmod  = image_height % out_height;
  2174.   errval  = 0;
  2175.   errlast = -1;
  2176.   errline  = 0;
  2177.  
  2178.   QUANT(0);
  2179.   for (y = 0; y < out_height; y ++)
  2180.   {
  2181.     int duplicate_line = 1;
  2182.     if ((y & 63) == 0)
  2183.       Image_note_progress(image, y, out_height);
  2184.  
  2185.     if (errline != errlast)
  2186.     {
  2187.       errlast = errline;
  2188.       duplicate_line = 0;
  2189.       Image_get_row(image, in, errline);
  2190.       (*colorfunc)(in, out, image_width, image_bpp, cmap, &nv);
  2191.     }
  2192.     QUANT(1);
  2193.  
  2194.     if (nv.image_type == IMAGE_MONOCHROME)
  2195.       dither_monochrome(out, y, dither, black, duplicate_line);
  2196.     else if (output_type == OUTPUT_GRAY)
  2197.       dither_black(out, y, dither, black, duplicate_line);
  2198.     else
  2199.       dither_cmyk(out, y, dither, cyan, lcyan, magenta, lmagenta,
  2200.           yellow, 0, black, duplicate_line);
  2201.     QUANT(2);
  2202.  
  2203.     if (use_softweave)
  2204.       escp2_write_weave(weave, prn, length, ydpi, model, out_width, left,
  2205.             xdpi, physical_xdpi,
  2206.             cyan, magenta, yellow, black, lcyan, lmagenta);
  2207.     else
  2208.       escp2_write_microweave(prn, black, cyan, magenta, yellow, lcyan,
  2209.                  lmagenta, length, xdpi, ydpi, model,
  2210.                  out_width, left, bits);
  2211.     QUANT(3);
  2212.     errval += errmod;
  2213.     errline += errdiv;
  2214.     if (errval >= out_height)
  2215.     {
  2216.       errval -= out_height;
  2217.       errline ++;
  2218.     }
  2219.     QUANT(4);
  2220.   }
  2221.   Image_progress_conclude(image);
  2222.   if (use_softweave)
  2223.     escp2_flush_all(weave, model, out_width, left, ydpi, xdpi, physical_xdpi,
  2224.             prn);
  2225.   else
  2226.     escp2_free_microweave();
  2227.   QUANT(5);
  2228.  
  2229.   free_dither(dither);
  2230.  
  2231.  /*
  2232.   * Cleanup...
  2233.   */
  2234.  
  2235.   free_lut(&nv);
  2236.   free(in);
  2237.   free(out);
  2238.   if (use_softweave)
  2239.     destroy_weave(weave);
  2240.  
  2241.   if (black != NULL)
  2242.     free(black);
  2243.   if (cyan != NULL)
  2244.     {
  2245.       free(cyan);
  2246.       free(magenta);
  2247.       free(yellow);
  2248.     }
  2249.   if (lcyan != NULL)
  2250.     {
  2251.       free(lcyan);
  2252.       free(lmagenta);
  2253.     }
  2254.  
  2255.   escp2_deinit_printer(prn, &init);
  2256. #ifdef QUANTIFY
  2257.   print_timers();
  2258. #endif
  2259. }
  2260.  
  2261. static void
  2262. escp2_fold(const unsigned char *line,
  2263.        int single_length,
  2264.        unsigned char *outbuf)
  2265. {
  2266.   int i;
  2267.   memset(outbuf, 0, single_length * 2);
  2268.   for (i = 0; i < single_length; i++)
  2269.     {
  2270.       unsigned char l0 = line[0];
  2271.       unsigned char l1 = line[single_length];
  2272.       if (l0 || l1)
  2273.     {
  2274.       outbuf[0] =
  2275.         ((l0 & (1 << 7)) >> 1) +
  2276.         ((l0 & (1 << 6)) >> 2) +
  2277.         ((l0 & (1 << 5)) >> 3) +
  2278.         ((l0 & (1 << 4)) >> 4) +
  2279.         ((l1 & (1 << 7)) >> 0) +
  2280.         ((l1 & (1 << 6)) >> 1) +
  2281.         ((l1 & (1 << 5)) >> 2) +
  2282.         ((l1 & (1 << 4)) >> 3);
  2283.       outbuf[1] =
  2284.         ((l0 & (1 << 3)) << 3) +
  2285.         ((l0 & (1 << 2)) << 2) +
  2286.         ((l0 & (1 << 1)) << 1) +
  2287.         ((l0 & (1 << 0)) << 0) +
  2288.         ((l1 & (1 << 3)) << 4) +
  2289.         ((l1 & (1 << 2)) << 3) +
  2290.         ((l1 & (1 << 1)) << 2) +
  2291.         ((l1 & (1 << 0)) << 1);
  2292.     }
  2293.       line++;
  2294.       outbuf += 2;
  2295.     }
  2296. }
  2297.  
  2298. static void
  2299. escp2_split_2_1(int length,
  2300.         const unsigned char *in,
  2301.         unsigned char *outhi,
  2302.         unsigned char *outlo)
  2303. {
  2304.   unsigned char *outs[2];
  2305.   int i;
  2306.   int row = 0;
  2307.   int limit = length * 2;
  2308.   outs[0] = outhi;
  2309.   outs[1] = outlo;
  2310.   memset(outs[1], 0, limit);
  2311.   for (i = 0; i < limit; i++)
  2312.     {
  2313.       unsigned char inbyte = in[i];
  2314.       outs[0][i] = 0;
  2315.       if (inbyte == 0)
  2316.     continue;
  2317.       /* For some reason gcc isn't unrolling this, even with -funroll-loops */
  2318.       if (inbyte & 1)
  2319.     {
  2320.       outs[row][i] |= 1 & inbyte;
  2321.       row = row ^ 1;
  2322.     }
  2323.       if (inbyte & (1 << 1))
  2324.     {
  2325.       outs[row][i] |= (1 << 1) & inbyte;
  2326.       row = row ^ 1;
  2327.     }
  2328.       if (inbyte & (1 << 2))
  2329.     {
  2330.       outs[row][i] |= (1 << 2) & inbyte;
  2331.       row = row ^ 1;
  2332.     }
  2333.       if (inbyte & (1 << 3))
  2334.     {
  2335.       outs[row][i] |= (1 << 3) & inbyte;
  2336.       row = row ^ 1;
  2337.     }
  2338.       if (inbyte & (1 << 4))
  2339.     {
  2340.       outs[row][i] |= (1 << 4) & inbyte;
  2341.       row = row ^ 1;
  2342.     }
  2343.       if (inbyte & (1 << 5))
  2344.     {
  2345.       outs[row][i] |= (1 << 5) & inbyte;
  2346.       row = row ^ 1;
  2347.     }
  2348.       if (inbyte & (1 << 6))
  2349.     {
  2350.       outs[row][i] |= (1 << 6) & inbyte;
  2351.       row = row ^ 1;
  2352.     }
  2353.       if (inbyte & (1 << 7))
  2354.     {
  2355.       outs[row][i] |= (1 << 7) & inbyte;
  2356.       row = row ^ 1;
  2357.     }
  2358.     }
  2359. }
  2360.  
  2361. static void
  2362. escp2_split_2_2(int length,
  2363.         const unsigned char *in,
  2364.         unsigned char *outhi,
  2365.         unsigned char *outlo)
  2366. {
  2367.   unsigned char *outs[2];
  2368.   int i;
  2369.   unsigned row = 0;
  2370.   int limit = length * 2;
  2371.   outs[0] = outhi;
  2372.   outs[1] = outlo;
  2373.   memset(outs[1], 0, limit);
  2374.   for (i = 0; i < limit; i++)
  2375.     {
  2376.       unsigned char inbyte = in[i];
  2377.       outs[0][i] = 0;
  2378.       if (inbyte == 0)
  2379.     continue;
  2380.       /* For some reason gcc isn't unrolling this, even with -funroll-loops */
  2381.       if (inbyte & 3)
  2382.     {
  2383.       outs[row][i] |= (3 & inbyte);
  2384.       row = row ^ 1;
  2385.     }
  2386.       if (inbyte & (3 << 2))
  2387.     {
  2388.       outs[row][i] |= ((3 << 2) & inbyte);
  2389.       row = row ^ 1;
  2390.     }
  2391.       if (inbyte & (3 << 4))
  2392.     {
  2393.       outs[row][i] |= ((3 << 4) & inbyte);
  2394.       row = row ^ 1;
  2395.     }
  2396.       if (inbyte & (3 << 6))
  2397.     {
  2398.       outs[row][i] |= ((3 << 6) & inbyte);
  2399.       row = row ^ 1;
  2400.     }
  2401.     }
  2402. }
  2403.  
  2404. static void
  2405. escp2_split_2(int length,
  2406.           int bits,
  2407.           const unsigned char *in,
  2408.           unsigned char *outhi,
  2409.           unsigned char *outlo)
  2410. {
  2411.   if (bits == 2)
  2412.     escp2_split_2_2(length, in, outhi, outlo);
  2413.   else
  2414.     escp2_split_2_1(length, in, outhi, outlo);
  2415. }
  2416.  
  2417. static void
  2418. escp2_split_4_1(int length,
  2419.         const unsigned char *in,
  2420.         unsigned char *out0,
  2421.         unsigned char *out1,
  2422.         unsigned char *out2,
  2423.         unsigned char *out3)
  2424. {
  2425.   unsigned char *outs[4];
  2426.   int i;
  2427.   int row = 0;
  2428.   int limit = length * 2;
  2429.   outs[0] = out0;
  2430.   outs[1] = out1;
  2431.   outs[2] = out2;
  2432.   outs[3] = out3;
  2433.   memset(outs[1], 0, limit);
  2434.   memset(outs[2], 0, limit);
  2435.   memset(outs[3], 0, limit);
  2436.   for (i = 0; i < limit; i++)
  2437.     {
  2438.       unsigned char inbyte = in[i];
  2439.       outs[0][i] = 0;
  2440.       if (inbyte == 0)
  2441.     continue;
  2442.       /* For some reason gcc isn't unrolling this, even with -funroll-loops */
  2443.       if (inbyte & 1)
  2444.     {
  2445.       outs[row][i] |= 1 & inbyte;
  2446.       row = (row + 1) & 3;
  2447.     }
  2448.       if (inbyte & (1 << 1))
  2449.     {
  2450.       outs[row][i] |= (1 << 1) & inbyte;
  2451.       row = (row + 1) & 3;
  2452.     }
  2453.       if (inbyte & (1 << 2))
  2454.     {
  2455.       outs[row][i] |= (1 << 2) & inbyte;
  2456.       row = (row + 1) & 3;
  2457.     }
  2458.       if (inbyte & (1 << 3))
  2459.     {
  2460.       outs[row][i] |= (1 << 3) & inbyte;
  2461.       row = (row + 1) & 3;
  2462.     }
  2463.       if (inbyte & (1 << 4))
  2464.     {
  2465.       outs[row][i] |= (1 << 4) & inbyte;
  2466.       row = (row + 1) & 3;
  2467.     }
  2468.       if (inbyte & (1 << 5))
  2469.     {
  2470.       outs[row][i] |= (1 << 5) & inbyte;
  2471.       row = (row + 1) & 3;
  2472.     }
  2473.       if (inbyte & (1 << 6))
  2474.     {
  2475.       outs[row][i] |= (1 << 6) & inbyte;
  2476.       row = (row + 1) & 3;
  2477.     }
  2478.       if (inbyte & (1 << 7))
  2479.     {
  2480.       outs[row][i] |= (1 << 7) & inbyte;
  2481.       row = (row + 1) & 3;
  2482.     }
  2483.     }
  2484. }
  2485.  
  2486. static void
  2487. escp2_split_4_2(int length,
  2488.         const unsigned char *in,
  2489.         unsigned char *out0,
  2490.         unsigned char *out1,
  2491.         unsigned char *out2,
  2492.         unsigned char *out3)
  2493. {
  2494.   unsigned char *outs[4];
  2495.   int i;
  2496.   int row = 0;
  2497.   int limit = length * 2;
  2498.   outs[0] = out0;
  2499.   outs[1] = out1;
  2500.   outs[2] = out2;
  2501.   outs[3] = out3;
  2502.   memset(outs[1], 0, limit);
  2503.   memset(outs[2], 0, limit);
  2504.   memset(outs[3], 0, limit);
  2505.   for (i = 0; i < limit; i++)
  2506.     {
  2507.       unsigned char inbyte = in[i];
  2508.       outs[0][i] = 0;
  2509.       if (inbyte == 0)
  2510.     continue;
  2511.       /* For some reason gcc isn't unrolling this, even with -funroll-loops */
  2512.       if (inbyte & 3)
  2513.     {
  2514.       outs[row][i] |= 3 & inbyte;
  2515.       row = (row + 1) & 3;
  2516.     }
  2517.       if (inbyte & (3 << 2))
  2518.     {
  2519.       outs[row][i] |= (3 << 2) & inbyte;
  2520.       row = (row + 1) & 3;
  2521.     }
  2522.       if (inbyte & (3 << 4))
  2523.     {
  2524.       outs[row][i] |= (3 << 4) & inbyte;
  2525.       row = (row + 1) & 3;
  2526.     }
  2527.       if (inbyte & (3 << 6))
  2528.     {
  2529.       outs[row][i] |= (3 << 6) & inbyte;
  2530.       row = (row + 1) & 3;
  2531.     }
  2532.     }
  2533. }
  2534.  
  2535. static void
  2536. escp2_split_4(int length,
  2537.           int bits,
  2538.           const unsigned char *in,
  2539.           unsigned char *out0,
  2540.           unsigned char *out1,
  2541.           unsigned char *out2,
  2542.           unsigned char *out3)
  2543. {
  2544.   if (bits == 2)
  2545.     escp2_split_4_2(length, in, out0, out1, out2, out3);
  2546.   else
  2547.     escp2_split_4_1(length, in, out0, out1, out2, out3);
  2548. }
  2549.  
  2550.  
  2551. #if __BYTE_ORDER == __LITTLE_ENDIAN
  2552. #define SH20 0
  2553. #define SH21 8
  2554. #else
  2555. #define SH20 8
  2556. #define SH21 0
  2557. #endif
  2558.  
  2559. static void
  2560. escp2_unpack_2_1(int length,
  2561.          const unsigned char *in,
  2562.          unsigned char *out0,
  2563.          unsigned char *out1)
  2564. {
  2565.   unsigned char    tempin,
  2566.         bit,
  2567.         temp0,
  2568.         temp1;
  2569.  
  2570.  
  2571.   for (bit = 128, temp0 = 0, temp1 = 0;
  2572.        length > 0;
  2573.        length --)
  2574.     {
  2575.       tempin = *in++;
  2576.  
  2577.       if (tempin & 128)
  2578.         temp0 |= bit;
  2579.       if (tempin & 64)
  2580.         temp1 |= bit;
  2581.  
  2582.       bit >>= 1;
  2583.  
  2584.       if (tempin & 32)
  2585.         temp0 |= bit;
  2586.       if (tempin & 16)
  2587.         temp1 |= bit;
  2588.  
  2589.       bit >>= 1;
  2590.  
  2591.       if (tempin & 8)
  2592.         temp0 |= bit;
  2593.       if (tempin & 4)
  2594.         temp1 |= bit;
  2595.  
  2596.       bit >>= 1;
  2597.  
  2598.       if (tempin & 2)
  2599.         temp0 |= bit;
  2600.       if (tempin & 1)
  2601.         temp1 |= bit;
  2602.  
  2603.       if (bit > 1)
  2604.         bit >>= 1;
  2605.       else
  2606.       {
  2607.         bit     = 128;
  2608.     *out0++ = temp0;
  2609.     *out1++ = temp1;
  2610.  
  2611.     temp0   = 0;
  2612.     temp1   = 0;
  2613.       }
  2614.     }
  2615.  
  2616.   if (bit < 128)
  2617.     {
  2618.       *out0++ = temp0;
  2619.       *out1++ = temp1;
  2620.     }
  2621. }
  2622.  
  2623. static void
  2624. escp2_unpack_2_2(int length,
  2625.          const unsigned char *in,
  2626.          unsigned char *out0,
  2627.          unsigned char *out1)
  2628. {
  2629.   unsigned char    tempin,
  2630.         shift,
  2631.         temp0,
  2632.         temp1;
  2633.  
  2634.  
  2635.   length *= 2;
  2636.  
  2637.   for (shift = 0, temp0 = 0, temp1 = 0;
  2638.        length > 0;
  2639.        length --)
  2640.     {
  2641.      /*
  2642.       * Note - we can't use (tempin & N) >> (shift - M) since negative
  2643.       * right-shifts are not always implemented.
  2644.       */
  2645.  
  2646.       tempin = *in++;
  2647.  
  2648.       if (tempin & 192)
  2649.         temp0 |= (tempin & 192) >> shift;
  2650.       if (tempin & 48)
  2651.         temp1 |= ((tempin & 48) << 2) >> shift;
  2652.  
  2653.       shift += 2;
  2654.  
  2655.       if (tempin & 12)
  2656.         temp0 |= ((tempin & 12) << 4) >> shift;
  2657.       if (tempin & 3)
  2658.         temp1 |= ((tempin & 3) << 6) >> shift;
  2659.  
  2660.       if (shift < 6)
  2661.         shift += 2;
  2662.       else
  2663.       {
  2664.         shift   = 0;
  2665.     *out0++ = temp0;
  2666.     *out1++ = temp1;
  2667.  
  2668.     temp0   = 0;
  2669.     temp1   = 0;
  2670.       }
  2671.     }
  2672.  
  2673.   if (shift)
  2674.     {
  2675.       *out0++ = temp0;
  2676.       *out1++ = temp1;
  2677.     }
  2678. }
  2679.  
  2680. static void
  2681. escp2_unpack_2(int length,
  2682.            int bits,
  2683.            const unsigned char *in,
  2684.            unsigned char *outlo,
  2685.            unsigned char *outhi)
  2686. {
  2687.   if (bits == 1)
  2688.     escp2_unpack_2_1(length, in, outlo, outhi);
  2689.   else
  2690.     escp2_unpack_2_2(length, in, outlo, outhi);
  2691. }
  2692.  
  2693. #if __BYTE_ORDER == __LITTLE_ENDIAN
  2694. #define SH40 0
  2695. #define SH41 8
  2696. #define SH42 16
  2697. #define SH43 24
  2698. #else
  2699. #define SH40 24
  2700. #define SH41 16
  2701. #define SH42 8
  2702. #define SH43 0
  2703. #endif
  2704.  
  2705. static void
  2706. escp2_unpack_4_1(int length,
  2707.          const unsigned char *in,
  2708.          unsigned char *out0,
  2709.          unsigned char *out1,
  2710.          unsigned char *out2,
  2711.          unsigned char *out3)
  2712. {
  2713.   unsigned char    tempin,
  2714.         bit,
  2715.         temp0,
  2716.         temp1,
  2717.         temp2,
  2718.         temp3;
  2719.  
  2720.  
  2721.   for (bit = 128, temp0 = 0, temp1 = 0, temp2 = 0, temp3 = 0;
  2722.        length > 0;
  2723.        length --)
  2724.     {
  2725.       tempin = *in++;
  2726.  
  2727.       if (tempin & 128)
  2728.         temp0 |= bit;
  2729.       if (tempin & 64)
  2730.         temp1 |= bit;
  2731.       if (tempin & 32)
  2732.         temp2 |= bit;
  2733.       if (tempin & 16)
  2734.         temp3 |= bit;
  2735.  
  2736.       bit >>= 1;
  2737.  
  2738.       if (tempin & 8)
  2739.         temp0 |= bit;
  2740.       if (tempin & 4)
  2741.         temp1 |= bit;
  2742.       if (tempin & 2)
  2743.         temp2 |= bit;
  2744.       if (tempin & 1)
  2745.         temp3 |= bit;
  2746.  
  2747.       if (bit > 1)
  2748.         bit >>= 1;
  2749.       else
  2750.       {
  2751.         bit     = 128;
  2752.     *out0++ = temp0;
  2753.     *out1++ = temp1;
  2754.     *out2++ = temp2;
  2755.     *out3++ = temp3;
  2756.  
  2757.     temp0   = 0;
  2758.     temp1   = 0;
  2759.     temp2   = 0;
  2760.     temp3   = 0;
  2761.       }
  2762.     }
  2763.  
  2764.   if (bit < 128)
  2765.     {
  2766.       *out0++ = temp0;
  2767.       *out1++ = temp1;
  2768.       *out2++ = temp2;
  2769.       *out3++ = temp3;
  2770.     }
  2771. }
  2772.  
  2773. static void
  2774. escp2_unpack_4_2(int length,
  2775.          const unsigned char *in,
  2776.          unsigned char *out0,
  2777.          unsigned char *out1,
  2778.          unsigned char *out2,
  2779.          unsigned char *out3)
  2780. {
  2781.   unsigned char    tempin,
  2782.         shift,
  2783.         temp0,
  2784.         temp1,
  2785.         temp2,
  2786.         temp3;
  2787.  
  2788.  
  2789.   length *= 2;
  2790.  
  2791.   for (shift = 0, temp0 = 0, temp1 = 0, temp2 = 0, temp3 = 0;
  2792.        length > 0;
  2793.        length --)
  2794.     {
  2795.      /*
  2796.       * Note - we can't use (tempin & N) >> (shift - M) since negative
  2797.       * right-shifts are not always implemented.
  2798.       */
  2799.  
  2800.       tempin = *in++;
  2801.  
  2802.       if (tempin & 192)
  2803.         temp0 |= (tempin & 192) >> shift;
  2804.       if (tempin & 48)
  2805.         temp1 |= ((tempin & 48) << 2) >> shift;
  2806.       if (tempin & 12)
  2807.         temp2 |= ((tempin & 12) << 4) >> shift;
  2808.       if (tempin & 3)
  2809.         temp3 |= ((tempin & 3) << 6) >> shift;
  2810.  
  2811.       if (shift < 6)
  2812.         shift += 2;
  2813.       else
  2814.       {
  2815.         shift   = 0;
  2816.     *out0++ = temp0;
  2817.     *out1++ = temp1;
  2818.     *out2++ = temp2;
  2819.     *out3++ = temp3;
  2820.  
  2821.     temp0   = 0;
  2822.     temp1   = 0;
  2823.     temp2   = 0;
  2824.     temp3   = 0;
  2825.       }
  2826.     }
  2827.  
  2828.   if (shift)
  2829.     {
  2830.       *out0++ = temp0;
  2831.       *out1++ = temp1;
  2832.       *out2++ = temp2;
  2833.       *out3++ = temp3;
  2834.     }
  2835. }
  2836.  
  2837. static void
  2838. escp2_unpack_4(int length,
  2839.            int bits,
  2840.            const unsigned char *in,
  2841.            unsigned char *out0,
  2842.            unsigned char *out1,
  2843.            unsigned char *out2,
  2844.            unsigned char *out3)
  2845. {
  2846.   if (bits == 1)
  2847.     escp2_unpack_4_1(length, in, out0, out1, out2, out3);
  2848.   else
  2849.     escp2_unpack_4_2(length, in, out0, out1, out2, out3);
  2850. }
  2851.  
  2852. static void
  2853. escp2_unpack_8_1(int length,
  2854.          const unsigned char *in,
  2855.          unsigned char *out0,
  2856.          unsigned char *out1,
  2857.          unsigned char *out2,
  2858.          unsigned char *out3,
  2859.          unsigned char *out4,
  2860.          unsigned char *out5,
  2861.          unsigned char *out6,
  2862.          unsigned char *out7)
  2863. {
  2864.   unsigned char    tempin,
  2865.         bit,
  2866.         temp0,
  2867.         temp1,
  2868.         temp2,
  2869.         temp3,
  2870.         temp4,
  2871.         temp5,
  2872.         temp6,
  2873.         temp7;
  2874.  
  2875.  
  2876.   for (bit = 128, temp0 = 0, temp1 = 0, temp2 = 0,
  2877.        temp3 = 0, temp4 = 0, temp5 = 0, temp6 = 0, temp7 = 0;
  2878.        length > 0;
  2879.        length --)
  2880.     {
  2881.       tempin = *in++;
  2882.  
  2883.       if (tempin & 128)
  2884.         temp0 |= bit;
  2885.       if (tempin & 64)
  2886.         temp1 |= bit;
  2887.       if (tempin & 32)
  2888.         temp2 |= bit;
  2889.       if (tempin & 16)
  2890.         temp3 |= bit;
  2891.       if (tempin & 8)
  2892.         temp4 |= bit;
  2893.       if (tempin & 4)
  2894.         temp5 |= bit;
  2895.       if (tempin & 2)
  2896.         temp6 |= bit;
  2897.       if (tempin & 1)
  2898.         temp7 |= bit;
  2899.  
  2900.       if (bit > 1)
  2901.         bit >>= 1;
  2902.       else
  2903.       {
  2904.         bit     = 128;
  2905.     *out0++ = temp0;
  2906.     *out1++ = temp1;
  2907.     *out2++ = temp2;
  2908.     *out3++ = temp3;
  2909.     *out4++ = temp4;
  2910.     *out5++ = temp5;
  2911.     *out6++ = temp6;
  2912.     *out7++ = temp7;
  2913.  
  2914.     temp0   = 0;
  2915.     temp1   = 0;
  2916.     temp2   = 0;
  2917.     temp3   = 0;
  2918.     temp4   = 0;
  2919.     temp5   = 0;
  2920.     temp6   = 0;
  2921.     temp7   = 0;
  2922.       }
  2923.     }
  2924.  
  2925.   if (bit < 128)
  2926.     {
  2927.       *out0++ = temp0;
  2928.       *out1++ = temp1;
  2929.       *out2++ = temp2;
  2930.       *out3++ = temp3;
  2931.       *out4++ = temp4;
  2932.       *out5++ = temp5;
  2933.       *out6++ = temp6;
  2934.       *out7++ = temp7;
  2935.     }
  2936. }
  2937.  
  2938. static void
  2939. escp2_unpack_8_2(int length,
  2940.          const unsigned char *in,
  2941.          unsigned char *out0,
  2942.          unsigned char *out1,
  2943.          unsigned char *out2,
  2944.          unsigned char *out3,
  2945.          unsigned char *out4,
  2946.          unsigned char *out5,
  2947.          unsigned char *out6,
  2948.          unsigned char *out7)
  2949. {
  2950.   unsigned char    tempin,
  2951.         shift,
  2952.         temp0,
  2953.         temp1,
  2954.         temp2,
  2955.         temp3,
  2956.         temp4,
  2957.         temp5,
  2958.         temp6,
  2959.         temp7;
  2960.  
  2961.  
  2962.   for (shift = 0, temp0 = 0, temp1 = 0,
  2963.        temp2 = 0, temp3 = 0, temp4 = 0, temp5 = 0, temp6 = 0, temp7 = 0;
  2964.        length > 0;
  2965.        length --)
  2966.     {
  2967.      /*
  2968.       * Note - we can't use (tempin & N) >> (shift - M) since negative
  2969.       * right-shifts are not always implemented.
  2970.       */
  2971.  
  2972.       tempin = *in++;
  2973.  
  2974.       if (tempin & 192)
  2975.         temp0 |= (tempin & 192) >> shift;
  2976.       if (tempin & 48)
  2977.         temp1 |= ((tempin & 48) << 2) >> shift;
  2978.       if (tempin & 12)
  2979.         temp2 |= ((tempin & 12) << 4) >> shift;
  2980.       if (tempin & 3)
  2981.         temp3 |= ((tempin & 3) << 6) >> shift;
  2982.  
  2983.       tempin = *in++;
  2984.  
  2985.       if (tempin & 192)
  2986.         temp4 |= (tempin & 192) >> shift;
  2987.       if (tempin & 48)
  2988.         temp5 |= ((tempin & 48) << 2) >> shift;
  2989.       if (tempin & 12)
  2990.         temp6 |= ((tempin & 12) << 4) >> shift;
  2991.       if (tempin & 3)
  2992.         temp7 |= ((tempin & 3) << 6) >> shift;
  2993.  
  2994.       if (shift < 6)
  2995.         shift += 2;
  2996.       else
  2997.       {
  2998.         shift   = 0;
  2999.     *out0++ = temp0;
  3000.     *out1++ = temp1;
  3001.     *out2++ = temp2;
  3002.     *out3++ = temp3;
  3003.     *out4++ = temp4;
  3004.     *out5++ = temp5;
  3005.     *out6++ = temp6;
  3006.     *out7++ = temp7;
  3007.  
  3008.     temp0   = 0;
  3009.     temp1   = 0;
  3010.     temp2   = 0;
  3011.     temp3   = 0;
  3012.     temp4   = 0;
  3013.     temp5   = 0;
  3014.     temp6   = 0;
  3015.     temp7   = 0;
  3016.       }
  3017.     }
  3018.  
  3019.   if (shift)
  3020.     {
  3021.       *out0++ = temp0;
  3022.       *out1++ = temp1;
  3023.       *out2++ = temp2;
  3024.       *out3++ = temp3;
  3025.       *out4++ = temp4;
  3026.       *out5++ = temp5;
  3027.       *out6++ = temp6;
  3028.       *out7++ = temp7;
  3029.     }
  3030. }
  3031.  
  3032. static void
  3033. escp2_unpack_8(int length,
  3034.            int bits,
  3035.            const unsigned char *in,
  3036.            unsigned char *out0,
  3037.            unsigned char *out1,
  3038.            unsigned char *out2,
  3039.            unsigned char *out3,
  3040.            unsigned char *out4,
  3041.            unsigned char *out5,
  3042.            unsigned char *out6,
  3043.            unsigned char *out7)
  3044. {
  3045.   if (bits == 1)
  3046.     escp2_unpack_8_1(length, in, out0, out1, out2, out3,
  3047.              out4, out5, out6, out7);
  3048.   else
  3049.     escp2_unpack_8_2(length, in, out0, out1, out2, out3,
  3050.              out4, out5, out6, out7);
  3051. }
  3052.  
  3053. static int
  3054. escp2_pack(const unsigned char *line,
  3055.        int length,
  3056.        unsigned char *comp_buf,
  3057.        unsigned char **comp_ptr)
  3058. {
  3059.   const unsigned char *start;        /* Start of compressed data */
  3060.   unsigned char repeat;            /* Repeating char */
  3061.   int count;            /* Count of compressed bytes */
  3062.   int tcount;            /* Temporary count < 128 */
  3063.   int active = 0;        /* Have we found data? */
  3064.  
  3065.   /*
  3066.    * Compress using TIFF "packbits" run-length encoding...
  3067.    */
  3068.  
  3069.   (*comp_ptr) = comp_buf;
  3070.  
  3071.   while (length > 0)
  3072.     {
  3073.       /*
  3074.        * Get a run of non-repeated chars...
  3075.        */
  3076.  
  3077.       start  = line;
  3078.       line   += 2;
  3079.       length -= 2;
  3080.  
  3081.       while (length > 0 && (line[-2] != line[-1] || line[-1] != line[0]))
  3082.     {
  3083.       if (! active && (line[-2] || line[-1] || line[0]))
  3084.         active = 1;
  3085.       line ++;
  3086.       length --;
  3087.     }
  3088.  
  3089.       line   -= 2;
  3090.       length += 2;
  3091.  
  3092.       /*
  3093.        * Output the non-repeated sequences (max 128 at a time).
  3094.        */
  3095.  
  3096.       count = line - start;
  3097.       while (count > 0)
  3098.     {
  3099.       tcount = count > 128 ? 128 : count;
  3100.  
  3101.       (*comp_ptr)[0] = tcount - 1;
  3102.       memcpy((*comp_ptr) + 1, start, tcount);
  3103.  
  3104.       (*comp_ptr) += tcount + 1;
  3105.       start    += tcount;
  3106.       count    -= tcount;
  3107.     }
  3108.  
  3109.       if (length <= 0)
  3110.     break;
  3111.  
  3112.       /*
  3113.        * Find the repeated sequences...
  3114.        */
  3115.  
  3116.       start  = line;
  3117.       repeat = line[0];
  3118.       if (repeat)
  3119.     active = 1;
  3120.  
  3121.       line ++;
  3122.       length --;
  3123.  
  3124.       while (length > 0 && *line == repeat)
  3125.     {
  3126.       line ++;
  3127.       length --;
  3128.     }
  3129.  
  3130.       /*
  3131.        * Output the repeated sequences (max 128 at a time).
  3132.        */
  3133.  
  3134.       count = line - start;
  3135.       while (count > 0)
  3136.     {
  3137.       tcount = count > 128 ? 128 : count;
  3138.  
  3139.       (*comp_ptr)[0] = 1 - tcount;
  3140.       (*comp_ptr)[1] = repeat;
  3141.  
  3142.       (*comp_ptr) += 2;
  3143.       count    -= tcount;
  3144.     }
  3145.     }
  3146.   return active;
  3147. }
  3148.  
  3149. static unsigned char *microweave_s = 0;
  3150. static unsigned char *microweave_comp_ptr[6][4];
  3151. static int microweave_setactive[6][4];
  3152. static int accumulated_spacing = 0;
  3153. static int last_color = -1;
  3154.  
  3155. #define MICRO_S(c, l) (microweave_s + COMPBUFWIDTH * (l) + COMPBUFWIDTH * (c) * 4)
  3156.  
  3157. static void
  3158. escp2_init_microweave(int top)
  3159. {
  3160.   if (!microweave_s)
  3161.     microweave_s = malloc(6 * 4 * COMPBUFWIDTH);
  3162.   accumulated_spacing = top;
  3163. }
  3164.  
  3165. static void
  3166. escp2_free_microweave()
  3167. {
  3168.   if (microweave_s)
  3169.     {
  3170.       free(microweave_s);
  3171.       microweave_s = NULL;
  3172.     }
  3173. }
  3174.  
  3175. static int
  3176. escp2_do_microweave_pack(const unsigned char *line,
  3177.              int length,
  3178.              int oversample,
  3179.              int bits,
  3180.              int color)
  3181. {
  3182.   static unsigned char *pack_buf = NULL;
  3183.   static unsigned char *s[4] = { NULL, NULL, NULL, NULL };
  3184.   const unsigned char *in;
  3185.   int i;
  3186.   int retval = 0;
  3187.   if (!pack_buf)
  3188.     pack_buf = malloc(COMPBUFWIDTH);
  3189.   for (i = 0; i < oversample; i++)
  3190.     {
  3191.       if (!s[i])
  3192.     s[i] = malloc(COMPBUFWIDTH);
  3193.     }
  3194.  
  3195.   if (!line ||
  3196.       (line[0] == 0 && memcmp(line, line + 1, (bits * length) - 1) == 0))
  3197.     {
  3198.       for (i = 0; i < 4; i++)
  3199.     microweave_setactive[color][i] = 0;
  3200.       return 0;
  3201.     }
  3202.   if (bits == 1)
  3203.     in = line;
  3204.   else
  3205.     {
  3206.       escp2_fold(line, length, pack_buf);
  3207.       in = pack_buf;
  3208.     }
  3209.   switch (oversample)
  3210.     {
  3211.     case 1:
  3212.       memcpy(s[0], in, bits * length);
  3213.       break;
  3214.     case 2:
  3215.       escp2_unpack_2(length, bits, in, s[0], s[1]);
  3216.       break;
  3217.     case 4:
  3218.       escp2_unpack_4(length, bits, in, s[0], s[1], s[2], s[3]);
  3219.       break;
  3220.     }
  3221.   for (i = 0; i < oversample; i++)
  3222.     {
  3223.       microweave_setactive[color][i] =
  3224.     escp2_pack(s[i], length * bits, MICRO_S(color, i),
  3225.            &(microweave_comp_ptr[color][i]));
  3226.       retval |= microweave_setactive[color][i];
  3227.     }
  3228.   return retval;
  3229. }
  3230.  
  3231. static void
  3232. escp2_write_microweave(FILE          *prn,    /* I - Print file or command */
  3233.                const unsigned char *k,    /* I - Output bitmap data */
  3234.                const unsigned char *c,    /* I - Output bitmap data */
  3235.                const unsigned char *m,    /* I - Output bitmap data */
  3236.                const unsigned char *y,    /* I - Output bitmap data */
  3237.                const unsigned char *lc,    /* I - Output bitmap data */
  3238.                const unsigned char *lm,    /* I - Output bitmap data */
  3239.                int           length,    /* I - Length of bitmap data */
  3240.                int           xdpi,    /* I - Horizontal resolution */
  3241.                int           ydpi,    /* I - Vertical resolution */
  3242.                int           model,    /* I - Printer model */
  3243.                int           width,    /* I - Printed width */
  3244.                int           offset,    /* I - Offset from left side */
  3245.                int         bits)
  3246. {
  3247.   int i, j;
  3248.   int oversample = 1;
  3249.   int gsetactive = 0;
  3250.   if (xdpi > 720)
  3251.     oversample = xdpi / 720;
  3252.  
  3253.   gsetactive |= escp2_do_microweave_pack(k, length, oversample, bits, 0);
  3254.   gsetactive |= escp2_do_microweave_pack(m, length, oversample, bits, 1);
  3255.   gsetactive |= escp2_do_microweave_pack(c, length, oversample, bits, 2);
  3256.   gsetactive |= escp2_do_microweave_pack(y, length, oversample, bits, 3);
  3257.   gsetactive |= escp2_do_microweave_pack(lm, length, oversample, bits, 4);
  3258.   gsetactive |= escp2_do_microweave_pack(lc, length, oversample, bits, 5);
  3259.   if (!gsetactive)
  3260.     {
  3261.       accumulated_spacing++;
  3262.       return;
  3263.     }
  3264.   for (i = 0; i < oversample; i++)
  3265.     {
  3266.       for (j = 0; j < 6; j++)
  3267.     {
  3268.       if (!microweave_setactive[j][i])
  3269.         continue;
  3270.       if (accumulated_spacing > 0)
  3271.         fprintf(prn, "\033(v\002%c%c%c", 0, accumulated_spacing % 256,
  3272.             (accumulated_spacing >> 8) % 256);
  3273.       accumulated_spacing = 0;
  3274.       /*
  3275.        * Set the print head position.
  3276.        */
  3277.  
  3278.       if (escp2_has_cap(model, MODEL_1440DPI_MASK, MODEL_1440DPI_YES) &&
  3279.           xdpi > 720)
  3280.         {
  3281.           if (!escp2_has_cap(model, MODEL_VARIABLE_DOT_MASK,
  3282.                  MODEL_VARIABLE_NORMAL))
  3283.         {
  3284.           if (((offset * xdpi / 1440) + i) > 0)
  3285.             fprintf(prn, "\033($%c%c%c%c%c%c", 4, 0,
  3286.                 ((offset * xdpi / 1440) + i) & 255,
  3287.                 (((offset * xdpi / 1440) + i) >> 8) & 255,
  3288.                 (((offset * xdpi / 1440) + i) >> 16) & 255,
  3289.                 (((offset * xdpi / 1440) + i) >> 24) & 255);
  3290.         }
  3291.           else
  3292.         {
  3293.           if (((offset * 1440 / ydpi) + i) > 0)
  3294.             fprintf(prn, "\033(\\%c%c%c%c%c%c", 4, 0, 160, 5,
  3295.                 ((offset * 1440 / ydpi) + i) & 255,
  3296.                 ((offset * 1440 / ydpi) + i) >> 8);
  3297.         }
  3298.         }
  3299.       else
  3300.         {
  3301.           if (offset > 0)
  3302.         fprintf(prn, "\033\\%c%c", offset & 255, offset >> 8);
  3303.         }
  3304.       if (j != last_color)
  3305.         {
  3306.           if (escp2_has_cap(model, MODEL_6COLOR_MASK, MODEL_6COLOR_YES))
  3307.         fprintf(prn, "\033(r\002%c%c%c", 0, densities[j], colors[j]);
  3308.           else
  3309.         fprintf(prn, "\033r%c", colors[j]);
  3310.           last_color = j;
  3311.         }
  3312.       /*
  3313.        * Send a line of raster graphics...
  3314.        */
  3315.  
  3316.       switch (ydpi)                /* Raster graphics header */
  3317.         {
  3318.         case 180 :
  3319.           fwrite("\033.\001\024\024\001", 6, 1, prn);
  3320.           break;
  3321.         case 360 :
  3322.           fwrite("\033.\001\012\012\001", 6, 1, prn);
  3323.           break;
  3324.         case 720 :
  3325.           if (escp2_has_cap(model, MODEL_720DPI_MODE_MASK,
  3326.                 MODEL_720DPI_600))
  3327.         fwrite("\033.\001\050\005\001", 6, 1, prn);
  3328.           else
  3329.         fwrite("\033.\001\005\005\001", 6, 1, prn);
  3330.           break;
  3331.         }
  3332.       putc(width & 255, prn);    /* Width of raster line in pixels */
  3333.       putc(width >> 8, prn);
  3334.  
  3335.       fwrite(MICRO_S(j, i), microweave_comp_ptr[j][i] - MICRO_S(j, i),
  3336.          1, prn);
  3337.       putc('\r', prn);
  3338.     }
  3339.     }
  3340.   accumulated_spacing++;
  3341. }
  3342.  
  3343.  
  3344. /*
  3345.  * "Soft" weave
  3346.  *
  3347.  * The Epson Stylus Color/Photo printers don't have memory to print
  3348.  * using all of the nozzles in the print head.  For example, the Stylus Photo
  3349.  * 700/EX has 32 nozzles.  At 720 dpi, with an 8" wide image, a single line
  3350.  * requires (8 * 720 * 6 / 8) bytes, or 4320 bytes (because the Stylus Photo
  3351.  * printers have 6 ink colors).  To use 32 nozzles would require 138240 bytes.
  3352.  * It's actually worse than that, though, because the nozzles are spaced 8
  3353.  * rows apart.  Therefore, in order to store enough data to permit sending the
  3354.  * page as a simple raster, the printer would require enough memory to store
  3355.  * 256 rows, or 1105920 bytes.  Considering that the Photo EX can print
  3356.  * 11" wide, we're looking at more like 1.5 MB.  In fact, these printers are
  3357.  * capable of 1440 dpi horizontal resolution.  This would require 3 MB.  The
  3358.  * printers actually have 64K-256K.
  3359.  *
  3360.  * With the newer (740/750 and later) printers it's even worse, since these
  3361.  * printers support multiple dot sizes.  But that's neither here nor there.
  3362.  *
  3363.  * Older Epson printers had a mode called MicroWeave (tm).  In this mode, the
  3364.  * host fed the printer individual rows of dots, and the printer bundled them
  3365.  * up and sent them to the print head in the correct order to achieve high
  3366.  * quality.  This MicroWeave mode still works in new printers, but the
  3367.  * implementation is very minimal: the printer uses exactly one nozzle of
  3368.  * each color (the first one).  This makes printing extremely slow (more than
  3369.  * 30 minutes for one 8.5x11" page), although the quality is extremely high
  3370.  * with no visible banding whatsoever.  It's not good for the print head,
  3371.  * though, since no ink is flowing through the other nozzles.  This leads to
  3372.  * drying of ink and possible permanent damage to the print head.
  3373.  *
  3374.  * By the way, although the Epson manual says that microweave mode should be
  3375.  * used at 720 dpi, 360 dpi continues to work in much the same way.  At 360
  3376.  * dpi, data is fed to the printer one row at a time on all Epson printers.
  3377.  * The pattern that the printer uses to print is very prone to banding.
  3378.  * However, 360 dpi is inherently a low quality mode; if you're using it,
  3379.  * presumably you don't much care about quality.
  3380.  *
  3381.  * Printers from roughly the Stylus Color 600 and later do not have the
  3382.  * capability to do MicroWeave correctly.  Instead, the host must arrange
  3383.  * the output in the order that it will be sent to the print head.  This
  3384.  * is a very complex process; the jets in the print head are spaced more
  3385.  * than one row (1/720") apart, so we can't simply send consecutive rows
  3386.  * of dots to the printer.  Instead, we have to pass e. g. the first, ninth,
  3387.  * 17th, 25th... rows in order for them to print in the correct position on
  3388.  * the paper.  This interleaving process is called "soft" weaving.
  3389.  *
  3390.  * This decision was probably made to save money on memory in the printer.
  3391.  * It certainly makes the driver code far more complicated than it would
  3392.  * be if the printer could arrange the output.  Is that a bad thing?
  3393.  * Usually this takes far less CPU time than the dithering process, and it
  3394.  * does allow us more control over the printing process, e. g. to reduce
  3395.  * banding.  Conceivably, we could even use this ability to map out bad
  3396.  * jets.
  3397.  *
  3398.  * Interestingly, apparently the Windows (and presumably Macintosh) drivers
  3399.  * for most or all Epson printers still list a "microweave" mode.
  3400.  * Experiments have demonstrated that this does not in fact use the
  3401.  * "microweave" mode of the printer.  Possibly it does nothing, or it uses
  3402.  * a different weave pattern from what the non-"microweave" mode does.
  3403.  * This is unnecessarily confusing.
  3404.  *
  3405.  * What makes this interesting is that there are many different ways of
  3406.  * of accomplishing this goal.  The naive way would be to divide the image
  3407.  * up into groups of 256 rows, and print all the mod8=0 rows in the first pass,
  3408.  * mod8=1 rows in the second, and so forth.  The problem with this approach
  3409.  * is that the individual ink jets are not perfectly uniform; some emit
  3410.  * slightly bigger or smaller drops than others.  Since each group of 8
  3411.  * adjacent rows is printed with the same nozzle, that means that there will
  3412.  * be distinct streaks of lighter and darker bands within the image (8 rows
  3413.  * is 1/90", which is visible; 1/720" is not).  Possibly worse is that these
  3414.  * patterns will repeat every 256 rows.  This creates banding patterns that
  3415.  * are about 1/3" wide.
  3416.  *
  3417.  * So we have to do something to break up this patterning.
  3418.  *
  3419.  * Epson does not publish the weaving algorithms that they use in their
  3420.  * bundled drivers.  Indeed, their developer web site
  3421.  * (http://www.ercipd.com/isv/edr_docs.htm) does not even describe how to
  3422.  * do this weaving at all; it says that the only way to achieve 720 dpi
  3423.  * is to use MicroWeave.  It does note (correctly) that 1440 dpi horizontal
  3424.  * can only be achieved by the driver (i. e. in software).  The manual
  3425.  * actually makes it fairly clear how to do this (it requires two passes
  3426.  * with horizontal head movement between passes), and it is presumably
  3427.  * possible to do this with MicroWeave.
  3428.  *
  3429.  * The information about how to do this is apparently available under NDA.
  3430.  * It's actually easy enough to reverse engineer what's inside a print file
  3431.  * with a simple Perl script.  There are presumably other printer commands
  3432.  * that are not documented and may not be as easy to reverse engineer.
  3433.  *
  3434.  * I considered a few algorithms to perform the weave.  The first one I
  3435.  * devised let me use only (jets - distance_between_jets + 1) nozzles, or
  3436.  * 25.  This is OK in principle, but it's slower than using all nozzles.
  3437.  * By playing around with it some more, I came up with an algorithm that
  3438.  * lets me use all of the nozzles, except near the top and bottom of the
  3439.  * page.
  3440.  *
  3441.  * This still produces some banding, though.  Even better quality can be
  3442.  * achieved by using multiple nozzles on the same line.  How do we do this?
  3443.  * In 1440x720 mode, we're printing two output lines at the same vertical
  3444.  * position.  However, if we want four passes, we have to effectively print
  3445.  * each line twice.  Actually doing this would increase the density, so
  3446.  * what we do is print half the dots on each pass.  This produces near-perfect
  3447.  * output, and it's far faster than using (pseudo) "MicroWeave".
  3448.  *
  3449.  * The current algorithm is not completely general.  The number of passes
  3450.  * is limited to (nozzles / gap).  On the Photo EX class printers, that limits
  3451.  * it to 4 -- 32 nozzles, an inter-nozzle gap of 8 lines.  Furthermore, there
  3452.  * are a number of routines that are only coded up to 8 passes.  Fortunately,
  3453.  * this is enough passes to get rid of most banding.  What's left is a very
  3454.  * fine pattern that is sometimes described as "corduroy", since the pattern
  3455.  * looks like that kind of fabric.
  3456.  *
  3457.  * Newer printers (those that support variable dot sizes, such as the 740,
  3458.  * 1200, etc.) have an additional complication: when used in softweave mode,
  3459.  * they operate at 360 dpi horizontal resolution.  This requires FOUR passes
  3460.  * to achieve 1440x720 dpi.  Thus, to enable us to break up each row
  3461.  * into separate sub-rows, we have to actually print each row eight times.
  3462.  * Fortunately, all such printers have 48 nozzles and a gap of 6 rows,
  3463.  * except for the high-speed 900, which uses 96 nozzles and a gap of 2 rows.
  3464.  *
  3465.  * I cannot let this entirely pass without commenting on the Stylus Color 440.
  3466.  * This is a very low-end printer with 21 (!) nozzles and a separation of 8.
  3467.  * The weave routine works correctly with single-pass printing, which is enough
  3468.  * to minimally achieve 720 dpi output (it's physically a 720 dpi printer).
  3469.  * However, the routine does not work correctly at more than one pass per row.
  3470.  * Therefore, this printer bands badly.
  3471.  *
  3472.  * Yet another complication is how to get near the top and bottom of the page.
  3473.  * This algorithm lets us print to within one head width of the top of the
  3474.  * page, and a bit more than one head width from the bottom.  That leaves a
  3475.  * lot of blank space.  Doing the weave properly outside of this region is
  3476.  * increasingly difficult as we get closer to the edge of the paper; in the
  3477.  * interior region, any nozzle can print any line, but near the top and
  3478.  * bottom edges, only some nozzles can print.  We've handled this for now by
  3479.  * using the naive way mentioned above near the borders, and switching over
  3480.  * to the high quality method in the interior.  Unfortunately, this means
  3481.  * that the quality is quite visibly degraded near the top and bottom of the
  3482.  * page.  Algorithms that degrade more gracefully are more complicated.
  3483.  * Epson does not advertise that the printers can print at the very top of the
  3484.  * page, although in practice most or all of them can.  I suspect that the
  3485.  * quality that can be achieved very close to the top is poor enough that
  3486.  * Epson does not want to allow printing there.  That is a valid decision,
  3487.  * although we have taken another approach.
  3488.  *
  3489.  * To compute the weave information, we need to start with the following
  3490.  * information:
  3491.  *
  3492.  * 1) The number of jets the print head has for each color;
  3493.  *
  3494.  * 2) The separation in rows between the jets;
  3495.  *
  3496.  * 3) The horizontal resolution of the printer;
  3497.  *
  3498.  * 4) The desired horizontal resolution of the output;
  3499.  *
  3500.  * 5) The desired extra passes to reduce banding.
  3501.  *
  3502.  * As discussed above, each row is actually printed in one or more passes
  3503.  * of the print head; we refer to these as subpasses.  For example, if we're
  3504.  * printing at 1440(h)x720(v) on a printer with true horizontal resolution of
  3505.  * 360 dpi, and we wish to print each line twice with different nozzles
  3506.  * to reduce banding, we need to use 8 subpasses.  The dither routine
  3507.  * will feed us a complete row of bits for each color; we have to split that
  3508.  * up, first by round robining the bits to ensure that they get printed at
  3509.  * the right micro-position, and then to split up the bits that are actually
  3510.  * turned on into two equal chunks to reduce banding.
  3511.  *
  3512.  * Given the above information, and the desired row index and subpass (which
  3513.  * together form a line number), we can compute:
  3514.  *
  3515.  * 1) Which pass this line belongs to.  Passes are numbered consecutively,
  3516.  *    and each pass must logically (see #3 below) start at no smaller a row
  3517.  *    number than the previous pass, as the printer cannot advance by a
  3518.  *    negative amount.
  3519.  *
  3520.  * 2) Which jet will print this line.
  3521.  *
  3522.  * 3) The "logical" first line of this pass.  That is, what line would be
  3523.  *    printed by jet 0 in this pass.  This number may be less than zero.
  3524.  *    If it is, there are ghost lines that don't actually contain any data.
  3525.  *    The difference between the logical first line of this pass and the
  3526.  *    logical first line of the preceding pass tells us how many lines must
  3527.  *    be advanced.
  3528.  *
  3529.  * 4) The "physical" first line of this pass.  That is, the first line index
  3530.  *    that is actually printed in this pass.  This information lets us know
  3531.  *    when we must prepare this pass.
  3532.  *
  3533.  * 5) The last line of this pass.  This lets us know when we must actually
  3534.  *    send this pass to the printer.
  3535.  *
  3536.  * 6) The number of ghost rows this pass contains.  We must still send the
  3537.  *    ghost data to the printer, so this lets us know how much data we must
  3538.  *    fill in prior to the start of the pass.
  3539.  *
  3540.  * The bookkeeping to keep track of all this stuff is quite hairy, and needs
  3541.  * to be documented separately.
  3542.  *
  3543.  * The routine initialize_weave calculates the basic parameters, given
  3544.  * the number of jets and separation between jets, in rows.
  3545.  *
  3546.  * -- Robert Krawitz <rlk@alum.mit.edu) November 3, 1999
  3547.  */
  3548.  
  3549. #endif /* !WEAVETEST */
  3550.  
  3551. typedef struct            /* Weave parameters for a specific row */
  3552. {
  3553.   int row;            /* Absolute row # */
  3554.   int pass;            /* Computed pass # */
  3555.   int jet;            /* Which physical nozzle we're using */
  3556.   int missingstartrows;        /* Phantom rows (nonexistent rows that */
  3557.                 /* would be printed by nozzles lower than */
  3558.                 /* the first nozzle we're using this pass; */
  3559.                 /* with the current algorithm, always zero */
  3560.   int logicalpassstart;        /* Offset in rows (from start of image) */
  3561.                 /* that the printer must be for this row */
  3562.                 /* to print correctly with the specified jet */
  3563.   int physpassstart;        /* Offset in rows to the first row printed */
  3564.                 /* in this pass.  Currently always equal to */
  3565.                 /* logicalpassstart */
  3566.   int physpassend;        /* Offset in rows (from start of image) to */
  3567.                 /* the last row that will be printed this */
  3568.                 /* pass (assuming that we're printing a full */
  3569.                 /* pass). */
  3570. } weave_t;
  3571.  
  3572. typedef struct            /* Weave parameters for a specific pass */
  3573. {
  3574.   int pass;            /* Absolute pass number */
  3575.   int missingstartrows;        /* All other values the same as weave_t */
  3576.   int logicalpassstart;
  3577.   int physpassstart;
  3578.   int physpassend;
  3579.   int subpass;
  3580. } pass_t;
  3581.  
  3582. typedef union {            /* Offsets from the start of each line */
  3583.   unsigned long v[6];        /* (really pass) */
  3584.   struct {
  3585.     unsigned long k;
  3586.     unsigned long m;
  3587.     unsigned long c;
  3588.     unsigned long y;
  3589.     unsigned long M;
  3590.     unsigned long C;
  3591.   } p;
  3592. } lineoff_t;
  3593.  
  3594. typedef union {            /* Is this line active? */
  3595.   char v[6];            /* (really pass) */
  3596.   struct {
  3597.     char k;
  3598.     char m;
  3599.     char c;
  3600.     char y;
  3601.     char M;
  3602.     char C;
  3603.   } p;
  3604. } lineactive_t;
  3605.  
  3606. typedef union {            /* Base pointers for each pass */
  3607.   unsigned char *v[6];
  3608.   struct {
  3609.     unsigned char *k;
  3610.     unsigned char *m;
  3611.     unsigned char *c;
  3612.     unsigned char *y;
  3613.     unsigned char *M;
  3614.     unsigned char *C;
  3615.   } p;
  3616. } linebufs_t;
  3617.  
  3618. typedef struct {
  3619.   linebufs_t *linebases;    /* Base address of each row buffer */
  3620.   lineoff_t *lineoffsets;    /* Offsets within each row buffer */
  3621.   lineactive_t *lineactive;    /* Does this line have anything printed? */
  3622.   int *linecounts;        /* How many rows we've printed this pass */
  3623.   pass_t *passes;        /* Circular list of pass numbers */
  3624.   int last_pass_offset;        /* Starting row (offset from the start of */
  3625.                 /* the page) of the most recently printed */
  3626.                 /* pass (so we can determine how far to */
  3627.                 /* advance the paper) */
  3628.   int last_pass;        /* Number of the most recently printed pass */
  3629.  
  3630.   int jets;            /* Number of jets per color */
  3631.   int separation;        /* Offset from one jet to the next in rows */
  3632.   void *weaveparm;        /* Weave calculation parameter block */
  3633.  
  3634.   int horizontal_weave;        /* Number of horizontal passes required */
  3635.                 /* This is > 1 for some of the ultra-high */
  3636.                 /* resolution modes */
  3637.   int vertical_subpasses;    /* Number of passes per line (for better */
  3638.                 /* quality) */
  3639.   int vmod;            /* Number of banks of passes */
  3640.   int oversample;        /* Excess precision per row */
  3641.   int ncolors;            /* How many colors (1, 4, or 6) */
  3642.   int horizontal_width;        /* Line width in output pixels */
  3643.   int vertical_height;        /* Image height in output pixels */
  3644.   int firstline;        /* Actual first line (referenced to paper) */
  3645.  
  3646.   int bitwidth;            /* Bits per pixel */
  3647.   int lineno;
  3648.   int vertical_oversample;    /* Vertical oversampling */
  3649.   int current_vertical_subpass;
  3650.   int separation_rows;        /* Vertical spacing between rows. */
  3651.                 /* This is used for the 1520/3000, which */
  3652.                 /* use a funny value for the "print density */
  3653.                 /* in the vertical direction". */
  3654.   int last_color;
  3655. } escp2_softweave_t;
  3656.  
  3657. #ifndef WEAVETEST
  3658. static inline int
  3659. get_color_by_params(int plane, int density)
  3660. {
  3661.   if (plane > 4 || plane < 0 || density > 1 || density < 0)
  3662.     return -1;
  3663.   return color_indices[density * 8 + plane];
  3664. }
  3665. #endif
  3666.  
  3667.  
  3668. /*
  3669.  * Initialize the weave parameters
  3670.  *
  3671.  * Rules:
  3672.  *
  3673.  * 1) Currently, osample * v_subpasses * v_subsample <= 8, and no one
  3674.  *    of these variables may exceed 4.
  3675.  *
  3676.  * 2) first_line >= 0
  3677.  *
  3678.  * 3) line_height < physlines
  3679.  *
  3680.  * 4) phys_lines >= 2 * jets * sep
  3681.  */
  3682. static void *
  3683. initialize_weave(int jets,    /* Width of print head */
  3684.          int sep,    /* Separation in rows between jets */
  3685.          int osample,    /* Horizontal oversample */
  3686.          int v_subpasses, /* Vertical passes */
  3687.          int v_subsample, /* Vertical oversampling */
  3688.          colormode_t colormode,    /* mono, 4 color, 6 color */
  3689.          int width,    /* bits/pixel */
  3690.          int linewidth,    /* Width of a line, in pixels */
  3691.          int lineheight, /* Number of lines that will be printed */
  3692.          int separation_rows, /* Vertical spacing adjustment */
  3693.                 /* for weird printers (1520/3000, */
  3694.                 /* although they don't seem to do softweave */
  3695.                 /* anyway) */
  3696.          int first_line, /* First line that will be printed on page */
  3697.          int phys_lines, /* Total height of the page in rows */
  3698.          int weave_strategy) /* Which weaving pattern to use */
  3699. {
  3700.   int i;
  3701.   escp2_softweave_t *sw = malloc(sizeof (escp2_softweave_t));
  3702.   if (sw == 0)
  3703.     return sw;
  3704.  
  3705.   if (jets < 1)
  3706.     jets = 1;
  3707.   if (jets == 1 || sep < 1)
  3708.     sep = 1;
  3709.   if (v_subpasses < 1)
  3710.     v_subpasses = 1;
  3711.  
  3712.   sw->separation = sep;
  3713.   sw->jets = jets;
  3714.   sw->horizontal_weave = osample;
  3715.   sw->vertical_oversample = v_subsample;
  3716.   sw->vertical_subpasses = v_subpasses;
  3717.   sw->oversample = osample * v_subpasses * v_subsample;
  3718.   sw->firstline = first_line;
  3719.   sw->lineno = first_line;
  3720.  
  3721.   if (sw->oversample > jets) {
  3722.     fprintf(stderr, "Weave error: oversample (%d) > jets (%d)\n",
  3723.                     sw->oversample, jets);
  3724.     free(sw);
  3725.     return 0;
  3726.   }
  3727.   sw->weaveparm = initialize_weave_params(sw->separation, sw->jets,
  3728.                                           sw->oversample, first_line,
  3729.                                           first_line + lineheight - 1,
  3730.                                           phys_lines, weave_strategy);
  3731.  
  3732.   /*
  3733.    * The value of vmod limits how many passes may be unfinished at a time.
  3734.    * If pass x is not yet printed, pass x+vmod cannot be started.
  3735.    */
  3736.   sw->vmod = 2 * sw->separation * sw->oversample;
  3737.   sw->separation_rows = separation_rows;
  3738.  
  3739.   sw->bitwidth = width;
  3740.  
  3741.   sw->last_pass_offset = 0;
  3742.   sw->last_pass = -1;
  3743.   sw->current_vertical_subpass = 0;
  3744.   sw->last_color = -1;
  3745.  
  3746.   switch (colormode)
  3747.     {
  3748.     case COLOR_MONOCHROME:
  3749.       sw->ncolors = 1;
  3750.       break;
  3751.     case COLOR_CMYK:
  3752.       sw->ncolors = 4;
  3753.       break;
  3754.     case COLOR_CCMMYK:
  3755.     default:
  3756.       sw->ncolors = 6;
  3757.       break;
  3758.     }
  3759.  
  3760.   /*
  3761.    * It's possible for the "compression" to actually expand the line by
  3762.    * one part in 128.
  3763.    */
  3764.  
  3765.   sw->horizontal_width = (linewidth + 128 + 7) * 129 / 128;
  3766.   sw->vertical_height = lineheight;
  3767.   sw->lineoffsets = malloc(sw->vmod * sizeof(lineoff_t));
  3768.   memset(sw->lineoffsets, 0, sw->vmod * sizeof(lineoff_t));
  3769.   sw->lineactive = malloc(sw->vmod * sizeof(lineactive_t));
  3770.   sw->linebases = malloc(sw->vmod * sizeof(linebufs_t));
  3771.   sw->passes = malloc(sw->vmod * sizeof(pass_t));
  3772.   sw->linecounts = malloc(sw->vmod * sizeof(int));
  3773.   memset(sw->linecounts, 0, sw->vmod * sizeof(int));
  3774.  
  3775.   for (i = 0; i < sw->vmod; i++)
  3776.     {
  3777.       int j;
  3778.       sw->passes[i].pass = -1;
  3779.       for (j = 0; j < sw->ncolors; j++)
  3780.     {
  3781.       sw->linebases[i].v[j] =
  3782.         malloc(jets * sw->bitwidth * sw->horizontal_width / 8);
  3783.     }
  3784.     }
  3785.   return (void *) sw;
  3786. }
  3787.  
  3788. static void
  3789. destroy_weave(void *vsw)
  3790. {
  3791.   int i, j;
  3792.   escp2_softweave_t *sw = (escp2_softweave_t *) vsw;
  3793.   free(sw->linecounts);
  3794.   free(sw->passes);
  3795.   free(sw->lineactive);
  3796.   free(sw->lineoffsets);
  3797.   for (i = 0; i < sw->vmod; i++)
  3798.     {
  3799.       for (j = 0; j < sw->ncolors; j++)
  3800.     {
  3801.       free(sw->linebases[i].v[j]);
  3802.     }
  3803.     }
  3804.   free(sw->linebases);
  3805.   destroy_weave_params(sw->weaveparm);
  3806.   free(vsw);
  3807. }
  3808.  
  3809. static void
  3810. weave_parameters_by_row(const escp2_softweave_t *sw, int row,
  3811.             int vertical_subpass, weave_t *w)
  3812. {
  3813.   static const escp2_softweave_t *scache = 0;
  3814.   static weave_t wcache;
  3815.   static int rcache = -2;
  3816.   static int vcache = -2;
  3817.   int jetsused;
  3818.  
  3819.   if (scache == sw && rcache == row && vcache == vertical_subpass)
  3820.     {
  3821.       memcpy(w, &wcache, sizeof(weave_t));
  3822.       return;
  3823.     }
  3824.   scache = sw;
  3825.   rcache = row;
  3826.   vcache = vertical_subpass;
  3827.  
  3828.   w->row = row;
  3829.   calculate_row_parameters(sw->weaveparm, row, vertical_subpass,
  3830.                            &w->pass, &w->jet, &w->logicalpassstart,
  3831.                            &w->missingstartrows, &jetsused);
  3832.  
  3833.   w->physpassstart = w->logicalpassstart + sw->separation * w->missingstartrows;
  3834.   w->physpassend = w->physpassstart + sw->separation * (jetsused - 1);
  3835.  
  3836.   memcpy(&wcache, w, sizeof(weave_t));
  3837. #if 0
  3838.   printf("row %d, jet %d of pass %d "
  3839.          "(pos %d, start %d, end %d, missing rows %d\n",
  3840.          w->row, w->jet, w->pass, w->logicalpassstart, w->physpassstart,
  3841.          w->physpassend, w->missingstartrows);
  3842. #endif
  3843. }
  3844.  
  3845. #ifndef WEAVETEST
  3846.  
  3847. static lineoff_t *
  3848. get_lineoffsets(const escp2_softweave_t *sw, int row, int subpass)
  3849. {
  3850.   weave_t w;
  3851.   weave_parameters_by_row(sw, row, subpass, &w);
  3852.   return &(sw->lineoffsets[w.pass % sw->vmod]);
  3853. }
  3854.  
  3855. static lineactive_t *
  3856. get_lineactive(const escp2_softweave_t *sw, int row, int subpass)
  3857. {
  3858.   weave_t w;
  3859.   weave_parameters_by_row(sw, row, subpass, &w);
  3860.   return &(sw->lineactive[w.pass % sw->vmod]);
  3861. }
  3862.  
  3863. static int *
  3864. get_linecount(const escp2_softweave_t *sw, int row, int subpass)
  3865. {
  3866.   weave_t w;
  3867.   weave_parameters_by_row(sw, row, subpass, &w);
  3868.   return &(sw->linecounts[w.pass % sw->vmod]);
  3869. }
  3870.  
  3871. static const linebufs_t *
  3872. get_linebases(const escp2_softweave_t *sw, int row, int subpass)
  3873. {
  3874.   weave_t w;
  3875.   weave_parameters_by_row(sw, row, subpass, &w);
  3876.   return &(sw->linebases[w.pass % sw->vmod]);
  3877. }
  3878.  
  3879. static pass_t *
  3880. get_pass_by_row(const escp2_softweave_t *sw, int row, int subpass)
  3881. {
  3882.   weave_t w;
  3883.   weave_parameters_by_row(sw, row, subpass, &w);
  3884.   return &(sw->passes[w.pass % sw->vmod]);
  3885. }
  3886.  
  3887. static lineoff_t *
  3888. get_lineoffsets_by_pass(const escp2_softweave_t *sw, int pass)
  3889. {
  3890.   return &(sw->lineoffsets[pass % sw->vmod]);
  3891. }
  3892.  
  3893. static lineactive_t *
  3894. get_lineactive_by_pass(const escp2_softweave_t *sw, int pass)
  3895. {
  3896.   return &(sw->lineactive[pass % sw->vmod]);
  3897. }
  3898.  
  3899. static int *
  3900. get_linecount_by_pass(const escp2_softweave_t *sw, int pass)
  3901. {
  3902.   return &(sw->linecounts[pass % sw->vmod]);
  3903. }
  3904.  
  3905. static const linebufs_t *
  3906. get_linebases_by_pass(const escp2_softweave_t *sw, int pass)
  3907. {
  3908.   return &(sw->linebases[pass % sw->vmod]);
  3909. }
  3910.  
  3911. static pass_t *
  3912. get_pass_by_pass(const escp2_softweave_t *sw, int pass)
  3913. {
  3914.   return &(sw->passes[pass % sw->vmod]);
  3915. }
  3916.  
  3917. /*
  3918.  * If there are phantom rows at the beginning of a pass, fill them in so
  3919.  * that the printer knows exactly what it doesn't have to print.  We're
  3920.  * using RLE compression here.  Each line must be specified independently,
  3921.  * so we have to compute how many full blocks (groups of 128 bytes, or 1024
  3922.  * "off" pixels) and how much leftover is needed.  Note that we can only
  3923.  * RLE-encode groups of 2 or more bytes; single bytes must be specified
  3924.  * with a count of 1.
  3925.  */
  3926.  
  3927. static void
  3928. fillin_start_rows(const escp2_softweave_t *sw, int row, int subpass,
  3929.           int width, int missingstartrows)
  3930. {
  3931.   lineoff_t *offsets = get_lineoffsets(sw, row, subpass);
  3932.   const linebufs_t *bufs = get_linebases(sw, row, subpass);
  3933.   int i = 0;
  3934.   int k = 0;
  3935.   int j;
  3936.   width = sw->bitwidth * width * 8;
  3937.   for (k = 0; k < missingstartrows; k++)
  3938.     {
  3939.       int bytes_to_fill = width;
  3940.       int full_blocks = bytes_to_fill / (128 * 8);
  3941.       int leftover = (7 + (bytes_to_fill % (128 * 8))) / 8;
  3942.       int l = 0;
  3943.  
  3944.       while (l < full_blocks)
  3945.     {
  3946.       for (j = 0; j < sw->ncolors; j++)
  3947.         {
  3948.           (bufs[0].v[j][2 * i]) = 129;
  3949.           (bufs[0].v[j][2 * i + 1]) = 0;
  3950.         }
  3951.       i++;
  3952.       l++;
  3953.     }
  3954.       if (leftover == 1)
  3955.     {
  3956.       for (j = 0; j < sw->ncolors; j++)
  3957.         {
  3958.           (bufs[0].v[j][2 * i]) = 1;
  3959.           (bufs[0].v[j][2 * i + 1]) = 0;
  3960.         }
  3961.       i++;
  3962.     }
  3963.       else if (leftover > 0)
  3964.     {
  3965.       for (j = 0; j < sw->ncolors; j++)
  3966.         {
  3967.           (bufs[0].v[j][2 * i]) = 257 - leftover;
  3968.           (bufs[0].v[j][2 * i + 1]) = 0;
  3969.         }
  3970.       i++;
  3971.     }
  3972.     }
  3973.   for (j = 0; j < sw->ncolors; j++)
  3974.     offsets[0].v[j] = 2 * i;
  3975. }
  3976.  
  3977. static void
  3978. initialize_row(const escp2_softweave_t *sw, int row, int width)
  3979. {
  3980.   weave_t w;
  3981.   int i;
  3982.   for (i = 0; i < sw->oversample; i++)
  3983.     {
  3984.       weave_parameters_by_row(sw, row, i, &w);
  3985.       if (w.physpassstart == row)
  3986.     {
  3987.       lineoff_t *lineoffs = get_lineoffsets(sw, row, i);
  3988.       lineactive_t *lineactive = get_lineactive(sw, row, i);
  3989.       int *linecount = get_linecount(sw, row, i);
  3990.       int j;
  3991.       pass_t *pass = get_pass_by_row(sw, row, i);
  3992.       pass->pass = w.pass;
  3993.       pass->missingstartrows = w.missingstartrows;
  3994.       pass->logicalpassstart = w.logicalpassstart;
  3995.       pass->physpassstart = w.physpassstart;
  3996.       pass->physpassend = w.physpassend;
  3997.       pass->subpass = i;
  3998.       for (j = 0; j < sw->ncolors; j++)
  3999.         {
  4000.           if (lineoffs[0].v[j] != 0)
  4001.         fprintf(stderr,
  4002.             "WARNING: pass %d subpass %d row %d: lineoffs %ld\n",
  4003.             w.pass, i, row, lineoffs[0].v[j]);
  4004.           lineoffs[0].v[j] = 0;
  4005.           lineactive[0].v[j] = 0;
  4006.         }
  4007.       if (*linecount != 0)
  4008.         fprintf(stderr,
  4009.             "WARNING: pass %d subpass %d row %d: linecount %d\n",
  4010.             w.pass, i, row, *linecount);
  4011.       *linecount = 0;
  4012.       if (w.missingstartrows > 0)
  4013.         fillin_start_rows(sw, row, i, width, w.missingstartrows);
  4014.     }
  4015.     }
  4016. }
  4017.  
  4018. /*
  4019.  * A fair bit of this code is duplicated from escp2_write.  That's rather
  4020.  * a pity.  It's also not correct for any but the 6-color printers.  One of
  4021.  * these days I'll unify it.
  4022.  */
  4023. static void
  4024. flush_pass(escp2_softweave_t *sw, int passno, int model, int width,
  4025.        int hoffset, int ydpi, int xdpi, int physical_xdpi,
  4026.        FILE *prn, int vertical_subpass)
  4027. {
  4028.   int j;
  4029.   lineoff_t *lineoffs = get_lineoffsets_by_pass(sw, passno);
  4030.   lineactive_t *lineactive = get_lineactive_by_pass(sw, passno);
  4031.   const linebufs_t *bufs = get_linebases_by_pass(sw, passno);
  4032.   pass_t *pass = get_pass_by_pass(sw, passno);
  4033.   int *linecount = get_linecount_by_pass(sw, passno);
  4034.   int lwidth = (width + (sw->horizontal_weave - 1)) / sw->horizontal_weave;
  4035.   int microoffset = vertical_subpass & (sw->horizontal_weave - 1);
  4036.   if (ydpi > 720)
  4037.     ydpi = 720;
  4038.   for (j = 0; j < sw->ncolors; j++)
  4039.     {
  4040.       if (lineactive[0].v[j] == 0)
  4041.     {
  4042.       lineoffs[0].v[j] = 0;
  4043.       continue;
  4044.     }
  4045.       if (pass->logicalpassstart > sw->last_pass_offset)
  4046.     {
  4047.       int advance = pass->logicalpassstart - sw->last_pass_offset -
  4048.         (sw->separation_rows - 1);
  4049.       int alo = advance % 256;
  4050.       int ahi = advance / 256;
  4051.       if (!escp2_has_cap(model, MODEL_VARIABLE_DOT_MASK,
  4052.                  MODEL_VARIABLE_NORMAL))
  4053.         {
  4054.           int a3 = (advance >> 16) % 256;
  4055.           int a4 = (advance >> 24) % 256;
  4056.           ahi = ahi % 256;
  4057.           fprintf(prn, "\033(v\004%c%c%c%c%c", 0, alo, ahi, a3, a4);
  4058.         }
  4059.       else
  4060.         fprintf(prn, "\033(v\002%c%c%c", 0, alo, ahi);
  4061.       sw->last_pass_offset = pass->logicalpassstart;
  4062.     }
  4063.       if (last_color != j)
  4064.     {
  4065.       if (!escp2_has_cap(model, MODEL_VARIABLE_DOT_MASK,
  4066.                  MODEL_VARIABLE_NORMAL))
  4067.         ;
  4068.       else if (escp2_has_cap(model, MODEL_6COLOR_MASK, MODEL_6COLOR_YES))
  4069.         fprintf(prn, "\033(r\002%c%c%c", 0, densities[j], colors[j]);
  4070.       else
  4071.         fprintf(prn, "\033r%c", colors[j]);
  4072.       last_color = j;
  4073.     }
  4074.       if (escp2_has_cap(model, MODEL_1440DPI_MASK, MODEL_1440DPI_YES))
  4075.     {
  4076.       /* FIXME need a more general way of specifying column */
  4077.       /* separation */
  4078.       if (escp2_has_cap(model, MODEL_COMMAND_MASK, MODEL_COMMAND_1999) &&
  4079.           !(escp2_has_cap(model, MODEL_VARIABLE_DOT_MASK,
  4080.                   MODEL_VARIABLE_NORMAL)))
  4081.         {
  4082.           int pos = ((hoffset * xdpi / ydpi) + microoffset);
  4083.           if (pos > 0)
  4084.         fprintf(prn, "\033($%c%c%c%c%c%c", 4, 0,
  4085.             pos & 255, (pos >> 8) & 255,
  4086.             (pos >> 16) & 255, (pos >> 24) & 255);
  4087.         }
  4088.       else
  4089.         {
  4090.           int pos = ((hoffset * 1440 / ydpi) + microoffset);
  4091.           if (pos > 0)
  4092.         fprintf(prn, "\033(\\%c%c%c%c%c%c", 4, 0, 160, 5,
  4093.             pos & 255, pos >> 8);
  4094.         }
  4095.     }
  4096.       else
  4097.     {
  4098.       int pos = (hoffset + microoffset);
  4099.       if (pos > 0)
  4100.         fprintf(prn, "\033\\%c%c", pos & 255, pos >> 8);
  4101.     }
  4102.       if (!escp2_has_cap(model, MODEL_VARIABLE_DOT_MASK,
  4103.              MODEL_VARIABLE_NORMAL))
  4104.     {
  4105.       int ncolor = (densities[j] << 4) | colors[j];
  4106.       int nlines = *linecount + pass->missingstartrows;
  4107.       int nwidth = sw->bitwidth * ((lwidth + 7) / 8);
  4108.       fprintf(prn, "\033i%c%c%c%c%c%c%c", ncolor, 1, sw->bitwidth,
  4109.           nwidth & 255, nwidth >> 8, nlines & 255, nlines >> 8);
  4110.     }
  4111.       else
  4112.     {
  4113.       int ydotsep = 3600 / ydpi;
  4114.       int xdotsep = 3600 / physical_xdpi;
  4115.       if (escp2_has_cap(model, MODEL_720DPI_MODE_MASK,
  4116.                 MODEL_720DPI_600))
  4117.         fprintf(prn, "\033.%c%c%c%c", 1, 8 * ydotsep, xdotsep,
  4118.             *linecount + pass->missingstartrows);
  4119.       else if (escp2_pseudo_separation_rows(model) > 0)
  4120.         fprintf(prn, "\033.%c%c%c%c", 1,
  4121.             ydotsep * escp2_pseudo_separation_rows(model) , xdotsep,
  4122.             *linecount + pass->missingstartrows);
  4123.       else
  4124.         fprintf(prn, "\033.%c%c%c%c", 1, ydotsep * sw->separation_rows,
  4125.             xdotsep, *linecount + pass->missingstartrows);
  4126.       putc(lwidth & 255, prn);    /* Width of raster line in pixels */
  4127.       putc(lwidth >> 8, prn);
  4128.     }
  4129.  
  4130.       fwrite(bufs[0].v[j], lineoffs[0].v[j], 1, prn);
  4131.       lineoffs[0].v[j] = 0;
  4132.       putc('\r', prn);
  4133.     }
  4134.   *linecount = 0;
  4135.   sw->last_pass = pass->pass;
  4136.   pass->pass = -1;
  4137. }
  4138.  
  4139. static void
  4140. add_to_row(escp2_softweave_t *sw, int row, unsigned char *buf, size_t nbytes,
  4141.        int plane, int density, int setactive,
  4142.        lineoff_t *lineoffs, lineactive_t *lineactive,
  4143.        const linebufs_t *bufs)
  4144. {
  4145.   int color = get_color_by_params(plane, density);
  4146.   memcpy(bufs[0].v[color] + lineoffs[0].v[color], buf, nbytes);
  4147.   lineoffs[0].v[color] += nbytes;
  4148.   if (setactive)
  4149.     lineactive[0].v[color] = 1;
  4150. }
  4151.  
  4152. static void
  4153. escp2_flush(void *vsw, int model, int width, int hoffset,
  4154.         int ydpi, int xdpi, int physical_xdpi, FILE *prn)
  4155. {
  4156.   escp2_softweave_t *sw = (escp2_softweave_t *) vsw;
  4157.   while (1)
  4158.     {
  4159.       pass_t *pass = get_pass_by_pass(sw, sw->last_pass + 1);
  4160.       /*
  4161.        * This ought to be   pass->physpassend >  sw->lineno
  4162.        * but that causes rubbish to be output for some reason.
  4163.        */
  4164.       if (pass->pass < 0 || pass->physpassend >= sw->lineno)
  4165.     return;
  4166.       flush_pass(sw, pass->pass, model, width, hoffset, ydpi, xdpi,
  4167.          physical_xdpi, prn, pass->subpass);
  4168.     }
  4169. }
  4170.  
  4171. static void
  4172. escp2_flush_all(void *vsw, int model, int width, int hoffset,
  4173.         int ydpi, int xdpi, int physical_xdpi, FILE *prn)
  4174. {
  4175.   escp2_softweave_t *sw = (escp2_softweave_t *) vsw;
  4176.   while (1)
  4177.     {
  4178.       pass_t *pass = get_pass_by_pass(sw, sw->last_pass + 1);
  4179.       /*
  4180.        * This ought to be   pass->physpassend >  sw->lineno
  4181.        * but that causes rubbish to be output for some reason.
  4182.        */
  4183.       if (pass->pass < 0)
  4184.     return;
  4185.       flush_pass(sw, pass->pass, model, width, hoffset, ydpi, xdpi,
  4186.          physical_xdpi, prn, pass->subpass);
  4187.     }
  4188. }
  4189.  
  4190. static void
  4191. finalize_row(escp2_softweave_t *sw, int row, int model, int width,
  4192.          int hoffset, int ydpi, int xdpi, int physical_xdpi,
  4193.          FILE *prn)
  4194. {
  4195.   int i;
  4196. #if 0
  4197. printf("Finalizing row %d...\n", row);
  4198. #endif
  4199.   for (i = 0; i < sw->oversample; i++)
  4200.     {
  4201.       weave_t w;
  4202.       int *lines = get_linecount(sw, row, i);
  4203.       weave_parameters_by_row(sw, row, i, &w);
  4204.       (*lines)++;
  4205.       if (w.physpassend == row)
  4206.        {
  4207. #if 0
  4208. printf("Pass=%d, physpassend=%d, row=%d, lineno=%d, trying to flush...\n", w.pass, w.physpassend, row, sw->lineno);
  4209. #endif
  4210.     escp2_flush(sw, model, width, hoffset, ydpi, xdpi, physical_xdpi, prn);
  4211.        }
  4212.     }
  4213. }
  4214.  
  4215. static void
  4216. escp2_write_weave(void *        vsw,
  4217.           FILE          *prn,    /* I - Print file or command */
  4218.           int           length,    /* I - Length of bitmap data */
  4219.           int           ydpi,    /* I - Vertical resolution */
  4220.           int           model,    /* I - Printer model */
  4221.           int           width,    /* I - Printed width */
  4222.           int           offset,    /* I - Offset from left side of page */
  4223.           int        xdpi,
  4224.           int        physical_xdpi,
  4225.           const unsigned char *c,
  4226.           const unsigned char *m,
  4227.           const unsigned char *y,
  4228.           const unsigned char *k,
  4229.           const unsigned char *C,
  4230.           const unsigned char *M)
  4231. {
  4232.   escp2_softweave_t *sw = (escp2_softweave_t *) vsw;
  4233.   static unsigned char *s[8];
  4234.   static unsigned char *fold_buf;
  4235.   static unsigned char *comp_buf;
  4236.   lineoff_t *lineoffs[8];
  4237.   lineactive_t *lineactives[8];
  4238.   const linebufs_t *bufs[8];
  4239.   int xlength = (length + sw->horizontal_weave - 1) / sw->horizontal_weave;
  4240.   unsigned char *comp_ptr;
  4241.   int i, j;
  4242.   int setactive;
  4243.   int h_passes = sw->horizontal_weave * sw->vertical_subpasses;
  4244.   const unsigned char *cols[6];
  4245.   cols[0] = k;
  4246.   cols[1] = m;
  4247.   cols[2] = c;
  4248.   cols[3] = y;
  4249.   cols[4] = M;
  4250.   cols[5] = C;
  4251.   if (!fold_buf)
  4252.     fold_buf = malloc(COMPBUFWIDTH);
  4253.   if (!comp_buf)
  4254.     comp_buf = malloc(COMPBUFWIDTH);
  4255.   if (sw->current_vertical_subpass == 0)
  4256.     initialize_row(sw, sw->lineno, xlength);
  4257.  
  4258.   for (i = 0; i < h_passes; i++)
  4259.     {
  4260.       int cpass = sw->current_vertical_subpass * h_passes;
  4261.       if (!s[i])
  4262.     s[i] = malloc(COMPBUFWIDTH);
  4263.       lineoffs[i] = get_lineoffsets(sw, sw->lineno, cpass + i);
  4264.       lineactives[i] = get_lineactive(sw, sw->lineno, cpass + i);
  4265.       bufs[i] = get_linebases(sw, sw->lineno, cpass + i);
  4266.     }
  4267.  
  4268.   for (j = 0; j < sw->ncolors; j++)
  4269.     {
  4270.       if (cols[j])
  4271.     {
  4272.       const unsigned char *in;
  4273.       if (sw->bitwidth == 2)
  4274.         {
  4275.           escp2_fold(cols[j], length, fold_buf);
  4276.           in = fold_buf;
  4277.         }
  4278.       else
  4279.         in = cols[j];
  4280.       if (h_passes > 1)
  4281.         {
  4282.           switch (sw->horizontal_weave)
  4283.         {
  4284.         case 2:
  4285.           escp2_unpack_2(length, sw->bitwidth, in, s[0], s[1]);
  4286.           break;
  4287.         case 4:
  4288.           escp2_unpack_4(length, sw->bitwidth, in,
  4289.                  s[0], s[1], s[2], s[3]);
  4290.           break;
  4291.         case 8:
  4292.           escp2_unpack_8(length, sw->bitwidth, in,
  4293.                  s[0], s[1], s[2], s[3],
  4294.                  s[4], s[5], s[6], s[7]);
  4295.           break;
  4296.         }
  4297.           switch (sw->vertical_subpasses)
  4298.         {
  4299.         case 4:
  4300.           switch (sw->horizontal_weave)
  4301.             {
  4302.             case 1:
  4303.               escp2_split_4(length, sw->bitwidth, in,
  4304.                     s[0], s[1], s[2], s[3]);
  4305.               break;
  4306.             case 2:
  4307.               escp2_split_4(length, sw->bitwidth, s[0],
  4308.                     s[0], s[2], s[4], s[6]);
  4309.               escp2_split_4(length, sw->bitwidth, s[1],
  4310.                     s[1], s[3], s[5], s[7]);
  4311.               break;
  4312.             }
  4313.           break;
  4314.         case 2:
  4315.           switch (sw->horizontal_weave)
  4316.             {
  4317.             case 1:
  4318.               escp2_split_2(xlength, sw->bitwidth, in, s[0], s[1]);
  4319.               break;
  4320.             case 2:
  4321.               escp2_split_2(xlength, sw->bitwidth, s[0], s[0], s[2]);
  4322.               escp2_split_2(xlength, sw->bitwidth, s[1], s[1], s[3]);
  4323.               break;
  4324.             case 4:
  4325.               escp2_split_2(xlength, sw->bitwidth, s[0], s[0], s[4]);
  4326.               escp2_split_2(xlength, sw->bitwidth, s[1], s[1], s[5]);
  4327.               escp2_split_2(xlength, sw->bitwidth, s[2], s[2], s[6]);
  4328.               escp2_split_2(xlength, sw->bitwidth, s[3], s[3], s[7]);
  4329.               break;
  4330.             }
  4331.           break;
  4332.           /* case 1 is taken care of because the various unpack */
  4333.           /* functions will do the trick themselves */
  4334.         }
  4335.           for (i = 0; i < h_passes; i++)
  4336.         {
  4337.           setactive = escp2_pack(s[i], sw->bitwidth * xlength,
  4338.                      comp_buf, &comp_ptr);
  4339.           add_to_row(sw, sw->lineno, comp_buf, comp_ptr - comp_buf,
  4340.                  colors[j], densities[j], setactive,
  4341.                  lineoffs[i], lineactives[i], bufs[i]);
  4342.         }
  4343.         }
  4344.       else
  4345.         {
  4346.           setactive = escp2_pack(in, length * sw->bitwidth,
  4347.                      comp_buf, &comp_ptr);
  4348.           add_to_row(sw, sw->lineno, comp_buf, comp_ptr - comp_buf,
  4349.              colors[j], densities[j], setactive,
  4350.              lineoffs[0], lineactives[0], bufs[0]);
  4351.         }
  4352.     }
  4353.     }
  4354.   sw->current_vertical_subpass++;
  4355.   if (sw->current_vertical_subpass >= sw->vertical_oversample)
  4356.     {
  4357.       finalize_row(sw, sw->lineno, model, width, offset, ydpi, xdpi,
  4358.            physical_xdpi, prn);
  4359.       sw->lineno++;
  4360.       sw->current_vertical_subpass = 0;
  4361.     }
  4362. }
  4363.  
  4364. #endif
  4365.