monitor.c 10.6 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
 /************************************************************************\
 *                                                                        *
 *               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.  *
 *                                                                        *
 \************************************************************************/

/* Software monitor, takes output of video interface module and simulates */
/* a simple television.                                                   */
/*
/* NOTE: This won't work if the first input vector has a vertical sync    */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <PRimage.h>

int StartLine  =  21;
int EndLine    = 483;
int FirstPixel = 100;
int LastPixel  = 420;
int CurrentRow;

unsigned short **RedRows, **GreenRows, **BlueRows;
unsigned short *OutputRed, *OutputGreen, *OutputBlue;
int ImageWidth, ImageHeight;

char *InputFileName, *OutputFileName;
IMAGE *image;
FILE *InFile;
char InData[80] = "foo";
char *ReadOk = "foo";
int CurrentLine = 0;

unsigned int vbus_data, vbus_sync;

void usage(void)
{
    fprintf(stderr, "!!!ERROR, try again \n");
    exit(1);
}

void GetData(void)
{

    ReadOk = fgets(InData, 80, InFile);
    CurrentLine++;

    while (InData[0] == '#')
	{
	    ReadOk = fgets(InData, 80, InFile);
	    CurrentLine++;
	}
    
    sscanf(InData, "%x %d", &vbus_data, &vbus_sync);    
}

