frame_buf.c 2.9 KB
#include <stdio.h>
#include <string.h>
#ifdef __sgi__
#include <bstring.h>
#include <gl/gl.h>
#include <gl/device.h>
#endif
#include <PRimage.h>

#include "image.h"
#include "acc_user.h"
#include "vcsuser.h"

#define	TAB_LINE_SIZE	2048

static s_setval_value value_s = { accHexStrVal };
static s_setval_delay delay_s = { { accRealTime, 0, 0, 0 }, accNoDelay };

static Image FrameBuf;

/*
 *  save_image -- save 32b RGBA frame buffer in 24b .rgb format
 *
 *
 */
static void
  save_image(char *name, Image *image)
{
  IMAGE           *img;
  unsigned short  *cb;
  unsigned char   *fb;
  int             x, xl, y, xsize, ysize;

  xsize = image->xsize;
  ysize = image->ysize;

  img = iopen(name, "w", RLE(1), 3, xsize, ysize, 3);
  if (img == (IMAGE *)0) {
    fprintf(stderr, "error in creating image %s\n", name);
    return;
  }

  cb = (unsigned short *)calloc(sizeof(unsigned short),  xsize);
  fb = image->base;

  for (y = 0; y < ysize; y++, fb += image->lsize) {
    for (xl = 0, x = 0; xl < xsize; x += 4, xl++) cb[xl] = fb[x];
    putrow(img, cb, y, 0);
    for (xl = 0, x = 1; xl < xsize; x += 4, xl++) cb[xl] = fb[x];
    putrow(img, cb, y, 1);
    for (xl = 0, x = 2; xl < xsize; x += 4, xl++) cb[xl] = fb[x];
    putrow(img, cb, y, 2);
  }
  free(cb);
  iclose(img);
}


void
open_frame_buffer(void)
{
  int x, y;

  acc_initialize();
  acc_configure(accDevelopmentVersion, "1.6");

  if (tf_nump() != 2) 
  {
    tf_error("illegal number of arguments") ;
    tf_putp(0, -1);
  }
  else
  {
    if (((x = tf_getp(1)) == NULL) ||
	((y = tf_getp(2)) == NULL)) 
    {
      tf_putp(0, -1);
    } 
    else
    {
      FrameBuf.xsize = x;
      FrameBuf.ysize = y;
      FrameBuf.lsize = x * 4; /* 32 bit pixels */
      FrameBuf.base = (unsigned char *) 
	calloc(FrameBuf.ysize * FrameBuf.lsize, sizeof(unsigned char));

      tf_putp(0, 0);
    }
  }
}

void
write_frame_buffer(void)
{
  char *fb_file;

acc_initialize();
acc_configure(accDevelopmentVersion, "1.6");

  if (((fb_file = tf_getcstringp(1)) == NULL))
  {
    tf_putp(0, -1);
  } 
  else
  {       
    save_image(fb_file, &FrameBuf);
    tf_putp(0, 0);
  }
}



void
put_pixel(void)
{
  int x, y, r, g, b, a;
  unsigned char *pixel;

  acc_initialize();
  acc_configure(accDevelopmentVersion, "1.6");

  if (tf_nump() != 6) 
  {
    tf_error("illegal number of arguments") ;
    tf_putp(0, -1);
  }
  else
  {
    x = tf_getp(1);
    y = tf_getp(2);
    r = tf_getp(3);
    g = tf_getp(4);
    b = tf_getp(5);
    a = tf_getp(6);
    if((x >= FrameBuf.xsize) || (y >= FrameBuf.ysize))
    {
      fprintf(stderr,"Error, x(%d) or y(%d) > max x,y(%d,%d)\n",
           x, y, FrameBuf.xsize, FrameBuf.ysize);
      tf_putp(0, -1);
    }
    else
    {
      pixel = FrameBuf.base + y * FrameBuf.lsize + x * 4;
      pixel[0] = (unsigned char) r;	
      pixel[1] = (unsigned char) g;	
      pixel[2] = (unsigned char) b;	
      pixel[3] = (unsigned char) a;	
      tf_putp(0, 0);
    }
  }
}