📄 postdmd.c
字号:
/* * * postdmd - PostScript translator for DMD bitmap files. * * A simple program that can be used to print DMD bitmaps on PostScript printers. * Much of the code was borrowed from abm, which was written by Guy Riddle. * * Although the program supports two different input bitmap formats, by far the * most important is the Eighth (and Ninth) Edition bitfile format. A bitmap in * the bitfile format begins with a 10 byte header with the first two bytes set to * zero. The next 8 bytes set the x and y coordinates of the bitmap's origin and * corner (ie. the upper left and lower right corners). The compressed raster data * follows the header and consists of control bytes followed an appropriate number * of data bytes. Control bytes (ie. n) less than 127 means read the next 2*n bytes * of raster data directly from the input file, while if n is larger than 128 we * read two bytes from the input file and replicate the bytes n-128 times. After * each scan line is recovered it's exclusive-or'd with the preceeding line to * generate the real raster data. * * After each raster line is recovered postdmd encodes it in a slightly different * format that's designed to be unpacked by a PostScript procedure that's defined * in the prologue. By default no exclusive-or'ing is done and packing of pattern * data can be based on any number of bytes rather than just the next two bytes. * By default 6 byte patterns are used, but any number can be selected with the -b * option. A non-positive argument (eg. -b0) disables all pattern encoding. Larger * patterns increase the size of the output file, but reduce the work load that's * forced on the PostScript interpreter. The default choices I've made (ie. 6 byte * patterns and no exclusive-or'ing) do a decent balancing job across currently * available PostScript printers. Larger patterns (eg. -b16) increase the output * file size, but may be appropriate if you're running at a high baud rate (eg. * 19.2KB), while smaller patter size (eg. -b4) may help if you've got a printer * with a fast processor (eg. a PS-810). * * The encoding produced by the program (and decoded on the printer) looks like, * * bytes patterns count * * where bytes and count are decimal integers and patterns is a hex string. Bytes * is the number of bytes represented by the hex patterns and count is the number * of additional times the patterns should be repeated. For example, * * 2 FFFF 4 * 5 FFFFFFFFFF 1 * 10 FFFFFFFFFFFFFFFFFFFF 0 * * all represent 10 consecutive bytes of ones. Scanlines are terminated by a 0 on * a line by itself. * * The PostScript prologue is copied from *prologue before any of the input files * are translated. The program expects that the following PostScript procedures * are defined in that file: * * setup * * mark ... setup - * * Handles special initialization stuff that depends on how this program * was called. Expects to find a mark followed by key/value pairs on the * stack. The def operator is applied to each pair up to the mark, then * the default state is set up. * * pagesetup * * page pagesetup - * * Does whatever is needed to set things up for the next page. Expects * to find the current page number on the stack. * * bitmap * * v8format flip scanlength scanlines bitmap - * * Prints the bitmap that's read from standard input. The bitmap consists * of scanlines lines, each of which includes scanlength pixels. If * v8format is true the picture is assumed to be an Eighth Edition bitmap, * and the exclusive-or'ing will be done on the printer. * * done * * done * * Makes sure the last page is printed. Only needed when we're printing * more than one page on each sheet of paper. * * Many default values, like the magnification and orientation, are defined in * the prologue, which is where they belong. If they're changed (by options), an * appropriate definition is made after the prologue is added to the output file. * The -P option passes arbitrary PostScript through to the output file. Among * other things it can be used to set (or change) values that can't be accessed by * other options. * */#include <stdio.h>#include <signal.h>#include <ctype.h>#ifdef plan9#define isascii(c) ((unsigned char)(c)<=0177)#endif#include <sys/types.h>#include <fcntl.h>#include "comments.h" /* PostScript file structuring comments */#include "gen.h" /* general purpose definitions */#include "path.h" /* for the prologue */#include "ext.h" /* external variable declarations */char *optnames = "a:b:c:fm:n:o:p:ux:y:A:C:E:J:L:P:DI";char *prologue = POSTDMD; /* default PostScript prologue */char *formfile = FORMFILE; /* stuff for multiple pages per sheet */int bbox[2] = {0, 0}; /* upper right coordinates only */int formsperpage = 1; /* page images on each piece of paper */int copies = 1; /* and this many copies of each sheet */int bytespp = 6; /* bytes per pattern - on output */int flip = FALSE; /* ones complement the bitmap */int v8undo = TRUE; /* xor'ing done on host if TRUE */int v8format = FALSE; /* for Eighth Edition bitmaps */int page = 0; /* last page we worked on */int printed = 0; /* and the number of pages printed */int patterns; /* 16 bit patterns per scan line */int scanlines; /* lines in the bitmap */int patcount = 0; /* should be patterns * scanlines */char *raster = NULL; /* next raster line */char *prevrast = NULL; /* and the previous one - v8format */char *rptr; /* next free byte in raster */char *eptr; /* one past the last byte in raster */FILE *fp_in = NULL; /* read from this file */FILE *fp_out = stdout; /* and write stuff here */FILE *fp_acct = NULL; /* for accounting data *//*****************************************************************************/main(agc, agv) int agc; char *agv[];{/* * * A simple program that translates DMD bitmap files into PostScript. There can * be more than one bitmap per file, but none can be split across input files. * Each bitmap goes on a page by itself. * */ argc = agc; /* other routines may want them */ argv = agv; prog_name = argv[0]; /* really just for error messages */ init_signals(); /* sets up interrupt handling */ header(); /* PostScript header comments */ options(); /* handle the command line options */ setup(); /* for PostScript */ arguments(); /* followed by each input file */ done(); /* print the last page etc. */ account(); /* job accounting data */ exit(x_stat); /* not much could be wrong */} /* End of main *//*****************************************************************************/init_signals(){/* * * Make sure we handle interrupts. * */ if ( signal(SIGINT, interrupt) == SIG_IGN ) { signal(SIGINT, SIG_IGN); signal(SIGQUIT, SIG_IGN); signal(SIGHUP, SIG_IGN); } else { signal(SIGHUP, interrupt); signal(SIGQUIT, interrupt); } /* End else */ signal(SIGTERM, interrupt);} /* End of init_signals *//*****************************************************************************/header(){ int ch; /* return value from getopt() */ int old_optind = optind; /* for restoring optind - should be 1 *//* * * Scans the option list looking for things, like the prologue file, that we need * right away but could be changed from the default. Doing things this way is an * attempt to conform to Adobe's latest file structuring conventions. In particular * they now say there should be nothing executed in the prologue, and they have * added two new comments that delimit global initialization calls. Once we know * where things really are we write out the job header, follow it by the prologue, * and then add the ENDPROLOG and BEGINSETUP comments. * */ while ( (ch = getopt(argc, argv, optnames)) != EOF ) if ( ch == 'L' ) prologue = optarg; else if ( ch == '?' ) error(FATAL, ""); optind = old_optind; /* get ready for option scanning */ fprintf(stdout, "%s", CONFORMING); fprintf(stdout, "%s %s\n", VERSION, PROGRAMVERSION); fprintf(stdout, "%s %s\n", DOCUMENTFONTS, ATEND); fprintf(stdout, "%s %s\n", PAGES, ATEND); fprintf(stdout, "%s", ENDCOMMENTS); if ( cat(prologue) == FALSE ) error(FATAL, "can't read %s", prologue); fprintf(stdout, "%s", ENDPROLOG); fprintf(stdout, "%s", BEGINSETUP); fprintf(stdout, "mark\n");} /* End of header *//*****************************************************************************/options(){ int ch; /* return value from getopt() *//* * * Reads and processes the command line options. Added the -P option so arbitrary * PostScript code can be passed through. Expect it could be useful for changing * definitions in the prologue for which options have not been defined. * */ while ( (ch = getopt(argc, argv, optnames)) != EOF ) { switch ( ch ) { case 'a': /* aspect ratio */ fprintf(stdout, "/aspectratio %s def\n", optarg); break; case 'b': /* bytes per pattern */ bytespp = atoi(optarg); break; case 'c': /* copies */ copies = atoi(optarg); fprintf(stdout, "/#copies %s store\n", optarg); break; case 'f': /* ones complement - sort of */ flip = TRUE; break; case 'm': /* magnification */ fprintf(stdout, "/magnification %s def\n", optarg); break; case 'n': /* forms per page */ formsperpage = atoi(optarg); fprintf(stdout, "%s %s\n", FORMSPERPAGE, optarg); fprintf(stdout, "/formsperpage %s def\n", optarg); break; case 'o': /* output page list */ out_list(optarg); break; case 'p': /* landscape or portrait mode */ if ( *optarg == 'l' ) fprintf(stdout, "/landscape true def\n"); else fprintf(stdout, "/landscape false def\n"); break; case 'u': /* don't undo Eighth Edition bitmaps */ v8undo = FALSE; break; case 'x': /* shift things horizontally */ fprintf(stdout, "/xoffset %s def\n", optarg); break; case 'y': /* and vertically on the page */ fprintf(stdout, "/yoffset %s def\n", optarg); break; case 'A': /* force job accounting */ case 'J': if ( (fp_acct = fopen(optarg, "a")) == NULL ) error(FATAL, "can't open accounting file %s", optarg); break; case 'C': /* copy file straight to output */ if ( cat(optarg) == FALSE ) error(FATAL, "can't read %s", optarg); break; case 'E': /* text font encoding - unnecessary */ fontencoding = optarg; break; case 'L': /* PostScript prologue file */ prologue = optarg; break; case 'P': /* PostScript pass through */ fprintf(stdout, "%s\n", optarg); break; case 'R': /* special global or page level request */ saverequest(optarg); break; case 'D': /* debug flag */ debug = ON; break; case 'I': /* ignore FATAL errors */ ignore = ON; break; case '?': /* don't understand the option */ error(FATAL, ""); break; default: /* don't know what to do for ch */ error(FATAL, "missing case for option %c\n", ch); break; } /* End switch */ } /* End while */ argc -= optind; /* get ready for non-option args */ argv += optind;} /* End of options *//*****************************************************************************/setup(){/*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -