display.c 6.22 KB
#include <stdio.h>
#include <PRimage.h>

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

FILE *OutFile;
IMAGE *RGBFile;

int CurrentX, CurrentY, SyncsSeen;
unsigned short *BufferPtr[3];
unsigned short *RedBuffer, *GreenBuffer, *BlueBuffer;
int ImageWidth, ImageHeight, BufferIndex, PixelsValid;

void InitPixelVariables(int Width, int Height)
{
    ImageWidth  = Width;
    ImageHeight = Height;

    RedBuffer   = (unsigned short *) malloc(ImageWidth * sizeof(unsigned short));
    GreenBuffer = (unsigned short *) malloc(ImageWidth * sizeof(unsigned short));
    BlueBuffer  = (unsigned short *) malloc(ImageWidth * sizeof(unsigned short));

    BufferPtr[0] = RedBuffer;
    BufferPtr[1] = GreenBuffer;
    BufferPtr[2] = BlueBuffer;

    CurrentX = 0; CurrentY = 0;
    BufferIndex = 0; 
    PixelsValid = 0;
    SyncsSeen   = 0;
}

void open_rgb_file(void)
{
    char *filename;
    int Width, Height;

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

    if (tf_nump() != 3)
	{
	    tf_error("Illegal number of arguments to open_rgb_file");
	    tf_putp(0, -1);
	}
    else
	{
	    if (((Width    = tf_getp(2))        == NULL) || 
		((Height   = tf_getp(3))        == NULL) || 
		((filename = tf_getcstringp(1)) == NULL))
		{
		    tf_error("Illegal parameters passed to open_rgb_file");
		    tf_putp(0, -1);
		}
	    else
		{
		    InitPixelVariables(Width, Height);

		    if ((RGBFile = iopen(filename, "w", RLE(1), 3, ImageWidth, ImageHeight, 3)) == NULL)
			{
			    tf_error("Couldn't open output rgb file");
			    tf_putp(0, -1);
			}
		}
	}
    tf_putp(0, 0);
}

void WritePixel(int sync, int srgb)
{
    if (!sync & (srgb >> 3))
	{
	    SyncsSeen++;
	}    
    else 
	{
	    if (SyncsSeen >= 12)
		{
		    PixelsValid = 1;
		}
	    if (PixelsValid && (CurrentX >= 0) && (CurrentX <= ImageWidth-1) && 
		(CurrentY <= ImageHeight-1))
		{
		    *(BufferPtr[BufferIndex]) = srgb;
		    (BufferPtr[BufferIndex])++; 
		}
	    if ((CurrentX == ImageWidth+1) && (BufferIndex == 2))
		{
		    putrow(RGBFile, RedBuffer,   ImageHeight - 1 - CurrentY, 0);
		    putrow(RGBFile, GreenBuffer, ImageHeight - 1 - CurrentY, 1);
		    putrow(RGBFile, BlueBuffer,  ImageHeight - 1 - CurrentY, 2);

		    CurrentX = -3; CurrentY++;

		    BufferPtr[0] = RedBuffer;
		    BufferPtr[1] = GreenBuffer;
		    BufferPtr[2] = BlueBuffer;
		}
	    if (PixelsValid) BufferIndex++;
	    if (BufferIndex == 3) 
		{
		    CurrentX++;
		    BufferIndex = 0;
		}
	}
}

void output_pixel(void)
{
    int srgb, sync;

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

    if (tf_nump() != 2)
	{
	    tf_error("Illegal number of arguments to output_pixel");
	    tf_putp(0, -1);
	}
    else
	{
	    srgb = tf_getp(1);
	    sync = tf_getp(2);
	    if (RGBFile != NULL)
		{
		    WritePixel(sync, srgb);
		}
	}
    tf_putp(0, 0);
}

