| 1 | /* |
| 2 | * Generate PPM files from printer output |
| 3 | * |
| 4 | * Copyright 2000-2001 Eric Sharkey <[email protected]> |
| 5 | * Andy Thaller <[email protected]> |
| 6 | * Robert Krawitz <[email protected]> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the Free |
| 10 | * Software Foundation; either version 2 of the License, or (at your option) |
| 11 | * any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 16 | * for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | |
| 22 | #ifdef HAVE_CONFIG_H |
| 23 | #include <config.h> |
| 24 | #endif |
| 25 | #include <gutenprint/util.h> |
| 26 | #include<stdio.h> |
| 27 | #include<stdlib.h> |
| 28 | #ifdef HAVE_LIMITS_H |
| 29 | #include<limits.h> |
| 30 | #endif |
| 31 | #include<string.h> |
| 32 | |
| 33 | #ifdef __GNUC__ |
| 34 | #define inline __inline__ |
| 35 | #endif |
| 36 | |
| 37 | typedef enum { |
| 38 | QT_NONE, |
| 39 | QT_QUAD, |
| 40 | QT_MIS |
| 41 | } quadtone_t; |
| 42 | |
| 43 | /* |
| 44 | * Printer state variable. |
| 45 | */ |
| 46 | typedef struct { |
| 47 | unsigned char unidirectional; |
| 48 | unsigned char printer_weave; |
| 49 | int page_management_units; /* dpi */ |
| 50 | int relative_horizontal_units; /* dpi */ |
| 51 | int absolute_horizontal_units; /* dpi, assumed to be >= relative */ |
| 52 | unsigned int relative_vertical_units; /* dpi */ |
| 53 | unsigned int absolute_vertical_units; /* dpi, assumed to be >= relative */ |
| 54 | int horizontal_spacing; /* Horizontal dot spacing */ |
| 55 | unsigned int top_margin; /* dots */ |
| 56 | unsigned int bottom_margin; /* dots */ |
| 57 | unsigned int page_height; /* dots */ |
| 58 | int dotsize; |
| 59 | int bpp; /* bits per pixel */ |
| 60 | int current_color; |
| 61 | int xposition; /* dots */ |
| 62 | unsigned int yposition; /* dots */ |
| 63 | int monomode; |
| 64 | int nozzle_separation; |
| 65 | int nozzles; |
| 66 | int extraskip; |
| 67 | int got_graphics; |
| 68 | int left_edge; |
| 69 | int right_edge; |
| 70 | unsigned int top_edge; |
| 71 | unsigned int bottom_edge; |
| 72 | quadtone_t quadtone; |
| 73 | } pstate_t; |
| 74 | |
| 75 | /* We'd need about a gigabyte of ram to hold a ppm file of an 8.5 x 11 |
| 76 | * 1440 x 720 dpi page. That's more than I have in my laptop, so, let's |
| 77 | * play some games to reduce memory. Allocate each scan line separately, |
| 78 | * and don't require that the allocated height be full page width. This |
| 79 | * way, if we only want to print a 2x2 image, we only need to allocate the |
| 80 | * ram that we need. We'll build up the printed image in ram at low |
| 81 | * color depth, KCMYcm color basis, and then write out the RGB ppm file |
| 82 | * as output. This way we never need to have the full data in RAM at any |
| 83 | * time. 2 bits per color of KCMYcm is half the size of 8 bits per color |
| 84 | * of RGB. |
| 85 | * |
| 86 | * We would like to be able to print in bands, so that we don't have to |
| 87 | * read the entire page into memory in order to print it. Unfortunately, |
| 88 | * we may not know what the left and right margins are until we've read the |
| 89 | * entire file. If we can read it in two passes we could do it; use one |
| 90 | * pass to scan the file looking at the margins, and another pass to |
| 91 | * actually read in the data. This optimization may be worthwhile. |
| 92 | */ |
| 93 | |
| 94 | #define MAX_INKS 20 |
| 95 | typedef struct { |
| 96 | unsigned char *line[MAX_INKS]; |
| 97 | int startx[MAX_INKS]; |
| 98 | int stopx[MAX_INKS]; |
| 99 | } line_type; |
| 100 | |
| 101 | typedef unsigned char ppmpixel[3]; |
| 102 | |
| 103 | unsigned char *buf; |
| 104 | unsigned valid_bufsize; |
| 105 | unsigned char minibuf[256]; |
| 106 | unsigned bufsize; |
| 107 | unsigned save_bufsize; |
| 108 | unsigned char ch; |
| 109 | unsigned short sh; |
| 110 | int eject = 0; |
| 111 | int global_counter = 0; |
| 112 | int global_count = 0; |
| 113 | unsigned color_mask = 0xffffffff; |
| 114 | |
| 115 | pstate_t pstate; |
| 116 | int unweave; |
| 117 | |
| 118 | line_type **page=NULL; |
| 119 | |
| 120 | /* Color Codes: |
| 121 | color Epson1 Epson2 Sequential |
| 122 | Black 0 0 0/16 |
| 123 | Magenta 1 1 1/17 |
| 124 | Cyan 2 2 2/18 |
| 125 | Yellow 4 4 3/19 |
| 126 | L.Mag. 17 257 4 |
| 127 | L.Cyan 18 258 5 |
| 128 | L.Black 16 256 6 |
| 129 | D.Yellow 36 516 7 |
| 130 | Red 7 N/A 8 |
| 131 | Blue 8 N/A 9 |
| 132 | P.Black 64 N/A 0 |
| 133 | Gloss 9 N/A 10 |
| 134 | LL.Black 48 768 11 |
| 135 | Orange 10 N/A 12 |
| 136 | */ |
| 137 | |
| 138 | /* convert either Epson1 or Epson2 color encoding into a sequential encoding */ |
| 139 | static int |
| 140 | seqcolor(int c) |
| 141 | { |
| 142 | switch (c) |
| 143 | { |
| 144 | case 0: |
| 145 | return 0; |
| 146 | case 1: |
| 147 | return 1; |
| 148 | case 2: |
| 149 | return 2; |
| 150 | case 4: |
| 151 | return 3; |
| 152 | case 17: |
| 153 | case 257: |
| 154 | return 4; |
| 155 | case 18: |
| 156 | case 258: |
| 157 | return 5; |
| 158 | case 16: |
| 159 | case 256: |
| 160 | return 6; |
| 161 | case 36: |
| 162 | case 516: |
| 163 | return 7; |
| 164 | case 7: |
| 165 | return 8; |
| 166 | case 8: |
| 167 | return 9; |
| 168 | case 9: |
| 169 | return 10; |
| 170 | case 48: |
| 171 | case 768: |
| 172 | return 11; |
| 173 | case 10: |
| 174 | return 12; |
| 175 | case 64: |
| 176 | return 16; |
| 177 | case 65: |
| 178 | return 17; |
| 179 | case 66: |
| 180 | return 18; |
| 181 | case 68: |
| 182 | return 19; |
| 183 | default: |
| 184 | return 0; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | extern void merge_line(line_type *p, unsigned char *l, int startl, int stopl, |
| 189 | int color); |
| 190 | extern void expand_line (unsigned char *src, unsigned char *dst, int height, |
| 191 | int skip, int left_ignore); |
| 192 | extern void write_output (FILE *fp_w, int dontwrite, int allblack); |
| 193 | extern void find_white (unsigned char *buff,unsigned int npix, unsigned int *left, unsigned int *right); |
| 194 | extern int update_page (unsigned char *buff, unsigned int buffsize, unsigned int m, unsigned int n, |
| 195 | int color, int density); |
| 196 | extern void parse_escp2 (FILE *fp_r); |
| 197 | extern void reverse_bit_order (unsigned char *buff, int n); |
| 198 | extern int rle_decode (unsigned char *inbuf, int n, int max); |
| 199 | extern void parse_canon (FILE *fp_r); |
| 200 | |
| 201 | unsigned get_mask_1[] = { 7, 6, 5, 4, 3, 2, 1, 0 }; |
| 202 | unsigned get_mask_2[] = { 6, 4, 2, 0 }; |
| 203 | unsigned get_mask_4[] = { 4, 0 }; |
| 204 | |
| 205 | static inline int |
| 206 | get_bits(unsigned char *p, int idx) |
| 207 | { |
| 208 | /* |
| 209 | * p is a pointer to a bit stream, ordered MSb first. Extract the |
| 210 | * indexth bpp bit width field and return that value. Ignore byte |
| 211 | * boundaries. |
| 212 | */ |
| 213 | int value, b; |
| 214 | unsigned addr; |
| 215 | switch (pstate.bpp) |
| 216 | { |
| 217 | case 1: |
| 218 | return (p[idx >> 3] >> (7 - (idx & 7))) & 1; |
| 219 | case 2: |
| 220 | b = get_mask_2[idx & 3]; |
| 221 | return (p[idx >> 2] >> b) & 3; |
| 222 | case 4: |
| 223 | b = get_mask_4[idx & 1]; |
| 224 | return (p[idx >> 1] >> b) & 0xf; |
| 225 | case 8: |
| 226 | return p[idx]; |
| 227 | default: |
| 228 | addr = (idx * pstate.bpp); |
| 229 | value = 0; |
| 230 | for (b = 0; b < pstate.bpp; b++) |
| 231 | { |
| 232 | value += value; |
| 233 | value |= (p[(addr + b) >> 3] >> (7 - ((addr + b) & 7))) & 1; |
| 234 | } |
| 235 | return(value); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | static unsigned clr_mask_1[] = { 0xfe, 0xfd, 0xfb, 0xf7, |
| 240 | 0xef, 0xdf, 0xbf, 0x7f }; |
| 241 | static unsigned clr_mask_2[] = { 0xfc, 0, 0xf3, 0, 0xcf, 0, 0x3f, 0 }; |
| 242 | static unsigned clr_mask_4[] = { 0xf0, 0, 0, 0, 0xf, 0, 0, 0 }; |
| 243 | |
| 244 | static inline void |
| 245 | set_bits(unsigned char *p,int idx,int value) |
| 246 | { |
| 247 | |
| 248 | /* |
| 249 | * p is a pointer to a bit stream, ordered MSb first. Set the |
| 250 | * indexth bpp bit width field to value value. Ignore byte |
| 251 | * boundaries. |
| 252 | */ |
| 253 | |
| 254 | int b; |
| 255 | switch (pstate.bpp) |
| 256 | { |
| 257 | case 1: |
| 258 | b = (7 - (idx & 7)); |
| 259 | p[idx >> 3] &= clr_mask_1[b]; |
| 260 | p[idx >> 3] |= value << b; |
| 261 | break; |
| 262 | case 2: |
| 263 | b = get_mask_2[idx & 3]; |
| 264 | p[idx >> 2] &= clr_mask_2[b]; |
| 265 | p[idx >> 2] |= value << b; |
| 266 | break; |
| 267 | case 4: |
| 268 | b = get_mask_4[idx & 1]; |
| 269 | p[idx >> 1] &= clr_mask_4[b]; |
| 270 | p[idx >> 1] |= value << b; |
| 271 | break; |
| 272 | case 8: |
| 273 | p[idx] = value; |
| 274 | break; |
| 275 | default: |
| 276 | for (b = pstate.bpp - 1; b >= 0; b--) |
| 277 | { |
| 278 | if (value & 1) |
| 279 | p[(idx * pstate.bpp + b) / 8] |= |
| 280 | 1 << (7 - ((idx * pstate.bpp + b) % 8)); |
| 281 | else |
| 282 | p[(idx * pstate.bpp + b) / 8] &= |
| 283 | ~(1 << (7 - ((idx * pstate.bpp + b) % 8))); |
| 284 | value/=2; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | static float ink_colors[MAX_INKS][4] = |
| 290 | /* C(R) M(G) Y(B) K(W) */ |
| 291 | {{ 0, 0, 0, 1 }, /* 0 K */ |
| 292 | { 1, .1, 1, 1 }, /* 1 M */ |
| 293 | { .1, 1, 1, 1 }, /* 2 C */ |
| 294 | { 1, 1, .1, 1 }, /* 3 Y */ |
| 295 | { 1, .7, 1, 1 }, /* 4 m */ |
| 296 | { .7, 1, 1, 1 }, /* 5 c */ |
| 297 | { .7, .7, .7, 1 }, /* 6 k */ |
| 298 | { .7, .7, 0, 1 }, /* 7 dY */ |
| 299 | { 1, 0, 0, 1 }, /* 8 R */ |
| 300 | { 0, 0, 1, 1 }, /* 8 B */ |
| 301 | { 1, 1, 1, 1 }, /* 10 Gloss */ |
| 302 | { .8, .8, .8, 1 }, /* 11 llk */ |
| 303 | { .9, .3, 0, 1 }, /* 12 Orange */ |
| 304 | { 0, 0, 0, 1 }, /* 13 K */ |
| 305 | { 0, 0, 0, 1 }, /* 14 K */ |
| 306 | { 0, 0, 0, 1 }, /* 15 K */ |
| 307 | { 0, 0, 0, 1 }, /* 16 K */ |
| 308 | { 1, .1, 1, 1 }, /* 17 M */ |
| 309 | { .1, 1, 1, 1 }, /* 18 C */ |
| 310 | { 1, 1, .1, 1 }, /* 19 Y */ |
| 311 | }; |
| 312 | |
| 313 | static float quadtone_inks[] = { 0.0, .5, .25, .75, .9, .8 }; |
| 314 | |
| 315 | static float mis_quadtone_inks[] = { 0.0, .25, .75, .5, .55, .85 }; |
| 316 | |
| 317 | static float bpp_shift[] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 }; |
| 318 | |
| 319 | static inline void |
| 320 | mix_ink(ppmpixel p, int color, unsigned int amount, float *ink, quadtone_t quadtone) |
| 321 | { |
| 322 | /* this is pretty crude */ |
| 323 | |
| 324 | if (((1 << color) & color_mask) && amount) |
| 325 | { |
| 326 | int i; |
| 327 | float size; |
| 328 | |
| 329 | size = (float) amount / bpp_shift[pstate.bpp]; |
| 330 | switch (quadtone) |
| 331 | { |
| 332 | case QT_QUAD: |
| 333 | for (i = 0; i < 3; i++) |
| 334 | p[i] *= (1 - size) + size * quadtone_inks[color]; |
| 335 | break; |
| 336 | case QT_MIS: |
| 337 | for (i = 0; i < 3; i++) |
| 338 | p[i] *= (1 - size) + size * mis_quadtone_inks[color]; |
| 339 | break; |
| 340 | default: |
| 341 | for (i = 0; i < 3; i++) |
| 342 | p[i] *= (1 - size) + size * ink[i]; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | void |
| 348 | merge_line(line_type *p, unsigned char *l, int startl, int stopl, int color) |
| 349 | { |
| 350 | |
| 351 | int i; |
| 352 | int temp, shift, height, lvalue, pvalue, oldstop; |
| 353 | int width, owidth; |
| 354 | unsigned char *tempp; |
| 355 | int reversed = 0; |
| 356 | int need_realloc = 0; |
| 357 | |
| 358 | /* |
| 359 | * If we have a pixel to the left of anything previously printed, |
| 360 | * we need to expand our margins to the left. This is a bit tricky... |
| 361 | */ |
| 362 | if (startl < p->startx[color]) |
| 363 | { |
| 364 | temp = p->startx[color]; |
| 365 | p->startx[color] = startl; |
| 366 | startl = temp; |
| 367 | |
| 368 | temp = p->stopx[color]; |
| 369 | p->stopx[color] = stopl; |
| 370 | stopl = temp; |
| 371 | |
| 372 | tempp = p->line[color]; |
| 373 | p->line[color] = l; |
| 374 | l = tempp; |
| 375 | reversed = 1; |
| 376 | } |
| 377 | shift = startl - p->startx[color]; |
| 378 | height = stopl - startl + 1; |
| 379 | |
| 380 | oldstop = p->stopx[color]; |
| 381 | if (stopl > p->stopx[color]) |
| 382 | { |
| 383 | p->stopx[color] = stopl; |
| 384 | need_realloc = 1; |
| 385 | } |
| 386 | if (need_realloc || reversed) |
| 387 | { |
| 388 | width = ((p->stopx[color] - p->startx[color] + 1) * pstate.bpp + 7) / 8; |
| 389 | owidth = ((oldstop - p->startx[color] + 1) * pstate.bpp + 7) / 8; |
| 390 | p->line[color] = stp_realloc(p->line[color], width); |
| 391 | memset((p->line[color] + owidth), 0, (width - owidth)); |
| 392 | } |
| 393 | /* |
| 394 | * Can we do an empty line optimization? |
| 395 | */ |
| 396 | for (i = 0; i < height; i++) |
| 397 | { |
| 398 | lvalue = get_bits(l, i); |
| 399 | if (lvalue) |
| 400 | { |
| 401 | pvalue = get_bits(p->line[color], i + shift); |
| 402 | pvalue += lvalue; |
| 403 | if (pvalue > (1 << pstate.bpp) - 1) |
| 404 | pvalue = (1 << pstate.bpp) - 1; |
| 405 | set_bits(p->line[color], i + shift, pvalue); |
| 406 | } |
| 407 | } |
| 408 | stp_free(l); |
| 409 | } |
| 410 | |
| 411 | void |
| 412 | expand_line (unsigned char *src, unsigned char *dst, int height, int skip, |
| 413 | int left_ignore) |
| 414 | { |
| 415 | |
| 416 | /* |
| 417 | * src is a pointer to a bit stream which is composed of fields of height |
| 418 | * bpp starting with the most significant bit of the first byte and |
| 419 | * proceeding from there with no regard to byte boundaries. For the |
| 420 | * existing Epson printers, bpp is 1 or 2, which means fields will never |
| 421 | * cross byte boundaries. However, if bpp were 3, this would undoubtedly |
| 422 | * happen. This routine will make no assumptions about bpp, and handle each |
| 423 | * bit individually. It's slow, but, it's the only way that will work in |
| 424 | * the general case of arbitrary bpp. |
| 425 | * |
| 426 | * We want to copy each field from the src to the dst, spacing the fields |
| 427 | * out every skip fields. We should ignore the first left_ignore fields |
| 428 | * pointed to by src, though. |
| 429 | */ |
| 430 | |
| 431 | int i; |
| 432 | |
| 433 | if ((skip == 1) && !(left_ignore * pstate.bpp % 8)) |
| 434 | { |
| 435 | /* the trivial case, this should be faster */ |
| 436 | memcpy(dst, src + left_ignore * pstate.bpp / 8, |
| 437 | (height * pstate.bpp + 7) / 8); |
| 438 | } |
| 439 | else |
| 440 | { |
| 441 | for (i = 0; i < height; i++) |
| 442 | set_bits(dst, i * skip, get_bits(src, i + left_ignore)); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | int donothing; |
| 447 | |
| 448 | void |
| 449 | write_output(FILE *fp_w, int dontwrite, int allblack) |
| 450 | { |
| 451 | int c, l, p, left, right, first, last, width, height, i; |
| 452 | unsigned int amount; |
| 453 | ppmpixel *out_row; |
| 454 | int oversample = pstate.absolute_horizontal_units / |
| 455 | pstate.absolute_vertical_units; |
| 456 | if (oversample == 0) |
| 457 | oversample = 1; |
| 458 | |
| 459 | fprintf(stderr, "Margins: top: %d bottom: top+%d\n", pstate.top_margin, |
| 460 | pstate.bottom_margin); |
| 461 | |
| 462 | first = pstate.top_edge; |
| 463 | last = pstate.bottom_edge; |
| 464 | left = pstate.left_edge; |
| 465 | right = pstate.right_edge; |
| 466 | height = oversample * (last - first + 1); |
| 467 | |
| 468 | fprintf(stderr, "Image from (%d,%d) (%.3fx%.3f) to (%d,%d) (%.3fx%.3f) size (%dx%d) (%.3fx%.3f)\n", |
| 469 | left, first - pstate.top_margin, |
| 470 | (left / (double) pstate.page_management_units), |
| 471 | (first / (double) pstate.page_management_units), |
| 472 | right, last - pstate.top_margin, |
| 473 | (right / (double) pstate.page_management_units), |
| 474 | (last / (double) pstate.page_management_units), |
| 475 | right - left + 1, last - first + 1, |
| 476 | (right - left + 1) / (double) pstate.page_management_units, |
| 477 | (last - first + 1) / (double) pstate.page_management_units); |
| 478 | |
| 479 | width = right - left + 1; |
| 480 | if (width < 0) |
| 481 | width=0; |
| 482 | |
| 483 | out_row = stp_malloc(sizeof(ppmpixel) * width); |
| 484 | fprintf(stderr, "Writing output...\n"); |
| 485 | |
| 486 | if (dontwrite) |
| 487 | return; |
| 488 | /* write out the PPM header */ |
| 489 | fprintf(fp_w, "P6\n"); |
| 490 | fprintf(fp_w, "%d %d\n", width, height); |
| 491 | fprintf(fp_w, "255\n"); |
| 492 | for (l = first; l <= last; l++) |
| 493 | { |
| 494 | line_type *lt = page[l]; |
| 495 | memset(out_row, ~0, (sizeof(ppmpixel) * width)); |
| 496 | if (lt) |
| 497 | { |
| 498 | for (c = 0; c < MAX_INKS; c++) |
| 499 | { |
| 500 | int inknum = allblack ? 0 : c; |
| 501 | float *ink = ink_colors[inknum]; |
| 502 | if (lt->line[c]) |
| 503 | { |
| 504 | for (p = lt->startx[c]; p <= lt->stopx[c]; p++) |
| 505 | { |
| 506 | amount = get_bits(lt->line[c], p - lt->startx[c]); |
| 507 | mix_ink(out_row[p - left], c, amount, ink, |
| 508 | pstate.quadtone); |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | for (i = 0; i < oversample; i++) |
| 514 | fwrite(out_row, sizeof(ppmpixel), width, fp_w); |
| 515 | } |
| 516 | stp_free(out_row); |
| 517 | } |
| 518 | |
| 519 | void |
| 520 | find_white(unsigned char *buff,unsigned int npix, unsigned int *left, unsigned int *right) |
| 521 | { |
| 522 | |
| 523 | /* |
| 524 | * If a line has white borders on either side, count the number of |
| 525 | * pixels and fill that info into left and right. |
| 526 | */ |
| 527 | |
| 528 | unsigned int i, j, max; |
| 529 | unsigned int words, bytes, bits, extra; |
| 530 | |
| 531 | *left = *right = 0; |
| 532 | bits = npix * pstate.bpp; |
| 533 | bytes = bits / 8; |
| 534 | extra = bits % 8; |
| 535 | words = bytes / sizeof(int); |
| 536 | |
| 537 | /* |
| 538 | * First, find the leftmost pixel. We first identify the word |
| 539 | * containing the byte, then the byte, and finally the pixel within |
| 540 | * the byte. It does seem like this is unnecessarily complex, perhaps? |
| 541 | */ |
| 542 | max = words; |
| 543 | for (i = 0; (i < max) && (((int *)buff)[i] == 0); i++) |
| 544 | ; |
| 545 | max = (i < words) ? (i + 1) * sizeof(int) : bytes; |
| 546 | |
| 547 | i *= sizeof(int); /* Convert from ints to bytes */ |
| 548 | for (; (i < max) && (buff[i] == 0); i++) |
| 549 | ; |
| 550 | max = (i < bytes) ? 8 : extra; |
| 551 | for (j = 0; (j < max) && !(buff[i] & (1 << (7 - j))); j++) |
| 552 | ; |
| 553 | *left = (i * 8 + j) / pstate.bpp; |
| 554 | *right = 0; |
| 555 | |
| 556 | /* if left is everything, then right is nothing */ |
| 557 | if (*left == npix) |
| 558 | return; |
| 559 | |
| 560 | /* right side, this is a little trickier */ |
| 561 | for (i = 0; (i < extra) && !(buff[bytes] & (1 << (i + 8 - extra))); i++) |
| 562 | ; |
| 563 | if (i < extra) |
| 564 | { |
| 565 | *right = i / pstate.bpp; |
| 566 | return; |
| 567 | } |
| 568 | *right = extra; /*temporarily store right in bits to avoid rounding error*/ |
| 569 | |
| 570 | for (i = 0; (i < bytes % sizeof(int)) && !(buff[bytes - 1 - i]); i++) |
| 571 | ; |
| 572 | if (i < bytes % sizeof(int)) |
| 573 | { |
| 574 | for (j = 0; (j < 8) && !(buff[bytes - 1 - i] & (1 << j)); j++) |
| 575 | ; |
| 576 | *right = (*right + i * 8 + j) / pstate.bpp; |
| 577 | return; |
| 578 | } |
| 579 | *right += i * 8; |
| 580 | |
| 581 | for (i = 0; (i < words) && !(((int *)buff)[words - 1 - i]); i++) |
| 582 | ; |
| 583 | |
| 584 | if (i < words) |
| 585 | { |
| 586 | *right += i * sizeof(int) * 8; |
| 587 | for (j = 0; |
| 588 | (j < sizeof(int)) && !(buff[(words - i) * sizeof(int) - 1 - j]); |
| 589 | j++) |
| 590 | ; |
| 591 | if (j < sizeof(int)) |
| 592 | { |
| 593 | *right += j * 8; |
| 594 | max = (words - i) * sizeof(int) - 1 - j; |
| 595 | for (j = 0; (j < 8) && !(buff[max] & (1 << j)); j++) |
| 596 | ; |
| 597 | if (j < 8) |
| 598 | { |
| 599 | *right = (*right + j) / pstate.bpp; |
| 600 | return; |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | fprintf(stderr, "Warning: Reality failure. The impossible happened.\n"); |
| 605 | } |
| 606 | |
| 607 | int |
| 608 | update_page(unsigned char *buff, /* I - pixel data */ |
| 609 | unsigned int buffsize, /* I - size of buff in bytes */ |
| 610 | unsigned int m, /* I - height of area in pixels */ |
| 611 | unsigned int n, /* I - width of area in pixels */ |
| 612 | int color, /* I - color of pixel data */ |
| 613 | int density /* I - horizontal density in dpi */ |
| 614 | ) |
| 615 | { |
| 616 | unsigned int y; |
| 617 | int skip, oldstart, oldstop, mi = 0; |
| 618 | unsigned int left_white, right_white, width; |
| 619 | unsigned char *oldline; |
| 620 | int sep; |
| 621 | |
| 622 | (void)buffsize; |
| 623 | |
| 624 | if ((n == 0) || (m == 0)) |
| 625 | return(0); /* shouldn't happen */ |
| 626 | |
| 627 | skip = pstate.relative_horizontal_units / density; |
| 628 | skip *= pstate.extraskip; |
| 629 | |
| 630 | if (skip == 0) |
| 631 | { |
| 632 | fprintf(stderr, "Warning! Attempting to print at %d DPI but units are " |
| 633 | "set to %d DPI.\n", density, pstate.relative_horizontal_units); |
| 634 | return(0); |
| 635 | } |
| 636 | |
| 637 | if (!page) |
| 638 | { |
| 639 | fprintf(stderr, |
| 640 | "Warning! Attempting to print before setting up page!\n"); |
| 641 | /* |
| 642 | * Let's hope that we've at least initialized the printer with |
| 643 | * with an ESC @ and allocate the default page. Otherwise, we'll |
| 644 | * have unpredictable results. But, that's a pretty acurate statement |
| 645 | * for a real printer, too! |
| 646 | */ |
| 647 | page = (line_type **) |
| 648 | stp_zalloc((pstate.bottom_margin - pstate.top_margin) * |
| 649 | sizeof(line_type *)); |
| 650 | } |
| 651 | if (pstate.printer_weave) |
| 652 | sep = 1; |
| 653 | else |
| 654 | sep = pstate.nozzle_separation; |
| 655 | for (y=pstate.yposition; y < pstate.yposition + m * sep; y += sep, mi++) |
| 656 | { |
| 657 | if (y >= pstate.bottom_margin - pstate.top_margin) |
| 658 | { |
| 659 | fprintf(stderr, |
| 660 | "Warning: Unprinter out of unpaper (limit %d, pos %d).\n", |
| 661 | pstate.bottom_margin, y); |
| 662 | return(1); |
| 663 | } |
| 664 | find_white(buff + mi * ((n * pstate.bpp + 7) / 8), n, |
| 665 | &left_white, &right_white); |
| 666 | if (left_white == n) |
| 667 | continue; /* ignore blank lines */ |
| 668 | if (!(page[y])) |
| 669 | { |
| 670 | page[y] = (line_type *) stp_zalloc(sizeof(line_type)); |
| 671 | if (y < pstate.top_edge) |
| 672 | pstate.top_edge = y; |
| 673 | if (y > pstate.bottom_edge) |
| 674 | pstate.bottom_edge = y; |
| 675 | } |
| 676 | if ((left_white * pstate.bpp < 8) && (skip == 1)) |
| 677 | { |
| 678 | left_white=0; /* if it's just a few bits, don't bother cropping */ |
| 679 | } /* unless we need to expand the line anyway */ |
| 680 | if (page[y]->line[color]) |
| 681 | { |
| 682 | oldline = page[y]->line[color]; |
| 683 | oldstart = page[y]->startx[color]; |
| 684 | oldstop = page[y]->stopx[color]; |
| 685 | } |
| 686 | else |
| 687 | { |
| 688 | oldline = NULL; |
| 689 | oldstart = -1; |
| 690 | oldstop = -1; |
| 691 | } |
| 692 | page[y]->startx[color] = pstate.xposition + left_white * skip; |
| 693 | page[y]->stopx[color] =pstate.xposition + ((n - 1 - right_white) * skip); |
| 694 | if (page[y]->startx[color] < pstate.left_edge) |
| 695 | pstate.left_edge = page[y]->startx[color]; |
| 696 | if (page[y]->stopx[color] > pstate.right_edge) |
| 697 | pstate.right_edge = page[y]->stopx[color]; |
| 698 | width = page[y]->stopx[color] - page[y]->startx[color]; |
| 699 | page[y]->line[color] = |
| 700 | stp_zalloc(((width * skip + 1) * pstate.bpp + 7) / 8); |
| 701 | expand_line(buff + mi * ((n * pstate.bpp + 7) / 8), page[y]->line[color], |
| 702 | width+1, skip, left_white); |
| 703 | if (oldline) |
| 704 | merge_line(page[y], oldline, oldstart, oldstop, color); |
| 705 | } |
| 706 | if (n) |
| 707 | pstate.xposition += (n - 1) * skip + 1; |
| 708 | return(0); |
| 709 | } |
| 710 | |
| 711 | #define get1(error) \ |
| 712 | do \ |
| 713 | { \ |
| 714 | if (!(global_count = fread(&ch, 1, 1, fp_r))) \ |
| 715 | { \ |
| 716 | fprintf(stderr, "%s at %d (%x), read %d", \ |
| 717 | error, global_counter, global_counter, global_count); \ |
| 718 | eject = 1; \ |
| 719 | continue; \ |
| 720 | } \ |
| 721 | else \ |
| 722 | global_counter += global_count; \ |
| 723 | } while (0) |
| 724 | |
| 725 | #define get2(error) \ |
| 726 | do \ |
| 727 | { \ |
| 728 | if (!(global_count = fread(minibuf, 1, 2, fp_r))) \ |
| 729 | { \ |
| 730 | fprintf(stderr, "%s at %d (%x), read %d", \ |
| 731 | error, global_counter, global_counter, global_count); \ |
| 732 | eject = 1; \ |
| 733 | continue; \ |
| 734 | } \ |
| 735 | else \ |
| 736 | { \ |
| 737 | global_counter += global_count; \ |
| 738 | sh = minibuf[0] + minibuf[1] * 256; \ |
| 739 | } \ |
| 740 | } while (0) |
| 741 | |
| 742 | #define getn(n,error) \ |
| 743 | do \ |
| 744 | { \ |
| 745 | if (!(global_count = fread(buf, 1, n, fp_r))) \ |
| 746 | { \ |
| 747 | fprintf(stderr, "%s at %d (%x), read %d", \ |
| 748 | error, global_counter, global_counter, global_count); \ |
| 749 | eject = 1; \ |
| 750 | continue; \ |
| 751 | } \ |
| 752 | else \ |
| 753 | global_counter += global_count; \ |
| 754 | } while (0) |
| 755 | |
| 756 | #define getnoff(n,offset,error) \ |
| 757 | do \ |
| 758 | { \ |
| 759 | if (!(global_count = fread(buf + offset, 1, n, fp_r))) \ |
| 760 | { \ |
| 761 | fprintf(stderr, "%s at %d (%x), read %d", \ |
| 762 | error, global_counter, global_counter, global_count); \ |
| 763 | eject = 1; \ |
| 764 | continue; \ |
| 765 | } \ |
| 766 | else \ |
| 767 | global_counter += global_count; \ |
| 768 | } while (0) |
| 769 | |
| 770 | static void |
| 771 | parse_escp2_data(FILE *fp_r) |
| 772 | { |
| 773 | unsigned int i, m = 0, n = 0, c = 0; |
| 774 | int currentcolor = 0; |
| 775 | int density = 0; |
| 776 | unsigned int bandsize; |
| 777 | switch (ch) |
| 778 | { |
| 779 | case 'i': |
| 780 | get1("Error reading color.\n"); |
| 781 | currentcolor = seqcolor(ch); |
| 782 | get1("Error reading compression mode!\n"); |
| 783 | c = ch; |
| 784 | get1("Error reading bpp!\n"); |
| 785 | if (ch != pstate.bpp) |
| 786 | { |
| 787 | fprintf(stderr, "Warning! Color depth altered by ESC i.\n"); |
| 788 | pstate.bpp=ch; |
| 789 | } |
| 790 | if (pstate.bpp > 2) |
| 791 | fprintf(stderr, "Warning! Excessively deep color detected.\n"); |
| 792 | if (pstate.bpp == 0) |
| 793 | fprintf(stderr, "Warning! Zero bit pixel depth detected.\n"); |
| 794 | get2("Error reading number of horizontal dots!\n"); |
| 795 | n = (unsigned) sh * 8 / pstate.bpp; |
| 796 | get2("Error reading number of vertical dots!\n"); |
| 797 | m = (unsigned) sh; |
| 798 | density = pstate.horizontal_spacing; |
| 799 | break; |
| 800 | case '.': |
| 801 | get1("Error reading compression mode!\n"); |
| 802 | c=ch; |
| 803 | if (c > 2) |
| 804 | { |
| 805 | fprintf(stderr,"Warning! Unknown compression mode.\n"); |
| 806 | break; |
| 807 | } |
| 808 | get1("Error reading vertical density!\n"); |
| 809 | /* What should we do with the vertical density here??? */ |
| 810 | get1("Error reading horizontal density!\n"); |
| 811 | density=3600/ch; |
| 812 | get1("Error reading number of vertical dots!\n"); |
| 813 | m=ch; |
| 814 | get2("Error reading number of horizontal dots!\n"); |
| 815 | n=sh; |
| 816 | currentcolor=pstate.current_color; |
| 817 | break; |
| 818 | } |
| 819 | bandsize = m * ((n * pstate.bpp + 7) / 8); |
| 820 | if (valid_bufsize < bandsize) |
| 821 | { |
| 822 | buf = stp_realloc(buf, bandsize); |
| 823 | valid_bufsize = bandsize; |
| 824 | } |
| 825 | switch (c) |
| 826 | { |
| 827 | case 0: /* uncompressed */ |
| 828 | bufsize = bandsize; |
| 829 | getn(bufsize,"Error reading raster data!\n"); |
| 830 | update_page(buf, bufsize, m, n, currentcolor, density); |
| 831 | break; |
| 832 | case 1: /* run length encoding */ |
| 833 | i = 0; |
| 834 | while (!eject && (i < bandsize)) |
| 835 | { |
| 836 | get1("Error reading global_counter!\n"); |
| 837 | if (ch < 128) |
| 838 | { |
| 839 | bufsize = ch + 1; |
| 840 | getnoff(bufsize, i, "Error reading RLE raster data!\n"); |
| 841 | } |
| 842 | else |
| 843 | { |
| 844 | bufsize = 257 - (unsigned int) ch; |
| 845 | get1("Error reading compressed RLE raster data!\n"); |
| 846 | memset(buf + i, ch, bufsize); |
| 847 | } |
| 848 | i += bufsize; |
| 849 | } |
| 850 | if (i != bandsize) |
| 851 | { |
| 852 | fprintf(stderr, "Error decoding RLE data.\n"); |
| 853 | fprintf(stderr, "Total bufsize %d, expected %d\n", |
| 854 | i, bandsize); |
| 855 | eject = 1; |
| 856 | } |
| 857 | else |
| 858 | update_page(buf, i, m, n, currentcolor, density); |
| 859 | break; |
| 860 | case 2: /* TIFF compression */ |
| 861 | fprintf(stderr, "TIFF mode not yet supported!\n"); |
| 862 | /* FIXME: write TIFF stuff */ |
| 863 | break; |
| 864 | default: /* unknown */ |
| 865 | fprintf(stderr, "Unknown compression mode %d.\n", c); |
| 866 | break; |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | static void |
| 871 | parse_escp2_extended(FILE *fp_r) |
| 872 | { |
| 873 | int unit_base; |
| 874 | int i; |
| 875 | |
| 876 | get1("Corrupt file. Incomplete extended command.\n"); |
| 877 | if (eject) |
| 878 | return; |
| 879 | get2("Corrupt file. Error reading buffer size.\n"); |
| 880 | bufsize = sh; |
| 881 | getn(bufsize, "Corrupt file. Error reading command payload.\n"); |
| 882 | /* fprintf(stderr,"Command %X bufsize %d.\n",ch,bufsize); */ |
| 883 | switch (ch) |
| 884 | { |
| 885 | case 'R': |
| 886 | if (bufsize == 8 && memcmp(buf, "\0REMOTE1", 8) == 0) |
| 887 | { |
| 888 | int rc1 = 0, rc2 = 0; |
| 889 | /* Remote mode 1 */ |
| 890 | do |
| 891 | { |
| 892 | get1("Corrupt file. Error in remote mode.\n"); |
| 893 | rc1 = ch; |
| 894 | get1("Corrupt file. Error reading remote mode command.\n"); |
| 895 | rc2 = ch; |
| 896 | get2("Corrupt file. Error reading remote mode command size.\n"); |
| 897 | bufsize = sh; |
| 898 | if (bufsize) |
| 899 | getn(bufsize, "Corrupt file. Error reading remote mode command parameters.\n"); |
| 900 | if (rc1 == 0x1b && rc2 == 0) /* ignore quietly */ |
| 901 | ; |
| 902 | else |
| 903 | fprintf(stderr, |
| 904 | "Remote mode command `%c%c' ignored.\n", |
| 905 | rc1,rc2); |
| 906 | } |
| 907 | while (!eject && !(rc1 == 0x1b && rc2 == 0)); |
| 908 | } |
| 909 | else |
| 910 | { |
| 911 | /* |
| 912 | fprintf(stderr,"Warning! Commands in unrecognised remote mode %s ignored.\n", buf); |
| 913 | */ |
| 914 | do |
| 915 | { |
| 916 | while((!eject) && (ch!=0x1b)) |
| 917 | get1("Error in remote mode.\n"); |
| 918 | get1("Error reading remote mode terminator\n"); |
| 919 | } |
| 920 | while ((!eject) && (ch != 0)); |
| 921 | } |
| 922 | break; |
| 923 | case 'G': /* select graphics mode */ |
| 924 | /* FIXME: this is supposed to have more side effects */ |
| 925 | pstate.printer_weave = 0; |
| 926 | pstate.dotsize = 0; |
| 927 | pstate.bpp = 1; |
| 928 | break; |
| 929 | case 'U': /* set page units */ |
| 930 | switch (bufsize) |
| 931 | { |
| 932 | case 1: |
| 933 | pstate.page_management_units = |
| 934 | pstate.absolute_horizontal_units = |
| 935 | pstate.relative_horizontal_units = |
| 936 | pstate.relative_vertical_units = |
| 937 | pstate.horizontal_spacing = |
| 938 | pstate.absolute_vertical_units = 3600 / buf[0]; |
| 939 | if (pstate.page_management_units < 720) |
| 940 | pstate.extraskip = 1; |
| 941 | fprintf(stderr, "Setting units to 1/%d\n", |
| 942 | pstate.absolute_horizontal_units); |
| 943 | break; |
| 944 | case 5: |
| 945 | unit_base = buf[4] * 256 + buf[3]; |
| 946 | pstate.extraskip=1; |
| 947 | pstate.page_management_units= unit_base / buf[0]; |
| 948 | pstate.relative_vertical_units = |
| 949 | pstate.absolute_vertical_units = unit_base/buf[1]; |
| 950 | pstate.relative_horizontal_units = |
| 951 | pstate.horizontal_spacing = |
| 952 | pstate.absolute_horizontal_units = unit_base / buf[2]; |
| 953 | fprintf(stderr, "Setting page management units to 1/%d\n", |
| 954 | pstate.page_management_units); |
| 955 | fprintf(stderr, "Setting vertical units to 1/%d\n", |
| 956 | pstate.relative_vertical_units); |
| 957 | fprintf(stderr, "Setting horizontal units to 1/%d\n", |
| 958 | pstate.relative_horizontal_units); |
| 959 | break; |
| 960 | } |
| 961 | break; |
| 962 | case 'i': /* set printer weave mode */ |
| 963 | if (bufsize != 1) |
| 964 | fprintf(stderr,"Malformed printer weave setting command.\n"); |
| 965 | else |
| 966 | pstate.printer_weave = buf[0] % 0x30; |
| 967 | break; |
| 968 | case 'e': /* set dot size */ |
| 969 | if ((bufsize != 2) || (buf[0] != 0)) |
| 970 | fprintf(stderr,"Malformed dotsize setting command.\n"); |
| 971 | else if (pstate.got_graphics) |
| 972 | fprintf(stderr,"Changing dotsize while printing not supported.\n"); |
| 973 | else |
| 974 | { |
| 975 | pstate.dotsize = buf[1]; |
| 976 | if (pstate.dotsize > 0x10) |
| 977 | pstate.bpp = 2; |
| 978 | else |
| 979 | pstate.bpp = 1; |
| 980 | } |
| 981 | fprintf(stderr, "Setting dot size to 0x%x (bits %d)\n", |
| 982 | pstate.dotsize, pstate.bpp); |
| 983 | break; |
| 984 | case 'c': /* set page format */ |
| 985 | if (page) |
| 986 | { |
| 987 | fprintf(stderr,"Setting the page format in the middle of printing a page is not supported.\n"); |
| 988 | break; |
| 989 | } |
| 990 | switch (bufsize) |
| 991 | { |
| 992 | case 4: |
| 993 | pstate.top_margin = buf[1] * 256 + buf[0]; |
| 994 | pstate.bottom_margin = buf[3] * 256 + buf[2]; |
| 995 | break; |
| 996 | case 8: |
| 997 | pstate.top_margin = (buf[3] << 24) + |
| 998 | (buf[2] << 16) + (buf[1] << 8) + buf[0]; |
| 999 | pstate.bottom_margin = (buf[7] << 24) + |
| 1000 | (buf[6] << 16) + (buf[5] << 8) + buf[4]; |
| 1001 | break; |
| 1002 | default: |
| 1003 | fprintf(stderr,"Malformed page format. Ignored.\n"); |
| 1004 | } |
| 1005 | pstate.yposition = 0; |
| 1006 | if (pstate.top_margin + pstate.bottom_margin > pstate.page_height) |
| 1007 | pstate.page_height = pstate.top_margin + pstate.bottom_margin; |
| 1008 | fprintf(stderr, "Setting top margin to %d (%.3f)\n", |
| 1009 | pstate.top_margin, |
| 1010 | (double) pstate.top_margin / pstate.page_management_units); |
| 1011 | fprintf(stderr, "Setting bottom margin to %d (%.3f)\n", |
| 1012 | pstate.bottom_margin, |
| 1013 | (double) pstate.bottom_margin / pstate.page_management_units); |
| 1014 | page = (line_type **) |
| 1015 | stp_zalloc((pstate.bottom_margin - pstate.top_margin) * |
| 1016 | sizeof(line_type *)); |
| 1017 | break; |
| 1018 | case 'V': /* set absolute vertical position */ |
| 1019 | i = 0; |
| 1020 | switch (bufsize) |
| 1021 | { |
| 1022 | case 4: |
| 1023 | i = (buf[2] << 16) + (buf[3]<<24); |
| 1024 | /* FALLTHROUGH */ |
| 1025 | case 2: |
| 1026 | i += (buf[0]) + (256 * buf[1]); |
| 1027 | if (pstate.top_margin + i * (pstate.relative_vertical_units / |
| 1028 | pstate.absolute_vertical_units) >= |
| 1029 | pstate.yposition) |
| 1030 | pstate.yposition = i * (pstate.relative_vertical_units / |
| 1031 | pstate.absolute_vertical_units) + |
| 1032 | pstate.top_margin; |
| 1033 | else |
| 1034 | fprintf(stderr, "Warning: Setting Y position in negative direction ignored\n"); |
| 1035 | break; |
| 1036 | default: |
| 1037 | fprintf(stderr, "Malformed absolute vertical position set.\n"); |
| 1038 | } |
| 1039 | if (pstate.yposition > pstate.bottom_margin - pstate.top_margin) |
| 1040 | { |
| 1041 | fprintf(stderr, |
| 1042 | "Warning! Printer head moved past bottom margin. Dumping output and exiting.\n"); |
| 1043 | eject = 1; |
| 1044 | } |
| 1045 | break; |
| 1046 | case 'v': /* set relative vertical position */ |
| 1047 | i = 0; |
| 1048 | switch (bufsize) |
| 1049 | { |
| 1050 | case 4: |
| 1051 | i = (buf[2] << 16) + (buf[3] << 24); |
| 1052 | /* FALLTHROUGH */ |
| 1053 | case 2: |
| 1054 | i += (buf[0]) + (256 * buf[1]); |
| 1055 | if (unweave) |
| 1056 | i = pstate.nozzles; |
| 1057 | pstate.yposition+=i; |
| 1058 | break; |
| 1059 | default: |
| 1060 | fprintf(stderr,"Malformed relative vertical position set.\n"); |
| 1061 | } |
| 1062 | if (pstate.yposition > pstate.bottom_margin - pstate.top_margin) |
| 1063 | { |
| 1064 | fprintf(stderr,"Warning! Printer head moved past bottom margin. Dumping output and exiting.\n"); |
| 1065 | eject = 1; |
| 1066 | } |
| 1067 | break; |
| 1068 | case 'K': |
| 1069 | if (bufsize!=2) |
| 1070 | fprintf(stderr,"Malformed monochrome/color mode selection.\n"); |
| 1071 | else if (buf[0]) |
| 1072 | fprintf(stderr,"Non-zero first byte in monochrome selection command. Ignored.\n"); |
| 1073 | else if (buf[0] > 0x02) |
| 1074 | fprintf(stderr,"Unknown color mode 0x%X.\n",buf[1]); |
| 1075 | else |
| 1076 | pstate.monomode = buf[1]; |
| 1077 | |
| 1078 | break; |
| 1079 | case 's': /* Set print speed */ |
| 1080 | case 'm': /* Set paper type */ |
| 1081 | break; |
| 1082 | case 'S': /* set paper dimensions */ |
| 1083 | switch (bufsize) |
| 1084 | { |
| 1085 | case 4: |
| 1086 | i = (buf[1] << 16) + buf[0]; |
| 1087 | fprintf(stderr, "Setting paper width to %d (%.3f)\n", i, |
| 1088 | (double) i / pstate.page_management_units); |
| 1089 | i = (buf[3] << 16) + buf[2]; |
| 1090 | fprintf(stderr, "Setting paper height to %d (%.3f)\n", i, |
| 1091 | (double) i / pstate.page_management_units); |
| 1092 | break; |
| 1093 | case 8: |
| 1094 | i = (buf[3] << 24) + (buf[2] << 16) + (buf[1] << 8) + buf[0]; |
| 1095 | fprintf(stderr, "Setting paper width to %d (%.3f)\n", i, |
| 1096 | (double) i / pstate.page_management_units); |
| 1097 | i = (buf[7] << 24) + (buf[6] << 16) + (buf[5] << 8) + buf[4]; |
| 1098 | fprintf(stderr, "Setting paper height to %d (%.3f)\n", i, |
| 1099 | (double) i / pstate.page_management_units); |
| 1100 | break; |
| 1101 | default: |
| 1102 | fprintf(stderr, "Invalid set paper dimensions command.\n"); |
| 1103 | } |
| 1104 | break; |
| 1105 | case 'D': |
| 1106 | if (bufsize != 4) |
| 1107 | fprintf(stderr, "Malformed set resolution request.\n"); |
| 1108 | else |
| 1109 | { |
| 1110 | int res_base = (256 * buf[1]) + buf[0]; |
| 1111 | pstate.nozzle_separation = |
| 1112 | pstate.absolute_vertical_units / (res_base / buf[2]); |
| 1113 | pstate.horizontal_spacing = res_base / buf[3]; |
| 1114 | fprintf(stderr, "Setting nozzle separation to %d\n", |
| 1115 | pstate.nozzle_separation); |
| 1116 | fprintf(stderr, "Setting vertical spacing to 1/%d\n", |
| 1117 | res_base / buf[2]); |
| 1118 | fprintf(stderr, "Setting horizontal spacing to 1/%d\n", |
| 1119 | pstate.horizontal_spacing); |
| 1120 | } |
| 1121 | break; |
| 1122 | case 'r': /* select color */ |
| 1123 | if (bufsize!=2) |
| 1124 | fprintf(stderr,"Malformed color selection request.\n"); |
| 1125 | else |
| 1126 | { |
| 1127 | sh = 256 * buf[0] + buf[1]; |
| 1128 | if ((buf[1] > 4) || (buf[1] == 3) || (buf[0] > 1) || |
| 1129 | (buf[0] && (buf[1]==0))) |
| 1130 | fprintf(stderr,"Invalid color 0x%X.\n",sh); |
| 1131 | else |
| 1132 | pstate.current_color = seqcolor(sh); |
| 1133 | } |
| 1134 | break; |
| 1135 | case '\\': /* set relative horizontal position */ |
| 1136 | case '/': |
| 1137 | i = (buf[3] << 8) + buf[2]; |
| 1138 | if (pstate.xposition + i < 0) |
| 1139 | { |
| 1140 | fprintf(stderr,"Warning! Attempt to move to -X region ignored.\n"); |
| 1141 | fprintf(stderr," Command: ESC ( %c %X %X %X %X Original " |
| 1142 | "position: %d\n", |
| 1143 | ch, buf[0], buf[1], buf[2], buf[3], pstate.xposition); |
| 1144 | } |
| 1145 | else /* FIXME: Where is the right margin??? */ |
| 1146 | pstate.xposition+=i; |
| 1147 | break; |
| 1148 | case '$': /* set absolute horizontal position */ |
| 1149 | i = (buf[3] << 24) + (buf[2] << 16) + (buf[1] << 8) + buf[0]; |
| 1150 | pstate.xposition = i * (pstate.relative_horizontal_units / |
| 1151 | pstate.absolute_horizontal_units); |
| 1152 | break; |
| 1153 | case 'C': /* set page height */ |
| 1154 | switch (bufsize) |
| 1155 | { |
| 1156 | case 2: |
| 1157 | i = (buf[1] << 8) | buf[0]; |
| 1158 | fprintf(stderr, "Setting page height to %d (%.3f)\n", i, |
| 1159 | (double) i / pstate.page_management_units); |
| 1160 | break; |
| 1161 | case 4: |
| 1162 | i = (buf[3] << 24) + (buf[2] << 16) + (buf[1] << 8) + buf[0]; |
| 1163 | fprintf(stderr, "Setting page height to %d (%.3f)\n", i, |
| 1164 | (double) i / pstate.page_management_units); |
| 1165 | break; |
| 1166 | default: |
| 1167 | fprintf(stderr, "Invalid set page height command.\n"); |
| 1168 | } |
| 1169 | break; |
| 1170 | default: |
| 1171 | fprintf(stderr,"Warning: Unknown command ESC ( 0x%X at 0x%08X.\n", |
| 1172 | ch, global_counter - 5 - bufsize); |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | static void |
| 1177 | parse_escp2_command(FILE *fp_r) |
| 1178 | { |
| 1179 | get1("Corrupt file. No command found.\n"); |
| 1180 | switch (ch) |
| 1181 | { |
| 1182 | case 1: /* Magic EJL stuff to get USB port working */ |
| 1183 | fprintf(stderr,"Ignoring EJL commands.\n"); |
| 1184 | do |
| 1185 | { |
| 1186 | get1("Error reading EJL commands.\n"); |
| 1187 | } |
| 1188 | while (!eject && ch != 0x1b); |
| 1189 | if (eject) |
| 1190 | break; |
| 1191 | get1("Expect esc-NULL to close EJL command.\n"); |
| 1192 | if (ch != 0x40) |
| 1193 | fprintf(stderr, "Expect esc-NULL to close EJL command.\n"); |
| 1194 | break; |
| 1195 | case '@': /* initialize printer */ |
| 1196 | if (page) |
| 1197 | eject = 1; |
| 1198 | else |
| 1199 | { |
| 1200 | pstate.unidirectional = 0; |
| 1201 | pstate.printer_weave = 0; |
| 1202 | pstate.dotsize = 0; |
| 1203 | pstate.bpp = 1; |
| 1204 | pstate.page_management_units = 360; |
| 1205 | pstate.relative_horizontal_units = 180; |
| 1206 | pstate.absolute_horizontal_units = 60; |
| 1207 | pstate.relative_vertical_units = 360; |
| 1208 | pstate.absolute_vertical_units = 360; |
| 1209 | pstate.top_margin = 120; |
| 1210 | pstate.bottom_margin = |
| 1211 | pstate.page_height = 22 * 360; /* 22 inches is default ??? */ |
| 1212 | pstate.monomode = 0; |
| 1213 | pstate.left_edge = INT_MAX; |
| 1214 | pstate.right_edge = 0; |
| 1215 | pstate.top_edge = INT_MAX; |
| 1216 | pstate.bottom_edge = 0; |
| 1217 | } |
| 1218 | break; |
| 1219 | case 'U': /* turn unidirectional mode on/off */ |
| 1220 | get1("Error reading unidirectionality.\n"); |
| 1221 | if ((ch <= 2) || ((ch >= 0x30) && (ch <= 0x32))) |
| 1222 | pstate.unidirectional=ch; |
| 1223 | break; |
| 1224 | case 'i': /* transfer raster image */ |
| 1225 | case '.': |
| 1226 | pstate.got_graphics = 1; |
| 1227 | parse_escp2_data(fp_r); |
| 1228 | break; |
| 1229 | case '\\': /* set relative horizontal position */ |
| 1230 | get2("Error reading relative horizontal position.\n"); |
| 1231 | if (pstate.xposition + (signed short)sh < 0) |
| 1232 | { |
| 1233 | fprintf(stderr, "Warning! Move off left of region ignored.\n"); |
| 1234 | fprintf(stderr, " Command: ESC %c %X %X " |
| 1235 | "Original Position: %d\n", |
| 1236 | ch, minibuf[0], minibuf[1], pstate.xposition); |
| 1237 | } |
| 1238 | else /* FIXME: Where is the right margin??? */ |
| 1239 | pstate.xposition += (signed short)sh; |
| 1240 | break; |
| 1241 | case '$': /* set absolute horizontal position */ |
| 1242 | get2("Error reading absolute horizontal position.\n"); |
| 1243 | pstate.xposition = sh * (pstate.relative_horizontal_units / |
| 1244 | pstate.absolute_horizontal_units); |
| 1245 | break; |
| 1246 | case 0x0: /* Exit remote mode */ |
| 1247 | get2("Error exiting remote mode.\n"); |
| 1248 | break; |
| 1249 | case 0x6: /* flush buffers */ |
| 1250 | /* Woosh. Consider them flushed. */ |
| 1251 | break; |
| 1252 | case 0x19: /* control paper loading */ |
| 1253 | get1("Error reading paper control byte.\n"); |
| 1254 | /* paper? */ |
| 1255 | break; |
| 1256 | case 'r': /* select printing color */ |
| 1257 | get1("Error reading color.\n"); |
| 1258 | if ((ch <= 4) && (ch != 3)) |
| 1259 | pstate.current_color = seqcolor(ch); |
| 1260 | else |
| 1261 | fprintf(stderr, "Invalid color %d.\n", ch); |
| 1262 | break; |
| 1263 | case '(': /* commands with a payload */ |
| 1264 | parse_escp2_extended(fp_r); |
| 1265 | break; |
| 1266 | default: |
| 1267 | fprintf(stderr,"Warning: Unknown command ESC 0x%X at 0x%08X.\n",ch,global_counter-2); |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | void |
| 1272 | parse_escp2(FILE *fp_r) |
| 1273 | { |
| 1274 | global_counter = 0; |
| 1275 | |
| 1276 | while ((!eject) && (fread(&ch, 1, 1, fp_r))) |
| 1277 | { |
| 1278 | global_counter++; |
| 1279 | switch (ch) |
| 1280 | { |
| 1281 | case 0xd: /* carriage return */ |
| 1282 | pstate.xposition = 0; |
| 1283 | break; |
| 1284 | case 0xc: /* form feed */ |
| 1285 | eject = 1; |
| 1286 | break; |
| 1287 | case 0x0: |
| 1288 | break; |
| 1289 | case 0x1b: /* Command! */ |
| 1290 | parse_escp2_command(fp_r); |
| 1291 | break; |
| 1292 | default: |
| 1293 | fprintf(stderr, |
| 1294 | "Corrupt file? No ESC found. Found: %02X at 0x%08X\n", |
| 1295 | ch, global_counter-1); |
| 1296 | break; |
| 1297 | } |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | |
| 1302 | /* 'reverse_bit_order' |
| 1303 | * |
| 1304 | * reverse the bit order in an array of bytes - does not reverse byte order! |
| 1305 | */ |
| 1306 | void |
| 1307 | reverse_bit_order(unsigned char *buff, int n) |
| 1308 | { |
| 1309 | int i; |
| 1310 | unsigned char a; |
| 1311 | if (!n) return; /* nothing to do */ |
| 1312 | |
| 1313 | for (i= 0; i<n; i++) { |
| 1314 | a= buff[i]; |
| 1315 | buff[i]= |
| 1316 | (a & 0x01) << 7 | |
| 1317 | (a & 0x02) << 5 | |
| 1318 | (a & 0x04) << 3 | |
| 1319 | (a & 0x08) << 1 | |
| 1320 | (a & 0x10) >> 1 | |
| 1321 | (a & 0x20) >> 3 | |
| 1322 | (a & 0x40) >> 5 | |
| 1323 | (a & 0x80) >> 7; |
| 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | /* 'rle_decode' |
| 1328 | * |
| 1329 | * run-length-decodes a given buffer of height "n" |
| 1330 | * and stores the result in the same buffer |
| 1331 | * not exceeding a size of "max" bytes. |
| 1332 | */ |
| 1333 | int |
| 1334 | rle_decode(unsigned char *inbuf, int n, int max) |
| 1335 | { |
| 1336 | unsigned char outbuf[1440*3]; |
| 1337 | signed char *ib= (signed char *)inbuf; |
| 1338 | signed char cnt; |
| 1339 | int num; |
| 1340 | int i= 0, j; |
| 1341 | int o= 0; |
| 1342 | |
| 1343 | #ifdef DEBUG_RLE |
| 1344 | fprintf(stderr,"input: %d\n",n); |
| 1345 | #endif |
| 1346 | if (n<=0) return 0; |
| 1347 | if (max>1440*3) max= 1440*3; /* FIXME: this can be done much better! */ |
| 1348 | |
| 1349 | while (i<n && o<max) { |
| 1350 | cnt= ib[i]; |
| 1351 | if (cnt<0) { |
| 1352 | /* cnt identical bytes */ |
| 1353 | /* fprintf(stderr,"rle 0x%02x = %4d = %4d\n",cnt&0xff,cnt,1-cnt); */ |
| 1354 | num= 1-cnt; |
| 1355 | /* fprintf (stderr,"+%6d ",num); */ |
| 1356 | for (j=0; j<num && o+j<max; j++) outbuf[o+j]= inbuf[i+1]; |
| 1357 | o+= num; |
| 1358 | i+= 2; |
| 1359 | } else { |
| 1360 | /* cnt individual bytes */ |
| 1361 | /* fprintf(stderr,"raw 0x%02x = %4d = %4d\n",cnt&0xff,cnt,cnt + 1); */ |
| 1362 | num= cnt+1; |
| 1363 | /* fprintf (stderr,"*%6d ",num); */ |
| 1364 | for (j=0; j<num && o+j<max; j++) outbuf[o+j]= inbuf[i+j+1]; |
| 1365 | o+= num; |
| 1366 | i+= num+1; |
| 1367 | } |
| 1368 | } |
| 1369 | if (o>=max) { |
| 1370 | fprintf(stderr,"Warning: rle decompression exceeds output buffer.\n"); |
| 1371 | return 0; |
| 1372 | } |
| 1373 | /* copy decompressed data to inbuf: */ |
| 1374 | memset(inbuf,0,max-1); |
| 1375 | memcpy(inbuf,outbuf,o); |
| 1376 | #ifdef DEBUG_RLE |
| 1377 | fprintf(stderr,"output: %d\n",o); |
| 1378 | #endif |
| 1379 | return o; |
| 1380 | } |
| 1381 | |
| 1382 | void |
| 1383 | parse_canon(FILE *fp_r) |
| 1384 | { |
| 1385 | |
| 1386 | int m=0; |
| 1387 | int currentcolor,currentbpp,density,l_eject; |
| 1388 | // int cmdcounter; |
| 1389 | int delay_c=0, delay_m=0, delay_y=0, delay_C=0, |
| 1390 | delay_M=0, delay_Y=0, delay_K=0, currentdelay=0; |
| 1391 | |
| 1392 | global_counter=0; |
| 1393 | |
| 1394 | page= 0; |
| 1395 | l_eject=pstate.got_graphics=currentbpp=currentcolor=density=0; |
| 1396 | while ((!l_eject)&&(fread(&ch,1,1,fp_r))){ |
| 1397 | global_counter++; |
| 1398 | if (ch==0xd) { /* carriage return */ |
| 1399 | pstate.xposition=0; |
| 1400 | #ifdef DEBUG_CANON |
| 1401 | fprintf(stderr,"< "); |
| 1402 | #endif |
| 1403 | continue; |
| 1404 | } |
| 1405 | if (ch==0xc) { /* form feed */ |
| 1406 | l_eject=1; |
| 1407 | continue; |
| 1408 | } |
| 1409 | if (ch=='B') { |
| 1410 | fgets((char *)buf,sizeof(buf),fp_r); |
| 1411 | global_counter+= strlen((char *)buf); |
| 1412 | if (!strncmp((char *)buf,"JLSTART",7)) { |
| 1413 | while (strncmp((char *)buf,"BJLEND",6)) { |
| 1414 | fgets((char *)buf,sizeof(buf),fp_r); |
| 1415 | global_counter+= strlen((char *)buf); |
| 1416 | fprintf(stderr,"got BJL-plaintext-command %s",buf); |
| 1417 | } |
| 1418 | } else { |
| 1419 | fprintf(stderr,"Error: expected BJLSTART but got B%s",buf); |
| 1420 | } |
| 1421 | global_counter= ftell(fp_r); |
| 1422 | continue; |
| 1423 | } |
| 1424 | if (ch!=0x1b) { |
| 1425 | fprintf(stderr,"Corrupt file? No ESC found. Found: %02X at 0x%08X\n", |
| 1426 | ch,global_counter-1); |
| 1427 | continue; |
| 1428 | } |
| 1429 | get1("Corrupt file. No command found.\n"); |
| 1430 | /* fprintf(stderr,"Got a %X.\n",ch); */ |
| 1431 | switch (ch) { |
| 1432 | case '[': /* 0x5b initialize printer */ |
| 1433 | get1("Error reading CEM-code.\n"); |
| 1434 | // cmdcounter= global_counter; |
| 1435 | get2("Error reading CEM-data size.\n"); |
| 1436 | getn(sh,"Error reading CEM-data.\n"); |
| 1437 | |
| 1438 | if (ch=='K') /* 0x4b */ { |
| 1439 | if (sh!=2 || buf[0]!=0x00 ) { |
| 1440 | fprintf(stderr,"Error initializing printer with ESC [ K\n"); |
| 1441 | l_eject=1; |
| 1442 | continue; |
| 1443 | } |
| 1444 | if (page) { |
| 1445 | l_eject=1; |
| 1446 | continue; |
| 1447 | } else { |
| 1448 | pstate.unidirectional=0; |
| 1449 | pstate.printer_weave=0; |
| 1450 | pstate.dotsize=0; |
| 1451 | pstate.bpp=1; |
| 1452 | pstate.page_management_units=360; |
| 1453 | pstate.relative_horizontal_units=180; |
| 1454 | pstate.absolute_horizontal_units=60; |
| 1455 | pstate.relative_vertical_units=360; |
| 1456 | pstate.absolute_vertical_units=360; |
| 1457 | pstate.top_margin=120; |
| 1458 | pstate.bottom_margin= |
| 1459 | pstate.page_height=22*360; /* 22 inches is default ??? */ |
| 1460 | pstate.monomode=0; |
| 1461 | pstate.xposition= 0; |
| 1462 | pstate.yposition= 0; |
| 1463 | pstate.left_edge = INT_MAX; |
| 1464 | pstate.right_edge = 0; |
| 1465 | pstate.top_edge = INT_MAX; |
| 1466 | pstate.bottom_edge = 0; |
| 1467 | fprintf(stderr,"canon: init printer\n"); |
| 1468 | } |
| 1469 | } else { |
| 1470 | fprintf(stderr,"Warning: Unknown command ESC %c 0x%X at 0x%08X.\n", |
| 1471 | 0x5b,ch,global_counter); |
| 1472 | } |
| 1473 | break; |
| 1474 | |
| 1475 | case '@': /* 0x40 */ |
| 1476 | l_eject=1; |
| 1477 | break; |
| 1478 | |
| 1479 | case '(': /* 0x28 */ |
| 1480 | get1("Corrupt file. Incomplete extended command.\n"); |
| 1481 | // cmdcounter= global_counter; |
| 1482 | get2("Corrupt file. Error reading buffer size.\n"); |
| 1483 | bufsize=sh; |
| 1484 | getn(bufsize,"Corrupt file. Error reading data buffer.\n"); |
| 1485 | |
| 1486 | switch(ch) { |
| 1487 | /* Color Codes: |
| 1488 | color Epson1 Epson2 Sequential |
| 1489 | Black 0 0 0 K |
| 1490 | Magenta 1 1 1 M |
| 1491 | Cyan 2 2 2 C |
| 1492 | Yellow 4 4 3 Y |
| 1493 | L.Mag. 17 257 4 m |
| 1494 | L.Cyan 18 258 5 c |
| 1495 | L.Yellow NA NA 6 y |
| 1496 | */ |
| 1497 | case 'A': /* 0x41 - transfer graphics data */ |
| 1498 | switch (*buf) { |
| 1499 | case 'K': currentcolor= 0; currentdelay= delay_K; break; |
| 1500 | case 'M': currentcolor= 1; currentdelay= delay_M; break; |
| 1501 | case 'C': currentcolor= 2; currentdelay= delay_C; break; |
| 1502 | case 'Y': currentcolor= 3; currentdelay= delay_Y; break; |
| 1503 | case 'm': currentcolor= 4; currentdelay= delay_m; break; |
| 1504 | case 'c': currentcolor= 5; currentdelay= delay_c; break; |
| 1505 | case 'y': currentcolor= 6; currentdelay= delay_y; break; |
| 1506 | default: |
| 1507 | fprintf(stderr,"Error: unsupported color type 0x%02x.\n",*buf); |
| 1508 | /* exit(-1); */ |
| 1509 | } |
| 1510 | pstate.current_color= currentcolor; |
| 1511 | m= rle_decode(buf+1,bufsize-1,sizeof(buf)-1); |
| 1512 | /* reverse_bit_order(buf+1,m); */ |
| 1513 | pstate.yposition+= currentdelay; |
| 1514 | if (m) update_page(buf+1,m,1,(m*8)/pstate.bpp,currentcolor, |
| 1515 | pstate.absolute_horizontal_units); |
| 1516 | pstate.yposition-= currentdelay; |
| 1517 | #ifdef DEBUG_CANON |
| 1518 | fprintf(stderr,"%c:%d>%d ",*buf,sh-1,m); |
| 1519 | #endif |
| 1520 | break; |
| 1521 | case 'a': /* 0x61 - turn something on/off */ |
| 1522 | break; |
| 1523 | case 'b': /* 0x62 - turn something else on/off */ |
| 1524 | break; |
| 1525 | case 'c': /* 0x63 - some information about the print job */ |
| 1526 | break; |
| 1527 | case 'd': /* 0x64 - set resolution */ |
| 1528 | if (page) { |
| 1529 | fprintf(stderr,"Setting the page format in the middle of printing " |
| 1530 | "a page is not supported.\n"); |
| 1531 | exit(-1); |
| 1532 | } |
| 1533 | pstate.relative_vertical_units= |
| 1534 | pstate.absolute_vertical_units= |
| 1535 | buf[1]+256*buf[0]; |
| 1536 | pstate.relative_horizontal_units= |
| 1537 | pstate.absolute_horizontal_units= |
| 1538 | buf[3]+256*buf[2]; |
| 1539 | pstate.bottom_margin= pstate.relative_vertical_units* 22; |
| 1540 | /* FIXME: replace with real page height */ |
| 1541 | fprintf(stderr,"canon: res is %d x %d dpi\n", |
| 1542 | pstate.relative_horizontal_units, |
| 1543 | pstate.relative_vertical_units); |
| 1544 | |
| 1545 | page= (line_type **) stp_zalloc(pstate.bottom_margin * |
| 1546 | sizeof(line_type *)); |
| 1547 | break; |
| 1548 | case 'e': /* 0x65 - vertical head movement */ |
| 1549 | pstate.yposition+= (buf[1]+256*buf[0]); |
| 1550 | #ifdef DEBUG_CANON |
| 1551 | fprintf(stderr,"\n"); |
| 1552 | #endif |
| 1553 | break; |
| 1554 | case 'l': /* 0x6c - some more information about the print job*/ |
| 1555 | break; |
| 1556 | case 'm': /* 0x6d - used printheads and other things */ |
| 1557 | break; |
| 1558 | case 'p': /* 0x70 - set printable area */ |
| 1559 | break; |
| 1560 | case 'q': /* 0x71 - turn yet something else on/off */ |
| 1561 | break; |
| 1562 | case 't': /* 0x74 - contains bpp and line delaying*/ |
| 1563 | pstate.bpp= buf[0]; |
| 1564 | fprintf(stderr,"canon: using %d bpp\n",pstate.bpp); |
| 1565 | if (buf[1]&0x04) { |
| 1566 | delay_y= 0; |
| 1567 | delay_m= 0; |
| 1568 | delay_c= 0; |
| 1569 | delay_Y= 0; |
| 1570 | delay_M= delay_Y+112; |
| 1571 | delay_C= delay_M+112; |
| 1572 | delay_K= delay_C+112; |
| 1573 | fprintf(stderr,"canon: using line delay code\n"); |
| 1574 | } |
| 1575 | break; |
| 1576 | |
| 1577 | default: |
| 1578 | fprintf(stderr,"Warning: Unknown command ESC ( 0x%X at 0x%08X.\n", |
| 1579 | ch,global_counter); |
| 1580 | } |
| 1581 | break; |
| 1582 | |
| 1583 | default: |
| 1584 | fprintf(stderr,"Warning: Unknown command ESC 0x%X at 0x%08X.\n", |
| 1585 | ch,global_counter-2); |
| 1586 | } |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | int |
| 1591 | main(int argc,char *argv[]) |
| 1592 | { |
| 1593 | |
| 1594 | int arg; |
| 1595 | char *s; |
| 1596 | char *UNPRINT; |
| 1597 | FILE *fp_r, *fp_w; |
| 1598 | int force_extraskip = -1; |
| 1599 | int no_output = 0; |
| 1600 | int all_black = 0; |
| 1601 | |
| 1602 | unweave = 0; |
| 1603 | pstate.nozzle_separation = 6; |
| 1604 | fp_r = fp_w = NULL; |
| 1605 | for (arg = 1; arg < argc; arg++) |
| 1606 | { |
| 1607 | if (argv[arg][0] == '-') |
| 1608 | { |
| 1609 | switch (argv[arg][1]) |
| 1610 | { |
| 1611 | case 0: |
| 1612 | if (fp_r) |
| 1613 | fp_w = stdout; |
| 1614 | else |
| 1615 | fp_r = stdin; |
| 1616 | break; |
| 1617 | case 'h': |
| 1618 | fprintf(stderr, "Usage: %s [-m mask] [-n nozzle_sep] [-s extra] [-b] [-q] [-Q] [-M] [-u] [in [out]]\n", argv[0]); |
| 1619 | fprintf(stderr, " -m mask Color mask to unprint\n"); |
| 1620 | fprintf(stderr, " -n nozzle_sep Nozzle separation in vertical units for old printers\n"); |
| 1621 | fprintf(stderr, " -s extra Extra feed requirement for old printers (typically 1)\n"); |
| 1622 | fprintf(stderr, " -b Unprint everything in black\n"); |
| 1623 | fprintf(stderr, " -q Don't produce output\n"); |
| 1624 | fprintf(stderr, " -Q Assume quadtone inks\n"); |
| 1625 | fprintf(stderr, " -M Assume MIS quadtone inks\n"); |
| 1626 | fprintf(stderr, " -u Unweave\n"); |
| 1627 | return 1; |
| 1628 | case 'm': |
| 1629 | if (argv[arg][2]) |
| 1630 | { |
| 1631 | s = argv[arg] + 2; |
| 1632 | } |
| 1633 | else |
| 1634 | { |
| 1635 | if (argc <= arg + 1) |
| 1636 | { |
| 1637 | fprintf(stderr, "Missing color mask\n"); |
| 1638 | exit(-1); |
| 1639 | } |
| 1640 | else |
| 1641 | { |
| 1642 | s = argv[++arg]; |
| 1643 | } |
| 1644 | } |
| 1645 | if (!sscanf(s, "%x", &color_mask)) |
| 1646 | { |
| 1647 | fprintf(stderr,"Error parsing mask\n"); |
| 1648 | exit(-1); |
| 1649 | } |
| 1650 | break; |
| 1651 | case 'n': |
| 1652 | if (argv[arg][2]) |
| 1653 | { |
| 1654 | s = argv[arg] + 2; |
| 1655 | } |
| 1656 | else |
| 1657 | { |
| 1658 | if (argc <= arg + 1) |
| 1659 | { |
| 1660 | fprintf(stderr, "Missing nozzle separation\n"); |
| 1661 | exit(-1); |
| 1662 | } |
| 1663 | else |
| 1664 | { |
| 1665 | s = argv[++arg]; |
| 1666 | } |
| 1667 | } |
| 1668 | if (!sscanf(s, "%d", &pstate.nozzle_separation)) |
| 1669 | { |
| 1670 | fprintf(stderr,"Error parsing nozzle separation\n"); |
| 1671 | exit(-1); |
| 1672 | } |
| 1673 | break; |
| 1674 | case 's': |
| 1675 | if (argv[arg][2]) |
| 1676 | { |
| 1677 | s = argv[arg] + 2; |
| 1678 | } |
| 1679 | else |
| 1680 | { |
| 1681 | if (argc <= arg + 1) |
| 1682 | { |
| 1683 | fprintf(stderr,"Missing extra skip\n"); |
| 1684 | exit(-1); |
| 1685 | } |
| 1686 | else |
| 1687 | { |
| 1688 | s = argv[++arg]; |
| 1689 | } |
| 1690 | } |
| 1691 | if (!sscanf(s, "%d", &force_extraskip)) |
| 1692 | { |
| 1693 | fprintf(stderr, "Error parsing extra skip\n"); |
| 1694 | exit(-1); |
| 1695 | } |
| 1696 | break; |
| 1697 | case 'b': |
| 1698 | all_black = 1; |
| 1699 | break; |
| 1700 | case 'q': |
| 1701 | no_output = 1; |
| 1702 | break; |
| 1703 | case 'Q': |
| 1704 | pstate.quadtone = QT_QUAD; |
| 1705 | break; |
| 1706 | case 'M': |
| 1707 | pstate.quadtone = QT_MIS; |
| 1708 | break; |
| 1709 | case 'u': |
| 1710 | unweave = 1; |
| 1711 | break; |
| 1712 | } |
| 1713 | } |
| 1714 | else |
| 1715 | { |
| 1716 | if (fp_r) |
| 1717 | { |
| 1718 | if (!(fp_w = fopen(argv[arg],"wb"))) |
| 1719 | { |
| 1720 | perror("Error opening output file"); |
| 1721 | exit(-1); |
| 1722 | } |
| 1723 | } |
| 1724 | else |
| 1725 | { |
| 1726 | if (!(fp_r = fopen(argv[arg],"rb"))) |
| 1727 | { |
| 1728 | perror("Error opening input file"); |
| 1729 | exit(-1); |
| 1730 | } |
| 1731 | } |
| 1732 | } |
| 1733 | } |
| 1734 | if (!fp_r) |
| 1735 | fp_r = stdin; |
| 1736 | if (!fp_w) |
| 1737 | fp_w = stdout; |
| 1738 | |
| 1739 | if (unweave) { |
| 1740 | pstate.nozzle_separation = 1; |
| 1741 | } |
| 1742 | pstate.nozzles = 96; |
| 1743 | buf = stp_malloc(256 * 256); |
| 1744 | valid_bufsize = 256 * 256; |
| 1745 | |
| 1746 | UNPRINT = getenv("UNPRINT"); |
| 1747 | if ((UNPRINT)&&(!strcmp(UNPRINT,"canon"))) |
| 1748 | { |
| 1749 | if (force_extraskip > 0) |
| 1750 | pstate.extraskip = force_extraskip; |
| 1751 | else |
| 1752 | pstate.extraskip = 1; |
| 1753 | parse_canon(fp_r); |
| 1754 | } |
| 1755 | else |
| 1756 | { |
| 1757 | if (force_extraskip > 0) |
| 1758 | pstate.extraskip = force_extraskip; |
| 1759 | else |
| 1760 | pstate.extraskip = 2; |
| 1761 | parse_escp2(fp_r); |
| 1762 | } |
| 1763 | fprintf(stderr,"Done reading.\n"); |
| 1764 | write_output(fp_w, no_output, all_black); |
| 1765 | fclose(fp_w); |
| 1766 | fprintf(stderr,"Image dump complete.\n"); |
| 1767 | |
| 1768 | return(0); |
| 1769 | } |