void monitor()
{
    int FieldBegin = 0;
    unsigned short *(BufferArray[3]);
    int BufferArrayPtr = 0;
    int ScanLine = -1;
    int CurrentScanLine;
    int PixelNumber;
    int i;

    BufferArray[0] = OutputRed;
    BufferArray[1] = OutputGreen;
    BufferArray[2] = OutputBlue;

    /* printf("Using width %d height %d \n", ImageWidth, ImageHeight); */

    /* Loop until vbus_data == 0x0f since the bus is
       basically garbage until then */

    while (vbus_data != 0x0f)
	{
	    GetData();
	}

    /* printf("Got 0x0f on line %d \n", CurrentLine); */

    while (!feof(InFile))
	{
	    /* printf("!!!!!!!!!!Entering FieldBegin loop on line %d \n", CurrentLine); */
	    
	    /* Loop until get vertical sync */
	    while (!FieldBegin && !feof(InFile))
		{
		    GetData();
		    if ((!vbus_sync) && !(vbus_data & (1 << 3)))
			FieldBegin = 1;	    
		}

	    /* if (!feof(InFile)) printf("Got vertical sync on line %d \n", CurrentLine); */

	    /* Loop until hit first horizontal line visible on screen */
	    while ((ScanLine < StartLine) && !feof(InFile))
		{
		    /* This if statement is to catch when we get a vertical
		       and horizontal sync on the same clock */
		    if ((!vbus_sync) && !(vbus_data & (1 << 1)))
			{
			    int HitEndOfSync = 0;
		    
			    /* printf("Entering hsync on line %d \n", CurrentLine); */
		    
			    while (!HitEndOfSync && !feof(InFile))
				{
				    GetData();
				    if (!vbus_sync)
					{
					    if ((vbus_data & (1 << 1)))
						HitEndOfSync = 1;
					}				    
				}
			    /* printf("Leaving hsync on line %d \n", CurrentLine); */

			    ScanLine++;	    	    
			}

		    GetData();

		    if ((!vbus_sync) && !(vbus_data & (1 << 1)))
			{
			    int HitEndOfSync = 0;

			    /* printf("Entering hsync on line %d \n", CurrentLine); */
		    
			    while (!HitEndOfSync && !feof(InFile))
				{
				    GetData();
				    if (!vbus_sync)
					{
					    if ((vbus_data & (1 << 1)))
						HitEndOfSync = 1;
					}				    
				}

			    /* printf("Leaving hsync on line %d \n", CurrentLine); */

			    ScanLine++;	    	    
			}

		}

	    /* if (!feof(InFile)) printf("Starting visible lines on line %d \n", CurrentLine); */

	    /* Now loop through all the visible horizontal lines */
	    while ((ScanLine <= EndLine) && !feof(InFile))
		{
		    PixelNumber = 0;

		    /* printf("->Current Row  %d <- \n", CurrentRow); */

		    if (CurrentRow == 2*ImageHeight)
			{
			    printf("ERROR: input data has too many rows, exiting \n");
			    exit(1);
			}

		    OutputRed   = RedRows[CurrentRow];
		    OutputGreen = GreenRows[CurrentRow];
		    OutputBlue  = BlueRows[CurrentRow];

		    BufferArray[0] = OutputRed;
		    BufferArray[1] = OutputGreen;
		    BufferArray[2] = OutputBlue;

		    /* init output arrays so that if we get a truncated line later we don't
		       have crap showing through */
		    for (i=0; i< ImageWidth ; i++)
			{
			    OutputRed[i]   = 0;
			    OutputGreen[i] = 0;
			    OutputBlue[i]  = 0;
			}


		    /* Loop along pixels until hit first visible pixel in line */
		    while ((PixelNumber < FirstPixel) && !feof(InFile))
			{
			    GetData();
			    if (!vbus_sync) PixelNumber++;
			}

		    /* printf("Starting visible pixel data on line %d \n", CurrentLine); */

		    /* Now loop through the visible pixels in this line */
		    /* Note that the pixel numbers are starting inclusive and
		       ending exclusive */
		    while ((PixelNumber < LastPixel) && !feof(InFile))
			{
			    /* Read in srgb values */

			    GetData();
			    if (!vbus_sync)
				{
				    PixelNumber++;
				    BufferArrayPtr = 0;
				}
			    else
				{
				    if (BufferArrayPtr > 2)
					{
					    fprintf(stderr, "!!!ERROR bad rgb data, line %d \n", CurrentLine);
					    exit(1);
					}

				    *(BufferArray[BufferArrayPtr++])++ = (short) vbus_data;
				/* printf("Valid rgb data -> 0x%.2x \n", vbus_data); */
				}		    
			}

		    /* printf("Finished visible pixel data on line %d CurrentRow-> %d \n",  
			   CurrentLine, CurrentRow); */

		    CurrentRow++;

		    /* Now loop through the pixels on the current line that are off the visible edge */
		    /* Stop when we have passed the horizontal sync for the next line */

		    CurrentScanLine = ScanLine;
		    while ((CurrentScanLine == ScanLine) && !feof(InFile))
			{
			    if ((!vbus_sync) && !(vbus_data & (1 << 1)))
				{
				    int HitEndOfSync = 0;
			    
				/* printf("Entering hsync on line %d \n", CurrentLine); */
			    
				    while (!HitEndOfSync && !feof(InFile))
					{
					    GetData();
					    if (!vbus_sync)
						{
						    if ((vbus_data & (1 << 1)))
							HitEndOfSync = 1;
						}				    
					}
				/* printf("Leaving hsync on line %d \n", CurrentLine); */
			    
				    ScanLine++;	    	    
				}
			    GetData();
			}
		}

	    FieldBegin = 0;
	    ScanLine   = -1;
	}

    /* Stop reading after last visible line, don't care what else is happening in file */
    /* All that we require is that there is a final horizontal sync after the end of   */
    /* the last visible line */
}

void OpenRGBFile(int Height)
{
    if ((image = iopen(OutputFileName, "w", RLE(1), 3, ImageWidth, Height, 3)) == NULL)
	{
	    fprintf(stderr, "!!!ERROR, could not open output file %s \n", OutputFileName);
	    exit(1);
	}
}

int Foo(int i)
{
    return(ImageHeight*2 - 1 - (2 * (i % (CurrentRow / 2)) + 2 * i / CurrentRow));
}

void WriteRGBFile(void)
{
    int i;

    /* printf("Current Row %d, ImageHeight %d \n", CurrentRow, ImageHeight); */

    OpenRGBFile(CurrentRow); 

    if (CurrentRow <= ImageHeight)
	{
	    for (i=0; i<CurrentRow; i++)
		{
		    putrow(image, RedRows[i],   CurrentRow - i - 1, 0);
		    putrow(image, GreenRows[i], CurrentRow - i - 1, 1);
		    putrow(image, BlueRows[i],  CurrentRow - i - 1, 2); 
		}
	}
    else
	{
	    /* Do interlacing crap-o-la here */

	    for (i=0; i<CurrentRow; i++)
		{
		    /* printf("(%d) Putting row %d \n", i, Foo(i)); */
		    
		    putrow(image, RedRows[i],   Foo(i), 0);
		    putrow(image, GreenRows[i], Foo(i), 1);
		    putrow(image, BlueRows[i],  Foo(i), 2);
		}
	}

    iclose(image);
}

