| 1 |
/*============================================================================= |
| 2 |
pbmtolps |
| 3 |
=============================================================================== |
| 4 |
|
| 5 |
Convert a PBM image to Postscript. The output Postscript uses lines instead |
| 6 |
of the image operator to generate a (device dependent) picture which will be |
| 7 |
imaged much faster. |
| 8 |
|
| 9 |
The Postscript path length is constrained to be at most 1000 vertices so that |
| 10 |
no limits are overrun on the Apple Laserwriter and (presumably) no other |
| 11 |
printers. The typical limit is 1500. See "4.4 Path Construction" and |
| 12 |
"Appendix B: Implementation Limits" in: PostScript Language Reference Manual |
| 13 |
https://www.adobe.com/content/dam/acom/en/devnet/actionscript/ |
| 14 |
articles/psrefman.pdf |
| 15 |
|
| 16 |
To do: |
| 17 |
make sure encapsulated format is correct |
| 18 |
repetition of black-white strips |
| 19 |
make it more device independent (is this possible?) |
| 20 |
|
| 21 |
Author: |
| 22 |
George Phillips <[email protected]> |
| 23 |
Department of Computer Science |
| 24 |
University of British Columbia |
| 25 |
=============================================================================*/ |
| 26 |
#include <stdbool.h> |
| 27 |
|
| 28 |
#include "pm_c_util.h" |
| 29 |
#include "mallocvar.h" |
| 30 |
#include "nstring.h" |
| 31 |
#include "shhopt.h" |
| 32 |
#include "pbm.h" |
| 33 |
|
| 34 |
|
| 35 |
static float const MAX_DPI = 5000; |
| 36 |
static float const MIN_DPI = 10; |
| 37 |
static unsigned int const MAX_PATH_VERTICES = 1000; |
| 38 |
|
| 39 |
|
| 40 |
struct CmdlineInfo { |
| 41 |
/* All the information the user supplied in the command line, in a form |
| 42 |
easy for the program to use. |
| 43 |
*/ |
| 44 |
const char * inputFileName; /* File name of input file */ |
| 45 |
unsigned int inputFileSpec; /* Input file name specified */ |
| 46 |
float lineWidth; /* Line width, if specified */ |
| 47 |
unsigned int lineWidthSpec; /* Line width specified */ |
| 48 |
float dpi; /* Resolution in DPI, if specified */ |
| 49 |
unsigned int dpiSpec; /* Resolution specified */ |
| 50 |
}; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
static void |
| 55 |
validateDpi(float const dpi) { |
| 56 |
|
| 57 |
if (dpi > MAX_DPI || dpi < MIN_DPI) |
| 58 |
pm_error("Specified DPI value out of range (%f)", dpi); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
static void |
| 64 |
parseCommandLine(int argc, |
| 65 |
const char ** const argv, |
| 66 |
struct CmdlineInfo * const cmdlineP) { |
| 67 |
/*---------------------------------------------------------------------------- |
| 68 |
Parse program command line described in Unix standard form by argc |
| 69 |
and argv. Return the information in the options as *cmdlineP. |
| 70 |
-----------------------------------------------------------------------------*/ |
| 71 |
optEntry * option_def; /* malloc'ed */ |
| 72 |
optStruct3 opt; |
| 73 |
unsigned int option_def_index; |
| 74 |
|
| 75 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 76 |
|
| 77 |
option_def_index = 0; /* incremented by OPTENTRY */ |
| 78 |
OPTENT3(0, "linewidth", OPT_FLOAT, &cmdlineP->lineWidth, |
| 79 |
&cmdlineP->lineWidthSpec, 0); |
| 80 |
OPTENT3(0, "dpi", OPT_FLOAT, &cmdlineP->dpi, |
| 81 |
&cmdlineP->dpiSpec, 0); |
| 82 |
|
| 83 |
opt.opt_table = option_def; |
| 84 |
opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ |
| 85 |
opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */ |
| 86 |
|
| 87 |
pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0); |
| 88 |
/* Uses and sets argc, argv, and some of *cmdlineP and others. */ |
| 89 |
|
| 90 |
if (cmdlineP->dpiSpec) |
| 91 |
validateDpi(cmdlineP->dpi); |
| 92 |
else |
| 93 |
cmdlineP->dpi = 300; |
| 94 |
|
| 95 |
if (argc-1 < 1) |
| 96 |
cmdlineP->inputFileName = "-"; |
| 97 |
else { |
| 98 |
if (argc-1 > 1) |
| 99 |
pm_error("Program takes zero or one argument (filename). You " |
| 100 |
"specified %u", argc-1); |
| 101 |
else |
| 102 |
cmdlineP->inputFileName = argv[1]; |
| 103 |
} |
| 104 |
|
| 105 |
if (cmdlineP->inputFileName[0] == '-' && |
| 106 |
cmdlineP->inputFileName[1] == '\0') |
| 107 |
cmdlineP->inputFileSpec = false; |
| 108 |
else |
| 109 |
cmdlineP->inputFileSpec = true; |
| 110 |
|
| 111 |
free(option_def); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
static void |
| 117 |
validateLineWidth(float const scCols, |
| 118 |
float const scRows, |
| 119 |
float const lineWidth) { |
| 120 |
|
| 121 |
if (lineWidth >= scCols || lineWidth >= scRows) |
| 122 |
pm_error("Absurdly large -linewidth value (%f)", lineWidth); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
static void |
| 128 |
doRaster(FILE * const ifP, |
| 129 |
unsigned int const cols, |
| 130 |
unsigned int const rows, |
| 131 |
int const format, |
| 132 |
FILE * const ofP) { |
| 133 |
|
| 134 |
bit * bitrow; |
| 135 |
unsigned int row; |
| 136 |
unsigned int vertexCt; |
| 137 |
/* Number of vertices drawn since last stroke command */ |
| 138 |
|
| 139 |
bitrow = pbm_allocrow(cols); |
| 140 |
|
| 141 |
for (row = 0, vertexCt = 0; row < rows; ++row) { |
| 142 |
unsigned int col; |
| 143 |
bool firstRun; |
| 144 |
|
| 145 |
firstRun = true; /* initial value */ |
| 146 |
|
| 147 |
pbm_readpbmrow(ifP, bitrow, cols, format); |
| 148 |
|
| 149 |
/* output white-strip + black-strip sequences */ |
| 150 |
|
| 151 |
for (col = 0; col < cols; ) { |
| 152 |
unsigned int whiteCt; |
| 153 |
unsigned int blackCt; |
| 154 |
|
| 155 |
for (whiteCt = 0; col < cols && bitrow[col] == PBM_WHITE; ++col) |
| 156 |
++whiteCt; |
| 157 |
for (blackCt = 0; col < cols && bitrow[col] == PBM_BLACK; ++col) |
| 158 |
++blackCt; |
| 159 |
|
| 160 |
if (blackCt > 0) { |
| 161 |
if (vertexCt > MAX_PATH_VERTICES) { |
| 162 |
printf("m "); |
| 163 |
vertexCt = 0; |
| 164 |
} |
| 165 |
|
| 166 |
if (firstRun) { |
| 167 |
printf("%u %u moveto %u 0 rlineto\n", |
| 168 |
whiteCt, row, blackCt); |
| 169 |
firstRun = false; |
| 170 |
} else |
| 171 |
printf("%u %u a\n", blackCt, whiteCt); |
| 172 |
|
| 173 |
vertexCt += 2; |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
pbm_freerow(bitrow); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
static void |
| 183 |
pbmtolps(FILE * const ifP, |
| 184 |
FILE * const ofP, |
| 185 |
struct CmdlineInfo const cmdline) { |
| 186 |
|
| 187 |
const char * const psName = |
| 188 |
cmdline.inputFileSpec ? cmdline.inputFileName : "noname"; |
| 189 |
|
| 190 |
int rows; |
| 191 |
int cols; |
| 192 |
int format; |
| 193 |
float scRows, scCols; |
| 194 |
/* Dimensions of the printed image in points */ |
| 195 |
|
| 196 |
pbm_readpbminit(ifP, &cols, &rows, &format); |
| 197 |
|
| 198 |
scRows = (float) rows / (cmdline.dpi / 72.0); |
| 199 |
scCols = (float) cols / (cmdline.dpi / 72.0); |
| 200 |
|
| 201 |
if (cmdline.lineWidthSpec) |
| 202 |
validateLineWidth(scCols, scRows, cmdline.lineWidth); |
| 203 |
|
| 204 |
fputs("%!PS-Adobe-2.0 EPSF-2.0\n", ofP); |
| 205 |
fputs("%%Creator: pbmtolps\n", ofP); |
| 206 |
fprintf(ofP, "%%%%Title: %s\n", psName); |
| 207 |
fprintf(ofP, "%%%%BoundingBox: %d %d %d %d\n", |
| 208 |
(int)(305.5 - scCols / 2.0), |
| 209 |
(int)(395.5 - scRows / 2.0), |
| 210 |
(int)(306.5 + scCols / 2.0), |
| 211 |
(int)(396.5 + scRows / 2.0)); |
| 212 |
fputs("%%EndComments\n", ofP); |
| 213 |
fputs("%%EndProlog\n", ofP); |
| 214 |
fputs("gsave\n", ofP); |
| 215 |
|
| 216 |
fprintf(ofP, "%f %f translate\n", |
| 217 |
306.0 - scCols / 2.0, 396.0 + scRows / 2.0); |
| 218 |
fprintf(ofP, "72 %f div dup neg scale\n", cmdline.dpi); |
| 219 |
|
| 220 |
if (cmdline.lineWidthSpec) |
| 221 |
fprintf(ofP, "%f setlinewidth\n", cmdline.lineWidth); |
| 222 |
|
| 223 |
fputs("/a { 0 rmoveto 0 rlineto } def\n", ofP); |
| 224 |
fputs("/m { currentpoint stroke newpath moveto } def\n", ofP); |
| 225 |
fputs("newpath 0 0 moveto\n", ofP); |
| 226 |
|
| 227 |
doRaster(ifP, cols, rows, format, ofP); |
| 228 |
|
| 229 |
fputs("stroke grestore showpage\n", ofP); |
| 230 |
fputs("%%Trailer\n", ofP); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
int |
| 236 |
main(int argc, const char *argv[]) { |
| 237 |
FILE * ifP; |
| 238 |
struct CmdlineInfo cmdline; |
| 239 |
|
| 240 |
pm_proginit(&argc, argv); |
| 241 |
|
| 242 |
parseCommandLine(argc, argv, &cmdline); |
| 243 |
|
| 244 |
ifP = pm_openr(cmdline.inputFileName); |
| 245 |
|
| 246 |
pbmtolps(ifP, stdout, cmdline); |
| 247 |
|
| 248 |
pm_close(ifP); |
| 249 |
|
| 250 |
return 0; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|