blink.c 12.8 KB
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

/**************************************************************************
 *                                                                        *
 *               Copyright (C) 1994, Silicon Graphics, Inc.               *
 *                                                                        *
 *  These coded instructions, statements, and computer programs  contain  *
 *  unpublished  proprietary  information of Silicon Graphics, Inc., and  *
 *  are protected by Federal copyright  law.  They  may not be disclosed  *
 *  to  third  parties  or copied or duplicated in any form, in whole or  *
 *  in part, without the prior written consent of Silicon Graphics, Inc.  *
 *                                                                        *
 *************************************************************************/

/*
 * File:	blink.c
 *
 *  This program changes the color of a specified primitive to magenta
 *  in order to make it stand out in the display.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef __sgi__
#include <bstring.h>
#endif
#include <string.h>
#include <malloc.h>

#include "mbi.h"

#define UsageString	"[-p <prim_spec>] [-c <x> <y>] [-r <radius>] [-x] [-v]  <in.rdp> <out.rdp>"

#define SIGN_EXTEND_16(x)	((x & 0x8000) ? (x | ~0xffff) : (x))
#define MAX_SEL_PRIM		100
#define MAX_SEL_XY		100

#define MAX_BLINK_COLORS	(sizeof(BlinkColor) / sizeof(RGBColor))
#define MAKE_WORD_8(i)		((BlinkColor[i].r << 16) | (BlinkColor[i].g))
#define MAKE_WORD_9(i)		((BlinkColor[i].b << 16) | 0xff)

typedef int			bool;
typedef char 	  		i8;
typedef unsigned char 	  	u8;
typedef short int  		i16;
typedef unsigned short int  	u16;
typedef long int 		i32;
typedef unsigned long int 	u32;

typedef struct  {
  int x;
  int y;
} CoordElem;

typedef struct {
  int r, g, b;
} RGBColor;

void range_expand (char *range);

static FILE     *Outfp = NULL;

static u32	  NumPrimitives = 0;
static u32	  SelPrimitive[MAX_SEL_PRIM];
static u32        nSelPrim = 0;
static CoordElem  SelXY[MAX_SEL_XY];
static u32        nSelXY = 0;
static u32	  Verbose = 0;
static u32	  Extract = 0;
static float	  Radius = 0.0;
static u32        BlinkColorIndx = 0;
static RGBColor   BlinkColor[] = {
			{ 0xff, 0x00, 0xff }, /* magenta */
			{ 0x00, 0xff, 0xff }, /* cyan */
			{ 0xff, 0xff, 0x00 }, /* yellow */
			{ 0xff, 0xff, 0xff }, /* white */
			{ 0xff, 0x00, 0x00 }, /* red */
			{ 0x00, 0xff, 0x00 }, /* green */
			{ 0x00, 0x00, 0xff }, /* blue */
			{   64,  224,  208 }, /* turquoise */
			{   50,  255,   47 }  /* green-yellow */
};




/*
 *  select_prim() - test primitive number against all in list
 *  to see if matched.
 */
static int
  select_prim( void )
{
  int i;

  for(i = 0; (i < nSelPrim) && (i < MAX_SEL_PRIM); i++)
  {
    if(SelPrimitive[i] == NumPrimitives)
      return(1);
  }
  return(0);
}


/* 
 *  intersect_test - test for intersection of point with poly
 */