void ParseOptions(int argc, char **argv)
{
    int c;
    extern char *optarg;
    extern int optind;
    char *ofile = NULL;

    while ((c = getopt(argc, argv, "i:o:b:e:f:l:h:v:")) != EOF)
	switch (c)
	    {
	    case 'i':
		InputFileName = strdup(optarg);
		break;
	    case 'o':
		OutputFileName = strdup(optarg);
		break;
	    case 'b':
		StartLine = atoi(optarg);
		break;
	    case 'e':
		EndLine = atoi(optarg);
		break;
	    case 'f':
		FirstPixel = atoi(optarg);
		break;
	    case 'l':
		LastPixel = atoi(optarg);
		break;
	    case 'h':
		LastPixel  = strtoul(optarg, (char **) NULL, 16) & ((1 << 10) - 1);
		FirstPixel = (strtoul(optarg, (char **) NULL, 16) & (((1 << 10) - 1) << 16)) >> 16;
		/* printf("First 0x%.2x Last 0x%.2x \n", FirstPixel, LastPixel); */
		break;
	    case 'v':
		EndLine   = ((strtoul(optarg, (char **) NULL, 16)  & ((1 << 10) - 1)) - 1) >> 1;
		StartLine = (((strtoul(optarg, (char **) NULL, 16)  & (((1 << 10) - 1) << 16)) >> 16) + 1) >> 1;
		/* printf("Start 0x%.2x End 0x%.2x \n", StartLine, EndLine); */
		break;
	    case '?':
		usage();
		break;		
	    }    
}

void CheckOptions(void)
{
    if ((InputFileName == NULL) || (OutputFileName == NULL))
	usage();

    ImageWidth  = LastPixel - FirstPixel;
    ImageHeight = EndLine - StartLine + 1;

    if ((ImageWidth < 0) || (ImageHeight < 0))
	{
	    fprintf(stderr, "!!!ERROR Image size incorrect, %d %d \n",
		    ImageWidth, ImageHeight);
	}
}

void InitializeVariables(void)
{
    int i;

    RedRows =   (unsigned short **) malloc(ImageHeight * 2 * sizeof(unsigned short *));
    GreenRows = (unsigned short **) malloc(ImageHeight * 2 * sizeof(unsigned short *));
    BlueRows =  (unsigned short **) malloc(ImageHeight * 2 * sizeof(unsigned short *));

    for (i=0; i< ImageHeight * 2; i++)
	{
	    RedRows[i]   = (unsigned short *) malloc(ImageWidth * sizeof(unsigned short));
	    GreenRows[i] = (unsigned short *) malloc(ImageWidth * sizeof(unsigned short));
	    BlueRows[i]  = (unsigned short *) malloc(ImageWidth * sizeof(unsigned short));
	}

    CurrentRow = 0;
    OutputRed   = RedRows[CurrentRow];
    OutputGreen = GreenRows[CurrentRow];
    OutputBlue  = BlueRows[CurrentRow];
}

void OpenInFile(void)
{
    if ((InFile = fopen(InputFileName, "r")) == NULL)
	{
	    fprintf(stderr, "!!!ERROR, could not open input file %s \n", InputFileName);
	    exit(1);
	}    
}

main(int argc, char *argv[])
{
    char InData[80] = "foo";

    unsigned int vbus_data, vbus_sync, vbus_clock_enable_l, vi_int, 
	refresh_strobe, cbus_data;

    int ValidLines = 0;

    ParseOptions(argc, argv);
    CheckOptions();
    InitializeVariables();
    OpenInFile();

    while((InData[0] != '0') && (InData[1] != 'x'))
	{
	    fgets(InData, 80, InFile);
	    CurrentLine++;
	}

    monitor();
    WriteRGBFile();

    fclose(InFile);
}