display.c 2.35 KB
/*
 *  display.c - display texture output
 *
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

#include "display.h"
#include "test_image.h"

#define DEFAULT_NAME 	"tctm.rgb"
#define MAXX		320
#define MAXY		240

/*
 *   G l o b a l s
 */

static char *Fname = DEFAULT_NAME;
static int   MaxX = MAXX;
static int   MaxY = MAXY;
static Image FrameBuf;

/*
 *
 */

void
  display(display_t **pp0, display_t **pp1)
{
  unsigned char *pixel;
  /* pointers to memory structure */
  display_t *p0, *p1;
  int save_clock;

  /* get pointers */
  p0 = *pp0;
  p1 = *pp1;
  save_clock = p0->gclk;

  if(p0->gclk && !p1->gclk_old) /* posedge(gclk) */
  {
    /* transfer all next-clock register values to register outputs. */
    *pp0 = p1; /* swap */
    *pp1 = p0;
    p0 = *pp0; /* fix pointers */
    p1 = *pp1;

    if(p1->exit1)
      {
	save_image(Fname, &FrameBuf);
	p0->exit = 1; 
	printf("Display exit, saving %s\n", Fname);
      } 
    else
      p0->exit = 0; 
    
    if(p1->dv && p1->y < MaxY && p1->x < MaxX)
      {
	/* average 4 texels */
	/* write to frame buffer */
	pixel = FrameBuf.base + p1->y * FrameBuf.lsize + p1->x * 4;
	pixel[0] = (unsigned char) p1->ra;
	pixel[1] = (unsigned char) p1->ga;
	pixel[2] = (unsigned char) p1->ba;
	pixel[3] = (unsigned char) p1->aa;
      }
  } /* posedge */

  /* save last clock state */
  p1->gclk_old = save_clock;    
}


/*
 *
 */

void
  display_init(display_t *p0, display_t *p1)
{
  int c, i;

  extern char *optarg;
  extern int optind, opterr, optopt;

  optind = 1;
  opterr = 0;

  p0->gclk = p1->gclk = 0;
  p0->gclk_old = p1->gclk_old = 0;
  p0->exit1 = p1->exit1 = 0;
  p0->exit = p1->exit = 0;
  while((c = getopt(p1->argc, p1->argv, "d:f:i:t:s:x:y:h")) != EOF)
  {
     switch(c)
     {
       case 'f':
	  Fname = optarg;
          break;
       case 'x':
          MaxX = atoi(optarg);
          break;
       case 'y':
          MaxY = atoi(optarg);
          break;
       case 'h': 
		fprintf(stderr,"Usage for display:\n");
		fprintf(stderr," -f <rgb output file>\n");
		fprintf(stderr," -x <max_x>\n");
		fprintf(stderr," -y <max_y>\n");
		p0->exit = 1;
		return;
       default:
	  break;
     }
  }

  /*
   * Create Frame Buffer
   */

  FrameBuf.xsize = MaxX;
  FrameBuf.ysize = MaxY;
  FrameBuf.lsize = MaxX * 4; /* 32 bit pixels */
  FrameBuf.base = (unsigned char *) malloc(FrameBuf.ysize * FrameBuf.lsize);

}