Debian Codegraph @

src/main/print-weave.c

raw symbols SCIP
1 /*
2 *
3 * Softweave calculator for Gutenprint.
4 *
5 * Copyright 2000 Charles Briscoe-Smith <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21 /*
22 * This file must include only standard C header files. The core code must
23 * compile on generic platforms that don't support glib, gimp, gtk, etc.
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29 #include <string.h>
30 #include <gutenprint/gutenprint.h>
31 #include "gutenprint-internal.h"
32 #include <gutenprint/gutenprint-intl-internal.h>
33 #ifdef HAVE_LIMITS_H
34 #include <limits.h>
35 #endif
36
37 static int
38 gcd(int x, int y)
39 {
40 if (y == 0)
41 return x;
42 while (x != 0) {
43 if (y > x) {
44 int t = x;
45 x = y;
46 y = t;
47 }
48 x %= y;
49 }
50 return y;
51 }
52
53 typedef struct stpi_softweave
54 {
55 stp_linebufs_t *linebases; /* Base address of each row buffer */
56 stp_lineoff_t *lineoffsets; /* Offsets within each row buffer */
57 stp_lineactive_t *lineactive; /* Does this line have anything printed? */
58 stp_linecount_t *linecounts; /* How many rows we've printed this pass */
59 stp_linebounds_t *linebounds; /* Starting and ending print column */
60 stp_pass_t *passes; /* Circular list of pass numbers */
61 int last_pass_offset; /* Starting row (offset from the start of */
62 /* the page) of the most recently printed */
63 /* pass (so we can determine how far to */
64 /* advance the paper) */
65 int last_pass; /* Number of the most recently printed pass */
66
67 int jets; /* Number of jets per color */
68 int virtual_jets; /* Number of jets per color, taking into */
69 /* account the head offset */
70 int separation; /* Offset from one jet to the next in rows */
71 void *weaveparm; /* Weave calculation parameter block */
72
73 int horizontal_weave; /* Number of horizontal passes required */
74 /* This is > 1 for some of the ultra-high */
75 /* resolution modes */
76 int vertical_subpasses; /* Number of passes per line (for better */
77 /* quality) */
78 int vmod; /* Number of banks of passes */
79 int oversample; /* Excess precision per row */
80 int repeat_count; /* How many times a pass is repeated */
81 int ncolors; /* How many colors */
82 int linewidth; /* Line width in input pixels */
83 int vertical_height; /* Image height in output pixels */
84 int firstline; /* Actual first line (referenced to paper) */
85
86 int bitwidth; /* Bits per pixel */
87 int lineno;
88 int vertical_oversample; /* Vertical oversampling */
89 int current_vertical_subpass;
90 int horizontal_width; /* Horizontal width, in bits */
91 int *head_offset; /* offset of printheads */
92 unsigned char *s[STP_MAX_WEAVE];
93 unsigned char *fold_buf;
94 unsigned char *comp_buf;
95 stp_weave_t wcache;
96 int rcache;
97 int vcache;
98 stp_flushfunc *flushfunc;
99 stp_fillfunc *fillfunc;
100 stp_packfunc *pack;
101 stp_compute_linewidth_func *compute_linewidth;
102 } stpi_softweave_t;
103
104 /* RAW WEAVE */
105
106 typedef struct raw {
107 int separation;
108 int jets;
109 int oversampling;
110 int advancebasis;
111 int subblocksperpassblock;
112 int passespersubblock;
113 stp_weave_strategy_t strategy;
114 stp_vars_t *v;
115 } raw_t;
116
117 /*
118 * Strategy types currently defined:
119 *
120 * 0: zig-zag type pass block filling
121 * 1: ascending pass block filling
122 * 2: descending pass block filling
123 * 3: ascending fill with 2x expansion
124 * 4: staggered zig-zag neighbour-avoidance fill
125 * 5: ascending fill with 3x expansion
126 *
127 * In theory, strategy 0 should be optimal; in practice, it can lead
128 * to visible areas of banding. If it's necessary to avoid filling
129 * neighbouring rows in neighbouring passes, strategy 4 should be optimal,
130 * at least for some weaves.
131 */
132
133 static void
134 initialize_raw_weave(raw_t *w, /* I - weave struct to be filled in */
135 int separation, /* I - jet separation */
136 int jets, /* I - number of jets */
137 int oversample, /* I - oversampling factor */
138 stp_weave_strategy_t strat, /* I - weave pattern variation to use */
139 stp_vars_t *v)
140 {
141 w->separation = separation;
142 w->jets = jets;
143 w->oversampling = oversample;
144 w->advancebasis = jets / oversample;
145 if (w->advancebasis == 0)
146 w->advancebasis++;
147 w->subblocksperpassblock = gcd(separation, w->advancebasis);
148 w->passespersubblock = separation / w->subblocksperpassblock;
149 w->strategy = strat;
150 w->v = v;
151 }
152
153 static void
154 calculate_raw_pass_parameters(raw_t *w, /* I - weave parameters */
155 int pass, /* I - pass number ( >= 0) */
156 int *startrow, /* O - print head position */
157 int *subpass) /* O - subpass number */
158 {
159 int band, passinband, subpassblock, subpassoffset;
160
161 band = pass / (w->separation * w->oversampling);
162 passinband = pass % (w->separation * w->oversampling);
163 subpassblock = pass % w->separation
164 * w->subblocksperpassblock / w->separation;
165
166 switch (w->strategy) {
167 case STP_WEAVE_ZIGZAG:
168 if (subpassblock * 2 < w->subblocksperpassblock)
169 subpassoffset = 2 * subpassblock;
170 else
171 subpassoffset = 2 * (w->subblocksperpassblock
172 - subpassblock) - 1;
173 break;
174 case STP_WEAVE_ASCENDING:
175 subpassoffset = subpassblock;
176 break;
177 case STP_WEAVE_DESCENDING:
178 subpassoffset = w->subblocksperpassblock - 1 - subpassblock;
179 break;
180 case STP_WEAVE_ASCENDING_2X:
181 if (subpassblock * 2 < w->subblocksperpassblock)
182 subpassoffset = 2 * subpassblock;
183 else
184 subpassoffset = 1 + 2 * (subpassblock
185 - (w->subblocksperpassblock
186 + 1) / 2);
187 break;
188 case STP_WEAVE_ASCENDING_3X:
189 if (subpassblock * 3 < w->subblocksperpassblock)
190 subpassoffset = 3 * subpassblock;
191 else if (3 * (subpassblock - (w->subblocksperpassblock + 2) / 3)
192 < w->subblocksperpassblock - 2)
193 subpassoffset = 2 + 3 * (subpassblock
194 - (w->subblocksperpassblock
195 + 2) / 3);
196 else
197 subpassoffset = 1 + 3 * (subpassblock
198 - (w->subblocksperpassblock
199 + 2) / 3
200 - w->subblocksperpassblock
201 / 3);
202 break;
203 case STP_WEAVE_STAGGERED:
204 if (subpassblock * 2 < w->subblocksperpassblock)
205 subpassoffset = 2 * subpassblock;
206 else if (subpassblock * 2 < w->subblocksperpassblock + 2)
207 subpassoffset = 1;
208 else
209 subpassoffset = 2 * (w->subblocksperpassblock
210 - subpassblock) + 1;
211 break;
212 default:
213 subpassoffset = subpassblock;
214 break;
215 }
216
217 *startrow = w->separation * w->jets * band
218 + w->advancebasis * passinband + subpassoffset;
219 *subpass = passinband / w->separation;
220 }
221
222 static void
223 calculate_raw_row_parameters(raw_t *w, /* I - weave parameters */
224 int row, /* I - row number */
225 int subpass, /* I - subpass number */
226 int *pass, /* O - pass number */
227 int *jet, /* O - jet number in pass */
228 int *startrow) /* O - starting row of pass */
229 {
230 int subblockoffset, subpassblock, band, baserow, passinband, offset;
231 int pass_div_separation;
232 int pass_mod_separation;
233 int off_mod_separation;
234
235 subblockoffset = row % w->subblocksperpassblock;
236 switch (w->strategy) {
237 case STP_WEAVE_ZIGZAG:
238 if (subblockoffset % 2 == 0)
239 subpassblock = subblockoffset / 2;
240 else
241 subpassblock = w->subblocksperpassblock
242 - (subblockoffset + 1) / 2;
243 break;
244 case STP_WEAVE_ASCENDING:
245 subpassblock = subblockoffset;
246 break;
247 case STP_WEAVE_DESCENDING:
248 subpassblock = w->subblocksperpassblock - 1 - subblockoffset;
249 break;
250 case STP_WEAVE_ASCENDING_2X:
251 if (subblockoffset % 2 == 0)
252 subpassblock = subblockoffset / 2;
253 else
254 subpassblock = (subblockoffset - 1) / 2
255 + (w->subblocksperpassblock + 1) / 2;
256 break;
257 case STP_WEAVE_ASCENDING_3X:
258 if (subblockoffset % 3 == 0)
259 subpassblock = subblockoffset / 3;
260 else if (subblockoffset % 3 == 1)
261 subpassblock = (subblockoffset - 1) / 3
262 + (w->subblocksperpassblock + 2) / 3;
263 else
264 subpassblock = (subblockoffset - 2) / 3
265 + (w->subblocksperpassblock + 2) / 3
266 + (w->subblocksperpassblock + 1) / 3;
267 break;
268 case STP_WEAVE_STAGGERED:
269 if (subblockoffset % 2 == 0)
270 subpassblock = subblockoffset / 2;
271 else if (subblockoffset == 1)
272 subpassblock = (w->subblocksperpassblock + 1) / 2;
273 else
274 subpassblock = w->subblocksperpassblock
275 - (subblockoffset - 1) / 2;
276 break;
277 default:
278 subpassblock = subblockoffset;
279 break;
280 }
281
282 band = row / (w->separation * w->jets);
283 baserow = row - subblockoffset - band * w->separation * w->jets;
284 passinband = baserow / w->advancebasis;
285 offset = baserow % w->advancebasis;
286 pass_div_separation = passinband / w->separation;
287 pass_mod_separation = passinband % w->separation;
288 off_mod_separation = offset % w->separation;
289
290 while (off_mod_separation != 0
291 || pass_div_separation != subpass
292 || pass_mod_separation / w->passespersubblock
293 != subpassblock)
294 {
295 offset += w->advancebasis;
296 passinband--;
297 if (passinband >= 0)
298 {
299 pass_mod_separation--;
300 if (pass_mod_separation < 0)
301 {
302 pass_mod_separation += w->separation;
303 pass_div_separation--;
304 }
305 if (w->advancebasis < w->separation)
306 {
307 off_mod_separation += w->advancebasis;
308 if (off_mod_separation >= w->separation)
309 off_mod_separation -= w->separation;
310 }
311 else if (w->advancebasis > w->separation)
312 off_mod_separation = offset % w->separation;
313 }
314 else
315 {
316 const int roundedjets =
317 (w->advancebasis * w->oversampling) % w->jets;
318 band--;
319 passinband += w->separation * w->oversampling;
320 offset += w->separation * (w->jets - roundedjets);
321 pass_div_separation = passinband / w->separation;
322 pass_mod_separation = passinband % w->separation;
323 off_mod_separation = offset % w->separation;
324 }
325 }
326
327 *pass = band * w->oversampling * w->separation + passinband;
328 *jet = (offset / w->separation) % w->jets;
329 *startrow = row - (*jet * w->separation);
330 }
331
332 /* COOKED WEAVE */
333
334 typedef struct cooked {
335 raw_t rw;
336 int first_row_printed;
337 int last_row_printed;
338
339 int first_premapped_pass; /* First raw pass used by this page */
340 int first_normal_pass;
341 int first_postmapped_pass;
342 int first_unused_pass;
343
344 int *pass_premap;
345 int *stagger_premap;
346 int *pass_postmap;
347 int *stagger_postmap;
348 } cooked_t;
349
350 typedef struct startmap {
351 int startrow;
352 int map;
353 int pos; /* Secondary key for stable sort */
354 } startmap_t;
355
356 static int
357 smap_compare(const void *p1, const void *p2)
358 {
359 const startmap_t *s1 = (const startmap_t *)p1;
360 const startmap_t *s2 = (const startmap_t *)p2;
361 if (s1->startrow < s2->startrow)
362 return -1;
363 else if (s1->startrow > s2->startrow)
364 return 1;
365 else if (s1->pos < s2->pos)
366 return -1;
367 else
368 return 1;
369 }
370
371 static void
372 sort_by_start_row(int *map, int *startrows, int count)
373 {
374 startmap_t *smap = stp_malloc(sizeof(startmap_t) * count);
375 int i;
376 for (i = 0; i < count; i++)
377 {
378 smap[i].startrow = startrows[i];
379 smap[i].map = map[i];
380 smap[i].pos = i;
381 }
382 qsort(smap, count, sizeof(startmap_t), smap_compare);
383 for (i = 0; i < count; i++)
384 {
385 startrows[i] = smap[i].startrow;
386 map[i] = smap[i].map;
387 }
388 stp_free(smap);
389 }
390
391 static void
392 calculate_stagger(raw_t *w, int *map, int *startrows_stagger, int count)
393 {
394 int i;
395
396 for (i = 0; i < count; i++) {
397 int startrow, subpass;
398 calculate_raw_pass_parameters(w, map[i], &startrow, &subpass);
399 startrow -= w->separation * w->jets;
400 startrows_stagger[i] = (startrows_stagger[i] - startrow)
401 / w->separation;
402 }
403 }
404
405 static void
406 invert_map(int *map, int *stagger, int count, int oldfirstpass,
407 int newfirstpass)
408 {
409 int i;
410 int *newmap, *newstagger;
411 newmap = stp_malloc(count * sizeof(int));
412 newstagger = stp_malloc(count * sizeof(int));
413
414 for (i = 0; i < count; i++) {
415 newmap[map[i] - oldfirstpass] = i + newfirstpass;
416 newstagger[map[i] - oldfirstpass] = stagger[i];
417 }
418
419 memcpy(map, newmap, count * sizeof(int));
420 memcpy(stagger, newstagger, count * sizeof(int));
421 stp_free(newstagger);
422 stp_free(newmap);
423 }
424
425 static void
426 make_passmap(raw_t *w, int **map, int **starts, int first_pass_number,
427 int first_pass_to_map, int first_pass_after_map,
428 int first_pass_to_stagger, int first_pass_after_stagger,
429 int first_row_of_maximal_pass, int separations_to_distribute)
430 {
431 int *passmap, *startrows;
432 int passes_to_map = first_pass_after_map - first_pass_to_map;
433 int i;
434
435 STPI_ASSERT(first_pass_to_map <= first_pass_after_map, w->v);
436 STPI_ASSERT(first_pass_to_stagger <= first_pass_after_stagger, w->v);
437
438 *map = passmap = stp_malloc(passes_to_map * sizeof(int));
439 *starts = startrows = stp_malloc(passes_to_map * sizeof(int));
440
441 for (i = 0; i < passes_to_map; i++) {
442 int startrow, subpass;
443 int pass = i + first_pass_to_map;
444 calculate_raw_pass_parameters(w, pass, &startrow, &subpass);
445 passmap[i] = pass;
446 if (first_row_of_maximal_pass >= 0)
447 startrow = first_row_of_maximal_pass - startrow
448 + w->separation * w->jets;
449 else
450 startrow -= w->separation * w->jets;
451 while (startrow < 0)
452 startrow += w->separation;
453 startrows[i] = startrow;
454 }
455
456 sort_by_start_row(passmap, startrows, passes_to_map);
457
458 separations_to_distribute++;
459
460 for (i = 0; i < first_pass_after_stagger - first_pass_to_stagger; i++) {
461 int offset = first_pass_to_stagger - first_pass_to_map;
462 if (startrows[i + offset] / w->separation
463 < i % separations_to_distribute)
464 {
465 startrows[i + offset]
466 = startrows[i + offset] % w->separation
467 + w->separation * (i % separations_to_distribute);
468 }
469 }
470
471 if (first_row_of_maximal_pass >= 0) {
472 for (i = 0; i < passes_to_map; i++) {
473 startrows[i] = first_row_of_maximal_pass - startrows[i];
474 }
475 }
476
477 sort_by_start_row(passmap, startrows, passes_to_map);
478 calculate_stagger(w, passmap, startrows, passes_to_map);
479
480 invert_map(passmap, startrows, passes_to_map, first_pass_to_map,
481 first_pass_to_map - first_pass_number);
482 }
483
484 static void
485 calculate_pass_map(stp_vars_t *v,
486 cooked_t *w, /* I - weave parameters */
487 int pageheight, /* I - number of rows on page */
488 int firstrow, /* I - first printed row */
489 int lastrow) /* I - last printed row */
490 {
491 int startrow, subpass;
492 int pass = -1;
493
494 w->first_row_printed = firstrow;
495 w->last_row_printed = lastrow;
496
497 if (pageheight <= lastrow)
498 pageheight = lastrow + 1;
499
500 do {
501 calculate_raw_pass_parameters(&w->rw, ++pass,
502 &startrow, &subpass);
503 } while (startrow - w->rw.separation < firstrow);
504
505 w->first_premapped_pass = pass;
506
507 while (startrow < w->rw.separation * w->rw.jets
508 && startrow - w->rw.separation < pageheight
509 && startrow <= lastrow + w->rw.separation * w->rw.jets)
510 {
511 calculate_raw_pass_parameters(&w->rw, ++pass,
512 &startrow, &subpass);
513 }
514 w->first_normal_pass = pass;
515
516 while (startrow - w->rw.separation < pageheight
517 && startrow <= lastrow + w->rw.separation * w->rw.jets)
518 {
519 calculate_raw_pass_parameters(&w->rw, ++pass,
520 &startrow, &subpass);
521 }
522 w->first_postmapped_pass = pass;
523
524 while (startrow <= lastrow + w->rw.separation * w->rw.jets) {
525 calculate_raw_pass_parameters(&w->rw, ++pass,
526 &startrow, &subpass);
527 }
528 w->first_unused_pass = pass;
529
530 stp_dprintf(STP_DBG_WEAVE_PARAMS, v,
531 "first premapped %d first normal %d first postmapped %d "
532 "first unused %d\n",
533 w->first_premapped_pass, w->first_normal_pass,
534 w->first_postmapped_pass, w->first_unused_pass);
535 /*
536 * FIXME: make sure first_normal_pass doesn't advance beyond
537 * first_postmapped_pass, or first_postmapped_pass doesn't
538 * retreat before first_normal_pass.
539 */
540
541 if (w->first_normal_pass > w->first_premapped_pass) {
542 int spread, separations_to_distribute, normal_passes_mapped;
543 separations_to_distribute = firstrow / w->rw.separation;
544 spread = (separations_to_distribute + 1) * w->rw.separation;
545 normal_passes_mapped = (spread + w->rw.advancebasis - 1)
546 / w->rw.advancebasis;
547 w->first_normal_pass += normal_passes_mapped;
548 make_passmap(&w->rw, &w->pass_premap, &w->stagger_premap,
549 w->first_premapped_pass,
550 w->first_premapped_pass, w->first_normal_pass,
551 w->first_premapped_pass,
552 w->first_normal_pass - normal_passes_mapped,
553 -1, separations_to_distribute);
554 } else {
555 w->pass_premap = 0;
556 w->stagger_premap = 0;
557 }
558
559 if (w->first_unused_pass >= w->first_postmapped_pass) {
560 int spread, separations_to_distribute, normal_passes_mapped;
561 separations_to_distribute = (pageheight - lastrow - 1)
562 / w->rw.separation;
563 spread = (separations_to_distribute + 1) * w->rw.separation;
564 normal_passes_mapped = (spread + w->rw.advancebasis)
565 / w->rw.advancebasis;
566 w->first_postmapped_pass -= normal_passes_mapped;
567 make_passmap(&w->rw, &w->pass_postmap, &w->stagger_postmap,
568 w->first_premapped_pass,
569 w->first_postmapped_pass, w->first_unused_pass,
570 w->first_postmapped_pass + normal_passes_mapped,
571 w->first_unused_pass,
572 pageheight - 1
573 - w->rw.separation * (w->rw.jets - 1),
574 separations_to_distribute);
575 } else {
576 w->pass_postmap = 0;
577 w->stagger_postmap = 0;
578 }
579 }
580
581 static void * /* O - weave parameter block */
582 initialize_weave_params(int separation, /* I - jet separation */
583 int jets, /* I - number of jets */
584 int oversample, /* I - oversampling factor */
585 int firstrow, /* I - first row number to print */
586 int lastrow, /* I - last row number to print */
587 int pageheight, /* I - number of rows on the whole
588 page, without using any
589 expanded margin facilities */
590 stp_weave_strategy_t strategy, /* I - weave pattern variant to use */
591 stp_vars_t *v)
592 {
593 cooked_t *w = stp_malloc(sizeof(cooked_t));
594 if (w) {
595 initialize_raw_weave(&w->rw, separation, jets, oversample, strategy, v);
596 calculate_pass_map(v, w, pageheight, firstrow, lastrow);
597 }
598 return w;
599 }
600
601 static void
602 stpi_destroy_weave_params(void *vw)
603 {
604 cooked_t *w = (cooked_t *) vw;
605
606 if (w->pass_premap) stp_free(w->pass_premap);
607 if (w->stagger_premap) stp_free(w->stagger_premap);
608 if (w->pass_postmap) stp_free(w->pass_postmap);
609 if (w->stagger_postmap) stp_free(w->stagger_postmap);
610 stp_free(w);
611 }
612
613 static void
614 stpi_calculate_row_parameters(void *vw, /* I - weave parameters */
615 int row, /* I - row number */
616 int subpass, /* I - subpass */
617 int *pass, /* O - pass containing row */
618 int *jetnum, /* O - jet number of row */
619 int *startingrow, /* O - phys start of pass */
620 int *ophantomrows, /* O - missing rows @ start */
621 int *ojetsused) /* O - jets used by pass */
622 {
623 cooked_t *w = (cooked_t *) vw;
624 int raw_pass, jet, startrow, phantomrows, jetsused;
625 int stagger = 0;
626 int extra;
627
628 STPI_ASSERT(row >= w->first_row_printed, w->rw.v);
629 STPI_ASSERT(row <= w->last_row_printed, w->rw.v);
630 calculate_raw_row_parameters(&w->rw,
631 row + w->rw.separation * w->rw.jets,
632 subpass, &raw_pass, &jet, &startrow);
633 startrow -= w->rw.separation * w->rw.jets;
634 jetsused = w->rw.jets;
635 phantomrows = 0;
636
637 if (raw_pass < w->first_normal_pass) {
638 STPI_ASSERT(raw_pass >= w->first_premapped_pass, w->rw.v);
639 *pass = w->pass_premap[raw_pass - w->first_premapped_pass];
640 stagger = w->stagger_premap[raw_pass - w->first_premapped_pass];
641 } else if (raw_pass >= w->first_postmapped_pass) {
642 STPI_ASSERT(raw_pass >= w->first_postmapped_pass, w->rw.v);
643 *pass = w->pass_postmap[raw_pass - w->first_postmapped_pass];
644 stagger = w->stagger_postmap[raw_pass
645 - w->first_postmapped_pass];
646 } else {
647 *pass = raw_pass - w->first_premapped_pass;
648 }
649
650 startrow += stagger * w->rw.separation;
651 *jetnum = jet - stagger;
652 if (stagger < 0) {
653 stagger = -stagger;
654 phantomrows += stagger;
655 }
656 jetsused -= stagger;
657
658 extra = w->first_row_printed
659 - (startrow + w->rw.separation * phantomrows);
660 if (extra > 0) {
661 extra = (extra + w->rw.separation - 1) / w->rw.separation;
662 jetsused -= extra;
663 phantomrows += extra;
664 }
665
666 extra = startrow + w->rw.separation * (phantomrows + jetsused - 1)
667 - w->last_row_printed;
668 if (extra > 0) {
669 extra = (extra + w->rw.separation - 1) / w->rw.separation;
670 jetsused -= extra;
671 }
672
673 *startingrow = startrow;
674 *ophantomrows = phantomrows;
675 *ojetsused = jetsused;
676 }
677
678 /*
679 * "Soft" weave
680 *
681 * The Epson Stylus Color/Photo printers don't have memory to print
682 * using all of the nozzles in the print head. For example, the Stylus Photo
683 * 700/EX has 32 nozzles. At 720 dpi, with an 8" wide image, a single line
684 * requires (8 * 720 * 6 / 8) bytes, or 4320 bytes (because the Stylus Photo
685 * printers have 6 ink colors). To use 32 nozzles would require 138240 bytes.
686 * It's actually worse than that, though, because the nozzles are spaced 8
687 * rows apart. Therefore, in order to store enough data to permit sending the
688 * page as a simple raster, the printer would require enough memory to store
689 * 256 rows, or 1105920 bytes. Considering that the Photo EX can print
690 * 11" wide, we're looking at more like 1.5 MB. In fact, these printers are
691 * capable of 1440 dpi horizontal resolution. This would require 3 MB. The
692 * printers actually have 64K-256K.
693 *
694 * With the newer (740/750 and later) printers it's even worse, since these
695 * printers support multiple dot sizes. But that's neither here nor there.
696 *
697 * Older Epson printers had a mode called MicroWeave (tm). In this mode, the
698 * host fed the printer individual rows of dots, and the printer bundled them
699 * up and sent them to the print head in the correct order to achieve high
700 * quality. This MicroWeave mode still works in new printers, but the
701 * implementation is very minimal: the printer uses exactly one nozzle of
702 * each color (the first one). This makes printing extremely slow (more than
703 * 30 minutes for one 8.5x11" page), although the quality is extremely high
704 * with no visible banding whatsoever. It's not good for the print head,
705 * though, since no ink is flowing through the other nozzles. This leads to
706 * drying of ink and possible permanent damage to the print head.
707 *
708 * By the way, although the Epson manual says that microweave mode should be
709 * used at 720 dpi, 360 dpi continues to work in much the same way. At 360
710 * dpi, data is fed to the printer one row at a time on all Epson printers.
711 * The pattern that the printer uses to print is very prone to banding.
712 * However, 360 dpi is inherently a low quality mode; if you're using it,
713 * presumably you don't much care about quality.
714 *
715 * Printers from roughly the Stylus Color 600 and later do not have the
716 * capability to do MicroWeave correctly. Instead, the host must arrange
717 * the output in the order that it will be sent to the print head. This
718 * is a very complex process; the jets in the print head are spaced more
719 * than one row (1/720") apart, so we can't simply send consecutive rows
720 * of dots to the printer. Instead, we have to pass e. g. the first, ninth,
721 * 17th, 25th... rows in order for them to print in the correct position on
722 * the paper. This interleaving process is called "soft" weaving.
723 *
724 * This decision was probably made to save money on memory in the printer.
725 * It certainly makes the driver code far more complicated than it would
726 * be if the printer could arrange the output. Is that a bad thing?
727 * Usually this takes far less CPU time than the dithering process, and it
728 * does allow us more control over the printing process, e. g. to reduce
729 * banding. Conceivably, we could even use this ability to map out bad
730 * jets.
731 *
732 * Interestingly, apparently the Windows (and presumably Macintosh) drivers
733 * for most or all Epson printers still list a "microweave" mode.
734 * Experiments have demonstrated that this does not in fact use the
735 * "microweave" mode of the printer. Possibly it does nothing, or it uses
736 * a different weave pattern from what the non-"microweave" mode does.
737 * This is unnecessarily confusing.
738 *
739 * What makes this interesting is that there are many different ways of
740 * of accomplishing this goal. The naive way would be to divide the image
741 * up into groups of 256 rows, and print all the mod8=0 rows in the first pass,
742 * mod8=1 rows in the second, and so forth. The problem with this approach
743 * is that the individual ink jets are not perfectly uniform; some emit
744 * slightly bigger or smaller drops than others. Since each group of 8
745 * adjacent rows is printed with the same nozzle, that means that there will
746 * be distinct streaks of lighter and darker bands within the image (8 rows
747 * is 1/90", which is visible; 1/720" is not). Possibly worse is that these
748 * patterns will repeat every 256 rows. This creates banding patterns that
749 * are about 1/3" wide.
750 *
751 * So we have to do something to break up this patterning.
752 *
753 * Epson does not publish the weaving algorithms that they use in their
754 * bundled drivers. Indeed, their developer web site
755 * (http://www.ercipd.com/isv/edr_docs.htm) does not even describe how to
756 * do this weaving at all; it says that the only way to achieve 720 dpi
757 * is to use MicroWeave. It does note (correctly) that 1440 dpi horizontal
758 * can only be achieved by the driver (i. e. in software). The manual
759 * actually makes it fairly clear how to do this (it requires two passes
760 * with horizontal head movement between passes), and it is presumably
761 * possible to do this with MicroWeave.
762 *
763 * The information about how to do this is apparently available under NDA.
764 * It's actually easy enough to reverse engineer what's inside a print file
765 * with a simple Perl script. There are presumably other printer commands
766 * that are not documented and may not be as easy to reverse engineer.
767 *
768 * I considered a few algorithms to perform the weave. The first one I
769 * devised let me use only (jets - distance_between_jets + 1) nozzles, or
770 * 25. This is OK in principle, but it's slower than using all nozzles.
771 * By playing around with it some more, I came up with an algorithm that
772 * lets me use all of the nozzles, except near the top and bottom of the
773 * page.
774 *
775 * This still produces some banding, though. Even better quality can be
776 * achieved by using multiple nozzles on the same line. How do we do this?
777 * In 1440x720 mode, we're printing two output lines at the same vertical
778 * position. However, if we want four passes, we have to effectively print
779 * each line twice. Actually doing this would increase the density, so
780 * what we do is print half the dots on each pass. This produces near-perfect
781 * output, and it's far faster than using (pseudo) "MicroWeave".
782 *
783 * The current algorithm is not completely general. The number of passes
784 * is limited to (nozzles / gap). On the Photo EX class printers, that limits
785 * it to 4 -- 32 nozzles, an inter-nozzle gap of 8 lines. Furthermore, there
786 * are a number of routines that are only coded up to 8 passes. Fortunately,
787 * this is enough passes to get rid of most banding. What's left is a very
788 * fine pattern that is sometimes described as "corduroy", since the pattern
789 * looks like that kind of fabric.
790 *
791 * Newer printers (those that support variable dot sizes, such as the 740,
792 * 1200, etc.) have an additional complication: when used in softweave mode,
793 * they operate at 360 dpi horizontal resolution. This requires FOUR passes
794 * to achieve 1440x720 dpi. Thus, to enable us to break up each row
795 * into separate sub-rows, we have to actually print each row eight times.
796 * Fortunately, all such printers have 48 nozzles and a gap of 6 rows,
797 * except for the high-speed 900, which uses 96 nozzles and a gap of 2 rows.
798 *
799 * I cannot let this entirely pass without commenting on the Stylus Color 440.
800 * This is a very low-end printer with 21 (!) nozzles and a separation of 8.
801 * The weave routine works correctly with single-pass printing, which is enough
802 * to minimally achieve 720 dpi output (it's physically a 720 dpi printer).
803 * However, the routine does not work correctly at more than one pass per row.
804 * Therefore, this printer bands badly.
805 *
806 * Yet another complication is how to get near the top and bottom of the page.
807 * This algorithm lets us print to within one head width of the top of the
808 * page, and a bit more than one head width from the bottom. That leaves a
809 * lot of blank space. Doing the weave properly outside of this region is
810 * increasingly difficult as we get closer to the edge of the paper; in the
811 * interior region, any nozzle can print any line, but near the top and
812 * bottom edges, only some nozzles can print. We've handled this for now by
813 * using the naive way mentioned above near the borders, and switching over
814 * to the high quality method in the interior. Unfortunately, this means
815 * that the quality is quite visibly degraded near the top and bottom of the
816 * page. Algorithms that degrade more gracefully are more complicated.
817 * Epson does not advertise that the printers can print at the very top of the
818 * page, although in practice most or all of them can. I suspect that the
819 * quality that can be achieved very close to the top is poor enough that
820 * Epson does not want to allow printing there. That is a valid decision,
821 * although we have taken another approach.
822 *
823 * To compute the weave information, we need to start with the following
824 * information:
825 *
826 * 1) The number of jets the print head has for each color;
827 *
828 * 2) The separation in rows between the jets;
829 *
830 * 3) The horizontal resolution of the printer;
831 *
832 * 4) The desired horizontal resolution of the output;
833 *
834 * 5) The desired extra passes to reduce banding.
835 *
836 * As discussed above, each row is actually printed in one or more passes
837 * of the print head; we refer to these as subpasses. For example, if we're
838 * printing at 1440(h)x720(v) on a printer with true horizontal resolution of
839 * 360 dpi, and we wish to print each line twice with different nozzles
840 * to reduce banding, we need to use 8 subpasses. The dither routine
841 * will feed us a complete row of bits for each color; we have to split that
842 * up, first by round robining the bits to ensure that they get printed at
843 * the right micro-position, and then to split up the bits that are actually
844 * turned on into two equal chunks to reduce banding.
845 *
846 * Given the above information, and the desired row index and subpass (which
847 * together form a line number), we can compute:
848 *
849 * 1) Which pass this line belongs to. Passes are numbered consecutively,
850 * and each pass must logically (see #3 below) start at no smaller a row
851 * number than the previous pass, as the printer cannot advance by a
852 * negative amount.
853 *
854 * 2) Which jet will print this line.
855 *
856 * 3) The "logical" first line of this pass. That is, what line would be
857 * printed by jet 0 in this pass. This number may be less than zero.
858 * If it is, there are ghost lines that don't actually contain any data.
859 * The difference between the logical first line of this pass and the
860 * logical first line of the preceding pass tells us how many lines must
861 * be advanced.
862 *
863 * 4) The "physical" first line of this pass. That is, the first line index
864 * that is actually printed in this pass. This information lets us know
865 * when we must prepare this pass.
866 *
867 * 5) The last line of this pass. This lets us know when we must actually
868 * send this pass to the printer.
869 *
870 * 6) The number of ghost rows this pass contains. We must still send the
871 * ghost data to the printer, so this lets us know how much data we must
872 * fill in prior to the start of the pass.
873 *
874 * The bookkeeping to keep track of all this stuff is quite hairy, and needs
875 * to be documented separately.
876 *
877 * The routine initialize_weave calculates the basic parameters, given
878 * the number of jets and separation between jets, in rows.
879 *
880 * -- Robert Krawitz <[email protected]) November 3, 1999
881 */
882
883 static stp_lineoff_t *
884 allocate_lineoff(int count, int ncolors)
885 {
886 int i;
887 stp_lineoff_t *retval = stp_malloc(count * sizeof(stp_lineoff_t));
888 for (i = 0; i < count; i++)
889 {
890 retval[i].ncolors = ncolors;
891 retval[i].v = stp_zalloc(ncolors * sizeof(unsigned long));
892 }
893 return (retval);
894 }
895
896 static stp_lineactive_t *
897 allocate_lineactive(int count, int ncolors)
898 {
899 int i;
900 stp_lineactive_t *retval = stp_malloc(count * sizeof(stp_lineactive_t));
901 for (i = 0; i < count; i++)
902 {
903 retval[i].ncolors = ncolors;
904 retval[i].v = stp_zalloc(ncolors * sizeof(char));
905 }
906 return (retval);
907 }
908
909 static stp_linecount_t *
910 allocate_linecount(int count, int ncolors)
911 {
912 int i;
913 stp_linecount_t *retval = stp_malloc(count * sizeof(stp_linecount_t));
914 for (i = 0; i < count; i++)
915 {
916 retval[i].ncolors = ncolors;
917 retval[i].v = stp_zalloc(ncolors * sizeof(int));
918 }
919 return (retval);
920 }
921
922 static stp_linebounds_t *
923 allocate_linebounds(int count, int ncolors)
924 {
925 int i;
926 stp_linebounds_t *retval = stp_malloc(count * sizeof(stp_linebounds_t));
927 for (i = 0; i < count; i++)
928 {
929 retval[i].ncolors = ncolors;
930 retval[i].start_pos = stp_zalloc(ncolors * sizeof(int));
931 retval[i].end_pos = stp_zalloc(ncolors * sizeof(int));
932 }
933 return (retval);
934 }
935
936 static stp_linebufs_t *
937 allocate_linebuf(int count, int ncolors)
938 {
939 int i;
940 stp_linebufs_t *retval = stp_malloc(count * sizeof(stp_linebufs_t));
941 for (i = 0; i < count; i++)
942 {
943 retval[i].ncolors = ncolors;
944 retval[i].v = stp_zalloc(ncolors * sizeof(unsigned char *));
945 }
946 return (retval);
947 }
948
949 /*
950 * Initialize the weave parameters
951 *
952 * Rules:
953 *
954 * 1) Currently, osample * v_subpasses * v_subsample <= 8, and no one
955 * of these variables may exceed 4.
956 *
957 * 2) first_line >= 0
958 *
959 * 3) line_height < physlines
960 *
961 * 4) page_height >= 2 * jets * sep
962 */
963
964 static void
965 stpi_destroy_weave(void *vsw)
966 {
967 int i, j;
968 stpi_softweave_t *sw = (stpi_softweave_t *) vsw;
969 stp_free(sw->passes);
970 if (sw->fold_buf)
971 stp_free(sw->fold_buf);
972 if (sw->comp_buf)
973 stp_free(sw->comp_buf);
974 for (i = 0; i < STP_MAX_WEAVE; i++)
975 if (sw->s[i])
976 stp_free(sw->s[i]);
977 for (i = 0; i < sw->vmod; i++)
978 {
979 for (j = 0; j < sw->ncolors; j++)
980 {
981 if (sw->linebases[i].v[j])
982 stp_free(sw->linebases[i].v[j]);
983 }
984 stp_free(sw->linecounts[i].v);
985 stp_free(sw->linebases[i].v);
986 stp_free(sw->lineactive[i].v);
987 stp_free(sw->lineoffsets[i].v);
988 stp_free(sw->linebounds[i].start_pos);
989 stp_free(sw->linebounds[i].end_pos);
990 }
991 stp_free(sw->linecounts);
992 stp_free(sw->lineactive);
993 stp_free(sw->lineoffsets);
994 stp_free(sw->linebases);
995 stp_free(sw->linebounds);
996 stp_free(sw->head_offset);
997 stpi_destroy_weave_params(sw->weaveparm);
998 stp_free(vsw);
999 }
1000
1001 void
1002 stp_initialize_weave(stp_vars_t *v,
1003 int jets, /* Width of print head */
1004 int sep, /* Separation in rows between jets */
1005 int osample, /* Horizontal oversample */
1006 int v_subpasses, /* Vertical passes */
1007 int v_subsample, /* Vertical oversampling */
1008 int ncolors,
1009 int bitwidth, /* bits/pixel */
1010 int linewidth, /* Width of a line, in pixels */
1011 int line_count, /* Lines that will be printed */
1012 int first_line, /* First line that will be printed */
1013 int page_height, /* Total height of the page in rows */
1014 const int *head_offset,
1015 stp_weave_strategy_t weave_strategy,
1016 stp_flushfunc flushfunc,
1017 stp_fillfunc fillfunc,
1018 stp_packfunc pack,
1019 stp_compute_linewidth_func compute_linewidth)
1020 {
1021 int i;
1022 int last_line, maxHeadOffset;
1023 stpi_softweave_t *sw = stp_zalloc(sizeof (stpi_softweave_t));
1024
1025 if (jets < 1)
1026 jets = 1;
1027 if (jets == 1 || sep < 1)
1028 sep = 1;
1029 if (v_subpasses < 1)
1030 v_subpasses = 1;
1031 if (v_subsample < 1)
1032 v_subsample = 1;
1033
1034 sw->separation = sep;
1035 sw->jets = jets;
1036 sw->horizontal_weave = osample;
1037 sw->oversample = osample * v_subpasses * v_subsample;
1038 if (sw->oversample > jets)
1039 {
1040 int found = 0;
1041 for (i = 2; i <= osample; i++)
1042 {
1043 if ((osample % i == 0) && (sw->oversample / i <= jets))
1044 {
1045 sw->repeat_count = i;
1046 osample /= i;
1047 found = 1;
1048 break;
1049 }
1050 }
1051 if (!found)
1052 {
1053 stp_eprintf(v, "Weave error: oversample (%d) > jets (%d)\n",
1054 sw->oversample, jets);
1055 stp_free(sw);
1056 return;
1057 }
1058 }
1059 else
1060 sw->repeat_count = 1;
1061
1062 sw->vertical_oversample = v_subsample;
1063 sw->vertical_subpasses = v_subpasses;
1064 sw->oversample = osample * v_subpasses * v_subsample;
1065 sw->firstline = first_line;
1066 sw->lineno = first_line;
1067 sw->flushfunc = flushfunc;
1068
1069 if (sw->oversample > jets)
1070 {
1071 stp_eprintf(v, "Weave error: oversample (%d) > jets (%d)\n",
1072 sw->oversample, jets);
1073 stp_free(sw);
1074 return;
1075 }
1076
1077 /*
1078 * setup printhead offsets.
1079 * for monochrome (bw) printing, the offsets are 0.
1080 */
1081 sw->head_offset = stp_zalloc(ncolors * sizeof(int));
1082 if (ncolors > 1)
1083 for(i = 0; i < ncolors; i++)
1084 sw->head_offset[i] = head_offset[i];
1085
1086 maxHeadOffset = 0;
1087 for (i = 0; i < ncolors; i++)
1088 if (sw->head_offset[i] > maxHeadOffset)
1089 maxHeadOffset = sw->head_offset[i];
1090
1091 sw->virtual_jets = sw->jets;
1092 if (maxHeadOffset > 0)
1093 sw->virtual_jets += (maxHeadOffset + sw->separation - 1) / sw->separation;
1094 last_line = first_line + line_count - 1 + maxHeadOffset;
1095
1096 sw->weaveparm = initialize_weave_params(sw->separation, sw->jets,
1097 sw->oversample, first_line, last_line,
1098 page_height, weave_strategy, v);
1099 /*
1100 * The value of vmod limits how many passes may be unfinished at a time.
1101 * If pass x is not yet printed, pass x+vmod cannot be started.
1102 *
1103 * The multiplier of 2: 1 for the normal passes, 1 for the special passes
1104 * at the start or end.
1105 */
1106 sw->vmod = 2 * sw->separation * sw->oversample * sw->repeat_count;
1107 if (sw->virtual_jets > sw->jets)
1108 sw->vmod *= (sw->virtual_jets + sw->jets - 1) / sw->jets;
1109
1110 sw->bitwidth = bitwidth;
1111 sw->last_pass_offset = 0;
1112 sw->last_pass = -1;
1113 sw->current_vertical_subpass = 0;
1114 sw->ncolors = ncolors;
1115 sw->linewidth = linewidth;
1116 sw->vertical_height = line_count;
1117 sw->lineoffsets = allocate_lineoff(sw->vmod, ncolors);
1118 sw->lineactive = allocate_lineactive(sw->vmod, ncolors);
1119 sw->linebases = allocate_linebuf(sw->vmod, ncolors);
1120 sw->linebounds = allocate_linebounds(sw->vmod, ncolors);
1121 sw->passes = stp_zalloc(sw->vmod * sizeof(stp_pass_t));
1122 sw->linecounts = allocate_linecount(sw->vmod, ncolors);
1123 sw->rcache = -2;
1124 sw->vcache = -2;
1125 sw->fillfunc = fillfunc;
1126 sw->compute_linewidth = compute_linewidth;
1127 sw->pack = pack;
1128 sw->horizontal_width =
1129 (sw->compute_linewidth)(v, ((sw->linewidth + sw->horizontal_weave - 1) /
1130 sw->horizontal_weave));
1131 stp_dprintf(STP_DBG_WEAVE_PARAMS, v,
1132 "Computing linewidth for linewidth %d weave %d => %d (%d)\n",
1133 sw->linewidth, sw->horizontal_weave, sw->horizontal_width,
1134 ((sw->horizontal_width + 7) / 8));
1135 sw->horizontal_width = ((sw->horizontal_width + 7) / 8);
1136
1137 for (i = 0; i < sw->vmod; i++)
1138 {
1139 int j;
1140 sw->passes[i].pass = -1;
1141 for (j = 0; j < sw->ncolors; j++)
1142 {
1143 sw->linebases[i].v[j] = NULL;
1144 }
1145 }
1146 stp_dprintf(STP_DBG_WEAVE_PARAMS, v,
1147 "Weave parameters: separation %d jets %d (%d) h %d v %d v_sub %d\n",
1148 sw->separation, sw->jets, sw->virtual_jets, osample,
1149 v_subpasses, v_subsample);
1150 stp_dprintf(STP_DBG_WEAVE_PARAMS, v,
1151 " ncolors %d bpp %d width %d (%d) line_count %d first %d last %d max_offset %d\n",
1152 sw->ncolors, sw->bitwidth, linewidth, sw->horizontal_width,
1153 sw->vertical_height, first_line, last_line, maxHeadOffset);
1154 stp_dprintf(STP_DBG_WEAVE_PARAMS, v,
1155 " oversample %d line buffer count %d total buffer %d, page_height %d\n",
1156 sw->oversample, sw->vmod,
1157 sw->vmod * sw->virtual_jets * sw->bitwidth *
1158 sw->ncolors * sw->horizontal_width, page_height);
1159 stp_dprintf(STP_DBG_WEAVE_PARAMS, v,
1160 " ***Buffer limit %d (vj %d bw %d hw %d)\n",
1161 sw->virtual_jets * sw->bitwidth * sw->horizontal_width,
1162 sw->virtual_jets, sw->bitwidth, sw->horizontal_width);
1163 stp_allocate_component_data(v, "Weave", NULL, stpi_destroy_weave, sw);
1164 return;
1165 }
1166
1167 static void
1168 weave_parameters_by_row(const stp_vars_t *v, stpi_softweave_t *sw,
1169 int row, int vertical_subpass, stp_weave_t *w)
1170 {
1171 int jetsused;
1172 int sub_repeat_count = vertical_subpass % sw->repeat_count;
1173 /*
1174 * Conceptually, this does not modify the softweave state. We cache
1175 * the results, but this cache is considered hidden.
1176 */
1177 vertical_subpass /= sw->repeat_count;
1178
1179 if (sw->rcache == row && sw->vcache == vertical_subpass)
1180 {
1181 memcpy(w, &sw->wcache, sizeof(stp_weave_t));
1182 w->pass = (w->pass * sw->repeat_count) + sub_repeat_count;
1183 return;
1184 }
1185 sw->rcache = row;
1186 sw->vcache = vertical_subpass;
1187
1188 w->row = row;
1189 stpi_calculate_row_parameters(sw->weaveparm, row, vertical_subpass,
1190 &w->pass, &w->jet, &w->logicalpassstart,
1191 &w->missingstartrows, &jetsused);
1192
1193 w->physpassstart = w->logicalpassstart + sw->separation * w->missingstartrows;
1194 w->physpassend = w->physpassstart + sw->separation * (jetsused - 1);
1195
1196 memcpy(&(sw->wcache), w, sizeof(stp_weave_t));
1197 w->pass = (w->pass * sw->repeat_count) + sub_repeat_count;
1198 stp_dprintf(STP_DBG_ROWS, v, "row %d, jet %d of pass %d "
1199 "(pos %d, start %d, end %d, missing rows %d)\n",
1200 w->row, w->jet, w->pass, w->logicalpassstart, w->physpassstart,
1201 w->physpassend, w->missingstartrows);
1202 }
1203
1204 static stpi_softweave_t *
1205 get_sw(const stp_vars_t *v)
1206 {
1207 return (stpi_softweave_t *) stp_get_component_data(v, "Weave");
1208 }
1209
1210 void
1211 stp_weave_parameters_by_row(const stp_vars_t *v, int row,
1212 int vertical_subpass, stp_weave_t *w)
1213 {
1214 stpi_softweave_t *sw = get_sw(v);
1215 weave_parameters_by_row(v, sw, row, vertical_subpass, w);
1216 }
1217
1218
1219 static stp_lineoff_t *
1220 stpi_get_lineoffsets(const stp_vars_t *v, stpi_softweave_t *sw,
1221 int row, int subpass, int offset)
1222 {
1223 stp_weave_t w;
1224 weave_parameters_by_row(v, sw, row + offset, subpass, &w);
1225 return &(sw->lineoffsets[w.pass % sw->vmod]);
1226 }
1227
1228 static stp_lineactive_t *
1229 stpi_get_lineactive(const stp_vars_t *v, stpi_softweave_t *sw,
1230 int row, int subpass, int offset)
1231 {
1232 stp_weave_t w;
1233 weave_parameters_by_row(v, sw, row + offset, subpass, &w);
1234 return &(sw->lineactive[w.pass % sw->vmod]);
1235 }
1236
1237 static stp_linecount_t *
1238 stpi_get_linecount(const stp_vars_t *v, stpi_softweave_t *sw,
1239 int row, int subpass, int offset)
1240 {
1241 stp_weave_t w;
1242 weave_parameters_by_row(v, sw, row + offset, subpass, &w);
1243 return &(sw->linecounts[w.pass % sw->vmod]);
1244 }
1245
1246 static stp_linebufs_t *
1247 stpi_get_linebases(const stp_vars_t *v, stpi_softweave_t *sw,
1248 int row, int subpass, int offset)
1249 {
1250 stp_weave_t w;
1251 weave_parameters_by_row(v, sw, row + offset, subpass, &w);
1252 return &(sw->linebases[w.pass % sw->vmod]);
1253 }
1254
1255 static stp_linebounds_t *
1256 stpi_get_linebounds(const stp_vars_t *v, stpi_softweave_t *sw,
1257 int row, int subpass, int offset)
1258 {
1259 stp_weave_t w;
1260 weave_parameters_by_row(v, sw, row + offset, subpass, &w);
1261 return &(sw->linebounds[w.pass % sw->vmod]);
1262 }
1263
1264 static stp_pass_t *
1265 stpi_get_pass_by_row(stp_vars_t *v, stpi_softweave_t *sw,
1266 int row, int subpass,int offset)
1267 {
1268 stp_weave_t w;
1269 weave_parameters_by_row(v, sw, row + offset, subpass, &w);
1270 return &(sw->passes[w.pass % sw->vmod]);
1271 }
1272
1273 stp_lineoff_t *
1274 stp_get_lineoffsets_by_pass(const stp_vars_t *v, int pass)
1275 {
1276 const stpi_softweave_t *sw = get_sw(v);
1277 return &(sw->lineoffsets[pass % sw->vmod]);
1278 }
1279
1280 stp_lineactive_t *
1281 stp_get_lineactive_by_pass(const stp_vars_t *v, int pass)
1282 {
1283 const stpi_softweave_t *sw = get_sw(v);
1284 return &(sw->lineactive[pass % sw->vmod]);
1285 }
1286
1287 stp_linecount_t *
1288 stp_get_linecount_by_pass(const stp_vars_t *v, int pass)
1289 {
1290 const stpi_softweave_t *sw = get_sw(v);
1291 return &(sw->linecounts[pass % sw->vmod]);
1292 }
1293
1294 const stp_linebufs_t *
1295 stp_get_linebases_by_pass(const stp_vars_t *v, int pass)
1296 {
1297 const stpi_softweave_t *sw = get_sw(v);
1298 return &(sw->linebases[pass % sw->vmod]);
1299 }
1300
1301 stp_pass_t *
1302 stp_get_pass_by_pass(const stp_vars_t *v, int pass)
1303 {
1304 const stpi_softweave_t *sw = get_sw(v);
1305 return &(sw->passes[pass % sw->vmod]);
1306 }
1307
1308 static void
1309 check_linebases(stp_vars_t *v, stpi_softweave_t *sw,
1310 int row, int cpass, int head_offset, int color)
1311 {
1312 stp_linebufs_t *bufs =
1313 (stp_linebufs_t *) stpi_get_linebases(v, sw, row, cpass, head_offset);
1314 if (!(bufs->v[color]))
1315 bufs->v[color] =
1316 stp_zalloc (sw->virtual_jets * sw->bitwidth * sw->horizontal_width);
1317 }
1318
1319 /*
1320 * If there are phantom rows at the beginning of a pass, fill them in so
1321 * that the printer knows exactly what it doesn't have to print. We're
1322 * using RLE compression here. Each line must be specified independently,
1323 * so we have to compute how many full blocks (groups of 128 bytes, or 1024
1324 * "off" pixels) and how much leftover is needed. Note that we can only
1325 * RLE-encode groups of 2 or more bytes; single bytes must be specified
1326 * with a count of 1.
1327 */
1328
1329 void
1330 stp_fill_tiff(stp_vars_t *v, int row, int subpass,
1331 int width, int missingstartrows, int color)
1332 {
1333 stpi_softweave_t *sw = get_sw(v);
1334 stp_lineoff_t *lineoffs;
1335 stp_linecount_t *linecount;
1336 const stp_linebufs_t *bufs;
1337 int i = 0;
1338 int k = 0;
1339
1340 width = sw->bitwidth * width * 8;
1341 for (k = 0; k < missingstartrows; k++)
1342 {
1343 int bytes_to_fill = width;
1344 int full_blocks = bytes_to_fill / (128 * 8);
1345 int leftover = (7 + (bytes_to_fill % (128 * 8))) / 8;
1346 int l = 0;
1347 bufs = stpi_get_linebases(v, sw, row, subpass, sw->head_offset[color]);
1348
1349 while (l < full_blocks)
1350 {
1351 (bufs->v[color][2 * i]) = 129;
1352 (bufs->v[color][2 * i + 1]) = 0;
1353 i++;
1354 l++;
1355 }
1356 if (leftover == 1)
1357 {
1358 (bufs->v[color][2 * i]) = 1;
1359 (bufs->v[color][2 * i + 1]) = 0;
1360 i++;
1361 }
1362 else if (leftover > 0)
1363 {
1364 (bufs->v[color][2 * i]) = 257 - leftover;
1365 (bufs->v[color][2 * i + 1]) = 0;
1366 i++;
1367 }
1368 }
1369
1370 lineoffs = stpi_get_lineoffsets(v, sw, row, subpass, sw->head_offset[color]);
1371 linecount = stpi_get_linecount(v, sw, row, subpass, sw->head_offset[color]);
1372 lineoffs->v[color] = 2 * i;
1373 linecount->v[color] = missingstartrows;
1374 }
1375
1376 void
1377 stp_fill_uncompressed(stp_vars_t *v, int row, int subpass,
1378 int width, int missingstartrows, int color)
1379 {
1380 stpi_softweave_t *sw = get_sw(v);
1381 stp_lineoff_t *lineoffs;
1382 stp_linecount_t *linecount;
1383 const stp_linebufs_t *bufs;
1384
1385 bufs = stpi_get_linebases(v, sw, row, subpass, sw->head_offset[color]);
1386 lineoffs = stpi_get_lineoffsets(v, sw, row, subpass, sw->head_offset[color]);
1387 linecount = stpi_get_linecount(v, sw, row, subpass, sw->head_offset[color]);
1388 width *= sw->bitwidth * missingstartrows;
1389 memset(bufs->v[color], 0, width);
1390 lineoffs->v[color] = width;
1391 linecount->v[color] = missingstartrows;
1392 }
1393
1394 int
1395 stp_compute_tiff_linewidth(stp_vars_t *v, int n)
1396 {
1397 (void)v;
1398 /*
1399 * It's possible for the "compression" to actually expand the line by
1400 * roughly one part in 128.
1401 */
1402 return ((n + 128 + 7) * 129 / 128);
1403 }
1404
1405 int
1406 stp_compute_uncompressed_linewidth(stp_vars_t *v, int n)
1407 {
1408 (void)v;
1409 return (8 * ((n + 7) / 8));
1410 }
1411
1412 static void
1413 initialize_row(stp_vars_t *v, stpi_softweave_t *sw,
1414 int row, int width, unsigned char *const cols[])
1415 {
1416 stp_weave_t w;
1417 int i, j, jj;
1418 stp_pass_t *pass;
1419
1420 for (i = 0; i < sw->oversample; i++)
1421 {
1422 for (j = 0; j < sw->ncolors; j++)
1423 {
1424 if (cols[j])
1425 {
1426 stp_lineoff_t *lineoffs =
1427 stpi_get_lineoffsets(v, sw, row, i, sw->head_offset[j]);
1428 stp_lineactive_t *lineactive =
1429 stpi_get_lineactive(v, sw, row, i, sw->head_offset[j]);
1430 stp_linecount_t *linecount =
1431 stpi_get_linecount(v, sw, row, i, sw->head_offset[j]);
1432 stp_linebounds_t *linebounds =
1433 stpi_get_linebounds(v, sw, row, i, sw->head_offset[j]);
1434 check_linebases(v, sw, row, i, sw->head_offset[j], j);
1435 weave_parameters_by_row(v, sw, row+sw->head_offset[j], i, &w);
1436 pass = stpi_get_pass_by_row(v, sw, row, i, sw->head_offset[j]);
1437
1438 /* initialize pass if not initialized yet */
1439 if (pass->pass < 0)
1440 {
1441 pass->pass = w.pass;
1442 pass->missingstartrows = w.missingstartrows;
1443 pass->logicalpassstart = w.logicalpassstart;
1444 pass->physpassstart = w.physpassstart;
1445 pass->physpassend = w.physpassend;
1446 pass->subpass = i;
1447
1448 for(jj=0; jj<sw->ncolors; jj++)
1449 {
1450 if (lineoffs->v[jj] != 0)
1451 stp_eprintf(v, "WARNING: pass %d subpass %d row %d: "
1452 "lineoffs %ld should be zero!\n",
1453 w.pass, i, row, lineoffs->v[jj]);
1454 lineoffs->v[jj] = 0;
1455 lineactive->v[jj] = 0;
1456 if (linecount->v[jj] != 0)
1457 stp_eprintf(v, "WARNING: pass %d subpass %d row %d: "
1458 "linecount %d should be zero!\n",
1459 w.pass, i, row, linecount->v[jj]);
1460 linecount->v[jj] = 0;
1461 linebounds->start_pos[jj] = INT_MAX;
1462 linebounds->end_pos[jj] = -1;
1463 }
1464 }
1465
1466 if((linecount->v[j] == 0) && (w.jet > 0))
1467 {
1468 (sw->fillfunc)(v, row, i, width, w.jet, j);
1469 }
1470 }
1471 }
1472 }
1473 }
1474
1475 static void
1476 add_to_row_1(stp_vars_t *v, stpi_softweave_t *sw, int row, unsigned char *buf,
1477 size_t nbytes, int color, int setactive, int h_pass)
1478 {
1479 const stp_linebufs_t *bufs =
1480 stpi_get_linebases(v, sw, sw->lineno, h_pass, sw->head_offset[color]);
1481 stp_lineoff_t *lineoffs =
1482 stpi_get_lineoffsets(v, sw, sw->lineno, h_pass, sw->head_offset[color]);
1483 stp_lineactive_t *lineactive =
1484 stpi_get_lineactive(v, sw, sw->lineno, h_pass, sw->head_offset[color]);
1485 stp_linecount_t *linecount =
1486 stpi_get_linecount(v, sw, sw->lineno, h_pass, sw->head_offset[color]);
1487 size_t place = lineoffs->v[color];
1488 size_t count = linecount->v[color];
1489 if (place + nbytes > sw->virtual_jets * sw->bitwidth * sw->horizontal_width)
1490 {
1491 int i;
1492 stp_eprintf(v, "ERROR: %s\n", _("Fatal error!"));
1493 stp_eprintf(v, "ERROR: Static weave data follows:\n");
1494 stp_eprintf(v, "ERROR: jets: %d\n", sw->jets);
1495 stp_eprintf(v, "ERROR: virtual_jets: %d\n", sw->virtual_jets);
1496 stp_eprintf(v, "ERROR: separation: %d\n", sw->separation);
1497 stp_eprintf(v, "ERROR: horizontal_weave: %d\n", sw->horizontal_weave);
1498 stp_eprintf(v, "ERROR: vertical_subpasses: %d\n", sw->vertical_subpasses);
1499 stp_eprintf(v, "ERROR: vmod: %d\n", sw->vmod);
1500 stp_eprintf(v, "ERROR: oversample: %d\n", sw->oversample);
1501 stp_eprintf(v, "ERROR: repeat_count: %d\n", sw->repeat_count);
1502 stp_eprintf(v, "ERROR: ncolors: %d\n", sw->ncolors);
1503 stp_eprintf(v, "ERROR: linewidth: %d\n", sw->linewidth);
1504 stp_eprintf(v, "ERROR: vertical_height: %d\n", sw->vertical_height);
1505 stp_eprintf(v, "ERROR: firstline: %d\n", sw->firstline);
1506 stp_eprintf(v, "ERROR: bitwidth: %d\n", sw->bitwidth);
1507 stp_eprintf(v, "ERROR: vertical_oversample: %d\n", sw->vertical_oversample);
1508 stp_eprintf(v, "ERROR: horizontal_width: %d\n", sw->horizontal_width);
1509 if (sw->head_offset)
1510 {
1511 stp_eprintf(v, "ERROR: head_offset:\n");
1512 for (i = 0; i < sw->ncolors; i++)
1513 stp_eprintf(v, "ERROR: head %d: %d\n", i, sw->head_offset[i]);
1514 }
1515 stp_eprintf(v, "ERROR: Dynamic weave data follows:\n");
1516 stp_eprintf(v, "ERROR: last_pass_offset: %d\n", sw->last_pass_offset);
1517 stp_eprintf(v, "ERROR: last_pass: %d\n", sw->last_pass);
1518 stp_eprintf(v, "ERROR: lineno: %d\n", sw->lineno);
1519 stp_eprintf(v, "ERROR: current_vertical_subpass: %d\n", sw->current_vertical_subpass);
1520 stp_eprintf(v, "ERROR: rcache: %d\n", sw->rcache);
1521 stp_eprintf(v, "ERROR: vcache: %d\n", sw->vcache);
1522 stp_eprintf(v, "ERROR: Other parameters: row %d color %d setactive %d hpass %d\n",
1523 row, color, setactive, h_pass);
1524 stp_eprintf(v, "ERROR: Buffer overflow: limit %d (jets %d bits %d horizontal %d), actual %ld (current %d added %d), count %ld\n",
1525 sw->virtual_jets * sw->bitwidth * sw->horizontal_width,
1526 sw->virtual_jets, sw->bitwidth, sw->horizontal_width,
1527 (long) (place + nbytes), (int) place, (int) nbytes, (long) count);
1528 stp_eprintf(v, "ERROR: %s\n", _("Please report the above information to [email protected]"));
1529 stp_abort();
1530 }
1531 memcpy(bufs->v[color] + lineoffs->v[color], buf, nbytes);
1532 lineoffs->v[color] += nbytes;
1533 if (setactive)
1534 lineactive->v[color] = 1;
1535 }
1536
1537 static void
1538 add_to_row(stp_vars_t *v, stpi_softweave_t *sw, int row, unsigned char *buf,
1539 size_t nbytes, int color, int setactive, int h_pass)
1540 {
1541 if (!sw->head_offset)
1542 {
1543 stp_eprintf(v, "ERROR: %s\n", _("Fatal error!"));
1544 stp_eprintf(v, "ERROR: add_to_row: sw->head_offset == NULL\n");
1545 return;
1546 }
1547 add_to_row_1(v, sw, row, buf, nbytes, color, setactive, h_pass);
1548 }
1549
1550 static void
1551 stpi_flush_passes(stp_vars_t *v, int flushall)
1552 {
1553 stpi_softweave_t *sw = get_sw(v);
1554 while (1)
1555 {
1556 stp_pass_t *pass = stp_get_pass_by_pass(v, sw->last_pass + 1);
1557 /*
1558 * This ought to be pass->physpassend > sw->lineno
1559 * but that causes rubbish to be output for some reason.
1560 */
1561 if (pass->pass < 0 || (!flushall && pass->physpassend >= sw->lineno))
1562 return;
1563 (sw->flushfunc)(v, pass->pass, pass->subpass);
1564 sw->last_pass = pass->pass;
1565 pass->pass = -1;
1566 }
1567 }
1568
1569 void
1570 stp_flush_all(stp_vars_t *v)
1571 {
1572 stpi_flush_passes(v, 1);
1573 }
1574
1575 static void
1576 finalize_row(stp_vars_t *v, stpi_softweave_t *sw)
1577 {
1578 int row = sw->lineno;
1579 int i,j;
1580 stp_dprintf(STP_DBG_ROWS, v, "Finalizing row %d...\n", row);
1581 for (i = 0; i < sw->oversample; i++)
1582 {
1583 stp_weave_t w;
1584 stp_linecount_t *lines;
1585
1586 for(j=0; j<sw->ncolors; j++)
1587 {
1588 lines = stpi_get_linecount(v, sw, row, i, sw->head_offset[j]);
1589 lines->v[j]++;
1590 }
1591
1592 weave_parameters_by_row(v, sw, row, i, &w);
1593 if (w.physpassend == row)
1594 {
1595 stp_dprintf(STP_DBG_ROWS, v,
1596 "Pass=%d, physpassend=%d, row=%d, lineno=%d, flush..\n",
1597 w.pass, w.physpassend, row, sw->lineno);
1598 stpi_flush_passes(v, 0);
1599 }
1600 }
1601 }
1602
1603 void
1604 stp_write_weave(stp_vars_t *v, unsigned char *const cols[])
1605 {
1606 stpi_softweave_t *sw = get_sw(v);
1607 int length = (sw->linewidth + 7) / 8;
1608 stp_linebounds_t *linebounds[STP_MAX_WEAVE];
1609 int xlength = (length + sw->horizontal_weave - 1) / sw->horizontal_weave;
1610 int ylength = xlength * sw->horizontal_weave;
1611 unsigned char *comp_ptr;
1612 int i, j;
1613 int setactive;
1614 int h_passes = sw->horizontal_weave * sw->vertical_subpasses;
1615 int cpass = sw->current_vertical_subpass * h_passes;
1616
1617 if (!sw->fold_buf)
1618 {
1619 stp_dprintf(STP_DBG_WEAVE_PARAMS, v,
1620 "fold buffer allocation: length %d lw %d weave %d xlength %d ylength %d\n",
1621 length, sw->linewidth, sw->horizontal_weave, xlength, ylength);
1622 stp_dprintf(STP_DBG_WEAVE_PARAMS, v,
1623 "Allocating fold buf %d * %d (%d)\n", ylength, sw->bitwidth,
1624 sw->bitwidth * ylength);
1625 sw->fold_buf = stp_zalloc(sw->bitwidth * ylength);
1626 }
1627 if (!sw->comp_buf)
1628 {
1629 stp_dprintf(STP_DBG_WEAVE_PARAMS, v,
1630 "Allocating compression buffer based on %d, %d\n",
1631 sw->bitwidth, ylength);
1632 sw->comp_buf = stp_zalloc(sw->bitwidth *
1633 (sw->compute_linewidth)(v,ylength));
1634 }
1635 if (sw->current_vertical_subpass == 0)
1636 initialize_row(v, sw, sw->lineno, xlength, cols);
1637
1638 for (j = 0; j < sw->ncolors; j++)
1639 {
1640 if (cols[j])
1641 {
1642 const unsigned char *in;
1643 int idx;
1644
1645 for (i = 0; i < h_passes; i++)
1646 {
1647 int offset = sw->head_offset[j];
1648 int pass = cpass + i;
1649 if (!sw->s[i])
1650 sw->s[i] = stp_zalloc(sw->bitwidth *
1651 (sw->compute_linewidth)(v, ylength));
1652 linebounds[i] =
1653 stpi_get_linebounds(v, sw, sw->lineno, pass, offset);
1654 }
1655
1656 if (sw->bitwidth == 2)
1657 {
1658 stp_fold(cols[j], length, sw->fold_buf);
1659 in = sw->fold_buf;
1660 }
1661 else
1662 in = cols[j];
1663 if (sw->horizontal_weave == 1)
1664 memcpy(sw->s[0], in, length * sw->bitwidth);
1665 else
1666 stp_unpack(length, sw->bitwidth, sw->horizontal_weave, in, sw->s);
1667 if (sw->vertical_subpasses > 1)
1668 {
1669 for (idx = 0; idx < sw->horizontal_weave; idx++)
1670 stp_split(length, sw->bitwidth, sw->vertical_subpasses,
1671 sw->s[idx], sw->horizontal_weave, &(sw->s[idx]));
1672 }
1673 for (i = 0; i < h_passes; i++)
1674 {
1675 int first, last;
1676 setactive = (sw->pack)(v, sw->s[i], sw->bitwidth * xlength,
1677 sw->comp_buf, &comp_ptr, &first, &last);
1678 if (first < linebounds[i]->start_pos[j])
1679 linebounds[i]->start_pos[j] = first;
1680 if (last > linebounds[i]->end_pos[j])
1681 linebounds[i]->end_pos[j] = last;
1682 add_to_row(v, sw, sw->lineno, sw->comp_buf,
1683 comp_ptr - sw->comp_buf, j, setactive, cpass + i);
1684 }
1685 }
1686 }
1687 sw->current_vertical_subpass++;
1688 if (sw->current_vertical_subpass >= sw->vertical_oversample)
1689 {
1690 finalize_row(v, sw);
1691 sw->lineno++;
1692 sw->current_vertical_subpass = 0;
1693 }
1694 }
1695
1696 #if 0
1697 #define TEST_RAW
1698 #endif
1699 #if 0
1700 #define TEST_COOKED
1701 #endif
1702 #if 0
1703 #define ACCUMULATE
1704 #endif
1705
1706 #if defined(TEST_RAW) || defined(TEST_COOKED)
1707 #define TEST
1708 #endif
1709
1710 #ifdef TEST
1711 #define MAXCOLLECT (1000)
1712 #endif
1713
1714 #ifdef TEST_COOKED
1715 static void
1716 calculate_pass_parameters(cooked_t *w, /* I - weave parameters */
1717 int pass, /* I - pass number ( >= 0) */
1718 int *startrow, /* O - print head position */
1719 int *subpass, /* O - subpass number */
1720 int *phantomrows, /* O - missing rows */
1721 int *jetsused) /* O - jets used to print */
1722 {
1723 int raw_pass = pass + w->first_premapped_pass;
1724 int stagger = 0;
1725 int extra;
1726
1727 if (raw_pass < w->first_normal_pass) {
1728 int i = 0;
1729 while (i + w->first_premapped_pass < w->first_normal_pass) {
1730 if (w->pass_premap[i] == pass) {
1731 raw_pass = i + w->first_premapped_pass;
1732 stagger = w->stagger_premap[i];
1733 break;
1734 }
1735 i++;
1736 }
1737 } else if (raw_pass >= w->first_postmapped_pass) {
1738 int i = 0;
1739 while (i + w->first_postmapped_pass < w->first_unused_pass) {
1740 if (w->pass_postmap[i] == pass) {
1741 raw_pass = i + w->first_postmapped_pass;
1742 stagger = w->stagger_postmap[i];
1743 break;
1744 }
1745 i++;
1746 }
1747 }
1748
1749 calculate_raw_pass_parameters(&w->rw, raw_pass, startrow, subpass);
1750 *startrow -= w->rw.separation * w->rw.jets;
1751 *jetsused = w->rw.jets;
1752 *phantomrows = 0;
1753
1754 *startrow += stagger * w->rw.separation;
1755 if (stagger < 0) {
1756 stagger = -stagger;
1757 *phantomrows += stagger;
1758 }
1759 *jetsused -= stagger;
1760
1761 extra = w->first_row_printed - (*startrow + w->rw.separation * *phantomrows);
1762 extra = (extra + w->rw.separation - 1) / w->rw.separation;
1763 if (extra > 0) {
1764 *jetsused -= extra;
1765 *phantomrows += extra;
1766 }
1767
1768 extra = *startrow + w->rw.separation * (*phantomrows + *jetsused - 1)
1769 - w->last_row_printed;
1770 extra = (extra + w->rw.separation - 1) / w->rw.separation;
1771 if (extra > 0) {
1772 *jetsused -= extra;
1773 }
1774 }
1775 #endif /* TEST_COOKED */
1776
1777 #ifdef TEST
1778
1779 #ifdef ACCUMULATE
1780 #define PUTCHAR(x) /* nothing */
1781 #else
1782 #define PUTCHAR(x) putchar(x)
1783 #endif
1784
1785 static void
1786 plotpass(int startrow, int phantomjets, int jetsused, int totaljets,
1787 int separation, int subpass, int *collect, int *prints)
1788 {
1789 int hpos, i;
1790
1791 for (hpos = 0; hpos < startrow; hpos++)
1792 PUTCHAR(' ');
1793
1794 for (i = 0; i < phantomjets; i++) {
1795 int j;
1796 PUTCHAR('X');
1797 hpos++;
1798 for (j = 1; j < separation; j++) {
1799 PUTCHAR(' ');
1800 hpos++;
1801 }
1802 }
1803
1804 for (; i < phantomjets + jetsused; i++) {
1805 if (i > phantomjets) {
1806 int j;
1807 for (j = 1; j < separation; j++) {
1808 PUTCHAR('-');
1809 hpos++;
1810 }
1811 }
1812 if (hpos < MAXCOLLECT) {
1813 if (collect[hpos] & 1 << subpass)
1814 PUTCHAR('^');
1815 else if (subpass < 10)
1816 PUTCHAR('0' + subpass);
1817 else
1818 PUTCHAR('A' + subpass - 10);
1819 collect[hpos] |= 1 << subpass;
1820 prints[hpos]++;
1821 } else {
1822 PUTCHAR('0' + subpass);
1823 }
1824 hpos++;
1825 }
1826
1827 while (i++ < totaljets) {
1828 int j;
1829 for (j = 1; j < separation; j++) {
1830 PUTCHAR(' ');
1831 hpos++;
1832 }
1833 PUTCHAR('X');
1834 }
1835 #ifdef ACCUMULATE
1836 for (i=0; i<=MAXCOLLECT; i++) {
1837 if (collect[i] == 0)
1838 putchar(' ');
1839 else if (collect[i] < 10)
1840 putchar('0'+collect[i]);
1841 else
1842 putchar('A'+collect[i]-10);
1843 }
1844 #endif
1845 putchar('\n');
1846 }
1847 #endif /* TEST */
1848
1849 #ifdef TEST_COOKED
1850 int
1851 main(int ac, char *av[])
1852 {
1853 int S =ac>1 ? atoi(av[1]) : 4;
1854 int J =ac>2 ? atoi(av[2]) : 12;
1855 int H =ac>3 ? atoi(av[3]) : 1;
1856 int firstrow =ac>4 ? atoi(av[4]) : 1;
1857 int lastrow =ac>5 ? atoi(av[5]) : 100;
1858 int pageheight=ac>6 ? atoi(av[6]) : 1000;
1859 stp_weave_strategy_t strategy =ac>7 ? atoi(av[7]) : 1;
1860 cooked_t *weave;
1861 int passes;
1862
1863 int pass, x;
1864 int collect[MAXCOLLECT];
1865 int prints[MAXCOLLECT];
1866
1867 memset(collect, 0, MAXCOLLECT*sizeof(int));
1868 memset(prints, 0, MAXCOLLECT*sizeof(int));
1869 printf("S=%d J=%d H=%d firstrow=%d lastrow=%d "
1870 "pageheight=%d strategy=%d\n",
1871 S, J, H, firstrow, lastrow, pageheight, strategy);
1872
1873 weave = initialize_weave_params(S, J, H, firstrow, lastrow,
1874 pageheight, strategy);
1875 passes = weave->first_unused_pass - weave->first_premapped_pass;
1876
1877 for (pass = 0; pass < passes; pass++) {
1878 int startrow, subpass, phantomjets, jetsused;
1879
1880 calculate_pass_parameters(weave, pass, &startrow, &subpass,
1881 &phantomjets, &jetsused);
1882
1883 plotpass(startrow, phantomjets, jetsused, J, S, subpass,
1884 collect, prints);
1885 }
1886
1887 for (pass=MAXCOLLECT - 1; prints[pass] == 0; pass--)
1888 ;
1889
1890 for (x=0; x<=pass; x++) {
1891 if (collect[x] < 10)
1892 putchar('0'+collect[x]);
1893 else
1894 putchar('A'+collect[x]-10);
1895 }
1896 putchar('\n');
1897
1898 for (x=0; x<=pass; x++) {
1899 if (prints[x] < 10)
1900 putchar('0'+prints[x]);
1901 else
1902 putchar('A'+prints[x]-10);
1903 }
1904 putchar('\n');
1905
1906 return 0;
1907 }
1908 #endif /* TEST_COOKED */
1909
1910 #ifdef TEST_RAW
1911 int
1912 main(int ac, char *av[])
1913 {
1914 int S =ac>1 ? atoi(av[1]) : 4;
1915 int J =ac>2 ? atoi(av[2]) : 12;
1916 int h_pos =ac>3 ? atoi(av[3]) : 1;
1917 int h_over=ac>4 ? atoi(av[4]) : 1;
1918 int v_over=ac>5 ? atoi(av[5]) : 1;
1919 int H = h_pos * h_over * v_over;
1920
1921 int pass, passes, x;
1922 int collect[MAXCOLLECT];
1923 int prints[MAXCOLLECT];
1924 raw_t raw;
1925
1926 memset(collect, 0, MAXCOLLECT*sizeof(int));
1927 memset(prints, 0, MAXCOLLECT*sizeof(int));
1928 printf("S=%d J=%d H=%d\n", S, J, H);
1929
1930 if (H > J) {
1931 printf("H > J, so this weave will not work!\n");
1932 }
1933 passes = S * H * 3;
1934
1935 initialize_raw_weave(&raw, S, J, H);
1936
1937 for (pass=0; pass<passes + S * H; pass++) {
1938 int startrow, subpass, phantomjets, jetsused;
1939
1940 calculate_raw_pass_parameters(&raw, pass, &startrow, &subpass);
1941 phantomjets = 0;
1942 jetsused = J;
1943
1944 plotpass(startrow, phantomjets, jetsused, J, S, subpass,
1945 collect, prints);
1946 }
1947 for (pass=MAXCOLLECT - 1; prints[pass] == 0; pass--)
1948 ;
1949
1950 for (x=0; x<=pass; x++) {
1951 if (collect[x] < 10)
1952 putchar('0'+collect[x]);
1953 else
1954 putchar('A'+collect[x]-10);
1955 }
1956 putchar('\n');
1957
1958 for (x=0; x<=pass; x++) {
1959 if (prints[x] < 10)
1960 putchar('0'+prints[x]);
1961 else
1962 putchar('A'+prints[x]-10);
1963 }
1964 putchar('\n');
1965
1966 printf(" A first row given by pass lookup doesn't match row lookup\n"
1967 " B jet out of range\n"
1968 " C given jet number of pass doesn't print this row\n"
1969 " D subpass given by reverse lookup doesn't match requested subpass\n");
1970
1971 for (x = S * J; x < S * J * 20; x++) {
1972 int h;
1973 for (h = 0; h < H; h++) {
1974 int pass, jet, start, first, subpass, z;
1975 int a=0, b=0, c=0, d=0;
1976 calculate_raw_row_parameters(&raw, x, h, &pass, &jet, &start);
1977 for (z=0; z < pass; z++) {
1978 putchar(' ');
1979 }
1980 printf("%d", jet);
1981 calculate_raw_pass_parameters(&raw, pass, &first, &subpass);
1982 if (first != start)
1983 a=1;
1984 if (jet < 0 || jet >= J)
1985 b=1;
1986 if (x != first + jet * S)
1987 c=1;
1988 if (subpass != h)
1989 d=1;
1990 if (a || b || c || d) {
1991 printf(" (");
1992 if (a) putchar('A');
1993 if (b) putchar('B');
1994 if (c) putchar('C');
1995 if (d) putchar('D');
1996 putchar(')');
1997 printf("\npass=%d first=%d start=%d jet=%d subpass=%d", pass, first, start, jet, subpass);
1998 }
1999 putchar('\n');
2000 }
2001 /* putchar('\n'); */
2002 }
2003
2004 return 0;
2005 }
2006
2007 #endif /* TEST_RAW */