static int
  intersection_test(i32 *buf)
{
  int i;
  int left;
  float x[3], y[3]; 
  float dxh, dxm, dxl;
  float yh, ym, yl;
  float xh, xm, xl;
  float x_major, x_minor;

  xl  = (float) buf[2] / 65536.0;
  xh  = (float) buf[4] / 65536.0;
  xm  = (float) buf[6] / 65536.0;
  dxl = (float) buf[3] / 65536.0;
  dxh = (float) buf[5] / 65536.0;
  dxm = (float) buf[7] / 65536.0;
  yl  = (float) SIGN_EXTEND_16(buf[0] & 0xffff) / 4.0;
  ym  = (float) SIGN_EXTEND_16((buf[1] >> 16) & 0xffff) / 4.0;
  yh  = (float) SIGN_EXTEND_16(buf[1] & 0xffff) / 4.0;
  left = (buf[0] & 0x00800000) > 0;

  if(Verbose)
  {
    printf("========================================\n");
    printf("Primitive #%04d\n", NumPrimitives);
    printf(" xl  %f xm  %f xh  %f\n", xl, xm, xh);
    printf(" yl  %f ym  %f yh  %f\n", yl, ym, yh);
    printf(" dxl %f dxm %f dxh %f\n", dxl, dxm, dxh);
    printf(" left %d\n", left);
  }

  for(i = 0; (i < nSelXY) && (i < MAX_SEL_XY); i++)
  {
    if((SelXY[i].y >= (yh-Radius)) && (SelXY[i].y <= (yl+Radius)))
    {
      x_major = xh + (((float)SelXY[i].y - yh) * dxh);
      if(SelXY[i].y > ym) /* use lower minor edge */
      {
        x_minor = xl + (((float)SelXY[i].y - ym) * dxl);
      }
      else  /* use upper minor edge */
      {
        x_minor = xm + (((float)SelXY[i].y - yh) * dxm);
      }

      if(left)
      {
        if(SelXY[i].x >= (x_major-Radius) && SelXY[i].x <= (x_minor+Radius))
          return(1);
      }
      else
      {
        if(SelXY[i].x >= (x_minor-Radius) && SelXY[i].x <= (x_major+Radius))
          return(1);
      }
    }
  }
  return(0);
}

/*
 *  get_nwords
 */
int
  get_nwords(int type)
{
  switch(type)
  {
      case (char) G_TRI_FILL:
	return(6); 
      case (char) G_TRI_SHADE:
      case (char) G_TRI_TXTR:
	return(22); 
      case (char) G_TRI_SHADE_TXTR:
	return(38);
      case (char) G_TRI_FILL_ZBUFF:
	return(10);
      case (char) G_TRI_SHADE_ZBUFF:
      case (char) G_TRI_TXTR_ZBUFF:
	return(26);
      case (char) G_TRI_SHADE_TXTR_ZBUFF:
	return(42);
      default:
	fprintf(stderr,"Error, unrecognized type: %d\n", type);
	break;
  }
}


/*
 * Parse and dump a RDP-type display list command.
 */
static void
read_rdp(u32 gfx0, u32 gfx1, FILE *in)
{
    u32		addr;
    i32		buffer[44];
    char	op, b0, tstring[32];
    int         i, nwords;
    int         blink_en, write_en;

    /* every command has at least 64-bits... */
    buffer[0] = gfx0;
    buffer[1] = gfx1;

    op = (char) ((gfx0 & 0xff000000) >> 24);

    switch (op) {

      case (char) G_SETCIMG:
      case (char) G_SETZIMG:
      case (char) G_SETTIMG:
      case (char) G_SETENVCOLOR:
      case (char) G_SETPRIMCOLOR:
      case (char) G_SETBLENDCOLOR:
      case (char) G_SETFOGCOLOR:
      case (char) G_SETFILLCOLOR:
      case (char) G_FILLRECT:
      case (char) G_SETTILE:
      case (char) G_LOADTILE:
      case (char) G_LOADBLOCK:
      case (char) G_SETTILESIZE:
      case (char) G_LOADTLUT:
      case (char) G_RDPSETOTHERMODE:
      case (char) G_SETPRIMDEPTH:
      case (char) G_SETSCISSOR:
      case (char) G_SETCONVERT:
      case (char) G_SETKEYR:
      case (char) G_SETKEYGB:
      case (char) G_RDPFULLSYNC:
      case (char) G_RDPTILESYNC:
      case (char) G_RDPPIPESYNC:
      case (char) G_NOOP:
      case (char) G_TEXRECTFLIP:
      case (char) G_TEXRECT:
      case (char) G_SETCOMBINE:
        fwrite( &gfx0, sizeof(u32), 1, Outfp);
        fwrite( &gfx1, sizeof(u32), 1, Outfp);
	break;

      case (char) G_TRI_FILL:
      case (char) G_TRI_SHADE:
      case (char) G_TRI_TXTR:
      case (char) G_TRI_SHADE_TXTR:
      case (char) G_TRI_FILL_ZBUFF:
      case (char) G_TRI_SHADE_ZBUFF:
      case (char) G_TRI_TXTR_ZBUFF:
      case (char) G_TRI_SHADE_TXTR_ZBUFF:

	nwords = get_nwords(op);
	fread(&buffer[2], sizeof(u32), nwords, in);
        write_en = 0;
        blink_en = 0;

        if((select_prim()) == 1)
	  blink_en = 1;
        else if((intersection_test(buffer)) == 1)
	  blink_en = 1;
        else if(!Extract)
          write_en = 1;

        if(blink_en)
          printf("Blinked Primitive Number %d\n", NumPrimitives);

        if(write_en || blink_en)
          fwrite( buffer, sizeof(u32), nwords+2, Outfp);

        ++NumPrimitives;
	break;

      default:
	break;
    }

}

