| 1 |
/* |
| 2 |
|
| 3 |
Convert an AutoCAD slide (.sld) file to PPM format |
| 4 |
|
| 5 |
An AutoCAD slide is a compressed sequence of vectors and filled |
| 6 |
polygons. The ppmdraw package is used to scan convert these |
| 7 |
geometrical objects into a portable pixmap. |
| 8 |
|
| 9 |
Author: |
| 10 |
John Walker |
| 11 |
Autodesk SA |
| 12 |
Avenue des Champs-Montants 14b |
| 13 |
CH-2074 MARIN |
| 14 |
Switzerland |
| 15 |
Usenet: [email protected] |
| 16 |
Fax: 038/33 88 15 |
| 17 |
Voice: 038/33 76 33 |
| 18 |
|
| 19 |
Permission to use, copy, modify, and distribute this software and |
| 20 |
its documentation for any purpose and without fee is hereby |
| 21 |
granted, without any conditions or restrictions. This software is |
| 22 |
provided "as is" without express or implied warranty. |
| 23 |
|
| 24 |
*/ |
| 25 |
|
| 26 |
#include <limits.h> |
| 27 |
#include <assert.h> |
| 28 |
#include <string.h> |
| 29 |
#include <math.h> |
| 30 |
|
| 31 |
#include "pm_c_util.h" |
| 32 |
#include "ppm.h" |
| 33 |
#include "ppmdraw.h" |
| 34 |
#include "nstring.h" |
| 35 |
|
| 36 |
#include "autocad.h" /* AutoCAD standard color assignments */ |
| 37 |
|
| 38 |
|
| 39 |
/* Define a variable type accepting numbers -127 <= n <= 127. But note |
| 40 |
that we still expect it to act UNSIGNED. |
| 41 |
*/ |
| 42 |
|
| 43 |
#define smallint unsigned char /* Small integers */ |
| 44 |
|
| 45 |
#define EOS '\0' |
| 46 |
|
| 47 |
struct spoint { |
| 48 |
/*---------------------------------------------------------------------------- |
| 49 |
A screen point |
| 50 |
-----------------------------------------------------------------------------*/ |
| 51 |
int x, y; |
| 52 |
}; |
| 53 |
|
| 54 |
struct spolygon { |
| 55 |
/*---------------------------------------------------------------------------- |
| 56 |
A screen polygon |
| 57 |
-----------------------------------------------------------------------------*/ |
| 58 |
int npoints; /* Number of points in polygon */ |
| 59 |
int fill; /* Fill type */ |
| 60 |
struct spoint pt[11]; /* The points */ |
| 61 |
}; |
| 62 |
|
| 63 |
struct svector { |
| 64 |
/*---------------------------------------------------------------------------- |
| 65 |
A screen vector |
| 66 |
-----------------------------------------------------------------------------*/ |
| 67 |
struct spoint f; /* From point */ |
| 68 |
struct spoint t; /* To point */ |
| 69 |
}; |
| 70 |
|
| 71 |
typedef void (slvecfn)(struct svector * vec, int color); |
| 72 |
typedef void (slfloodfn)(struct spolygon * poly, int color); |
| 73 |
|
| 74 |
|
| 75 |
static unsigned long const pixmaxval = 255; /* Largest pixel value */ |
| 76 |
|
| 77 |
static int ixdots, iydots; /* Screen size in dots */ |
| 78 |
static FILE * slfile; /* Slide file descriptor */ |
| 79 |
static bool blither; /* Dump slide file information ? */ |
| 80 |
static bool info; /* Print header information */ |
| 81 |
static pixel ** pixels; /* Pixel map */ |
| 82 |
static int pixcols, pixrows; /* Pixel map size */ |
| 83 |
|
| 84 |
struct slhead { |
| 85 |
char slh[17]; /* Primate-readable header */ |
| 86 |
char sntype; /* Machine type (for number compat) */ |
| 87 |
char slevel; /* Format type */ |
| 88 |
short sxdots, sydots; /* Display X, Y dots */ |
| 89 |
double sdsar; /* Display aspect ratio */ |
| 90 |
short shwfill; /* Display hardware fill type */ |
| 91 |
char spad; /* Pad to even byte length */ |
| 92 |
}; |
| 93 |
|
| 94 |
static struct slhead slfrof; /* Slide file header */ |
| 95 |
static long xfac, yfac; /* Aspect ratio scale factors */ |
| 96 |
|
| 97 |
static bool sdrawkcab; |
| 98 |
/* Slide drawing kinematic conversion of ass-backwards data flag */ |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
static int |
| 103 |
extend(unsigned char const ch) { |
| 104 |
/*---------------------------------------------------------------------------- |
| 105 |
Turn a smallint into an int with sign extension, whether |
| 106 |
or not that happens automatically. |
| 107 |
-----------------------------------------------------------------------------*/ |
| 108 |
return ((int) ((ch & 0x80) ? (ch | ~0xFF) : ch)); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
static int |
| 114 |
sli(void) { |
| 115 |
/*---------------------------------------------------------------------------- |
| 116 |
Read and return a word from the slide file. |
| 117 |
-----------------------------------------------------------------------------*/ |
| 118 |
short wd; |
| 119 |
|
| 120 |
if (fread(&wd, sizeof wd, 1, slfile) != 1) { |
| 121 |
pm_error("error reading slide file"); |
| 122 |
} else { |
| 123 |
if (sdrawkcab) { |
| 124 |
wd = ((wd >> 8) & 0xFF) | (wd << 8); |
| 125 |
} |
| 126 |
} |
| 127 |
return wd; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
static int |
| 133 |
slib(void) { |
| 134 |
/*---------------------------------------------------------------------------- |
| 135 |
Read and return a byte from the slide file. |
| 136 |
-----------------------------------------------------------------------------*/ |
| 137 |
unsigned char ch; |
| 138 |
|
| 139 |
if (fread(&ch, sizeof(ch), 1, slfile) != 1) { |
| 140 |
pm_error("error reading slide file"); |
| 141 |
} |
| 142 |
return extend(ch); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
static void |
| 148 |
vscale(int * const px, |
| 149 |
int * const py) { |
| 150 |
/*---------------------------------------------------------------------------- |
| 151 |
Scale screen coordinates for mismatched display. |
| 152 |
-----------------------------------------------------------------------------*/ |
| 153 |
*px = (((unsigned) *px) * xfac) >> 16; |
| 154 |
*py = (((unsigned) *py) * yfac) >> 16; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
static void |
| 160 |
upcase(const char * const sname, |
| 161 |
char * const uname) { |
| 162 |
|
| 163 |
unsigned int i; |
| 164 |
const char * ip; |
| 165 |
|
| 166 |
for (i = 0, ip = sname; i < 31; ++i) { |
| 167 |
char const ch = *ip++; |
| 168 |
|
| 169 |
if (ch != EOS) |
| 170 |
uname[i] = islower(ch) ? toupper(ch) : ch; |
| 171 |
} |
| 172 |
uname[i] = EOS; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
static void |
| 178 |
skipBytes(FILE * const fileP, |
| 179 |
unsigned int const count) { |
| 180 |
|
| 181 |
unsigned int i; |
| 182 |
|
| 183 |
for (i = 0; i < count; ++i) |
| 184 |
getc(fileP); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
static void |
| 190 |
scanDirectory(FILE * const slFileP, |
| 191 |
long const dirPos, |
| 192 |
bool const dirOnly, |
| 193 |
const char * const uname, |
| 194 |
bool * const foundP) { |
| 195 |
/*---------------------------------------------------------------------------- |
| 196 |
Scan the directory at the current position in *slFileP, either listing |
| 197 |
the directory ('dirOnly' true) or searching for a slide named |
| 198 |
'uname' ('dirOnly' false). |
| 199 |
|
| 200 |
'dirPos' is the offset in the file of the directory, i.e. the current |
| 201 |
position of *slFileP. |
| 202 |
|
| 203 |
In the latter case, return as *foundP whether the slide name is there. |
| 204 |
-----------------------------------------------------------------------------*/ |
| 205 |
bool found; |
| 206 |
bool eof; |
| 207 |
long pos; |
| 208 |
unsigned char libent[36]; |
| 209 |
|
| 210 |
for (found = false, eof = false, pos = dirPos; !found && !eof; ) { |
| 211 |
size_t readCt; |
| 212 |
readCt = fread(libent, 36, 1, slFileP); |
| 213 |
if (readCt != 1) |
| 214 |
eof = true; |
| 215 |
else { |
| 216 |
/* The directory entry is 32 bytes of NUL-terminated slide name |
| 217 |
followed by 4 bytes of offset of the next directory entry. |
| 218 |
*/ |
| 219 |
const char * const slideName = (const char *)(&libent[0]); |
| 220 |
if (pm_strnlen(slideName, 32) == 32) |
| 221 |
pm_error("Invalid input: slide name field is not " |
| 222 |
"nul-terminated"); |
| 223 |
else { |
| 224 |
if (strlen(slideName) == 0) |
| 225 |
eof = true; |
| 226 |
else { |
| 227 |
pos += 36; |
| 228 |
if (dirOnly) { |
| 229 |
pm_message(" %s", slideName); |
| 230 |
} else if (streq(slideName, uname)) { |
| 231 |
long const dpos = |
| 232 |
(((((libent[35] << 8) | libent[34]) << 8) | |
| 233 |
libent[33]) << 8) | libent[32]; |
| 234 |
|
| 235 |
if ((slFileP == stdin) || |
| 236 |
(fseek(slFileP, dpos, 0) == -1)) { |
| 237 |
|
| 238 |
skipBytes(slFileP, dpos - pos); |
| 239 |
} |
| 240 |
found = true; |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
*foundP = found; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
static void |
| 252 |
slidefind(const char * const slideName, |
| 253 |
bool const dirOnly, |
| 254 |
bool const ucasen) { |
| 255 |
/*---------------------------------------------------------------------------- |
| 256 |
Find a slide in a library or, if DIRONLY is nonzero, print a directory |
| 257 |
listing of the library. If UCASEN is nonzero, the requested slide name is |
| 258 |
converted to upper case. |
| 259 |
-----------------------------------------------------------------------------*/ |
| 260 |
char uname[32]; /* upper case translation of 'slideName' */ |
| 261 |
char header[32]; /* (supposed) header read from file */ |
| 262 |
bool found; |
| 263 |
|
| 264 |
if (dirOnly) |
| 265 |
pm_message("Slides in library:"); |
| 266 |
else { |
| 267 |
upcase(slideName, uname); |
| 268 |
} |
| 269 |
|
| 270 |
/* Read slide library header and verify. */ |
| 271 |
|
| 272 |
if ((fread(header, 32, 1, slfile) != 1) || |
| 273 |
(!STRSEQ(header, "AutoCAD Slide Library 1.0\r\n\32"))) { |
| 274 |
pm_error("not an AutoCAD slide library file."); |
| 275 |
} |
| 276 |
|
| 277 |
scanDirectory(slfile, 32, dirOnly, ucasen ? uname : slideName, &found); |
| 278 |
|
| 279 |
if (!found && !dirOnly) |
| 280 |
pm_error("slide '%s' not in library.", slideName); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
static slvecfn draw; |
| 286 |
|
| 287 |
static void |
| 288 |
draw(struct svector * vec, |
| 289 |
int color) { |
| 290 |
/*---------------------------------------------------------------------------- |
| 291 |
Draw a vector in the given AutoCAD color. |
| 292 |
-----------------------------------------------------------------------------*/ |
| 293 |
pixel rgbcolor; |
| 294 |
|
| 295 |
if (blither) { |
| 296 |
pm_message("Vector (%d, %d) - (%d, %d) Color %d", |
| 297 |
vec->f.x, vec->f.y, vec->t.x, vec->t.y, color); |
| 298 |
} |
| 299 |
assert(vec->f.x >= 0 && vec->f.x < pixcols); |
| 300 |
assert(vec->f.y >= 0 && vec->f.y < pixrows); |
| 301 |
assert(vec->t.x >= 0 && vec->t.x < pixcols); |
| 302 |
assert(vec->t.y >= 0 && vec->t.y < pixrows); |
| 303 |
PPM_ASSIGN(rgbcolor, |
| 304 |
acadcol[color][0], acadcol[color][1], acadcol[color][2]); |
| 305 |
ppmd_line(pixels, pixcols, pixrows, pixmaxval, |
| 306 |
vec->f.x, iydots - vec->f.y, vec->t.x, iydots - vec->t.y, |
| 307 |
PPMD_NULLDRAWPROC, |
| 308 |
(char *) &rgbcolor); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
static slfloodfn flood; |
| 314 |
|
| 315 |
static void |
| 316 |
flood(struct spolygon * const poly, |
| 317 |
int const color) { |
| 318 |
/*---------------------------------------------------------------------------- |
| 319 |
Draw a filled polygon. |
| 320 |
-----------------------------------------------------------------------------*/ |
| 321 |
unsigned int i; |
| 322 |
struct fillobj * handle; |
| 323 |
pixel rgbcolor; |
| 324 |
|
| 325 |
handle = ppmd_fill_create(); |
| 326 |
|
| 327 |
if (blither) { |
| 328 |
unsigned int i; |
| 329 |
pm_message("Polygon: %d points, fill type %d, color %d", |
| 330 |
poly->npoints, poly->fill, color); |
| 331 |
for (i = 0; i < poly->npoints; i++) { |
| 332 |
pm_message(" Point %d: (%d, %d)", i + 1, |
| 333 |
poly->pt[i].x, poly->pt[i].y); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
PPM_ASSIGN(rgbcolor, |
| 338 |
acadcol[color][0], acadcol[color][1], acadcol[color][2]); |
| 339 |
for (i = 0; i < poly->npoints; i++) { |
| 340 |
assert(poly->pt[i].x >= 0 && poly->pt[i].x < pixcols); |
| 341 |
assert(poly->pt[i].y >= 0 && poly->pt[i].y < pixrows); |
| 342 |
ppmd_line(pixels, pixcols, pixrows, pixmaxval, |
| 343 |
poly->pt[i].x, iydots - poly->pt[i].y, |
| 344 |
poly->pt[(i + 1) % poly->npoints].x, |
| 345 |
iydots - poly->pt[(i + 1) % poly->npoints].y, |
| 346 |
ppmd_fill_drawproc, handle); |
| 347 |
} |
| 348 |
ppmd_fill(pixels, pixcols, pixrows, pixmaxval, |
| 349 |
handle, PPMD_NULLDRAWPROC, (char *) &rgbcolor); |
| 350 |
|
| 351 |
ppmd_fill_destroy(handle); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
static void |
| 357 |
computeSize(struct slhead const slfrof, |
| 358 |
bool const wantAdjust, |
| 359 |
double const uscale, |
| 360 |
int const sxsize, |
| 361 |
int const sysize, |
| 362 |
int * const ixdotsP, |
| 363 |
int * const iydotsP, |
| 364 |
double * const dsarP) { |
| 365 |
/*---------------------------------------------------------------------------- |
| 366 |
-----------------------------------------------------------------------------*/ |
| 367 |
double const originalDsar = ((double) slfrof.sxdots) / slfrof.sydots; |
| 368 |
|
| 369 |
double ixdots, iydots; |
| 370 |
/* Algorithmic variables -- value changes as algorithm progresses */ |
| 371 |
bool adjusted; |
| 372 |
|
| 373 |
/* |
| 374 |
If the display aspect ratio indicates that the pixels on the sending |
| 375 |
screen were not square, adjust the size of the generated bitmap to |
| 376 |
correct the aspect ratio to square the pixels. |
| 377 |
|
| 378 |
We always correct the aspect ratio by adjusting the width of the image. |
| 379 |
This guarantees that output from the SHADE command, which is essentially |
| 380 |
scan-line data written in vector form, will not be corrupted. |
| 381 |
*/ |
| 382 |
|
| 383 |
if (fabs(slfrof.sdsar - originalDsar) > 0.0001) { |
| 384 |
if (wantAdjust) { |
| 385 |
ixdots = slfrof.sxdots * (slfrof.sdsar / originalDsar); |
| 386 |
iydots = slfrof.sydots; |
| 387 |
*dsarP = ixdots / iydots; |
| 388 |
adjusted = true; |
| 389 |
} else { |
| 390 |
pm_message("Warning - pixels on source screen were non-square. " |
| 391 |
"Specifying -adjust will correct image width " |
| 392 |
"to compensate."); |
| 393 |
ixdots = slfrof.sxdots; |
| 394 |
iydots = slfrof.sydots; |
| 395 |
*dsarP = slfrof.sdsar; |
| 396 |
adjusted = false; |
| 397 |
} |
| 398 |
} else { |
| 399 |
/* Source pixels were square. */ |
| 400 |
ixdots = slfrof.sxdots; |
| 401 |
iydots = slfrof.sydots; |
| 402 |
*dsarP = slfrof.sdsar; |
| 403 |
adjusted = false; |
| 404 |
} |
| 405 |
|
| 406 |
if (uscale > 0) { |
| 407 |
/* There is a uniform scale factor specified. Apply it. */ |
| 408 |
ixdots *= uscale; |
| 409 |
iydots *= uscale; |
| 410 |
} |
| 411 |
|
| 412 |
/* If the image is to be stretched to a given width, set the |
| 413 |
output image sizes accordingly. If only a height or width is |
| 414 |
given, scale the other direction proportionally to preserve the |
| 415 |
aspect ratio. |
| 416 |
*/ |
| 417 |
|
| 418 |
if (sxsize > 0) { |
| 419 |
if (sysize > 0) |
| 420 |
iydots = sysize - 1; |
| 421 |
else { |
| 422 |
double const stretchFactor = (sxsize - 1) / ixdots; |
| 423 |
iydots = iydots * stretchFactor; |
| 424 |
} |
| 425 |
ixdots = sxsize - 1; |
| 426 |
} else if (sysize > 0) { |
| 427 |
if (sxsize > 0) |
| 428 |
ixdots = sxsize - 1; |
| 429 |
else { |
| 430 |
double const stretchFactor = (sysize - 1) / iydots; |
| 431 |
ixdots = ixdots * stretchFactor; |
| 432 |
} |
| 433 |
iydots = sysize - 1; |
| 434 |
} |
| 435 |
|
| 436 |
if (ixdots > INT_MAX-10) |
| 437 |
pm_error("width is uncomputable large (%f dots)", ixdots); |
| 438 |
else |
| 439 |
*ixdotsP = ROUND(ixdots); |
| 440 |
if (iydots > INT_MAX-10) |
| 441 |
pm_error("height is uncomputable large (%f dots)", iydots); |
| 442 |
else |
| 443 |
*iydotsP = ROUND(iydots); |
| 444 |
|
| 445 |
if (adjusted) { |
| 446 |
pm_message( |
| 447 |
"Resized from %dx%d to %dx%d to correct pixel aspect ratio.", |
| 448 |
slfrof.sxdots + 1, slfrof.sydots + 1, *ixdotsP + 1, *iydotsP + 1); |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
static void |
| 455 |
slider(slvecfn slvec, |
| 456 |
slfloodfn slflood, |
| 457 |
bool const wantAdjust, |
| 458 |
double const uscale, |
| 459 |
int const sxsize, |
| 460 |
int const sysize) { |
| 461 |
/*---------------------------------------------------------------------------- |
| 462 |
Read slide file. This is called with the name of the file to be read and |
| 463 |
function pointers to the routines which process vectors and polygon fill |
| 464 |
requests respectively. |
| 465 |
-----------------------------------------------------------------------------*/ |
| 466 |
int i, rescale; |
| 467 |
unsigned char ubfr[4]; /* Utility character buffer */ |
| 468 |
int lx, ly; /* Last x and y point */ |
| 469 |
int slx, sly; /* Last x and y scaled screen point */ |
| 470 |
struct svector vec; /* Screen vector */ |
| 471 |
struct spolygon poly; /* Screen polygon */ |
| 472 |
unsigned short cw; /* Control word */ |
| 473 |
double dsar; /* Screen aspect ratio */ |
| 474 |
long ldsar; /* Scaled long DSAR */ |
| 475 |
short rtest; /* Value to test byte reversal */ |
| 476 |
short btest = 0x1234; /* Value to test byte-reversal */ |
| 477 |
static struct slhead slhi = /* Master slide header sample */ |
| 478 |
{"AutoCAD Slide\r\n\32", 86,2, 0,0, 0.0, 0}; |
| 479 |
int curcolor = 7; /* Current vector color */ |
| 480 |
pixel rgbcolor; /* Pixel used to clear pixmap */ |
| 481 |
|
| 482 |
lx = ly = 32000; |
| 483 |
|
| 484 |
/* Process the header of the slide file. */ |
| 485 |
|
| 486 |
sdrawkcab = false; /* Initially guess byte order is OK */ |
| 487 |
fread(slfrof.slh, 17, 1, slfile); |
| 488 |
fread(&slfrof.sntype, sizeof(char), 1, slfile); |
| 489 |
fread(&slfrof.slevel, sizeof(char), 1, slfile); |
| 490 |
fread(&slfrof.sxdots, sizeof(short), 1, slfile); |
| 491 |
fread(&slfrof.sydots, sizeof(short), 1, slfile); |
| 492 |
fread(ubfr, 4, 1, slfile); |
| 493 |
fread(&slfrof.shwfill, sizeof(short), 1, slfile); |
| 494 |
fread(&rtest, sizeof rtest, 1, slfile); |
| 495 |
|
| 496 |
/* Verify that slide format is compatible with this program. */ |
| 497 |
|
| 498 |
if (!STRSEQ(slfrof.slh, slhi.slh)) |
| 499 |
pm_error("this is not an AutoCAD slide file."); |
| 500 |
|
| 501 |
/* Verify that the number format and file level in the header are |
| 502 |
compatible. All slides written by versions of AutoCAD released |
| 503 |
since September of 1987 are compatible with this format. */ |
| 504 |
|
| 505 |
if ((slfrof.sntype != slhi.sntype) || (slfrof.slevel != slhi.slevel)) |
| 506 |
pm_error("incompatible slide file format"); |
| 507 |
|
| 508 |
/* Build SDSAR value from long scaled version. */ |
| 509 |
|
| 510 |
ldsar = 0L; |
| 511 |
for (i = 3; i >= 0; --i) |
| 512 |
ldsar = (ldsar << 8) | ubfr[i]; |
| 513 |
slfrof.sdsar = ((double) ldsar) / 1E7; |
| 514 |
|
| 515 |
/* Examine the byte order test value. If it's backwards, set the |
| 516 |
byte-reversal flag and correct all of the values we've read in |
| 517 |
so far. |
| 518 |
*/ |
| 519 |
|
| 520 |
if (btest != rtest) { |
| 521 |
sdrawkcab = true; |
| 522 |
#define rshort(x) x = ((x >> 8) & 0xFF) | (x << 8) |
| 523 |
rshort(slfrof.sxdots); |
| 524 |
rshort(slfrof.sydots); |
| 525 |
rshort(slfrof.shwfill); |
| 526 |
#undef rshort |
| 527 |
} |
| 528 |
|
| 529 |
/* Dump the header if we're blithering. */ |
| 530 |
|
| 531 |
if (blither || info) { |
| 532 |
pm_message("Slide file type %d, level %d, hwfill type %d.", |
| 533 |
slfrof.sntype, slfrof.slevel, slfrof.shwfill); |
| 534 |
pm_message("Original screen size %dx%d, aspect ratio %.3f.", |
| 535 |
slfrof.sxdots + 1, slfrof.sydots + 1, slfrof.sdsar); |
| 536 |
pm_message("Byte order is %s.", |
| 537 |
sdrawkcab ? "being reversed" : "the same"); |
| 538 |
} |
| 539 |
|
| 540 |
computeSize(slfrof, wantAdjust, uscale, sxsize, sysize, |
| 541 |
&ixdots, &iydots, &dsar); |
| 542 |
|
| 543 |
/* Allocate image buffer and clear it to black. */ |
| 544 |
|
| 545 |
pixcols = ixdots + 1; |
| 546 |
pixrows = iydots + 1; |
| 547 |
pixels = ppm_allocarray(pixcols, pixrows); |
| 548 |
PPM_ASSIGN(rgbcolor, 0, 0, 0); |
| 549 |
ppmd_filledrectangle(pixels, pixcols, pixrows, pixmaxval, 0, 0, |
| 550 |
pixcols, pixrows, PPMD_NULLDRAWPROC, |
| 551 |
(char *) &rgbcolor); |
| 552 |
|
| 553 |
if ((rescale = slfrof.sxdots != ixdots || |
| 554 |
slfrof.sydots != iydots || |
| 555 |
slfrof.sdsar != dsar) != 0) { |
| 556 |
|
| 557 |
/* Rescale all coords. so they'll look (more or less) |
| 558 |
right on this display. |
| 559 |
*/ |
| 560 |
|
| 561 |
xfac = (ixdots + 1) * 0x10000L; |
| 562 |
xfac /= (long) (slfrof.sxdots + 1); |
| 563 |
yfac = (iydots + 1) * 0x10000L; |
| 564 |
yfac /= (long) (slfrof.sydots + 1); |
| 565 |
if (dsar < slfrof.sdsar) { |
| 566 |
yfac = yfac * dsar / slfrof.sdsar; |
| 567 |
} else { |
| 568 |
xfac = xfac * slfrof.sdsar / dsar; |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
poly.npoints = 0; /* No flood in progress. */ |
| 573 |
|
| 574 |
while ((cw = sli()) != 0xFC00) { |
| 575 |
switch (cw & 0xFF00) { |
| 576 |
case 0xFB00: /* Short vector compressed */ |
| 577 |
vec.f.x = lx + extend(cw & 0xFF); |
| 578 |
vec.f.y = ly + slib(); |
| 579 |
vec.t.x = lx + slib(); |
| 580 |
vec.t.y = ly + slib(); |
| 581 |
lx = vec.f.x; |
| 582 |
ly = vec.f.y; |
| 583 |
if (rescale) { |
| 584 |
vscale(&vec.f.x, &vec.f.y); |
| 585 |
vscale(&vec.t.x, &vec.t.y); |
| 586 |
} |
| 587 |
(*slvec)(&vec, curcolor);/* Draw vector on screen */ |
| 588 |
slx = vec.f.x; /* Save scaled point */ |
| 589 |
sly = vec.f.y; |
| 590 |
break; |
| 591 |
|
| 592 |
case 0xFC00: /* End of file */ |
| 593 |
break; |
| 594 |
|
| 595 |
case 0xFD00: /* Flood command */ |
| 596 |
vec.f.x = sli(); |
| 597 |
vec.f.y = sli(); |
| 598 |
if ((int) vec.f.y < 0) { /* start or end */ |
| 599 |
if (poly.npoints != 0) { /* end? */ |
| 600 |
if (poly.npoints > 2 && poly.npoints < 11) { |
| 601 |
(*slflood)(&poly, curcolor); |
| 602 |
} else { |
| 603 |
pm_error("Bad polygon vertex count (%d)", |
| 604 |
poly.npoints); |
| 605 |
} |
| 606 |
poly.npoints = 0; |
| 607 |
} else { |
| 608 |
poly.fill = -vec.f.y; /* Start */ |
| 609 |
} |
| 610 |
} else { /* Polygon vertex */ |
| 611 |
if (poly.npoints < 10) { |
| 612 |
if (rescale) { |
| 613 |
vscale(&vec.f.x, &vec.f.y); |
| 614 |
} |
| 615 |
poly.pt[poly.npoints].x = vec.f.x; |
| 616 |
poly.pt[poly.npoints].y = vec.f.y; |
| 617 |
} |
| 618 |
poly.npoints++; |
| 619 |
} |
| 620 |
break; |
| 621 |
|
| 622 |
case 0xFE00: /* Common endpoint compressed */ |
| 623 |
vec.f.x = lx + extend(cw & 0xFF); |
| 624 |
vec.f.y = ly + slib(); |
| 625 |
lx = vec.f.x; |
| 626 |
ly = vec.f.y; |
| 627 |
vec.t.x = slx; |
| 628 |
vec.t.y = sly; |
| 629 |
if (rescale) { |
| 630 |
vscale(&vec.f.x, &vec.f.y); |
| 631 |
} |
| 632 |
(*slvec)(&vec, curcolor);/* Draw vector */ |
| 633 |
slx = vec.f.x; /* Save scaled point */ |
| 634 |
sly = vec.f.y; |
| 635 |
break; |
| 636 |
|
| 637 |
case 0xFF00: /* Change color */ |
| 638 |
curcolor = cw & 0xFF; |
| 639 |
break; |
| 640 |
|
| 641 |
default: /* Coordinates */ |
| 642 |
lx = vec.f.x = cw; |
| 643 |
ly = vec.f.y = sli(); |
| 644 |
vec.t.x = sli(); |
| 645 |
vec.t.y = sli(); |
| 646 |
if (rescale) { |
| 647 |
vscale(&vec.f.x, &vec.f.y); |
| 648 |
vscale(&vec.t.x, &vec.t.y); |
| 649 |
} |
| 650 |
(*slvec)(&vec, curcolor); |
| 651 |
slx = vec.f.x; /* Save scaled point */ |
| 652 |
sly = vec.f.y; |
| 653 |
break; |
| 654 |
} |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
int |
| 661 |
main(int argc, |
| 662 |
const char * argv[]) { |
| 663 |
|
| 664 |
int argn; |
| 665 |
const char * const usage = "[-verbose] [-info] [-adjust] [-scale <s>]\n\ |
| 666 |
[-dir] [-lib|-Lib <name>]\n\ |
| 667 |
[-xsize|-width <x>] [-ysize|-height <y>] [sldfile]"; |
| 668 |
bool dironly; |
| 669 |
bool hgtspec; |
| 670 |
bool widspec; |
| 671 |
bool scalespec; |
| 672 |
bool ucasen; |
| 673 |
bool adjust; /* Adjust to correct aspect ratio ? */ |
| 674 |
double uscale; /* Uniform scale factor */ |
| 675 |
int sxsize, sysize; /* Scale to X, Y size ? */ |
| 676 |
const char * slobber; /* Slide library item */ |
| 677 |
|
| 678 |
pm_proginit(&argc, argv); |
| 679 |
argn = 1; |
| 680 |
|
| 681 |
slobber = NULL; |
| 682 |
dironly = false; |
| 683 |
hgtspec = false; |
| 684 |
widspec = false; |
| 685 |
scalespec = false; |
| 686 |
ucasen = false; |
| 687 |
blither = false; |
| 688 |
info = false; |
| 689 |
adjust = false; |
| 690 |
uscale = -1.0; |
| 691 |
sxsize = -1; |
| 692 |
sysize = -1; |
| 693 |
|
| 694 |
while (argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0') { |
| 695 |
if (pm_keymatch(argv[argn], "-verbose", 2)) { |
| 696 |
blither = true; |
| 697 |
} else if (pm_keymatch(argv[argn], "-adjust", 2)) { |
| 698 |
adjust = true; |
| 699 |
} else if (pm_keymatch(argv[argn], "-dir", 2)) { |
| 700 |
dironly = true; |
| 701 |
} else if (pm_keymatch(argv[argn], "-info", 2)) { |
| 702 |
info = true; |
| 703 |
} else if (pm_keymatch(argv[argn], "-lib", 2)) { |
| 704 |
if (slobber) |
| 705 |
pm_error("already specified a library item"); |
| 706 |
ucasen = argv[argn][1] != 'L'; |
| 707 |
argn++; |
| 708 |
if (argn == argc) { |
| 709 |
pm_usage(usage); |
| 710 |
} |
| 711 |
slobber = argv[argn]; |
| 712 |
} else if (pm_keymatch(argv[argn], "-scale", 2)) { |
| 713 |
if (scalespec) { |
| 714 |
pm_error("already specified a scale factor"); |
| 715 |
} |
| 716 |
argn++; |
| 717 |
if ((argn == argc) || (sscanf(argv[argn], "%lf", &uscale) != 1)) |
| 718 |
pm_usage(usage); |
| 719 |
if (uscale <= 0.0) { |
| 720 |
pm_error("scale factor must be greater than 0"); |
| 721 |
} |
| 722 |
scalespec = true; |
| 723 |
} else if (pm_keymatch(argv[argn], "-xsize", 2) || |
| 724 |
pm_keymatch(argv[argn], "-width", 2)) { |
| 725 |
if (widspec) { |
| 726 |
pm_error("already specified a width/xsize"); |
| 727 |
} |
| 728 |
argn++; |
| 729 |
if ((argn == argc) || (sscanf(argv[argn], "%d", &sxsize) != 1)) |
| 730 |
pm_usage(usage); |
| 731 |
widspec = true; |
| 732 |
} else if (pm_keymatch(argv[argn], "-ysize", 2) || |
| 733 |
pm_keymatch(argv[argn], "-height", 2)) { |
| 734 |
if (hgtspec) { |
| 735 |
pm_error("already specified a height/ysize"); |
| 736 |
} |
| 737 |
argn++; |
| 738 |
if ((argn == argc) || (sscanf(argv[argn], "%d", &sysize) != 1)) |
| 739 |
pm_usage(usage); |
| 740 |
hgtspec = true; |
| 741 |
} else { |
| 742 |
pm_usage(usage); |
| 743 |
} |
| 744 |
argn++; |
| 745 |
} |
| 746 |
|
| 747 |
/* If a file name is specified, open it. Otherwise read from |
| 748 |
standard input. |
| 749 |
*/ |
| 750 |
|
| 751 |
if (argn < argc) { |
| 752 |
slfile = pm_openr(argv[argn]); |
| 753 |
argn++; |
| 754 |
} else { |
| 755 |
slfile = stdin; |
| 756 |
} |
| 757 |
|
| 758 |
if (argn != argc) { /* Extra bogus arguments ? */ |
| 759 |
pm_usage(usage); |
| 760 |
} |
| 761 |
|
| 762 |
/* If we're extracting an item from a slide library, position the |
| 763 |
input stream to the start of the chosen slide. |
| 764 |
*/ |
| 765 |
|
| 766 |
if (dironly || slobber) |
| 767 |
slidefind(slobber, dironly, ucasen); |
| 768 |
|
| 769 |
if (!dironly) { |
| 770 |
slider(draw, flood, adjust, uscale, sxsize, sysize); |
| 771 |
ppm_writeppm(stdout, pixels, pixcols, pixrows, pixmaxval, 0); |
| 772 |
} |
| 773 |
pm_close(slfile); |
| 774 |
pm_close(stdout); |
| 775 |
|
| 776 |
return 0; |
| 777 |
} |
| 778 |
|
| 779 |
|
| 780 |
|