void output_all(void)
{
    int divot_enable, type, aa_off, gamma_enable, gamma_dither_enable, rand, hfrac,
	vfrac, rgb0i, rgb1i, rgb2i, rgb3i, cvg0i, cvg1i, cvg2i, cvg3i,
	synci, srgb, sync;

    if (tf_nump() != 19)
	{
	    tf_error("Illegal number of arguments to output_all");
	    tf_putp(0, -1);
	}

    if (OutFile != NULL)
	{
	    divot_enable        = tf_getp(1);
	    type                = tf_getp(2);
	    aa_off              = tf_getp(3);
	    gamma_enable        = tf_getp(4);
	    gamma_dither_enable = tf_getp(5);
	    rand                = tf_getp(6);
	    hfrac               = tf_getp(7);
	    vfrac               = tf_getp(8);
	    rgb0i               = tf_getp(9);
	    rgb1i               = tf_getp(10);
	    rgb2i               = tf_getp(11);
	    rgb3i               = tf_getp(12);
	    cvg0i               = tf_getp(13);
	    cvg1i               = tf_getp(14);
	    cvg2i               = tf_getp(15);
	    cvg3i               = tf_getp(16);
	    synci               = tf_getp(17);

	    srgb                = tf_getp(18);
	    sync                = tf_getp(19);

	    fprintf(OutFile, "%d 0x%.1x %d %d %d 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x ",
		    divot_enable, type, aa_off, gamma_enable, gamma_dither_enable, rand, 
		    hfrac, vfrac, rgb0i, rgb1i);

	    fprintf(OutFile, "0x%.2x 0x%.2x 0x%.1x 0x%.1x 0x%.1x 0x%.1x %d\t0x%.2x %d \n", 
		    rgb2i, rgb3i, cvg0i, cvg1i, cvg2i, cvg3i, synci, srgb, sync);	    
	}    
}

void PrintTabHeader(char *prog_name)
{
    fprintf(OutFile, "#\n");
    fprintf(OutFile, "#   created by %s.c\n", prog_name);
    fprintf(OutFile, "#\n");
    fprintf(OutFile, "vclk                @C 1(8) 0(8)\n");
    fprintf(OutFile, "divot_enable        @I @E 2\n");
    fprintf(OutFile, "type                @I @E 2\n");
    fprintf(OutFile, "aa_off              @I @E 2\n");
    fprintf(OutFile, "gamma_enable        @I @E 2\n");
    fprintf(OutFile, "gamma_dither_enable @I @E 2\n");
    fprintf(OutFile, "rand[5:0]           @I @E 2\n");
    fprintf(OutFile, "hfrac[4:0]          @I @E 2\n");
    fprintf(OutFile, "vfrac[4:0]          @I @E 2\n");
    fprintf(OutFile, "rgb0i[7:0]          @I @E 2\n");
    fprintf(OutFile, "rgb1i[7:0]          @I @E 2\n");
    fprintf(OutFile, "rgb2i[7:0]          @I @E 2\n");
    fprintf(OutFile, "rgb3i[7:0]          @I @E 2\n");
    fprintf(OutFile, "cvg0i[2:0]          @I @E 2\n");    
    fprintf(OutFile, "cvg1i[2:0]          @I @E 2\n");
    fprintf(OutFile, "cvg2i[2:0]          @I @E 2\n");
    fprintf(OutFile, "cvg3i[2:0]          @I @E 2\n");
    fprintf(OutFile, "synci               @I @E 2\n");

    fprintf(OutFile, "srgb[6:0]           @O @S 15 \n");
    fprintf(OutFile, "sync                @O @S 15 \n");
    
    fprintf(OutFile, "\n"); /* required newline */
}


void open_out_file(void)
{
    char *filename;

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

    if (tf_nump() != 1)
	{
	    tf_error("Illegal number of arguments to open_out_file");
	    tf_putp(0, -1);
	}
    else
	{
	    if ((filename = tf_getcstringp(1)) == NULL)
		{
		    tf_error("Illegal parameters passed to open_out_file");
		    tf_putp(0, -1);
		}
	    else
		{
		    if ((OutFile = fopen(filename, "w")) == NULL)
			{
			    tf_error("Couldn't open output file");
			    tf_putp(0, -1);
			}
		    PrintTabHeader("display");
		}
	}
    tf_putp(0, 0);
}

void close_output_files(void)
{
    if (OutFile != NULL) fclose(OutFile);
    if (RGBFile != NULL) iclose(RGBFile);

    tf_putp(0, 0);
}