/*
 *  Print Usage
 */
void
  print_usage(char *prog)
{
  printf("Usage: %s %s\n", prog, UsageString);
  printf("Where:\n");
  printf("  -p <prim_spec> is a range expression for primitive numbers you\n");
  printf("         wish to select.  For example: \"1-9,13,47\" or 112 are valid\n");
  printf("         range expressions.  Primitives are numbered as they are listed\n");
  printf("         in 'rdp2read'.\n");
  printf("\n");
  printf("  -c <x> <y> is the coordinate point of intersection used to select\n");
  printf("         all primtives which intersect it.\n");
  printf("\n");
  printf("  -r <radius> is used to increase the area around the point defined with\n");
  printf("         the -c option which blink uses to select primitives.\n");
  printf(" \n");
  printf("  -x is used to extract all attributes and selected primitives into the\n");
  printf("         output display list.  Otherwise, selected primitives are only\n");
  printf("         reported and all attributes and primitives are echoed to the output\n");
  printf("         display list.\n");
  printf(" \n");
  printf(" -v is the verbose flag.  blink will report details on why it selected primitives\n");
  printf("         when you set this flag\n");
}



/*
 * main routine.
 */
int
main(int argc, char **argv)
{
    FILE	*bin_file;
    u32		gfx0, gfx1;
    int		i, c;
    extern char *optarg;
    extern int  optind;
    
    while ((c = getopt(argc, argv, "r:p:c:vxC")) != EOF) 
    {
	switch(c) 
        {
	  case 'p':
            range_expand(optarg);
	    break;

	  case 'c':
            SelXY[nSelXY].x   = atoi(optarg);
            SelXY[nSelXY++].y = atoi(argv[optind++]);
	    break;

	  case 'v':
            Verbose = 1;
	    break;

	  case 'r':
            Radius = atof(optarg);
	    break;

	  case 'x':
            Extract = 1;
	    break;

          default:
	    break;
	}
    }

    if (argc < 3) {
        print_usage(argv[0]);
	exit(-1);
    }

    if ((bin_file=fopen(argv[optind++],"r"))==NULL) {
	fprintf(stderr,"can't open bin file [%s].\n",argv[optind-1]);
	exit(-1);
    }

    if ((Outfp=fopen(argv[optind],"w"))==NULL) {
	fprintf(stderr,"can't open output file [%s].\n",argv[optind]);
	exit(-1);
    }

    fread(&gfx0, sizeof(u32), 1, bin_file);
    fread(&gfx1, sizeof(u32), 1, bin_file);
    while (!feof(bin_file)) {

	read_rdp(gfx0, gfx1, bin_file);

	fread(&gfx0, sizeof(u32), 1, bin_file);
	fread(&gfx1, sizeof(u32), 1, bin_file);
    }

    fclose(bin_file);
    fclose(Outfp);
}



/*
 *  isnumber()
 */
static int
isnumber (char c)
{
   if (c >= '0' && c <= '9')
      return 1;
   else
      return 0;
}

