1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
|
// Copyright 1993, 1994, 1995
// Maurice LeBrun
// IFS, University of Texas at Austin
//
// This software may be freely copied, modified and redistributed under the
// terms of the GNU Library General Public License.
//
// There is no warranty or other guarantee of fitness of this software.
// It is provided solely "as is". The author(s) disclaim(s) all
// responsibility and liability with respect to this software's usage or
// its effect upon hardware or computer systems.
//
//--------------------------------------------------------------------------
//
// Support routines to render a PLplot byte stream, interpreting the PLplot
// metacode.
//
// Although this code is duplicated to some extent by plrender and the
// plot buffer redraw code (in plbuf.c), they are all different in some
// significant ways, namely:
//
// - plrender must always retain backward compatibility code to
// handle old metafiles, as well as stuff to support seeking.
//
// - plbuf by definition is redrawing on the same machine so no
// special effort is necessary to make the byte stream portable,
// also, only low-level graphics calls are used (grline, etc)
//
// The rendering code here must (by contrast to plbuf) use the high level
// plot routines (as does plrender), to support local zooms as well as the
// ability to dump the associated window into plot space to a file, but is
// otherwise pretty minimal. A portable byte stream is used since network
// communication over a socket may be used.
//
//--------------------------------------------------------------------------
//
// #define DEBUG
// #define DEBUG_ENTER
//
#include "plserver.h"
#include "plevent.h"
#include "metadefs.h"
// Some wrapper macros to return (-1) on error
// Note we use %lu and an explicit cast to unsigned long to print size_t pointers.
// C99 adds %zd as an explicit format specifier for size_t but this is not yet
// fully adopted.
#define plr_rd( code ) \
if ( code ) { fprintf( stderr, \
"Unable to read from %s in %s at line %d, bytecount %lu\n", \
plr->iodev->typeName, __FILE__, __LINE__, (unsigned long) plr->pdfs->bp ); \
return -1; }
#define plr_cmd( code ) \
if ( ( code ) == -1 ) return -1;
// Error termination
#define barf( msg ) \
{ fprintf( stderr, "%s\nCommand code: %d, byte count: %lu\n", \
msg, csave, (unsigned long) plr->pdfs->bp ); return -1; }
// Static function prototypes.
static int plr_process1( PLRDev *plr, int c );
static int plr_init( PLRDev *plr );
static int plr_line( PLRDev *plr, int c );
static int plr_eop( PLRDev *plr );
static int plr_bop( PLRDev *plr );
static int plr_state( PLRDev *plr );
static int plr_esc( PLRDev *plr );
static int plr_get( PLRDev *plr );
static int plr_unget( PLRDev *plr, U_CHAR c );
static int get_ncoords( PLRDev *plr, PLFLT *x, PLFLT *y, PLINT n );
static int plresc_fill( PLRDev *plr );
// variables
static int csave = -1;
static U_CHAR dum_uchar;
static U_SHORT dum_ushort;
static PLFLT xstatic[PL_MAXPOLY], ystatic[PL_MAXPOLY];
//--------------------------------------------------------------------------
// plr_start()
//
// Set default state parameters before anyone else has a chance to.
//--------------------------------------------------------------------------
void
plr_start( PLRDev *plr )
{
dbug_enter( "plr_start" );
plr->xmin = 0;
plr->xmax = PIXELS_X - 1;
plr->ymin = 0;
plr->ymax = PIXELS_Y - 1;
plr->xold = PL_UNDEFINED;
plr->yold = PL_UNDEFINED;
}
//--------------------------------------------------------------------------
// plr_process()
//
// Read & process commands until plr->nbytes bytes have been read.
//--------------------------------------------------------------------------
int
plr_process( PLRDev *plr )
{
int c;
dbug_enter( "plr_process" );
while ( plr->pdfs->bp < (size_t) plr->nbytes )
{
plr_cmd( c = plr_get( plr ) );
csave = c;
plr_cmd( plr_process1( plr, c ) );
}
return 0;
}
//--------------------------------------------------------------------------
// plr_process1()
//
// Process a command. Note: because of line->polyline compression, this
// may actually process an arbitrary number of LINE or LINETO commands.
// Since the data buffer (fifo or socket) is only flushed after a complete
// command, there should be no danger in rushing blindly ahead to execute
// each plot command.
//--------------------------------------------------------------------------
static int
plr_process1( PLRDev *plr, int c )
{
switch ( c )
{
case INITIALIZE:
plr_cmd( plr_init( plr ) );
break;
case LINE:
case LINETO:
case POLYLINE:
plr_cmd( plr_line( plr, c ) );
break;
case EOP:
plr->at_eop = 1;
plr_cmd( plr_eop( plr ) );
break;
case BOP:
plr->at_bop = 1;
plr_cmd( plr_bop( plr ) );
break;
case CHANGE_STATE:
plr_cmd( plr_state( plr ) );
break;
case ESCAPE:
plr_cmd( plr_esc( plr ) );
break;
default:
fprintf( stderr, "plr_process1: Unrecognized command code %d\n", c );
}
return 0;
}
//--------------------------------------------------------------------------
// void plr_init()
//
// Handle initialization.
//--------------------------------------------------------------------------
static int
plr_init( PLRDev *plr )
{
char tk_magic[80], tk_version[80], tag[80];
dbug_enter( "plr_init" );
// Read header info
plr_cmd( pdf_rd_header( plr->pdfs, tk_magic ) );
if ( strcmp( tk_magic, PLSERV_HEADER ) )
barf( "plr_init: Invalid header" );
// Read version field of header. We need to check that we can read the
// byte stream, in case this is an old version of plserver.
plr_cmd( pdf_rd_header( plr->pdfs, tk_version ) );
if ( strcmp( tk_version, PLSERV_VERSION ) > 0 )
{
fprintf( stderr,
"Error: incapable of reading output of version %s.\n", tk_version );
barf( "plr_init: Please obtain a newer copy of plserver." );
}
// Read tagged initialization info.
// Overkill, but a no-brainer since plrender already uses this
for (;; )
{
plr_cmd( pdf_rd_header( plr->pdfs, tag ) );
if ( *tag == '\0' )
break;
if ( !strcmp( tag, "xmin" ) )
{
plr_rd( pdf_rd_2bytes( plr->pdfs, &dum_ushort ) );
plr->xmin = (short) dum_ushort;
continue;
}
if ( !strcmp( tag, "xmax" ) )
{
plr_rd( pdf_rd_2bytes( plr->pdfs, &dum_ushort ) );
plr->xmax = (short) dum_ushort;
continue;
}
if ( !strcmp( tag, "ymin" ) )
{
plr_rd( pdf_rd_2bytes( plr->pdfs, &dum_ushort ) );
plr->ymin = (short) dum_ushort;
continue;
}
if ( !strcmp( tag, "ymax" ) )
{
plr_rd( pdf_rd_2bytes( plr->pdfs, &dum_ushort ) );
plr->ymax = (short) dum_ushort;
continue;
}
if ( !strcmp( tag, "width" ) )
{
plr_rd( pdf_rd_1byte( plr->pdfs, &dum_uchar ) );
plwidth( dum_uchar );
continue;
}
barf( "plr_init: Unrecognized initialization tag." );
}
return 0;
}
//--------------------------------------------------------------------------
// plr_line()
//
// Draw a line or polyline.
//--------------------------------------------------------------------------
static int
plr_line( PLRDev *plr, int c )
{
int c1;
U_SHORT npts;
PLFLT *x, *y;
// "Temporary" logic until can figure out what value of npts will
// actually be required which would allow use of malloc whenever
// that npts value > PL_MAXPOLY.
x = xstatic;
y = ystatic;
npts = 1;
x[0] = plr->xold;
y[0] = plr->yold;
switch ( (int) c )
{
case LINE:
plr_cmd( get_ncoords( plr, x, y, 1 ) );
// n.b. falls through to LINETO case.
case LINETO:
for (;; )
{
plr_cmd( get_ncoords( plr, x + npts, y + npts, 1 ) );
npts++;
if ( npts == PL_MAXPOLY || ( plr->pdfs->bp == (size_t) plr->nbytes ) )
break;
plr_cmd( c1 = plr_get( plr ) );
if ( c1 != LINETO )
{
plr_cmd( plr_unget( plr, (U_CHAR) c1 ) );
break;
}
}
break;
case POLYLINE:
plr_rd( pdf_rd_2bytes( plr->pdfs, &npts ) );
plr_cmd( get_ncoords( plr, x, y, npts ) );
break;
}
plline( npts, x, y );
plr->xold = x[npts - 1];
plr->yold = y[npts - 1];
return 0;
}
//--------------------------------------------------------------------------
// get_ncoords()
//
// Read n coordinate vectors.
//--------------------------------------------------------------------------
#define plr_rdn( code ) \
if ( code ) { fprintf( stderr, \
"Unable to read from %s in %s at line %d, bytecount %d\n\
Bytes requested: %d\n", plr->iodev->typeName, __FILE__, __LINE__, \
(int) plr->pdfs->bp, (int) 2 * n ); return -1; }
static int
get_ncoords( PLRDev *plr, PLFLT *x, PLFLT *y, PLINT n )
{
PLINT i;
short _xs[PL_MAXPOLY], _ys[PL_MAXPOLY];
short *xs, *ys;
if ( n > PL_MAXPOLY )
{
xs = (short *) malloc( sizeof ( short ) * (size_t) n );
ys = (short *) malloc( sizeof ( short ) * (size_t) n );
}
else
{
xs = _xs;
ys = _ys;
}
plr_rdn( pdf_rd_2nbytes( plr->pdfs, (U_SHORT *) xs, n ) );
plr_rdn( pdf_rd_2nbytes( plr->pdfs, (U_SHORT *) ys, n ) );
for ( i = 0; i < n; i++ )
{
x[i] = xs[i];
y[i] = ys[i];
}
if ( n > PL_MAXPOLY )
{
free( xs );
free( ys );
}
return 0;
}
//--------------------------------------------------------------------------
// plr_eop()
//
// Clear screen.
//--------------------------------------------------------------------------
static int
plr_eop( PLRDev * PL_UNUSED( plr ) )
{
dbug_enter( "plr_eop" );
pleop();
return 0;
}
//--------------------------------------------------------------------------
// plr_bop()
//
// Page advancement.
//--------------------------------------------------------------------------
static int
plr_bop( PLRDev *plr )
{
dbug_enter( "plr_bop" );
// Advance and setup the page
plbop();
plvpor( 0., 1., 0., 1. );
plwind( plr->xmin, plr->xmax, plr->ymin, plr->ymax );
return 0;
}
//--------------------------------------------------------------------------
// plr_state()
//
// Handle change in PLStream state (color, pen width, fill attribute,
// etc).
//--------------------------------------------------------------------------
static int
plr_state( PLRDev *plr )
{
U_CHAR op;
int i;
plr_rd( pdf_rd_1byte( plr->pdfs, &op ) );
switch ( op )
{
case PLSTATE_WIDTH: {
U_SHORT width;
plr_rd( pdf_rd_2bytes( plr->pdfs, &width ) );
plwidth( width );
break;
}
case PLSTATE_COLOR0: {
short icol0;
plr_rd( pdf_rd_2bytes( plr->pdfs, (unsigned short *) &icol0 ) );
if ( icol0 == PL_RGB_COLOR )
{
U_CHAR r, g, b;
plr_rd( pdf_rd_1byte( plr->pdfs, &r ) );
plr_rd( pdf_rd_1byte( plr->pdfs, &g ) );
plr_rd( pdf_rd_1byte( plr->pdfs, &b ) );
plscol0( icol0, r, g, b );
}
else
{
plcol0( icol0 );
}
break;
}
case PLSTATE_COLOR1: {
U_SHORT icol1;
PLFLT col1;
plr_rd( pdf_rd_2bytes( plr->pdfs, &icol1 ) );
col1 = (double) icol1 / (double) plsc->ncol1;
plcol1( col1 );
break;
}
case PLSTATE_FILL: {
signed char patt;
plr_rd( pdf_rd_1byte( plr->pdfs, (U_CHAR *) &patt ) );
plpsty( patt );
break;
}
case PLSTATE_CMAP0: {
U_SHORT ncol0;
plr_rd( pdf_rd_2bytes( plr->pdfs, &ncol0 ) );
plscmap0n( (PLINT) ncol0 );
for ( i = 0; i < plsc->ncol0; i++ )
{
plr_rd( pdf_rd_1byte( plr->pdfs, &plsc->cmap0[i].r ) );
plr_rd( pdf_rd_1byte( plr->pdfs, &plsc->cmap0[i].g ) );
plr_rd( pdf_rd_1byte( plr->pdfs, &plsc->cmap0[i].b ) );
}
plP_state( PLSTATE_CMAP0 );
break;
}
case PLSTATE_CMAP1: {
U_SHORT ncol1, ncp1;
float h, l, s;
U_CHAR rev;
plr_rd( pdf_rd_2bytes( plr->pdfs, &ncol1 ) );
plscmap1n( (PLINT) ncol1 );
for ( i = 0; i < plsc->ncol1; i++ )
{
plr_rd( pdf_rd_1byte( plr->pdfs, &plsc->cmap1[i].r ) );
plr_rd( pdf_rd_1byte( plr->pdfs, &plsc->cmap1[i].g ) );
plr_rd( pdf_rd_1byte( plr->pdfs, &plsc->cmap1[i].b ) );
}
// Get the control points
plr_rd( pdf_rd_2bytes( plr->pdfs, &ncp1 ) );
plsc->ncp1 = ncp1;
for ( i = 0; i < plsc->ncp1; i++ )
{
plr_rd( pdf_rd_ieeef( plr->pdfs, &h ) );
plr_rd( pdf_rd_ieeef( plr->pdfs, &l ) );
plr_rd( pdf_rd_ieeef( plr->pdfs, &s ) );
plr_rd( pdf_rd_1byte( plr->pdfs, &rev ) );
plsc->cmap1cp[i].c1 = h;
plsc->cmap1cp[i].c2 = l;
plsc->cmap1cp[i].c3 = s;
plsc->cmap1cp[i].alt_hue_path = rev;
}
plP_state( PLSTATE_CMAP1 );
break;
}
}
return 0;
}
//--------------------------------------------------------------------------
// plr_esc()
//
// Handle all escape functions.
// Only those that require additional data to be read need to be
// explicitly handled; the others are merely passed on to the actual
// driver.
//--------------------------------------------------------------------------
static int
plr_esc( PLRDev *plr )
{
U_CHAR op;
plr_rd( pdf_rd_1byte( plr->pdfs, &op ) );
switch ( op )
{
case PLESC_FILL:
plr_cmd( plresc_fill( plr ) );
break;
default:
pl_cmd( (PLINT) op, NULL );
break;
}
return 0;
}
//--------------------------------------------------------------------------
// plresc_fill()
//
// Fill polygon described in points plsc->dev_x[] and plsc->dev_y[].
//--------------------------------------------------------------------------
static int
plresc_fill( PLRDev *plr )
{
U_SHORT npts;
PLFLT *x, *y;
dbug_enter( "plresc_fill" );
plr_rd( pdf_rd_2bytes( plr->pdfs, &npts ) );
if ( npts > PL_MAXPOLY )
{
x = (PLFLT *) malloc( sizeof ( PLFLT ) * npts );
y = (PLFLT *) malloc( sizeof ( PLFLT ) * npts );
}
else
{
x = xstatic;
y = ystatic;
}
get_ncoords( plr, x, y, npts );
plfill( npts, x, y );
if ( npts > PL_MAXPOLY )
{
free( x );
free( y );
}
return 0;
}
//--------------------------------------------------------------------------
// plr_get()
//
// Read & return the next command
//--------------------------------------------------------------------------
static int
plr_get( PLRDev *plr )
{
int c;
c = pdf_getc( plr->pdfs );
if ( c == EOF )
{
barf( "plr_get: Unable to read character" );
}
return c;
}
//--------------------------------------------------------------------------
// plr_unget()
//
// Push back the last command read.
//--------------------------------------------------------------------------
static int
plr_unget( PLRDev *plr, U_CHAR c )
{
if ( pdf_ungetc( c, plr->pdfs ) == EOF )
{
barf( "plr_unget: Unable to push back character" );
}
return 0;
}
|