| File: | xpr.c |
| Location: | line 1594, column 13 |
| Description: | The left operand of '==' is a garbage value |
| 1 | /* | |||
| 2 | ||||
| 3 | Copyright (c) 1985 X Consortium | |||
| 4 | ||||
| 5 | Permission is hereby granted, free of charge, to any person obtaining | |||
| 6 | a copy of this software and associated documentation files (the | |||
| 7 | "Software"), to deal in the Software without restriction, including | |||
| 8 | without limitation the rights to use, copy, modify, merge, publish, | |||
| 9 | distribute, sublicense, and/or sell copies of the Software, and to | |||
| 10 | permit persons to whom the Software is furnished to do so, subject to | |||
| 11 | the following conditions: | |||
| 12 | ||||
| 13 | The above copyright notice and this permission notice shall be included | |||
| 14 | in all copies or substantial portions of the Software. | |||
| 15 | ||||
| 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |||
| 17 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |||
| 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |||
| 19 | IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR | |||
| 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |||
| 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |||
| 22 | OTHER DEALINGS IN THE SOFTWARE. | |||
| 23 | ||||
| 24 | Except as contained in this notice, the name of the X Consortium shall | |||
| 25 | not be used in advertising or otherwise to promote the sale, use or | |||
| 26 | other dealings in this Software without prior written authorization | |||
| 27 | from the X Consortium. | |||
| 28 | ||||
| 29 | */ | |||
| 30 | ||||
| 31 | /* | |||
| 32 | * XPR - process xwd(1) files for various printers | |||
| 33 | * | |||
| 34 | * Author: Michael R. Gretzinger, MIT Project Athena | |||
| 35 | * | |||
| 36 | * Modified by Marvin Solomon, Univeristy of Wisconsin, to handle Apple | |||
| 37 | * Laserwriter (PostScript) devices (-device ps). | |||
| 38 | * Also accepts the -compact flag that produces more compact output | |||
| 39 | * by using run-length encoding on white (1) pixels. | |||
| 40 | * This version does not (yet) support the following options | |||
| 41 | * -append -dump -noff -nosixopt -split | |||
| 42 | * | |||
| 43 | * Changes | |||
| 44 | * Copyright 1986 by Marvin Solomon and the University of Wisconsin | |||
| 45 | * | |||
| 46 | * Permission to use, copy, modify, and distribute this | |||
| 47 | * software and its documentation for any purpose and without | |||
| 48 | * fee is hereby granted, provided that the above copyright | |||
| 49 | * notice appear in all copies and that both that copyright | |||
| 50 | * notice and this permission notice appear in supporting | |||
| 51 | * documentation, and that the names of Marvin Solomon and | |||
| 52 | * the University of Wisconsin not be used in | |||
| 53 | * advertising or publicity pertaining to distribution of the | |||
| 54 | * software without specific, written prior permission. | |||
| 55 | * Neither Marvin Solomon nor the University of Wisconsin | |||
| 56 | * makes any representations about the suitability of | |||
| 57 | * this software for any purpose. It is provided "as is" | |||
| 58 | * without express or implied warranty. | |||
| 59 | * | |||
| 60 | * Modified by Bob Scheifler for 2x2 grayscale, then ... | |||
| 61 | * Modified by Angela Bock and E. Mike Durbin, Rich Inc., to produce output | |||
| 62 | * using 2x2, 3x3, or 4x4 grayscales. This version modifies the grayscale | |||
| 63 | * conversion option of -gray to accept an input of 2, 3, or 4 to signify | |||
| 64 | * the gray level desired. The output is produced, using 5, 10, or 17-level | |||
| 65 | * gray scales, respectively. | |||
| 66 | * | |||
| 67 | * Modifications by Larry Rupp, Hewlett-Packard Company, to support HP | |||
| 68 | * LaserJet, PaintJet, and other PCL printers. Added "ljet" and "pjet" | |||
| 69 | * to devices recognized. Also added -density, -cutoff, and -noposition | |||
| 70 | * command line options. | |||
| 71 | * | |||
| 72 | */ | |||
| 73 | ||||
| 74 | #ifdef HAVE_CONFIG_H1 | |||
| 75 | # include "config.h" | |||
| 76 | #endif | |||
| 77 | ||||
| 78 | #include <X11/Xos.h> | |||
| 79 | #include <X11/Xfuncs.h> | |||
| 80 | #include <X11/Xlib.h> | |||
| 81 | #include <X11/Xutil.h> | |||
| 82 | #include <stdio.h> | |||
| 83 | #include <string.h> | |||
| 84 | #ifndef WIN32 | |||
| 85 | #include <pwd.h> | |||
| 86 | #endif | |||
| 87 | #include "lncmd.h" | |||
| 88 | #include "xpr.h" | |||
| 89 | #include <X11/XWDFile.h> | |||
| 90 | #include <X11/Xmu/SysUtil.h> | |||
| 91 | #ifndef O_BINARY0 | |||
| 92 | #define O_BINARY0 0 | |||
| 93 | #endif | |||
| 94 | ||||
| 95 | #ifdef NLS16 | |||
| 96 | #ifndef NLS | |||
| 97 | #define NLS | |||
| 98 | #endif | |||
| 99 | #endif | |||
| 100 | ||||
| 101 | #ifndef NLS | |||
| 102 | #define catgets(i, sn,mn,s)(s) (s) | |||
| 103 | #else /* NLS */ | |||
| 104 | #define NL_SETN 1 /* set number */ | |||
| 105 | #include <nl_types.h> | |||
| 106 | ||||
| 107 | nl_catd nlmsg_fd; | |||
| 108 | #endif /* NLS */ | |||
| 109 | ||||
| 110 | int debug = 0; | |||
| 111 | ||||
| 112 | #define W_MAX2400 2400 | |||
| 113 | #define H_MAX3150 3150 | |||
| 114 | #define W_MARGIN75 75 | |||
| 115 | #define H_MARGIN37 37 | |||
| 116 | #define W_PAGE2550 2550 | |||
| 117 | #define H_PAGE3225 3225 | |||
| 118 | ||||
| 119 | #ifdef NOINLINE1 | |||
| 120 | #define min(x,y)(((x)<(y))?(x):(y)) (((x)<(y))?(x):(y)) | |||
| 121 | #endif /* NOINLINE */ | |||
| 122 | ||||
| 123 | #define F_PORTRAIT1 1 | |||
| 124 | #define F_LANDSCAPE2 2 | |||
| 125 | #define F_DUMP4 4 | |||
| 126 | #define F_NOSIXOPT8 8 | |||
| 127 | #define F_APPEND16 16 | |||
| 128 | #define F_NOFF32 32 | |||
| 129 | #define F_REPORT64 64 | |||
| 130 | #define F_COMPACT128 128 | |||
| 131 | #define F_INVERT256 256 | |||
| 132 | #define F_GRAY512 512 | |||
| 133 | #define F_NPOSITION1024 1024 | |||
| 134 | #define F_SLIDE2048 2048 | |||
| 135 | ||||
| 136 | #define DEFAULT_CUTOFF((unsigned int) (0xFFFF * 0.50)) ((unsigned int) (0xFFFF * 0.50)) | |||
| 137 | ||||
| 138 | static const char *infilename = NULL((void*)0); | |||
| 139 | const char *progname = NULL((void*)0); | |||
| 140 | ||||
| 141 | struct _grayRec { | |||
| 142 | int level; | |||
| 143 | int sizeX, sizeY; /* 2x2, 3x3, 4x4 */ | |||
| 144 | const unsigned long *grayscales; /* pointer to the encoded pixels */ | |||
| 145 | }; | |||
| 146 | typedef const struct _grayRec GrayRec, *GrayPtr; | |||
| 147 | ||||
| 148 | static const unsigned long grayscale2x2[] = | |||
| 149 | {0, 1, 9, 11, 15}; | |||
| 150 | static const unsigned long grayscale3x3[] = | |||
| 151 | {0, 16, 68, 81, 325, 341, 349, 381, 383, 511}; | |||
| 152 | static const unsigned long grayscale4x4[] = | |||
| 153 | {0, 64, 4160, 4161, 20545, 21057, 23105, | |||
| 154 | 23113, 23145, 24169, 24171, 56939, 55275, 55279, | |||
| 155 | 57327, 65519, 65535}; | |||
| 156 | ||||
| 157 | static GrayRec gray2x2 = {sizeof(grayscale2x2)/sizeof(long), 2, 2, grayscale2x2}; | |||
| 158 | static GrayRec gray3x3 = {sizeof(grayscale3x3)/sizeof(long), 3, 3, grayscale3x3}; | |||
| 159 | static GrayRec gray4x4 = {sizeof(grayscale4x4)/sizeof(long), 4, 4, grayscale4x4}; | |||
| 160 | ||||
| 161 | /* mapping tables to map a byte in to the hex representation of its | |||
| 162 | * bit-reversal | |||
| 163 | */ | |||
| 164 | static const | |||
| 165 | char hex1[]="084c2a6e195d3b7f084c2a6e195d3b7f084c2a6e195d3b7f084c2a6e195d3b7f\ | |||
| 166 | 084c2a6e195d3b7f084c2a6e195d3b7f084c2a6e195d3b7f084c2a6e195d3b7f\ | |||
| 167 | 084c2a6e195d3b7f084c2a6e195d3b7f084c2a6e195d3b7f084c2a6e195d3b7f\ | |||
| 168 | 084c2a6e195d3b7f084c2a6e195d3b7f084c2a6e195d3b7f084c2a6e195d3b7f"; | |||
| 169 | ||||
| 170 | static const | |||
| 171 | char hex2[]="000000000000000088888888888888884444444444444444cccccccccccccccc\ | |||
| 172 | 2222222222222222aaaaaaaaaaaaaaaa6666666666666666eeeeeeeeeeeeeeee\ | |||
| 173 | 111111111111111199999999999999995555555555555555dddddddddddddddd\ | |||
| 174 | 3333333333333333bbbbbbbbbbbbbbbb7777777777777777ffffffffffffffff"; | |||
| 175 | ||||
| 176 | ||||
| 177 | /* Local prototypes */ | |||
| 178 | static void usage(void) _X_NORETURN__attribute((noreturn)); | |||
| 179 | static | |||
| 180 | void parse_args( | |||
| 181 | int argc, | |||
| 182 | char **argv, | |||
| 183 | int *scale, | |||
| 184 | int *width, | |||
| 185 | int *height, | |||
| 186 | int *left, | |||
| 187 | int *top, | |||
| 188 | enum device *device, | |||
| 189 | int *flags, | |||
| 190 | int *split, | |||
| 191 | char **header, | |||
| 192 | char **trailer, | |||
| 193 | int *plane, | |||
| 194 | GrayPtr *gray, | |||
| 195 | int *density, | |||
| 196 | unsigned int *cutoff, | |||
| 197 | float *gamma, | |||
| 198 | int *render); | |||
| 199 | static | |||
| 200 | void setup_layout( | |||
| 201 | enum device device, | |||
| 202 | int win_width, | |||
| 203 | int win_height, | |||
| 204 | int flags, | |||
| 205 | int width, | |||
| 206 | int height, | |||
| 207 | char *header, | |||
| 208 | char *trailer, | |||
| 209 | int *scale, | |||
| 210 | enum orientation *orientation); | |||
| 211 | static | |||
| 212 | char *convert_data( | |||
| 213 | XWDFileHeader *win, | |||
| 214 | char *data, | |||
| 215 | int plane, | |||
| 216 | GrayPtr gray, | |||
| 217 | XColor *colors, | |||
| 218 | int flags); | |||
| 219 | static | |||
| 220 | void dump_sixmap( | |||
| 221 | register unsigned char (*sixmap)[], | |||
| 222 | int iw, | |||
| 223 | int ih); | |||
| 224 | static | |||
| 225 | void build_sixmap( | |||
| 226 | int ih, | |||
| 227 | int iw, | |||
| 228 | unsigned char (*sixmap)[], | |||
| 229 | int hpad, | |||
| 230 | XWDFileHeader *win, | |||
| 231 | const char *data); | |||
| 232 | static | |||
| 233 | void ln03_setup( | |||
| 234 | int iw, | |||
| 235 | int ih, | |||
| 236 | enum orientation orientation, | |||
| 237 | int scale, | |||
| 238 | int left, | |||
| 239 | int top, | |||
| 240 | int *left_margin, | |||
| 241 | int *top_margin, | |||
| 242 | int flags, | |||
| 243 | const char *header, | |||
| 244 | const char *trailer); | |||
| 245 | static void ln03_finish(void); | |||
| 246 | static void la100_setup(int iw, int ih, int scale); | |||
| 247 | static void la100_finish(void); | |||
| 248 | static void dump_prolog(int flags); | |||
| 249 | static int points(int n); | |||
| 250 | static char *escape(const char *s); | |||
| 251 | static | |||
| 252 | void ps_setup( | |||
| 253 | int iw, | |||
| 254 | int ih, | |||
| 255 | enum orientation orientation, | |||
| 256 | int scale, | |||
| 257 | int left, | |||
| 258 | int top, | |||
| 259 | int flags, | |||
| 260 | const char *header, | |||
| 261 | const char *trailer, | |||
| 262 | const char *name); | |||
| 263 | static void ps_finish(void); | |||
| 264 | static | |||
| 265 | void ln03_output_sixels( | |||
| 266 | unsigned char (*sixmap)[], | |||
| 267 | int iw, | |||
| 268 | int ih, | |||
| 269 | int nosixopt, | |||
| 270 | int split, | |||
| 271 | int scale, | |||
| 272 | int top_margin, | |||
| 273 | int left_margin); | |||
| 274 | static void la100_output_sixels( | |||
| 275 | unsigned char (*sixmap)[], | |||
| 276 | int iw, | |||
| 277 | int ih, | |||
| 278 | int nosixopt); | |||
| 279 | static void ps_output_bits( | |||
| 280 | int iw, | |||
| 281 | int ih, | |||
| 282 | int flags, | |||
| 283 | enum orientation orientation, | |||
| 284 | XWDFileHeader *win, | |||
| 285 | const char *data); | |||
| 286 | static int ps_putbuf( | |||
| 287 | register unsigned char *s, | |||
| 288 | register int n, | |||
| 289 | register int ocount, | |||
| 290 | int compact); | |||
| 291 | static void ps_bitrot( | |||
| 292 | unsigned char *s, | |||
| 293 | register int n, | |||
| 294 | int col, | |||
| 295 | register int owidth, | |||
| 296 | char *obuf); | |||
| 297 | static void fullread ( | |||
| 298 | int file, | |||
| 299 | char *data, | |||
| 300 | int nbytes); | |||
| 301 | ||||
| 302 | int main(int argc, char **argv) | |||
| 303 | { | |||
| 304 | unsigned long swaptest = 1; | |||
| 305 | XWDFileHeader win; | |||
| 306 | register unsigned char (*sixmap)[]; | |||
| 307 | register int i; | |||
| 308 | register int iw; | |||
| 309 | register int ih; | |||
| 310 | register int sixel_count; | |||
| 311 | char *w_name; | |||
| 312 | int scale, width, height, flags, split; | |||
| 313 | int left, top; | |||
| 314 | int top_margin, left_margin; | |||
| 315 | int hpad; | |||
| 316 | char *header, *trailer; | |||
| 317 | int plane; | |||
| 318 | int density, render; | |||
| 319 | unsigned int cutoff; | |||
| 320 | float gamma; | |||
| 321 | GrayPtr gray; | |||
| 322 | char *data; | |||
| 323 | long size; | |||
| 324 | enum orientation orientation; | |||
| 325 | enum device device; | |||
| 326 | XColor *colors = (XColor *)NULL((void*)0); | |||
| 327 | ||||
| 328 | if (!(progname = argv[0])) | |||
| ||||
| 329 | progname = "xpr"; | |||
| 330 | #ifdef NLS | |||
| 331 | nlmsg_fd = catopen("xpr", 0); | |||
| 332 | #endif | |||
| 333 | parse_args (argc, argv, &scale, &width, &height, &left, &top, &device, | |||
| 334 | &flags, &split, &header, &trailer, &plane, &gray, | |||
| 335 | &density, &cutoff, &gamma, &render); | |||
| 336 | ||||
| 337 | if (device == PP) { | |||
| 338 | x2pmp(stdin__stdinp, stdout__stdoutp, scale, | |||
| 339 | width >= 0? inch2pel((float)width/300.0)((int) (((float)width/300.0) * 240)): X_MAX_PELS((int) ((8.5) * 240)), | |||
| 340 | height >= 0? inch2pel((float)height/300.0)((int) (((float)height/300.0) * 240)): Y_MAX_PELS((int) ((11) * 240)), | |||
| 341 | left >= 0? inch2pel((float)left/300.0)((int) (((float)left/300.0) * 240)): inch2pel(0.60)((int) ((0.60) * 240)), | |||
| 342 | top >= 0? inch2pel((float)top/300.0)((int) (((float)top/300.0) * 240)): inch2pel(0.70)((int) ((0.70) * 240)), | |||
| 343 | header, trailer, | |||
| 344 | (flags & F_PORTRAIT1)? PORTRAIT: | |||
| 345 | ((flags & F_LANDSCAPE2)? LANDSCAPE: UNSPECIFIED), | |||
| 346 | (flags & F_INVERT256)); | |||
| 347 | exit(0); | |||
| 348 | } else if ((device == LJET) || (device == PJET) || (device == PJETXL)) { | |||
| 349 | x2jet(stdin__stdinp, stdout__stdoutp, scale, density, width, height, left, top, | |||
| 350 | header, trailer, | |||
| 351 | (flags & F_PORTRAIT1)? PORTRAIT: | |||
| 352 | ((flags & F_LANDSCAPE2)? LANDSCAPE: UNSPECIFIED), | |||
| 353 | (flags & F_INVERT256), | |||
| 354 | ((flags & F_APPEND16) && !(flags & F_NOFF32)), | |||
| 355 | !(flags & F_NPOSITION1024), | |||
| 356 | (flags & F_SLIDE2048), | |||
| 357 | device, cutoff, gamma, render); | |||
| 358 | exit(0); | |||
| 359 | } | |||
| 360 | ||||
| 361 | /* read in window header */ | |||
| 362 | fullread(0, (char *)&win, sizeof win); | |||
| 363 | if (*(char *) &swaptest) | |||
| 364 | _swaplong((char *) &win, (long)sizeof(win)); | |||
| 365 | ||||
| 366 | if (win.file_version != XWD_FILE_VERSION7) { | |||
| 367 | fprintf(stderr__stderrp,"xpr: file format version mismatch.\n"); | |||
| 368 | exit(1); | |||
| 369 | } | |||
| 370 | if (win.header_size < sizeof(win)) { | |||
| 371 | fprintf(stderr__stderrp,"xpr: header size is too small.\n"); | |||
| 372 | exit(1); | |||
| 373 | } | |||
| 374 | ||||
| 375 | w_name = malloc((unsigned)(win.header_size - sizeof win)); | |||
| 376 | fullread(0, w_name, (int) (win.header_size - sizeof win)); | |||
| 377 | ||||
| 378 | if(win.ncolors) { | |||
| 379 | XWDColor xwdcolor; | |||
| 380 | colors = (XColor *)malloc((unsigned) (win.ncolors * sizeof(XColor))); | |||
| 381 | for (i = 0; i < win.ncolors; i++) { | |||
| 382 | fullread(0, (char*)&xwdcolor, (int) sizeof xwdcolor); | |||
| 383 | colors[i].pixel = xwdcolor.pixel; | |||
| 384 | colors[i].red = xwdcolor.red; | |||
| 385 | colors[i].green = xwdcolor.green; | |||
| 386 | colors[i].blue = xwdcolor.blue; | |||
| 387 | colors[i].flags = xwdcolor.flags; | |||
| 388 | } | |||
| 389 | if (*(char *) &swaptest) { | |||
| 390 | for (i = 0; i < win.ncolors; i++) { | |||
| 391 | _swaplong((char *) &colors[i].pixel, (long)sizeof(long)); | |||
| 392 | _swapshort((char *) &colors[i].red, (long) (3 * sizeof(short))); | |||
| 393 | } | |||
| 394 | } | |||
| 395 | if ((win.ncolors == 2) && | |||
| 396 | (INTENSITY(&colors[0])(30L*(int)(&colors[0])->red + 59L*(int)(&colors[0] )->green + 11L*(int)(&colors[0])->blue) > INTENSITY(&colors[1])(30L*(int)(&colors[1])->red + 59L*(int)(&colors[1] )->green + 11L*(int)(&colors[1])->blue))) | |||
| 397 | flags ^= F_INVERT256; | |||
| 398 | } | |||
| 399 | if (plane >= (long)win.pixmap_depth) { | |||
| 400 | fprintf(stderr__stderrp,"xpr: plane number exceeds image depth\n"); | |||
| 401 | exit(1); | |||
| 402 | } | |||
| 403 | size = win.bytes_per_line * win.pixmap_height; | |||
| 404 | if (win.pixmap_format == XYPixmap1) | |||
| 405 | size *= win.pixmap_depth; | |||
| 406 | data = malloc((unsigned)size); | |||
| 407 | fullread(0, data, (int)size); | |||
| 408 | if ((win.pixmap_depth > 1) || (win.byte_order != win.bitmap_bit_order)) { | |||
| 409 | data = convert_data(&win, data, plane, gray, colors, flags); | |||
| 410 | size = win.bytes_per_line * win.pixmap_height; | |||
| 411 | } | |||
| 412 | if (win.bitmap_bit_order == MSBFirst1) { | |||
| 413 | _swapbits((unsigned char *)data, size); | |||
| 414 | win.bitmap_bit_order = LSBFirst0; | |||
| 415 | } | |||
| 416 | if (flags & F_INVERT256) | |||
| 417 | _invbits((unsigned char *)data, size); | |||
| 418 | ||||
| 419 | /* calculate orientation and scale */ | |||
| 420 | setup_layout(device, (int) win.pixmap_width, (int) win.pixmap_height, | |||
| 421 | flags, width, height, header, trailer, &scale, &orientation); | |||
| 422 | ||||
| 423 | if (device == PS) { | |||
| 424 | iw = win.pixmap_width; | |||
| 425 | ih = win.pixmap_height; | |||
| 426 | sixmap = NULL((void*)0); | |||
| 427 | } else { | |||
| 428 | /* calculate w and h cell count */ | |||
| 429 | iw = win.pixmap_width; | |||
| 430 | ih = (win.pixmap_height + 5) / 6; | |||
| 431 | hpad = (ih * 6) - win.pixmap_height; | |||
| 432 | ||||
| 433 | /* build pixcells from input file */ | |||
| 434 | sixel_count = iw * ih; | |||
| 435 | sixmap = (unsigned char (*)[])malloc((unsigned)sixel_count); | |||
| 436 | build_sixmap(iw, ih, sixmap, hpad, &win, data); | |||
| 437 | } | |||
| 438 | ||||
| 439 | /* output commands and sixel graphics */ | |||
| 440 | if (device == LN03) { | |||
| 441 | /* ln03_grind_fonts(sixmap, iw, ih, scale, &pixmap); */ | |||
| 442 | ln03_setup(iw, ih, orientation, scale, left, top, | |||
| 443 | &left_margin, &top_margin, flags, header, trailer); | |||
| 444 | ln03_output_sixels(sixmap, iw, ih, (flags & F_NOSIXOPT8), split, | |||
| 445 | scale, top_margin, left_margin); | |||
| 446 | ln03_finish(); | |||
| 447 | } else if (device == LA100) { | |||
| 448 | la100_setup(iw, ih, scale); | |||
| 449 | la100_output_sixels(sixmap, iw, ih, (flags & F_NOSIXOPT8)); | |||
| 450 | la100_finish(); | |||
| 451 | } else if (device == PS) { | |||
| 452 | ps_setup(iw, ih, orientation, scale, left, top, | |||
| 453 | flags, header, trailer, w_name); | |||
| 454 | ps_output_bits(iw, ih, flags, orientation, &win, data); | |||
| 455 | ps_finish(); | |||
| 456 | } else { | |||
| 457 | fprintf(stderr__stderrp, "xpr: device not supported\n"); | |||
| 458 | } | |||
| 459 | ||||
| 460 | /* print some statistics */ | |||
| 461 | if (flags & F_REPORT64) { | |||
| 462 | fprintf(stderr__stderrp, "Name: %s\n", w_name); | |||
| 463 | fprintf(stderr__stderrp, "Width: %d, Height: %d\n", (int)win.pixmap_width, | |||
| 464 | (int)win.pixmap_height); | |||
| 465 | fprintf(stderr__stderrp, "Orientation: %s, Scale: %d\n", | |||
| 466 | (orientation==PORTRAIT) ? "Portrait" : "Landscape", scale); | |||
| 467 | } | |||
| 468 | if (((device == LN03) || (device == LA100)) && (flags & F_DUMP4)) | |||
| 469 | dump_sixmap(sixmap, iw, ih); | |||
| 470 | exit(EXIT_SUCCESS0); | |||
| 471 | } | |||
| 472 | ||||
| 473 | static void _X_NORETURN__attribute((noreturn)) _X_COLD__attribute__((__cold__)) | |||
| 474 | invalid_arg_value(const char *arg, const char *value) | |||
| 475 | { | |||
| 476 | fprintf (stderr__stderrp, "%s: %s is not a valid value for %s\n\n", | |||
| 477 | progname, value, arg); | |||
| 478 | usage(); | |||
| 479 | } | |||
| 480 | ||||
| 481 | static void _X_NORETURN__attribute((noreturn)) _X_COLD__attribute__((__cold__)) | |||
| 482 | missing_arg(const char *arg) | |||
| 483 | { | |||
| 484 | fprintf (stderr__stderrp, "%s: %s requires an argument\n\n", progname, arg); | |||
| 485 | usage(); | |||
| 486 | } | |||
| 487 | ||||
| 488 | static void _X_NORETURN__attribute((noreturn)) _X_COLD__attribute__((__cold__)) | |||
| 489 | unknown_arg(const char *arg) | |||
| 490 | { | |||
| 491 | fprintf (stderr__stderrp, "%s: unrecognized argument %s\n\n", progname, arg); | |||
| 492 | usage(); | |||
| 493 | } | |||
| 494 | ||||
| 495 | static void _X_NORETURN__attribute((noreturn)) _X_COLD__attribute__((__cold__)) | |||
| 496 | usage(void) | |||
| 497 | { | |||
| 498 | fprintf(stderr__stderrp, "usage: %s [options] [file]\n%s", progname, | |||
| 499 | " -append <file> -noff -output <file>\n" | |||
| 500 | " -compact\n" | |||
| 501 | " -device {ln03 | la100 | ps | lw | pp | ljet | pjet | pjetxl}\n" | |||
| 502 | " -dump\n" | |||
| 503 | " -gamma <correction>\n" | |||
| 504 | " -gray {2 | 3 | 4}\n" | |||
| 505 | " -height <inches> -width <inches>\n" | |||
| 506 | " -header <string> -trailer <string>\n" | |||
| 507 | " -landscape -portrait\n" | |||
| 508 | " -left <inches> -top <inches>\n" | |||
| 509 | " -noposition\n" | |||
| 510 | " -nosixopt\n" | |||
| 511 | " -plane <n>\n" | |||
| 512 | " -psfig\n" | |||
| 513 | " -render <type>\n" | |||
| 514 | " -report\n" | |||
| 515 | " -rv\n" | |||
| 516 | " -scale <scale>\n" | |||
| 517 | " -slide\n" | |||
| 518 | " -split <n-pages>\n" | |||
| 519 | " -version\n" | |||
| 520 | ); | |||
| 521 | exit(EXIT_FAILURE1); | |||
| 522 | } | |||
| 523 | ||||
| 524 | static | |||
| 525 | void parse_args( | |||
| 526 | int argc, | |||
| 527 | char **argv, | |||
| 528 | int *scale, | |||
| 529 | int *width, | |||
| 530 | int *height, | |||
| 531 | int *left, | |||
| 532 | int *top, | |||
| 533 | enum device *device, | |||
| 534 | int *flags, | |||
| 535 | int *split, | |||
| 536 | char **header, | |||
| 537 | char **trailer, | |||
| 538 | int *plane, | |||
| 539 | GrayPtr *gray, | |||
| 540 | int *density, | |||
| 541 | unsigned int *cutoff, | |||
| 542 | float *gamma, | |||
| 543 | int *render) | |||
| 544 | { | |||
| 545 | register char *output_filename; | |||
| 546 | register int f; | |||
| 547 | register int pos; | |||
| 548 | ||||
| 549 | output_filename = NULL((void*)0); | |||
| 550 | *device = PS; /* default */ | |||
| 551 | *flags = 0; | |||
| 552 | *scale = 0; | |||
| 553 | *split = 1; | |||
| 554 | *width = -1; | |||
| 555 | *height = -1; | |||
| 556 | *top = -1; | |||
| 557 | *left = -1; | |||
| 558 | *header = NULL((void*)0); | |||
| 559 | *trailer = NULL((void*)0); | |||
| 560 | *plane = -1; | |||
| 561 | *gray = (GrayPtr)NULL((void*)0); | |||
| 562 | *density = 0; | |||
| 563 | *cutoff = DEFAULT_CUTOFF((unsigned int) (0xFFFF * 0.50)); | |||
| 564 | *gamma = -1.0; | |||
| 565 | *render = 0; | |||
| 566 | ||||
| 567 | for (argc--, argv++; argc > 0; argc--, argv++) { | |||
| 568 | const char *arg = argv[0]; | |||
| 569 | if (argv[0][0] != '-') { | |||
| 570 | infilename = *argv; | |||
| 571 | continue; | |||
| 572 | } | |||
| 573 | if (!strcmp(*argv, "-append")) { | |||
| 574 | argc--; argv++; | |||
| 575 | if (argc == 0) missing_arg(arg); | |||
| 576 | output_filename = *argv; | |||
| 577 | *flags |= F_APPEND16; | |||
| 578 | } else if (!strcmp(*argv, "-compact")) { | |||
| 579 | *flags |= F_COMPACT128; | |||
| 580 | } else if (!strcmp(*argv, "-cutoff")) { | |||
| 581 | argc--; argv++; | |||
| 582 | if (argc == 0) missing_arg(arg); | |||
| 583 | *cutoff = min((atof(*argv) / 100.0 * 0xFFFF), 0xFFFF)((((atof(*argv) / 100.0 * 0xFFFF))<(0xFFFF))?((atof(*argv) / 100.0 * 0xFFFF)):(0xFFFF)); | |||
| 584 | } else if (!strcmp(*argv, "-dump")) { | |||
| 585 | *flags |= F_DUMP4; | |||
| 586 | } else if (!strcmp(*argv, "-density")) { | |||
| 587 | argc--; argv++; | |||
| 588 | if (argc == 0) missing_arg(arg); | |||
| 589 | *density = atoi(*argv); | |||
| 590 | } else if (!strcmp(*argv, "-device")) { | |||
| 591 | argc--; argv++; | |||
| 592 | if (argc == 0) missing_arg(arg); | |||
| 593 | if (!strcmp(*argv, "ln03")) { | |||
| 594 | *device = LN03; | |||
| 595 | } else if (!strcmp(*argv, "la100")) { | |||
| 596 | *device = LA100; | |||
| 597 | } else if (!strcmp(*argv, "ps")) { | |||
| 598 | *device = PS; | |||
| 599 | } else if (!strcmp(*argv, "lw")) { | |||
| 600 | *device = PS; | |||
| 601 | } else if (!strcmp(*argv, "pp")) { | |||
| 602 | *device = PP; | |||
| 603 | } else if (!strcmp(*argv, "ljet")) { | |||
| 604 | *device = LJET; | |||
| 605 | } else if (!strcmp(*argv, "pjet")) { | |||
| 606 | *device = PJET; | |||
| 607 | } else if (!strcmp(*argv, "pjetxl")) { | |||
| 608 | *device = PJETXL; | |||
| 609 | } else | |||
| 610 | invalid_arg_value(arg, argv[0]); | |||
| 611 | } else if (!strcmp(*argv, "-gamma")) { | |||
| 612 | argc--; argv++; | |||
| 613 | if (argc == 0) missing_arg(arg); | |||
| 614 | *gamma = atof(*argv); | |||
| 615 | } else if (!strcmp(*argv, "-gray") || | |||
| 616 | !strcmp(*argv, "-grey")) { | |||
| 617 | argc--; argv++; | |||
| 618 | if (argc == 0) missing_arg(arg); | |||
| 619 | switch (atoi(*argv)) { | |||
| 620 | case 2: | |||
| 621 | *gray = &gray2x2; | |||
| 622 | break; | |||
| 623 | case 3: | |||
| 624 | *gray = &gray3x3; | |||
| 625 | break; | |||
| 626 | case 4: | |||
| 627 | *gray = &gray4x4; | |||
| 628 | break; | |||
| 629 | default: | |||
| 630 | invalid_arg_value(arg, argv[0]); | |||
| 631 | } | |||
| 632 | *flags |= F_GRAY512; | |||
| 633 | } else if (!strcmp(*argv, "-height")) { | |||
| 634 | argc--; argv++; | |||
| 635 | if (argc == 0) missing_arg(arg); | |||
| 636 | *height = (int)(300.0 * atof(*argv)); | |||
| 637 | } else if (!strcmp(*argv, "-header")) { | |||
| 638 | argc--; argv++; | |||
| 639 | if (argc == 0) missing_arg(arg); | |||
| 640 | *header = *argv; | |||
| 641 | } else if (!strcmp(*argv, "-landscape")) { | |||
| 642 | *flags |= F_LANDSCAPE2; | |||
| 643 | } else if (!strcmp(*argv, "-left")) { | |||
| 644 | argc--; argv++; | |||
| 645 | if (argc == 0) missing_arg(arg); | |||
| 646 | *left = (int)(300.0 * atof(*argv)); | |||
| 647 | } else if (!strcmp(*argv, "-nosixopt")) { | |||
| 648 | *flags |= F_NOSIXOPT8; | |||
| 649 | } else if (!strcmp(*argv, "-noff")) { | |||
| 650 | *flags |= F_NOFF32; | |||
| 651 | } else if (!strcmp(*argv, "-noposition")) { | |||
| 652 | *flags |= F_NPOSITION1024; | |||
| 653 | } else if (!strcmp(*argv, "-output")) { | |||
| 654 | argc--; argv++; | |||
| 655 | if (argc == 0) missing_arg(arg); | |||
| 656 | output_filename = *argv; | |||
| 657 | } else if (!strcmp(*argv, "-portrait")) { | |||
| 658 | *flags |= F_PORTRAIT1; | |||
| 659 | } else if (!strcmp(*argv, "-plane")) { | |||
| 660 | argc--; argv++; | |||
| 661 | if (argc == 0) missing_arg(arg); | |||
| 662 | *plane = atoi(*argv); | |||
| 663 | } else if (!strcmp(*argv, "-psfig")) { | |||
| 664 | *flags |= F_NPOSITION1024; | |||
| 665 | } else if (!strcmp(*argv, "-rv")) { | |||
| 666 | *flags |= F_INVERT256; | |||
| 667 | } else if (!strcmp(*argv, "-render")) { | |||
| 668 | argc--; argv++; | |||
| 669 | if (argc == 0) missing_arg(arg); | |||
| 670 | *render = atoi(*argv); | |||
| 671 | } else if (!strcmp(*argv, "-report")) { | |||
| 672 | *flags |= F_REPORT64; | |||
| 673 | } else if (!strcmp(*argv, "-scale")) { | |||
| 674 | argc--; argv++; | |||
| 675 | if (argc == 0) missing_arg(arg); | |||
| 676 | *scale = atoi(*argv); | |||
| 677 | } else if (!strcmp(*argv, "-slide")) { | |||
| 678 | *flags |= F_SLIDE2048; | |||
| 679 | } else if (!strcmp(*argv, "-split")) { | |||
| 680 | argc--; argv++; | |||
| 681 | if (argc == 0) missing_arg(arg); | |||
| 682 | *split = atoi(*argv); | |||
| 683 | } else if (!strcmp(*argv, "-top")) { | |||
| 684 | argc--; argv++; | |||
| 685 | if (argc == 0) missing_arg(arg); | |||
| 686 | *top = (int)(300.0 * atof(*argv)); | |||
| 687 | } else if (!strcmp(*argv, "-trailer")) { | |||
| 688 | argc--; argv++; | |||
| 689 | if (argc == 0) missing_arg(arg); | |||
| 690 | *trailer = *argv; | |||
| 691 | } else if (strcmp(*argv, "-version") == 0) { | |||
| 692 | puts(PACKAGE_STRING"xpr 1.0.4"); | |||
| 693 | exit(0); | |||
| 694 | } else if (!strcmp(*argv, "-width")) { | |||
| 695 | argc--; argv++; | |||
| 696 | if (argc == 0) missing_arg(arg); | |||
| 697 | *width = (int)(300.0 * atof(*argv)); | |||
| 698 | } else | |||
| 699 | unknown_arg(arg); | |||
| 700 | } | |||
| 701 | ||||
| 702 | if (infilename) { | |||
| 703 | f = open(infilename, O_RDONLY0x0000|O_BINARY0, 0); | |||
| 704 | if (f < 0) { | |||
| 705 | fprintf(stderr__stderrp, "xpr: error opening \"%s\" for input\n", | |||
| 706 | infilename); | |||
| 707 | perror(""); | |||
| 708 | exit(1); | |||
| 709 | } | |||
| 710 | dup2(f, 0); | |||
| 711 | close(f); | |||
| 712 | } else | |||
| 713 | infilename = "stdin"; | |||
| 714 | ||||
| 715 | if (output_filename != NULL((void*)0)) { | |||
| 716 | if (!(*flags & F_APPEND16)) { | |||
| 717 | f = open(output_filename, O_CREAT0x0200|O_WRONLY0x0001|O_TRUNC0x0400, 0664); | |||
| 718 | } else { | |||
| 719 | f = open(output_filename, O_WRONLY0x0001, 0); | |||
| 720 | } | |||
| 721 | if (f < 0) { | |||
| 722 | fprintf(stderr__stderrp, "xpr: error opening \"%s\" for output\n", | |||
| 723 | output_filename); | |||
| 724 | perror("xpr"); | |||
| 725 | exit(1); | |||
| 726 | } | |||
| 727 | if (*flags & F_APPEND16) { | |||
| 728 | pos = lseek(f, 0, 2); /* get eof position */ | |||
| 729 | if ((*flags & F_NOFF32) && | |||
| 730 | !(*device == LJET || *device == PJET || *device == PJETXL)) | |||
| 731 | pos -= 3; /* set position before trailing */ | |||
| 732 | /* formfeed and reset */ | |||
| 733 | lseek(f, pos, 0); /* set pointer */ | |||
| 734 | } | |||
| 735 | dup2(f, 1); | |||
| 736 | close(f); | |||
| 737 | } | |||
| 738 | } | |||
| 739 | ||||
| 740 | static | |||
| 741 | void setup_layout( | |||
| 742 | enum device device, | |||
| 743 | int win_width, | |||
| 744 | int win_height, | |||
| 745 | int flags, | |||
| 746 | int width, | |||
| 747 | int height, | |||
| 748 | char *header, | |||
| 749 | char *trailer, | |||
| 750 | int *scale, | |||
| 751 | enum orientation *orientation) | |||
| 752 | { | |||
| 753 | register int w_scale; | |||
| 754 | register int h_scale; | |||
| 755 | register int iscale = *scale; | |||
| 756 | register int w_max; | |||
| 757 | register int h_max; | |||
| 758 | ||||
| 759 | if (header != NULL((void*)0)) win_height += 75; | |||
| 760 | if (trailer != NULL((void*)0)) win_height += 75; | |||
| 761 | ||||
| 762 | /* check maximum width and height; set orientation and scale*/ | |||
| 763 | if (device == LN03 || device == PS) { | |||
| 764 | if ((win_width < win_height || (flags & F_PORTRAIT1)) && | |||
| 765 | !(flags & F_LANDSCAPE2)) { | |||
| 766 | *orientation = PORTRAIT; | |||
| 767 | w_max = (width > 0)? width : W_MAX2400; | |||
| 768 | h_max = (height > 0)? height : H_MAX3150; | |||
| 769 | w_scale = w_max / win_width; | |||
| 770 | h_scale = h_max / win_height; | |||
| 771 | *scale = min(w_scale, h_scale)(((w_scale)<(h_scale))?(w_scale):(h_scale)); | |||
| 772 | } else { | |||
| 773 | *orientation = LANDSCAPE; | |||
| 774 | w_max = (width > 0)? width : H_MAX3150; | |||
| 775 | h_max = (height > 0)? height : W_MAX2400; | |||
| 776 | w_scale = w_max / win_width; | |||
| 777 | h_scale = h_max / win_height; | |||
| 778 | *scale = min(w_scale, h_scale)(((w_scale)<(h_scale))?(w_scale):(h_scale)); | |||
| 779 | } | |||
| 780 | } else { /* device == LA100 */ | |||
| 781 | *orientation = PORTRAIT; | |||
| 782 | *scale = W_MAX2400 / win_width; | |||
| 783 | } | |||
| 784 | if (*scale == 0) *scale = 1; | |||
| 785 | if (*scale > 6) *scale = 6; | |||
| 786 | if (iscale > 0 && iscale < *scale) *scale = iscale; | |||
| 787 | } | |||
| 788 | ||||
| 789 | static | |||
| 790 | char *convert_data( | |||
| 791 | XWDFileHeader *win, | |||
| 792 | char *data, | |||
| 793 | int plane, | |||
| 794 | GrayPtr gray, | |||
| 795 | XColor *colors, | |||
| 796 | int flags) | |||
| 797 | { | |||
| 798 | XImage in_image_struct, out_image_struct; | |||
| 799 | register XImage *in_image, *out_image; | |||
| 800 | register int x, y; | |||
| 801 | ||||
| 802 | if ((win->pixmap_format == XYPixmap1) && (plane >= 0)) { | |||
| 803 | data += win->bytes_per_line * win->pixmap_height * | |||
| 804 | (win->pixmap_depth - (plane + 1)); | |||
| 805 | win->pixmap_format = XYBitmap0; | |||
| 806 | win->pixmap_depth = 1; | |||
| 807 | return data; | |||
| 808 | } | |||
| 809 | ||||
| 810 | /* initialize the input image */ | |||
| 811 | ||||
| 812 | in_image = &in_image_struct; | |||
| 813 | in_image->byte_order = win->byte_order; | |||
| 814 | in_image->bitmap_unit = win->bitmap_unit; | |||
| 815 | in_image->bitmap_bit_order = win->bitmap_bit_order; | |||
| 816 | in_image->depth = win->pixmap_depth; | |||
| 817 | in_image->bits_per_pixel = win->bits_per_pixel; | |||
| 818 | in_image->format = win->pixmap_format, | |||
| 819 | in_image->xoffset = win->xoffset, | |||
| 820 | in_image->data = data; | |||
| 821 | in_image->width = win->pixmap_width; | |||
| 822 | in_image->height = win->pixmap_height; | |||
| 823 | in_image->bitmap_pad = win->bitmap_pad; | |||
| 824 | in_image->bytes_per_line = win->bytes_per_line; | |||
| 825 | in_image->red_mask = win->red_mask; | |||
| 826 | in_image->green_mask = win->green_mask; | |||
| 827 | in_image->blue_mask = win->blue_mask; | |||
| 828 | in_image->obdata = NULL((void*)0); | |||
| 829 | if (!XInitImage(in_image)) { | |||
| 830 | fprintf(stderr__stderrp,"xpr: bad input image header data.\n"); | |||
| 831 | exit(1); | |||
| 832 | } | |||
| 833 | if ((flags & F_GRAY512) && (in_image->depth > 1) && (plane < 0)) { | |||
| 834 | win->pixmap_width *= gray->sizeX; | |||
| 835 | win->pixmap_height *= gray->sizeY; | |||
| 836 | } | |||
| 837 | win->xoffset = 0; | |||
| 838 | win->pixmap_format = XYBitmap0; | |||
| 839 | win->byte_order = LSBFirst0; | |||
| 840 | win->bitmap_unit = 8; | |||
| 841 | win->bitmap_bit_order = LSBFirst0; | |||
| 842 | win->bitmap_pad = 8; | |||
| 843 | win->pixmap_depth = 1; | |||
| 844 | win->bits_per_pixel = 1; | |||
| 845 | win->bytes_per_line = (win->pixmap_width + 7) >> 3; | |||
| 846 | ||||
| 847 | out_image = &out_image_struct; | |||
| 848 | out_image->byte_order = win->byte_order; | |||
| 849 | out_image->bitmap_unit = win->bitmap_unit; | |||
| 850 | out_image->bitmap_bit_order = win->bitmap_bit_order; | |||
| 851 | out_image->depth = win->pixmap_depth; | |||
| 852 | out_image->bits_per_pixel = win->bits_per_pixel; | |||
| 853 | out_image->format = win->pixmap_format; | |||
| 854 | out_image->xoffset = win->xoffset, | |||
| 855 | out_image->width = win->pixmap_width; | |||
| 856 | out_image->height = win->pixmap_height; | |||
| 857 | out_image->bitmap_pad = win->bitmap_pad; | |||
| 858 | out_image->bytes_per_line = win->bytes_per_line; | |||
| 859 | out_image->red_mask = 0; | |||
| 860 | out_image->green_mask = 0; | |||
| 861 | out_image->blue_mask = 0; | |||
| 862 | out_image->obdata = NULL((void*)0); | |||
| 863 | out_image->data = malloc((unsigned)out_image->bytes_per_line * | |||
| 864 | out_image->height); | |||
| 865 | if (!XInitImage(out_image)) { | |||
| 866 | fprintf(stderr__stderrp,"xpr: bad output image header data.\n"); | |||
| 867 | exit(1); | |||
| 868 | } | |||
| 869 | ||||
| 870 | if ((in_image->depth > 1) && (plane > 0)) { | |||
| 871 | for (y = 0; y < in_image->height; y++) | |||
| 872 | for (x = 0; x < in_image->width; x++) | |||
| 873 | XPutPixel(out_image, x, y,((*((out_image)->f.put_pixel))((out_image), (x), (y), (((( *((in_image)->f.get_pixel))((in_image), (x), (y))) >> plane) & 1))) | |||
| 874 | (XGetPixel(in_image, x, y) >> plane) & 1)((*((out_image)->f.put_pixel))((out_image), (x), (y), (((( *((in_image)->f.get_pixel))((in_image), (x), (y))) >> plane) & 1))); | |||
| 875 | } else if (plane == 0) { | |||
| 876 | for (y = 0; y < in_image->height; y++) | |||
| 877 | for (x = 0; x < in_image->width; x++) | |||
| 878 | XPutPixel(out_image, x, y, XGetPixel(in_image, x, y))((*((out_image)->f.put_pixel))((out_image), (x), (y), (((* ((in_image)->f.get_pixel))((in_image), (x), (y)))))); | |||
| 879 | } else if ((in_image->depth > 1) && | |||
| 880 | ((win->visual_class == TrueColor4) || | |||
| 881 | (win->visual_class == DirectColor5))) { | |||
| 882 | XColor color; | |||
| 883 | int direct = 0; | |||
| 884 | unsigned long rmask, gmask, bmask; | |||
| 885 | int rshift = 0, gshift = 0, bshift = 0; | |||
| 886 | ||||
| 887 | rmask = win->red_mask; | |||
| 888 | while (!(rmask & 1)) { | |||
| 889 | rmask >>= 1; | |||
| 890 | rshift++; | |||
| 891 | } | |||
| 892 | gmask = win->green_mask; | |||
| 893 | while (!(gmask & 1)) { | |||
| 894 | gmask >>= 1; | |||
| 895 | gshift++; | |||
| 896 | } | |||
| 897 | bmask = win->blue_mask; | |||
| 898 | while (!(bmask & 1)) { | |||
| 899 | bmask >>= 1; | |||
| 900 | bshift++; | |||
| 901 | } | |||
| 902 | if ((win->ncolors == 0) || (win->visual_class == DirectColor5)) | |||
| 903 | direct = 1; | |||
| 904 | if (flags & F_GRAY512) { | |||
| 905 | register int ox, oy; | |||
| 906 | int ix, iy; | |||
| 907 | unsigned long bits; | |||
| 908 | for (y = 0, oy = 0; y < in_image->height; y++, oy += gray->sizeY) | |||
| 909 | for (x = 0, ox = 0; x < in_image->width; x++, ox += gray->sizeX) | |||
| 910 | { | |||
| 911 | color.pixel = XGetPixel(in_image, x, y)((*((in_image)->f.get_pixel))((in_image), (x), (y))); | |||
| 912 | color.red = (color.pixel >> rshift) & rmask; | |||
| 913 | color.green = (color.pixel >> gshift) & gmask; | |||
| 914 | color.blue = (color.pixel >> bshift) & bmask; | |||
| 915 | if (!direct) { | |||
| 916 | color.red = colors[color.red].red; | |||
| 917 | color.green = colors[color.green].green; | |||
| 918 | color.blue = colors[color.blue].blue; | |||
| 919 | } | |||
| 920 | bits = gray->grayscales[(int)(gray->level * | |||
| 921 | INTENSITY(&color)(30L*(int)(&color)->red + 59L*(int)(&color)->green + 11L*(int)(&color)->blue)) / | |||
| 922 | (INTENSITYPER(100)(((1<<16)-1)*((long)(100))) + 1)]; | |||
| 923 | for (iy = 0; iy < gray->sizeY; iy++) | |||
| 924 | for (ix = 0; ix < gray->sizeX; ix++, bits >>= 1) | |||
| 925 | XPutPixel(out_image, ox + ix, oy + iy, bits)((*((out_image)->f.put_pixel))((out_image), (ox + ix), (oy + iy), (bits))); | |||
| 926 | } | |||
| 927 | } else { | |||
| 928 | for (y = 0; y < in_image->height; y++) | |||
| 929 | for (x = 0; x < in_image->width; x++) { | |||
| 930 | color.pixel = XGetPixel(in_image, x, y)((*((in_image)->f.get_pixel))((in_image), (x), (y))); | |||
| 931 | color.red = (color.pixel >> rshift) & rmask; | |||
| 932 | color.green = (color.pixel >> gshift) & gmask; | |||
| 933 | color.blue = (color.pixel >> bshift) & bmask; | |||
| 934 | if (!direct) { | |||
| 935 | color.red = colors[color.red].red; | |||
| 936 | color.green = colors[color.green].green; | |||
| 937 | color.blue = colors[color.blue].blue; | |||
| 938 | } | |||
| 939 | XPutPixel(out_image, x, y,((*((out_image)->f.put_pixel))((out_image), (x), (y), ((30L *(int)(&color)->red + 59L*(int)(&color)->green + 11L*(int)(&color)->blue) > (((1<<16)-1)*((long )(50)))))) | |||
| 940 | INTENSITY(&color) > HALFINTENSITY)((*((out_image)->f.put_pixel))((out_image), (x), (y), ((30L *(int)(&color)->red + 59L*(int)(&color)->green + 11L*(int)(&color)->blue) > (((1<<16)-1)*((long )(50)))))); | |||
| 941 | } | |||
| 942 | } | |||
| 943 | } else if (flags & F_GRAY512) { | |||
| 944 | register int ox, oy; | |||
| 945 | int ix, iy; | |||
| 946 | unsigned long bits; | |||
| 947 | ||||
| 948 | if (win->ncolors == 0) { | |||
| 949 | fprintf(stderr__stderrp, "no colors in data, can't remap\n"); | |||
| 950 | exit(1); | |||
| 951 | } | |||
| 952 | for (x = 0; x < win->ncolors; x++) { | |||
| 953 | register XColor *color = &colors[x]; | |||
| 954 | ||||
| 955 | color->pixel = gray->grayscales[(gray->level * INTENSITY(color)(30L*(int)(color)->red + 59L*(int)(color)->green + 11L* (int)(color)->blue)) / | |||
| 956 | (INTENSITYPER(100)(((1<<16)-1)*((long)(100))) + 1)]; | |||
| 957 | } | |||
| 958 | for (y = 0, oy = 0; y < in_image->height; y++, oy += gray->sizeY) | |||
| 959 | for (x = 0, ox = 0; x < in_image->width; x++, ox += gray->sizeX) { | |||
| 960 | bits = colors[XGetPixel(in_image, x, y)((*((in_image)->f.get_pixel))((in_image), (x), (y)))].pixel; | |||
| 961 | for (iy = 0; iy < gray->sizeY; iy++) | |||
| 962 | for (ix = 0; ix < gray->sizeX; ix++, bits >>= 1) | |||
| 963 | XPutPixel(out_image, ox + ix, oy + iy, bits)((*((out_image)->f.put_pixel))((out_image), (ox + ix), (oy + iy), (bits))); | |||
| 964 | } | |||
| 965 | } else { | |||
| 966 | if (win->ncolors == 0) { | |||
| 967 | fprintf(stderr__stderrp, "no colors in data, can't remap\n"); | |||
| 968 | exit(1); | |||
| 969 | } | |||
| 970 | for (x = 0; x < win->ncolors; x++) { | |||
| 971 | register XColor *color = &colors[x]; | |||
| 972 | color->pixel = (INTENSITY(color)(30L*(int)(color)->red + 59L*(int)(color)->green + 11L* (int)(color)->blue) > HALFINTENSITY(((1<<16)-1)*((long)(50)))); | |||
| 973 | } | |||
| 974 | for (y = 0; y < in_image->height; y++) | |||
| 975 | for (x = 0; x < in_image->width; x++) | |||
| 976 | XPutPixel(out_image, x, y,((*((out_image)->f.put_pixel))((out_image), (x), (y), (colors [((*((in_image)->f.get_pixel))((in_image), (x), (y)))].pixel ))) | |||
| 977 | colors[XGetPixel(in_image, x, y)].pixel)((*((out_image)->f.put_pixel))((out_image), (x), (y), (colors [((*((in_image)->f.get_pixel))((in_image), (x), (y)))].pixel ))); | |||
| 978 | } | |||
| 979 | free(data); | |||
| 980 | return (out_image->data); | |||
| 981 | } | |||
| 982 | ||||
| 983 | static | |||
| 984 | void dump_sixmap( | |||
| 985 | register unsigned char (*sixmap)[], | |||
| 986 | int iw, | |||
| 987 | int ih) | |||
| 988 | { | |||
| 989 | register int i, j; | |||
| 990 | register unsigned char *c; | |||
| 991 | ||||
| 992 | c = (unsigned char *)sixmap; | |||
| 993 | fprintf(stderr__stderrp, "Sixmap:\n"); | |||
| 994 | for (i = 0; i < ih; i++) { | |||
| 995 | for (j = 0; j < iw; j++) { | |||
| 996 | fprintf(stderr__stderrp, "%02X ", *c++); | |||
| 997 | } | |||
| 998 | fprintf(stderr__stderrp, "\n\n"); | |||
| 999 | } | |||
| 1000 | } | |||
| 1001 | ||||
| 1002 | static | |||
| 1003 | void build_sixmap( | |||
| 1004 | int ih, | |||
| 1005 | int iw, | |||
| 1006 | unsigned char (*sixmap)[], | |||
| 1007 | int hpad, | |||
| 1008 | XWDFileHeader *win, | |||
| 1009 | const char *data) | |||
| 1010 | { | |||
| 1011 | int iwb = win->bytes_per_line; | |||
| 1012 | unsigned char *line[6]; | |||
| 1013 | register unsigned char *c; | |||
| 1014 | register int i, j; | |||
| 1015 | #ifdef NOINLINE1 | |||
| 1016 | register int w; | |||
| 1017 | #endif | |||
| 1018 | register int sixel; | |||
| 1019 | unsigned char *buffer = (unsigned char *)data; | |||
| 1020 | ||||
| 1021 | c = (unsigned char *)sixmap; | |||
| 1022 | ||||
| 1023 | ||||
| 1024 | while (--ih >= 0) { | |||
| 1025 | for (i = 0; i <= 5; i++) { | |||
| 1026 | line[i] = buffer; | |||
| 1027 | buffer += iwb; | |||
| 1028 | } | |||
| 1029 | if ((ih == 0) && (hpad > 0)) { | |||
| 1030 | unsigned char *ffbuf; | |||
| 1031 | ||||
| 1032 | ffbuf = (unsigned char *)malloc((unsigned)iwb); | |||
| 1033 | for (j = 0; j < iwb; j++) | |||
| 1034 | ffbuf[j] = 0xFF; | |||
| 1035 | for (; --hpad >= 0; i--) | |||
| 1036 | line[i] = ffbuf; | |||
| 1037 | } | |||
| 1038 | ||||
| 1039 | #ifndef NOINLINE1 | |||
| 1040 | for (i = 0; i < iw; i++) { | |||
| 1041 | sixel = extzv(line[0], i, 1); | |||
| 1042 | sixel |= extzv(line[1], i, 1) << 1; | |||
| 1043 | sixel |= extzv(line[2], i, 1) << 2; | |||
| 1044 | sixel |= extzv(line[3], i, 1) << 3; | |||
| 1045 | sixel |= extzv(line[4], i, 1) << 4; | |||
| 1046 | sixel |= extzv(line[5], i, 1) << 5; | |||
| 1047 | *c++ = sixel; | |||
| 1048 | } | |||
| 1049 | #else | |||
| 1050 | for (i = 0, w = iw; w > 0; i++) { | |||
| 1051 | for (j = 0; j <= 7; j++) { | |||
| 1052 | sixel = ((line[0][i] >> j) & 1); | |||
| 1053 | sixel |= ((line[1][i] >> j) & 1) << 1; | |||
| 1054 | sixel |= ((line[2][i] >> j) & 1) << 2; | |||
| 1055 | sixel |= ((line[3][i] >> j) & 1) << 3; | |||
| 1056 | sixel |= ((line[4][i] >> j) & 1) << 4; | |||
| 1057 | sixel |= ((line[5][i] >> j) & 1) << 5; | |||
| 1058 | *c++ = sixel; | |||
| 1059 | if (--w == 0) break; | |||
| 1060 | } | |||
| 1061 | } | |||
| 1062 | #endif | |||
| 1063 | } | |||
| 1064 | } | |||
| 1065 | ||||
| 1066 | /* | |||
| 1067 | ln03_grind_fonts(sixmap, iw, ih, scale, pixmap) | |||
| 1068 | unsigned char (*sixmap)[]; | |||
| 1069 | int iw; | |||
| 1070 | int ih; | |||
| 1071 | int scale; | |||
| 1072 | struct pixmap (**pixmap)[]; | |||
| 1073 | { | |||
| 1074 | } | |||
| 1075 | */ | |||
| 1076 | ||||
| 1077 | static | |||
| 1078 | void ln03_setup( | |||
| 1079 | int iw, | |||
| 1080 | int ih, | |||
| 1081 | enum orientation orientation, | |||
| 1082 | int scale, | |||
| 1083 | int left, | |||
| 1084 | int top, | |||
| 1085 | int *left_margin, | |||
| 1086 | int *top_margin, | |||
| 1087 | int flags, | |||
| 1088 | const char *header, | |||
| 1089 | const char *trailer) | |||
| 1090 | { | |||
| 1091 | register int i; | |||
| 1092 | register int lm, tm, xm; | |||
| 1093 | char buf[256]; | |||
| 1094 | register char *bp = buf; | |||
| 1095 | ||||
| 1096 | if (!(flags & F_APPEND16)) { | |||
| 1097 | sprintf(bp, LN_STR)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[!p"); bp += 4; | |||
| 1098 | sprintf(bp, LN_SSU, 7)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[%d I", 7); bp += 5; | |||
| 1099 | sprintf(bp, LN_PUM_SET)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[11h"); bp += sizeof LN_PUM_SET"\033[11h" - 1; | |||
| 1100 | } | |||
| 1101 | ||||
| 1102 | if (orientation == PORTRAIT) { | |||
| 1103 | lm = (left > 0)? left : (((W_MAX2400 - scale * iw) / 2) + W_MARGIN75); | |||
| 1104 | tm = (top > 0)? top : (((H_MAX3150 - scale * ih * 6) / 2) + H_MARGIN37); | |||
| 1105 | sprintf(bp, LN_PFS, "?20")__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[%s J", "?20"); bp += 7; | |||
| 1106 | sprintf(bp, LN_DECOPM_SET)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[?52h"); bp += sizeof LN_DECOPM_SET"\033[?52h" - 1; | |||
| 1107 | sprintf(bp, LN_DECSLRM, lm, W_PAGE - lm)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[%d;%ds", lm, 2550 - lm); bp += strlen(bp); | |||
| 1108 | } else { | |||
| 1109 | lm = (left > 0)? left : (((H_MAX3150 - scale * iw) / 2) + H_MARGIN37); | |||
| 1110 | tm = (top > 0)? top : (((W_MAX2400 - scale * ih * 6) / 2) + W_MARGIN75); | |||
| 1111 | sprintf(bp, LN_PFS, "?21")__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[%s J", "?21"); bp += 7; | |||
| 1112 | sprintf(bp, LN_DECOPM_SET)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[?52h"); bp += sizeof LN_DECOPM_SET"\033[?52h" - 1; | |||
| 1113 | sprintf(bp, LN_DECSLRM, lm, H_PAGE - lm)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[%d;%ds", lm, 3225 - lm); bp += strlen(bp); | |||
| 1114 | } | |||
| 1115 | ||||
| 1116 | if (header != NULL((void*)0)) { | |||
| 1117 | sprintf(bp, LN_VPA, tm - 100)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[%dd", tm - 100); bp += strlen(bp); | |||
| 1118 | i = strlen(header); | |||
| 1119 | xm = (((scale * iw) - (i * 30)) / 2) + lm; | |||
| 1120 | sprintf(bp, LN_HPA, xm)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[%d`", xm); bp += strlen(bp); | |||
| 1121 | sprintf(bp, LN_SGR, 3)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[1%dm", 3); bp += strlen(bp); | |||
| 1122 | memmove(bp, header, i)__builtin___memmove_chk (bp, header, i, __builtin_object_size (bp, 0)); | |||
| 1123 | bp += i; | |||
| 1124 | } | |||
| 1125 | if (trailer != NULL((void*)0)) { | |||
| 1126 | sprintf(bp, LN_VPA, tm + (scale * ih * 6) + 75)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[%dd", tm + (scale * ih * 6) + 75); bp += strlen(bp); | |||
| 1127 | i = strlen(trailer); | |||
| 1128 | xm = (((scale * iw) - (i * 30)) / 2) + lm; | |||
| 1129 | sprintf(bp, LN_HPA, xm)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[%d`", xm); bp += strlen(bp); | |||
| 1130 | sprintf(bp, LN_SGR, 3)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[1%dm", 3); bp += strlen(bp); | |||
| 1131 | memmove(bp, trailer, i)__builtin___memmove_chk (bp, trailer, i, __builtin_object_size (bp, 0)); | |||
| 1132 | bp += i; | |||
| 1133 | } | |||
| 1134 | ||||
| 1135 | sprintf(bp, LN_HPA, lm)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[%d`", lm); bp += strlen(bp); | |||
| 1136 | sprintf(bp, LN_VPA, tm)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[%dd", tm); bp += strlen(bp); | |||
| 1137 | sprintf(bp, LN_SIXEL_GRAPHICS, 9, 0, scale)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033P%d;%d;%dq", 9, 0, scale); bp += strlen(bp); | |||
| 1138 | sprintf(bp, "\"1;1")__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\"1;1"); bp += 4; /* Pixel aspect ratio */ | |||
| 1139 | write(1, buf, bp-buf); | |||
| 1140 | *top_margin = tm; | |||
| 1141 | *left_margin = lm; | |||
| 1142 | } | |||
| 1143 | ||||
| 1144 | static | |||
| 1145 | void ln03_finish(void) | |||
| 1146 | { | |||
| 1147 | char buf[256]; | |||
| 1148 | register char *bp = buf; | |||
| 1149 | ||||
| 1150 | sprintf(bp, LN_DECOPM_RESET)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[?52I"); bp += sizeof LN_DECOPM_SET"\033[?52h" - 1; | |||
| 1151 | sprintf(bp, LN_LNM)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[20h"); bp += 5; | |||
| 1152 | sprintf(bp, LN_PUM)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[11I"); bp += 5; | |||
| 1153 | sprintf(bp, LN_PFS, "?20")__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[%s J", "?20"); bp += 7; | |||
| 1154 | sprintf(bp, LN_SGR, 0)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[1%dm", 0); bp += strlen(bp); | |||
| 1155 | sprintf(bp, LN_HPA, 1)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[%d`", 1); bp += strlen(bp); | |||
| 1156 | sprintf(bp, LN_VPA, 1)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[%dd", 1); bp += strlen(bp); | |||
| 1157 | ||||
| 1158 | ||||
| 1159 | write(1, buf, bp-buf); | |||
| 1160 | } | |||
| 1161 | ||||
| 1162 | /*ARGSUSED*/ | |||
| 1163 | static | |||
| 1164 | void la100_setup(int iw, int ih, int scale) | |||
| 1165 | { | |||
| 1166 | char buf[256]; | |||
| 1167 | register char *bp; | |||
| 1168 | int lm, tm; | |||
| 1169 | ||||
| 1170 | bp = buf; | |||
| 1171 | lm = ((80 - (int)((double)iw / 6.6)) / 2) - 1; | |||
| 1172 | if (lm < 1) lm = 1; | |||
| 1173 | tm = ((66 - (int)((double)ih / 2)) / 2) - 1; | |||
| 1174 | if (tm < 1) tm = 1; | |||
| 1175 | sprintf(bp, "\033[%d;%ds", lm, 81-lm)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[%d;%ds", lm, 81-lm); bp += strlen(bp); | |||
| 1176 | sprintf(bp, "\033[?7l")__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[?7l"); bp += 5; | |||
| 1177 | sprintf(bp, "\033[%dd", tm)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[%dd", tm); bp += strlen(bp); | |||
| 1178 | sprintf(bp, "\033[%d`", lm)__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033[%d`", lm); bp += strlen(bp); | |||
| 1179 | sprintf(bp, "\033P0q")__builtin___sprintf_chk (bp, 0, __builtin_object_size (bp, 2 > 1 ? 1 : 0), "\033P0q"); bp += 4; | |||
| 1180 | write(1, buf, bp-buf); | |||
| 1181 | } | |||
| 1182 | ||||
| 1183 | #define LA100_RESET"\033[1;80s\033[?7h" "\033[1;80s\033[?7h" | |||
| 1184 | ||||
| 1185 | static | |||
| 1186 | void la100_finish(void) | |||
| 1187 | { | |||
| 1188 | write(1, LA100_RESET"\033[1;80s\033[?7h", sizeof LA100_RESET"\033[1;80s\033[?7h" - 1); | |||
| 1189 | } | |||
| 1190 | ||||
| 1191 | #define COMMENTVERSION"PS-Adobe-1.0" "PS-Adobe-1.0" | |||
| 1192 | ||||
| 1193 | #ifdef XPROLOG | |||
| 1194 | /* for debugging, get the prolog from a file */ | |||
| 1195 | static | |||
| 1196 | void dump_prolog(int flags) | |||
| 1197 | { | |||
| 1198 | char *fname=(flags & F_COMPACT128) ? "prolog.compact" : "prolog"; | |||
| 1199 | FILE *fi = fopen(fname,"r"); | |||
| 1200 | char buf[1024]; | |||
| 1201 | ||||
| 1202 | if (fi==NULL((void*)0)) { | |||
| 1203 | perror(fname); | |||
| 1204 | exit(1); | |||
| 1205 | } | |||
| 1206 | while (fgets(buf,1024,fi)) fputs(buf,stdout__stdoutp); | |||
| 1207 | fclose(fi); | |||
| 1208 | } | |||
| 1209 | ||||
| 1210 | #else /* XPROLOG */ | |||
| 1211 | /* postscript "programs" to unpack and print the bitmaps being sent */ | |||
| 1212 | ||||
| 1213 | static const | |||
| 1214 | char * const ps_prolog_compact[] = { | |||
| 1215 | "%%Pages: 1", | |||
| 1216 | "%%EndProlog", | |||
| 1217 | "%%Page: 1 1", | |||
| 1218 | "", | |||
| 1219 | "/bitgen", | |||
| 1220 | " {", | |||
| 1221 | " /nextpos 0 def", | |||
| 1222 | " currentfile bufspace readhexstring pop % get a chunk of input", | |||
| 1223 | " % interpret each byte of the input", | |||
| 1224 | " {", | |||
| 1225 | " flag { % if the previous byte was FF", | |||
| 1226 | " /len exch def % this byte is a count", | |||
| 1227 | " result", | |||
| 1228 | " nextpos", | |||
| 1229 | " FFstring 0 len getinterval % grap a chunk of FF's", | |||
| 1230 | " putinterval % and stuff them into the result", | |||
| 1231 | " /nextpos nextpos len add def", | |||
| 1232 | " /flag false def", | |||
| 1233 | " }{ % otherwise", | |||
| 1234 | " dup 255 eq { % if this byte is FF", | |||
| 1235 | " /flag true def % just set the flag", | |||
| 1236 | " pop % and toss the FF", | |||
| 1237 | " }{ % otherwise", | |||
| 1238 | " % move this byte to the result", | |||
| 1239 | " result nextpos", | |||
| 1240 | " 3 -1 roll % roll the current byte back to the top", | |||
| 1241 | " put", | |||
| 1242 | " /nextpos nextpos 1 add def", | |||
| 1243 | " } ifelse", | |||
| 1244 | " } ifelse", | |||
| 1245 | " } forall", | |||
| 1246 | " % trim unused space from end of result", | |||
| 1247 | " result 0 nextpos getinterval", | |||
| 1248 | " } def", | |||
| 1249 | "", | |||
| 1250 | "", | |||
| 1251 | "/bitdump % stk: width, height, iscale", | |||
| 1252 | " % dump a bit image with lower left corner at current origin,", | |||
| 1253 | " % scaling by iscale (iscale=1 means 1/300 inch per pixel)", | |||
| 1254 | " {", | |||
| 1255 | " % read arguments", | |||
| 1256 | " /iscale exch def", | |||
| 1257 | " /height exch def", | |||
| 1258 | " /width exch def", | |||
| 1259 | "", | |||
| 1260 | " % scale appropriately", | |||
| 1261 | " width iscale mul height iscale mul scale", | |||
| 1262 | "", | |||
| 1263 | " % data structures:", | |||
| 1264 | "", | |||
| 1265 | " % allocate space for one line of input", | |||
| 1266 | " /bufspace 36 string def", | |||
| 1267 | "", | |||
| 1268 | " % string of FF's", | |||
| 1269 | " /FFstring 256 string def", | |||
| 1270 | " % for all i FFstring[i]=255", | |||
| 1271 | " 0 1 255 { FFstring exch 255 put } for", | |||
| 1272 | "", | |||
| 1273 | " % 'escape' flag", | |||
| 1274 | " /flag false def", | |||
| 1275 | "", | |||
| 1276 | " % space for a chunk of generated bits", | |||
| 1277 | " /result 4590 string def", | |||
| 1278 | "", | |||
| 1279 | " % read and dump the image", | |||
| 1280 | " width height 1 [width 0 0 height neg 0 height]", | |||
| 1281 | " { bitgen }", | |||
| 1282 | " image", | |||
| 1283 | " } def", | |||
| 1284 | NULL((void*)0) | |||
| 1285 | }; | |||
| 1286 | ||||
| 1287 | static const | |||
| 1288 | char * const ps_prolog[] = { | |||
| 1289 | "%%Pages: 1", | |||
| 1290 | "%%EndProlog", | |||
| 1291 | "%%Page: 1 1", | |||
| 1292 | "", | |||
| 1293 | "/bitdump % stk: width, height, iscale", | |||
| 1294 | "% dump a bit image with lower left corner at current origin,", | |||
| 1295 | "% scaling by iscale (iscale=1 means 1/300 inch per pixel)", | |||
| 1296 | "{", | |||
| 1297 | " % read arguments", | |||
| 1298 | " /iscale exch def", | |||
| 1299 | " /height exch def", | |||
| 1300 | " /width exch def", | |||
| 1301 | "", | |||
| 1302 | " % scale appropriately", | |||
| 1303 | " width iscale mul height iscale mul scale", | |||
| 1304 | "", | |||
| 1305 | " % allocate space for one scanline of input", | |||
| 1306 | " /picstr % picstr holds one scan line", | |||
| 1307 | " width 7 add 8 idiv % width of image in bytes = ceiling(width/8)", | |||
| 1308 | " string", | |||
| 1309 | " def", | |||
| 1310 | "", | |||
| 1311 | " % read and dump the image", | |||
| 1312 | " width height 1 [width 0 0 height neg 0 height]", | |||
| 1313 | " { currentfile picstr readhexstring pop }", | |||
| 1314 | " image", | |||
| 1315 | "} def", | |||
| 1316 | NULL((void*)0) | |||
| 1317 | }; | |||
| 1318 | ||||
| 1319 | static | |||
| 1320 | void dump_prolog(int flags) { | |||
| 1321 | const char * const *p = | |||
| 1322 | (flags & F_COMPACT128) ? ps_prolog_compact : ps_prolog; | |||
| 1323 | while (*p) | |||
| 1324 | printf("%s\n", *p++); | |||
| 1325 | } | |||
| 1326 | #endif /* XPROLOG */ | |||
| 1327 | ||||
| 1328 | #define PAPER_WIDTH85*30 85*30 /* 8.5 inches */ | |||
| 1329 | #define PAPER_LENGTH11*300 11*300 /* 11 inches */ | |||
| 1330 | ||||
| 1331 | static | |||
| 1332 | int points(int n) | |||
| 1333 | { | |||
| 1334 | /* scale n from pixels (1/300 inch) to points (1/72 inch) */ | |||
| 1335 | n *= 72; | |||
| 1336 | return n/300; | |||
| 1337 | } | |||
| 1338 | ||||
| 1339 | static | |||
| 1340 | char *escape(const char *s) | |||
| 1341 | { | |||
| 1342 | /* make a version of s in which control characters are deleted and | |||
| 1343 | * special characters are escaped. | |||
| 1344 | */ | |||
| 1345 | static char buf[200]; | |||
| 1346 | char *p = buf; | |||
| 1347 | ||||
| 1348 | for (;*s;s++) { | |||
| 1349 | if (*s < ' ' || *s > 0176) continue; | |||
| 1350 | if (*s==')' || *s=='(' || *s == '\\') { | |||
| 1351 | sprintf(p,"\\%03o",*s)__builtin___sprintf_chk (p, 0, __builtin_object_size (p, 2 > 1 ? 1 : 0), "\\%03o",*s); | |||
| 1352 | p += 4; | |||
| 1353 | } | |||
| 1354 | else *p++ = *s; | |||
| 1355 | } | |||
| 1356 | *p = 0; | |||
| 1357 | return buf; | |||
| 1358 | } | |||
| 1359 | ||||
| 1360 | static | |||
| 1361 | void ps_setup( | |||
| 1362 | int iw, | |||
| 1363 | int ih, | |||
| 1364 | enum orientation orientation, | |||
| 1365 | int scale, | |||
| 1366 | int left, | |||
| 1367 | int top, | |||
| 1368 | int flags, | |||
| 1369 | const char *header, | |||
| 1370 | const char *trailer, | |||
| 1371 | const char *name) | |||
| 1372 | { | |||
| 1373 | char hostname[256]; | |||
| 1374 | #ifdef WIN32 | |||
| 1375 | char *username; | |||
| 1376 | #else | |||
| 1377 | struct passwd *pswd; | |||
| 1378 | #endif | |||
| 1379 | long clock; | |||
| 1380 | int lm, bm; /* left (bottom) margin */ | |||
| 1381 | ||||
| 1382 | /* calculate margins */ | |||
| 1383 | if (orientation==PORTRAIT) { | |||
| 1384 | lm = (left > 0)? left : ((PAPER_WIDTH85*30 - scale * iw) / 2); | |||
| 1385 | bm = (top > 0)? (PAPER_LENGTH11*300 - top - scale * ih) | |||
| 1386 | : ((PAPER_LENGTH11*300 - scale * ih) / 2); | |||
| 1387 | } else { /* orientation == LANDSCAPE */ | |||
| 1388 | lm = (top > 0)? (PAPER_WIDTH85*30 - top - scale * ih) | |||
| 1389 | : ((PAPER_WIDTH85*30 - scale * ih) / 2); | |||
| 1390 | bm = (left > 0)? (PAPER_LENGTH11*300 - left - scale * iw) | |||
| 1391 | : ((PAPER_LENGTH11*300 - scale * iw) / 2); | |||
| 1392 | } | |||
| 1393 | printf ("%%!%s\n", COMMENTVERSION"PS-Adobe-1.0"); | |||
| 1394 | printf ("%%%%BoundingBox: %d %d %d %d\n", | |||
| 1395 | (flags & F_NPOSITION1024) ? points(lm) : 0, | |||
| 1396 | (flags & F_NPOSITION1024) ? points(bm) : 0, | |||
| 1397 | points(iw * scale), points(ih * scale)); | |||
| 1398 | (void) XmuGetHostname (hostname, sizeof hostname); | |||
| 1399 | #ifdef WIN32 | |||
| 1400 | username = getenv("USERNAME"); | |||
| 1401 | printf ("%%%%Creator: %s:%s\n", hostname, | |||
| 1402 | username ? username : "unknown"); | |||
| 1403 | #else | |||
| 1404 | pswd = getpwuid (getuid ()); | |||
| 1405 | printf ("%%%%Creator: %s:%s (%s)\n", hostname, | |||
| 1406 | pswd->pw_name, pswd->pw_gecos); | |||
| 1407 | #endif | |||
| 1408 | printf ("%%%%Title: %s (%s)\n", infilename,name); | |||
| 1409 | printf ("%%%%CreationDate: %s", | |||
| 1410 | (time (&clock), ctime (&clock))); | |||
| 1411 | printf ("%%%%EndComments\n"); | |||
| 1412 | ||||
| 1413 | dump_prolog(flags); | |||
| 1414 | ||||
| 1415 | if (orientation==PORTRAIT) { | |||
| 1416 | if (header || trailer) { | |||
| 1417 | printf("gsave\n"); | |||
| 1418 | printf("/Times-Roman findfont 15 scalefont setfont\n"); | |||
| 1419 | /* origin at bottom left corner of image */ | |||
| 1420 | printf("%d %d translate\n",points(lm),points(bm)); | |||
| 1421 | if (header) { | |||
| 1422 | char *label = escape(header); | |||
| 1423 | printf("%d (%s) stringwidth pop sub 2 div %d moveto\n", | |||
| 1424 | points(iw*scale), label, points(ih*scale) + 10); | |||
| 1425 | printf("(%s) show\n",label); | |||
| 1426 | } | |||
| 1427 | if (trailer) { | |||
| 1428 | char *label = escape(trailer); | |||
| 1429 | printf("%d (%s) stringwidth pop sub 2 div -20 moveto\n", | |||
| 1430 | points(iw*scale), label); | |||
| 1431 | printf("(%s) show\n",label); | |||
| 1432 | } | |||
| 1433 | printf("grestore\n"); | |||
| 1434 | } | |||
| 1435 | /* set resolution to device units (300/inch) */ | |||
| 1436 | printf("72 300 div dup scale\n"); | |||
| 1437 | /* move to lower left corner of image */ | |||
| 1438 | if (!(flags & F_NPOSITION1024)) | |||
| 1439 | printf("%d %d translate\n",lm,bm); | |||
| 1440 | /* dump the bitmap */ | |||
| 1441 | printf("%d %d %d bitdump\n",iw,ih,scale); | |||
| 1442 | } else { /* orientation == LANDSCAPE */ | |||
| 1443 | if (header || trailer) { | |||
| 1444 | printf("gsave\n"); | |||
| 1445 | printf("/Times-Roman findfont 15 scalefont setfont\n"); | |||
| 1446 | /* origin at top left corner of image */ | |||
| 1447 | printf("%d %d translate\n",points(lm),points(bm + scale * iw)); | |||
| 1448 | /* rotate to print the titles */ | |||
| 1449 | printf("-90 rotate\n"); | |||
| 1450 | if (header) { | |||
| 1451 | char *label = escape(header); | |||
| 1452 | printf("%d (%s) stringwidth pop sub 2 div %d moveto\n", | |||
| 1453 | points(iw*scale), label, points(ih*scale) + 10); | |||
| 1454 | printf("(%s) show\n",label); | |||
| 1455 | } | |||
| 1456 | if (trailer) { | |||
| 1457 | char *label = escape(trailer); | |||
| 1458 | printf("%d (%s) stringwidth pop sub 2 div -20 moveto\n", | |||
| 1459 | points(iw*scale), label); | |||
| 1460 | printf("(%s) show\n",label); | |||
| 1461 | } | |||
| 1462 | printf("grestore\n"); | |||
| 1463 | } | |||
| 1464 | /* set resolution to device units (300/inch) */ | |||
| 1465 | printf("72 300 div dup scale\n"); | |||
| 1466 | /* move to lower left corner of image */ | |||
| 1467 | if (!(flags & F_NPOSITION1024)) | |||
| 1468 | printf("%d %d translate\n",lm,bm); | |||
| 1469 | /* dump the bitmap */ | |||
| 1470 | printf("%d %d %d bitdump\n",ih,iw,scale); | |||
| 1471 | } | |||
| 1472 | } | |||
| 1473 | ||||
| 1474 | static const | |||
| 1475 | char * const ps_epilog[] = { | |||
| 1476 | "", | |||
| 1477 | "showpage", | |||
| 1478 | "%%Trailer", | |||
| 1479 | NULL((void*)0) | |||
| 1480 | }; | |||
| 1481 | ||||
| 1482 | static | |||
| 1483 | void ps_finish(void) | |||
| 1484 | { | |||
| 1485 | const char * const *p = ps_epilog; | |||
| 1486 | ||||
| 1487 | while (*p) printf("%s\n",*p++); | |||
| 1488 | } | |||
| 1489 | ||||
| 1490 | static | |||
| 1491 | void ln03_output_sixels( | |||
| 1492 | unsigned char (*sixmap)[], | |||
| 1493 | int iw, | |||
| 1494 | int ih, | |||
| 1495 | int nosixopt, | |||
| 1496 | int split, | |||
| 1497 | int scale, | |||
| 1498 | int top_margin, | |||
| 1499 | int left_margin) | |||
| 1500 | { | |||
| 1501 | unsigned char *buf; | |||
| 1502 | register unsigned char *bp; | |||
| 1503 | int i; | |||
| 1504 | int j; | |||
| 1505 | register int k; | |||
| 1506 | register unsigned char *c; | |||
| 1507 | register int lastc; | |||
| 1508 | register int count; | |||
| 1509 | char snum[6]; | |||
| 1510 | register char *snp; | |||
| 1511 | ||||
| 1512 | bp = (unsigned char *)malloc((unsigned)(iw*ih+512)); | |||
| 1513 | buf = bp; | |||
| 1514 | count = 0; | |||
| 1515 | lastc = -1; | |||
| 1516 | c = (unsigned char *)sixmap; | |||
| 1517 | split = ih / split; /* number of lines per page */ | |||
| 1518 | ||||
| 1519 | iw--; /* optimization */ | |||
| 1520 | for (i = 0; i < ih; i++) { | |||
| 1521 | for (j = 0; j <= iw; j++) { | |||
| 1522 | if (!nosixopt) { | |||
| 1523 | if (*c == lastc && j < iw) { | |||
| 1524 | count++; | |||
| 1525 | c++; | |||
| 1526 | continue; | |||
| 1527 | } | |||
| 1528 | if (count >= 3) { | |||
| 1529 | bp--; | |||
| 1530 | count++; | |||
| 1531 | *bp++ = '!'; | |||
| 1532 | snp = snum; | |||
| 1533 | while (count > 0) { | |||
| 1534 | k = count / 10; | |||
| 1535 | *snp++ = count - (k * 10) + '0'; | |||
| 1536 | count = k; | |||
| 1537 | } | |||
| 1538 | while (--snp >= snum) *bp++ = *snp; | |||
| 1539 | *bp++ = (~lastc & 0x3F) + 0x3F; | |||
| 1540 | } else if (count > 0) { | |||
| 1541 | lastc = (~lastc & 0x3F) + 0x3F; | |||
| 1542 | do { | |||
| 1543 | *bp++ = lastc; | |||
| 1544 | } while (--count > 0); | |||
| 1545 | } | |||
| 1546 | } | |||
| 1547 | lastc = *c++; | |||
| 1548 | *bp++ = (~lastc & 0x3F) + 0x3F; | |||
| 1549 | } | |||
| 1550 | *bp++ = '-'; /* New line */ | |||
| 1551 | lastc = -1; | |||
| 1552 | if ((i % split) == 0 && i != 0) { | |||
| 1553 | sprintf((char *)bp, LN_ST)__builtin___sprintf_chk ((char *)bp, 0, __builtin_object_size ((char *)bp, 2 > 1 ? 1 : 0), "\033\\"); bp += sizeof LN_ST"\033\\" - 1; | |||
| 1554 | *bp++ = '\f'; | |||
| 1555 | sprintf((char *)bp, LN_VPA, top_margin + (i * 6 * scale))__builtin___sprintf_chk ((char *)bp, 0, __builtin_object_size ((char *)bp, 2 > 1 ? 1 : 0), "\033[%dd", top_margin + (i * 6 * scale)); | |||
| 1556 | bp += strlen((char *)bp); | |||
| 1557 | sprintf((char *)bp, LN_HPA, left_margin)__builtin___sprintf_chk ((char *)bp, 0, __builtin_object_size ((char *)bp, 2 > 1 ? 1 : 0), "\033[%d`", left_margin); | |||
| 1558 | bp += strlen((char *)bp); | |||
| 1559 | sprintf((char *)bp, LN_SIXEL_GRAPHICS, 9, 0, scale)__builtin___sprintf_chk ((char *)bp, 0, __builtin_object_size ((char *)bp, 2 > 1 ? 1 : 0), "\033P%d;%d;%dq", 9, 0, scale ); | |||
| 1560 | bp += strlen((char *)bp); | |||
| 1561 | sprintf((char *)bp, "\"1;1")__builtin___sprintf_chk ((char *)bp, 0, __builtin_object_size ((char *)bp, 2 > 1 ? 1 : 0), "\"1;1"); bp += 4; | |||
| 1562 | } | |||
| 1563 | } | |||
| 1564 | ||||
| 1565 | sprintf((char *)bp, LN_ST)__builtin___sprintf_chk ((char *)bp, 0, __builtin_object_size ((char *)bp, 2 > 1 ? 1 : 0), "\033\\"); bp += sizeof LN_ST"\033\\" - 1; | |||
| 1566 | write(1, (char *)buf, bp-buf); | |||
| 1567 | } | |||
| 1568 | ||||
| 1569 | /*ARGSUSED*/ | |||
| 1570 | static | |||
| 1571 | void la100_output_sixels( | |||
| 1572 | unsigned char (*sixmap)[], | |||
| 1573 | int iw, | |||
| 1574 | int ih, | |||
| 1575 | int nosixopt) | |||
| 1576 | { | |||
| 1577 | unsigned char *buf; | |||
| 1578 | register unsigned char *bp; | |||
| 1579 | int i; | |||
| 1580 | register int j, k; | |||
| 1581 | register unsigned char *c; | |||
| 1582 | register int lastc; | |||
| 1583 | register int count; | |||
| 1584 | char snum[6]; | |||
| 1585 | ||||
| 1586 | bp = (unsigned char *)malloc((unsigned)(iw*ih+512)); | |||
| 1587 | buf = bp; | |||
| 1588 | count = 0; | |||
| 1589 | lastc = -1; | |||
| 1590 | c = (unsigned char *)sixmap; | |||
| 1591 | ||||
| 1592 | for (i = 0; i < ih; i++) { | |||
| 1593 | for (j = 0; j < iw; j++) { | |||
| 1594 | if (*c == lastc && (j+1) < iw) { | |||
| ||||
| 1595 | count++; | |||
| 1596 | c++; | |||
| 1597 | continue; | |||
| 1598 | } | |||
| 1599 | if (count >= 2) { | |||
| 1600 | bp -= 2; | |||
| 1601 | count = 2 * (count + 1); | |||
| 1602 | *bp++ = '!'; | |||
| 1603 | k = 0; | |||
| 1604 | while (count > 0) { | |||
| 1605 | snum[k++] = (count % 10) + '0'; | |||
| 1606 | count /= 10; | |||
| 1607 | } | |||
| 1608 | while (--k >= 0) *bp++ = snum[k]; | |||
| 1609 | *bp++ = (~lastc & 0x3F) + 0x3F; | |||
| 1610 | count = 0; | |||
| 1611 | } else if (count > 0) { | |||
| 1612 | lastc = (~lastc & 0x3F) + 0x3F; | |||
| 1613 | do { | |||
| 1614 | *bp++ = lastc; | |||
| 1615 | *bp++ = lastc; | |||
| 1616 | } while (--count > 0); | |||
| 1617 | } | |||
| 1618 | lastc = (~*c & 0x3F) + 0x3F; | |||
| 1619 | *bp++ = lastc; | |||
| 1620 | *bp++ = lastc; | |||
| 1621 | lastc = *c++; | |||
| 1622 | } | |||
| 1623 | *bp++ = '-'; /* New line */ | |||
| 1624 | lastc = -1; | |||
| 1625 | } | |||
| 1626 | ||||
| 1627 | sprintf((char *)bp, LN_ST)__builtin___sprintf_chk ((char *)bp, 0, __builtin_object_size ((char *)bp, 2 > 1 ? 1 : 0), "\033\\"); bp += sizeof LN_ST"\033\\" - 1; | |||
| 1628 | *bp++ = '\f'; | |||
| 1629 | write(1, (char *)buf, bp-buf); | |||
| 1630 | } | |||
| 1631 | ||||
| 1632 | #define LINELEN72 72 /* number of CHARS (bytes*2) per line of bitmap output */ | |||
| 1633 | ||||
| 1634 | static | |||
| 1635 | void ps_output_bits( | |||
| 1636 | int iw, | |||
| 1637 | int ih, | |||
| 1638 | int flags, | |||
| 1639 | enum orientation orientation, | |||
| 1640 | XWDFileHeader *win, | |||
| 1641 | const char *data) | |||
| 1642 | { | |||
| 1643 | unsigned long swaptest = 1; | |||
| 1644 | int iwb = win->bytes_per_line; | |||
| 1645 | register int i; | |||
| 1646 | int bytes; | |||
| 1647 | unsigned char *buffer = (unsigned char *)data; | |||
| 1648 | register int ocount=0; | |||
| 1649 | static char hex[] = "0123456789abcdef"; | |||
| 1650 | ||||
| 1651 | if (orientation == LANDSCAPE) { | |||
| 1652 | /* read in and rotate the entire image */ | |||
| 1653 | /* The Postscript language has a rotate operator, but using it | |||
| 1654 | * seem to make printing (at least on the Apple Laserwriter | |||
| 1655 | * take about 10 times as long (40 minutes for a 1024x864 full-screen | |||
| 1656 | * dump)! Therefore, we rotate the image here. | |||
| 1657 | */ | |||
| 1658 | int ocol = ih; | |||
| 1659 | int owidth = (ih+31)/32; /* width of rotated image, in bytes */ | |||
| 1660 | int oheight = (iw+31)/32; /* height of rotated image, in scanlines */ | |||
| 1661 | register char *p, *q; | |||
| 1662 | char *obuf; | |||
| 1663 | unsigned char *ibuf; | |||
| 1664 | owidth *= 4; | |||
| 1665 | oheight *= 32; | |||
| 1666 | ||||
| 1667 | /* Allocate buffer for the entire rotated image (output). | |||
| 1668 | * Owidth and Oheight are rounded up to a multiple of 32 bits, | |||
| 1669 | * to avoid special cases at the boundaries | |||
| 1670 | */ | |||
| 1671 | obuf = malloc((unsigned)(owidth*oheight)); | |||
| 1672 | if (obuf==NULL((void*)0)) { | |||
| 1673 | fprintf(stderr__stderrp,"xpr: cannot allocate %d bytes\n",owidth*oheight); | |||
| 1674 | exit(1); | |||
| 1675 | } | |||
| 1676 | bzero(obuf,owidth*oheight)__builtin___memset_chk (obuf, 0, owidth*oheight, __builtin_object_size (obuf, 0)); | |||
| 1677 | ||||
| 1678 | ibuf = (unsigned char *)malloc((unsigned)(iwb + 3)); | |||
| 1679 | for (i=0;i<ih;i++) { | |||
| 1680 | memmove((char *)ibuf, (char *)buffer, iwb)__builtin___memmove_chk ((char *)ibuf, (char *)buffer, iwb, __builtin_object_size ((char *)ibuf, 0)); | |||
| 1681 | buffer += iwb; | |||
| 1682 | if (!(*(char *) &swaptest)) | |||
| 1683 | _swaplong((char *)ibuf,(long)iwb); | |||
| 1684 | ps_bitrot(ibuf,iw,--ocol,owidth,obuf); | |||
| 1685 | } | |||
| 1686 | if (!(*(char *) &swaptest)) | |||
| 1687 | _swaplong(obuf,(long)(iw*owidth)); | |||
| 1688 | q = &obuf[iw*owidth]; | |||
| 1689 | bytes = (ih+7)/8; | |||
| 1690 | for (p=obuf;p<q;p+=owidth) | |||
| 1691 | ocount = ps_putbuf((unsigned char *)p,bytes,ocount,flags&F_COMPACT128); | |||
| 1692 | } | |||
| 1693 | else { | |||
| 1694 | for (i=0;i<ih;i++) { | |||
| 1695 | ocount = ps_putbuf(buffer,(iw+7)/8,ocount,flags&F_COMPACT128); | |||
| 1696 | buffer += iwb; | |||
| 1697 | } | |||
| 1698 | } | |||
| 1699 | if (flags & F_COMPACT128) { | |||
| 1700 | if (ocount) { | |||
| 1701 | /* pad to an integral number of lines */ | |||
| 1702 | while (ocount++ < LINELEN72) | |||
| 1703 | /* for debugging, pad with a "random" value */ | |||
| 1704 | putchar(hex[ocount&15]); | |||
| 1705 | putchar('\n'); | |||
| 1706 | } | |||
| 1707 | } | |||
| 1708 | } | |||
| 1709 | ||||
| 1710 | static const | |||
| 1711 | unsigned char _reverse_byte[0x100] = { | |||
| 1712 | 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, | |||
| 1713 | 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0, | |||
| 1714 | 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, | |||
| 1715 | 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, | |||
| 1716 | 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, | |||
| 1717 | 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, | |||
| 1718 | 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, | |||
| 1719 | 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc, | |||
| 1720 | 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, | |||
| 1721 | 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2, | |||
| 1722 | 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea, | |||
| 1723 | 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa, | |||
| 1724 | 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, | |||
| 1725 | 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6, | |||
| 1726 | 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, | |||
| 1727 | 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, | |||
| 1728 | 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1, | |||
| 1729 | 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1, | |||
| 1730 | 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, | |||
| 1731 | 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9, | |||
| 1732 | 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, | |||
| 1733 | 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5, | |||
| 1734 | 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, | |||
| 1735 | 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, | |||
| 1736 | 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, | |||
| 1737 | 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3, | |||
| 1738 | 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, | |||
| 1739 | 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, | |||
| 1740 | 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, | |||
| 1741 | 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, | |||
| 1742 | 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, | |||
| 1743 | 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff | |||
| 1744 | }; | |||
| 1745 | ||||
| 1746 | void _invbits ( | |||
| 1747 | register unsigned char *b, | |||
| 1748 | register long n) | |||
| 1749 | { | |||
| 1750 | do { | |||
| 1751 | *b = ~*b; | |||
| 1752 | b++; | |||
| 1753 | } while (--n > 0); | |||
| 1754 | ||||
| 1755 | } | |||
| 1756 | ||||
| 1757 | /* copied from lib/X/XPutImage.c */ | |||
| 1758 | ||||
| 1759 | void _swapbits ( | |||
| 1760 | register unsigned char *b, | |||
| 1761 | register long n) | |||
| 1762 | { | |||
| 1763 | do { | |||
| 1764 | *b = _reverse_byte[*b]; | |||
| 1765 | b++; | |||
| 1766 | } while (--n > 0); | |||
| 1767 | ||||
| 1768 | } | |||
| 1769 | ||||
| 1770 | void _swapshort ( | |||
| 1771 | register char *bp, | |||
| 1772 | register long n) | |||
| 1773 | { | |||
| 1774 | register char c; | |||
| 1775 | register char *ep = bp + n; | |||
| 1776 | do { | |||
| 1777 | c = *bp; | |||
| 1778 | *bp = *(bp + 1); | |||
| 1779 | bp++; | |||
| 1780 | *bp = c; | |||
| 1781 | bp++; | |||
| 1782 | } | |||
| 1783 | while (bp < ep); | |||
| 1784 | } | |||
| 1785 | ||||
| 1786 | void _swaplong ( | |||
| 1787 | register char *bp, | |||
| 1788 | register long n) | |||
| 1789 | { | |||
| 1790 | register char c; | |||
| 1791 | register char *ep = bp + n; | |||
| 1792 | register char *sp; | |||
| 1793 | do { | |||
| 1794 | sp = bp + 3; | |||
| 1795 | c = *sp; | |||
| 1796 | *sp = *bp; | |||
| 1797 | *bp++ = c; | |||
| 1798 | sp = bp + 1; | |||
| 1799 | c = *sp; | |||
| 1800 | *sp = *bp; | |||
| 1801 | *bp++ = c; | |||
| 1802 | bp += 2; | |||
| 1803 | } | |||
| 1804 | while (bp < ep); | |||
| 1805 | } | |||
| 1806 | ||||
| 1807 | /* Dump some bytes in hex, with bits in each byte reversed | |||
| 1808 | * Ocount is number of chacters that have been written to the current | |||
| 1809 | * output line. It's new value is returned as the result of the function. | |||
| 1810 | * Ocount is ignored (and the return value is meaningless) if compact==0. | |||
| 1811 | */ | |||
| 1812 | static | |||
| 1813 | int ps_putbuf( | |||
| 1814 | register unsigned char *s, /* buffer to dump */ | |||
| 1815 | register int n, /* number of BITS to dump */ | |||
| 1816 | register int ocount, /* position on output line for next char */ | |||
| 1817 | int compact) /* if non-zero, do compaction (see below) */ | |||
| 1818 | { | |||
| 1819 | register int ffcount = 0; | |||
| 1820 | static char hex[] = "0123456789abcdef"; | |||
| 1821 | #define PUT(c){ putchar(c); if (++ocount>=72) { putchar('\n'); ocount=0; }} { putchar(c); if (++ocount>=LINELEN72) \ | |||
| 1822 | { putchar('\n'); ocount=0; }} | |||
| 1823 | ||||
| 1824 | if (compact) { | |||
| 1825 | /* The following loop puts out the bits of the image in hex, | |||
| 1826 | * compressing runs of white space (represented by one bits) | |||
| 1827 | * according the the following simple algorithm: A run of n | |||
| 1828 | * 'ff' bytes (i.e., bytes with value 255--all ones), where | |||
| 1829 | * 1<=n<=255, is represented by a single 'ff' byte followed by a | |||
| 1830 | * byte containing n. | |||
| 1831 | * On a typical dump of a full screen pretty much covered by | |||
| 1832 | * black-on-white text windows, this compression decreased the | |||
| 1833 | * size of the file from 223 Kbytes to 63 Kbytes. | |||
| 1834 | * Of course, another factor of two could be saved by sending | |||
| 1835 | * the bytes 'as is' rather than in hex, using some sort of | |||
| 1836 | * escape convention to avoid problems with control characters. | |||
| 1837 | * Another popular encoding is to pack three bytes into 4 'sixels' | |||
| 1838 | * as in the LN03, etc, but I'm too lazy to write the necessary | |||
| 1839 | * PostScript code to unpack fancier representations. | |||
| 1840 | */ | |||
| 1841 | while (n--) { | |||
| 1842 | if (*s == 0xff) { | |||
| 1843 | if (++ffcount == 255) { | |||
| 1844 | PUT('f'){ putchar('f'); if (++ocount>=72) { putchar('\n'); ocount= 0; }}; PUT('f'){ putchar('f'); if (++ocount>=72) { putchar('\n'); ocount= 0; }}; | |||
| 1845 | PUT('f'){ putchar('f'); if (++ocount>=72) { putchar('\n'); ocount= 0; }}; PUT('f'){ putchar('f'); if (++ocount>=72) { putchar('\n'); ocount= 0; }}; | |||
| 1846 | ffcount = 0; | |||
| 1847 | } | |||
| 1848 | } | |||
| 1849 | else { | |||
| 1850 | if (ffcount) { | |||
| 1851 | PUT('f'){ putchar('f'); if (++ocount>=72) { putchar('\n'); ocount= 0; }}; PUT('f'){ putchar('f'); if (++ocount>=72) { putchar('\n'); ocount= 0; }}; | |||
| 1852 | PUT(hex[ffcount >> 4]){ putchar(hex[ffcount >> 4]); if (++ocount>=72) { putchar ('\n'); ocount=0; }}; | |||
| 1853 | PUT(hex[ffcount & 0xf]){ putchar(hex[ffcount & 0xf]); if (++ocount>=72) { putchar ('\n'); ocount=0; }}; | |||
| 1854 | ffcount = 0; | |||
| 1855 | } | |||
| 1856 | PUT(hex1[*s]){ putchar(hex1[*s]); if (++ocount>=72) { putchar('\n'); ocount =0; }}; | |||
| 1857 | PUT(hex2[*s]){ putchar(hex2[*s]); if (++ocount>=72) { putchar('\n'); ocount =0; }}; | |||
| 1858 | } | |||
| 1859 | s++; | |||
| 1860 | } | |||
| 1861 | if (ffcount) { | |||
| 1862 | PUT('f'){ putchar('f'); if (++ocount>=72) { putchar('\n'); ocount= 0; }}; PUT('f'){ putchar('f'); if (++ocount>=72) { putchar('\n'); ocount= 0; }}; | |||
| 1863 | PUT(hex[ffcount >> 4]){ putchar(hex[ffcount >> 4]); if (++ocount>=72) { putchar ('\n'); ocount=0; }}; | |||
| 1864 | PUT(hex[ffcount & 0xf]){ putchar(hex[ffcount & 0xf]); if (++ocount>=72) { putchar ('\n'); ocount=0; }}; | |||
| 1865 | ffcount = 0; | |||
| 1866 | } | |||
| 1867 | } | |||
| 1868 | else { /* no compaction: just dump the image in hex (bits reversed) */ | |||
| 1869 | while (n--) { | |||
| 1870 | putchar(hex1[*s]); | |||
| 1871 | putchar(hex2[*s++]); | |||
| 1872 | } | |||
| 1873 | putchar('\n'); | |||
| 1874 | } | |||
| 1875 | return ocount; | |||
| 1876 | } | |||
| 1877 | ||||
| 1878 | static | |||
| 1879 | void ps_bitrot( | |||
| 1880 | unsigned char *s, | |||
| 1881 | register int n, | |||
| 1882 | int col, | |||
| 1883 | register int owidth, | |||
| 1884 | char *obuf) | |||
| 1885 | /* s points to a chunk of memory and n is its width in bits. | |||
| 1886 | * The algorithm is, roughly, | |||
| 1887 | * for (i=0;i<n;i++) { | |||
| 1888 | * OR the ith bit of s into the ith row of the | |||
| 1889 | * (col)th column of obuf | |||
| 1890 | * } | |||
| 1891 | * Assume VAX bit and byte ordering for s: | |||
| 1892 | * The ith bit of s is s[j]&(1<<k) where i=8*j+k. | |||
| 1893 | * It can also be retrieved as t[j]&(1<<k), where t=(int*)s and i=32*j+k. | |||
| 1894 | * Also assume VAX bit and byte ordering for each row of obuf. | |||
| 1895 | * Ps_putbuf() takes care of converting to Motorola 68000 byte and bit | |||
| 1896 | * ordering. The following code is very carefully tuned to yield a very | |||
| 1897 | * tight loop on the VAX, since it easily dominates the entire running | |||
| 1898 | * time of this program. In particular, iwordp is declared last, since | |||
| 1899 | * there aren't enough registers, and iwordp is referenced only once | |||
| 1900 | * every 32 times through the loop. | |||
| 1901 | */ | |||
| 1902 | { | |||
| 1903 | register int mask = 1<<(col%32); | |||
| 1904 | register int iword; /* current input word (*iwordp) */ | |||
| 1905 | register int b = 0; /* number of bits in iword left to examine */ | |||
| 1906 | register char *opos = obuf + (col/32)*4; | |||
| 1907 | /* pointer to word of obuf to receive next output bit */ | |||
| 1908 | register int *iwordp = (int *) s; /* pointer to next word of s */ | |||
| 1909 | ||||
| 1910 | while (--n>=0) { | |||
| 1911 | if (--b < 0) { | |||
| 1912 | iword = *iwordp++; | |||
| 1913 | b = 31; | |||
| 1914 | } | |||
| 1915 | if (iword & 1) { | |||
| 1916 | *(int *)opos |= mask; | |||
| 1917 | } | |||
| 1918 | opos += owidth; | |||
| 1919 | iword >>= 1; | |||
| 1920 | } | |||
| 1921 | } | |||
| 1922 | ||||
| 1923 | /* fullread() is the same as read(), except that it guarantees to | |||
| 1924 | read all the bytes requested. */ | |||
| 1925 | ||||
| 1926 | static | |||
| 1927 | void fullread ( | |||
| 1928 | int file, | |||
| 1929 | char *data, | |||
| 1930 | int nbytes) | |||
| 1931 | { | |||
| 1932 | int bytes_read; | |||
| 1933 | while ((bytes_read = read(file, data, nbytes)) != nbytes) { | |||
| 1934 | if (bytes_read < 0) { | |||
| 1935 | perror ("error while reading standard input"); | |||
| 1936 | return; | |||
| 1937 | } | |||
| 1938 | else if (bytes_read == 0) { | |||
| 1939 | fprintf (stderr__stderrp, "xpr: premature end of file\n"); | |||
| 1940 | return; | |||
| 1941 | } | |||
| 1942 | nbytes -= bytes_read; | |||
| 1943 | data += bytes_read; | |||
| 1944 | } | |||
| 1945 | } |