/*
 *  add_number_to_list()
 */
static void
add_number_to_list (char *number)
{
  if(nSelPrim < MAX_SEL_PRIM)
    SelPrimitive[nSelPrim++] = atoi(number);
}


/*
 *  range_expand - exapnd simple range expression
 *
 *  returns NULL terminated list of numeric strings
 */
void
range_expand (char *range)
{
   char   *exp, start_num[20], stop_num[20], prnt_num[20];
   int     n, state, next_state, start_n, stop_n, sindx, pindx, cindx;

   sindx = 0;
   pindx = 0;
   cindx = 0;

   for (state = 0, exp = range; *exp != '\0'; exp++)
     {
	switch (state)
	   {
	   case 0:
	      if (isnumber (*exp))
		{
		   start_num[sindx++] = *exp;
		   next_state = 1;
		}
	      else
		{
		   fprintf (stderr, "Syntax Error: character #%d (%c), state = %d\n", cindx, *exp, state);
		   exit (1);
		}
	      break;
	   case 1:
	      if (isnumber (*exp))
		{
		   start_num[sindx++] = *exp;
		   next_state = 1;
		}
	      else if (*exp == ',')
		{
		   start_num[sindx] = '\0';	/*
						   terminate start number 
						 */
		   sindx = 0;	/*
				   reset start number 
				 */
		   start_n = atoi (start_num);
		   sprintf (prnt_num, "%03d\0", start_n);
		   add_number_to_list (prnt_num);
		   next_state = 0;
		}
	      else if (*exp == '-')
		{
		   start_num[sindx] = '\0';	/*
						   terminate start number 
						 */
		   start_n = atoi (start_num);
		   next_state = 2;
		}
	      else
		{
		   fprintf (stderr, "Syntax Error: character #%d (%c), state = %d\n", cindx, *exp, state);
		   exit (1);
		}
	      break;
	   case 2:
	      if (isnumber (*exp))
		{
		   stop_num[pindx++] = *exp;
		   next_state = 3;
		}
	      else
		{
		   fprintf (stderr, "Syntax Error: character #%d (%c), state = %d\n", cindx, *exp, state);
		   exit (1);
		}
	      break;
	   case 3:
	      if (isnumber (*exp))
		{
		   stop_num[pindx++] = *exp;
		   next_state = 3;
		}
	      else if (*exp == ',')
		{
		   stop_num[pindx] = '\0';	/*
						   terminate stop number 
						 */
		   sindx = 0;	/*
				   reset start number 
				 */
		   pindx = 0;	/*
				   reset stop number 
				 */
		   stop_n = atoi (stop_num);
		   if (start_n >= stop_n)
		     {
			fprintf (stderr, "Syntax Error: in range first number(%d) must be smaller than second(%d)\n", start_n, stop_n);
			exit (1);
		     }
		   for (n = start_n; n <= stop_n; n++)
		     {
			sprintf (prnt_num, "%03d\0", n);
			add_number_to_list (prnt_num);
		     }
		   next_state = 0;
		}
	      else
		{
		   fprintf (stderr, "Syntax Error: character #%d (%c), state = %d\n", cindx, *exp, state);
		   exit (1);
		}
	      break;
	   }
	state = next_state;
	cindx++;
     }

   /*
    *  Cleanup
    */
   start_num[sindx] = '\0';
   start_n = atoi (start_num);

   if (pindx)
     {
	stop_num[pindx] = '\0';
	stop_n = atoi (stop_num);
     }

   if (sindx && !pindx)
     {
	sprintf (prnt_num, "%03d\0", start_n);
	add_number_to_list (prnt_num);
     }
   else if (sindx && pindx)
     {
	if (start_n >= stop_n)
	  {
	     fprintf (stderr, "Syntax Error: in range first number(%d) must be smaller than second(%d)\n", start_n, stop_n);
	     exit (1);
	  }
	for (n = start_n; n <= stop_n; n++)
	  {
	     sprintf (prnt_num, "%03d\0", n);
	     add_number_to_list (prnt_num);
	  }
     }

}