rdram2image.c 12.9 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
/**************************************************************************
 *                                                                        *
 *               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.  *
 *                                                                        *
 **************************************************************************/

/*
 * This file steals the code from both lib/librcppli/rdram.c for mapping in
 * the .main and .hidden files and also from rdpsim/src/mspan.c for the
 * conversion to .rgb .cvg .z .dz files
 * 
 * XXX only knows about 16b RGB framebuffers
 */

#ifdef __sgi__
  #include <bstring.h>
#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <libgen.h>

/* XXX Need to put somewhere else 2 Meg system */
#define MAIN_MEMORY_SIZE        0x200000
#define HIDDEN_FACTOR           8
#define HIDDEN_MEMORY_SIZE      (MAIN_MEMORY_SIZE/HIDDEN_FACTOR)


/* Global file pointers  (Should be passed in as arguments, but it's a hassle) */
static unsigned char *mainaddr, *hiddenaddr;
static long mainsize, hiddensize;

static char UsageString[] = "[-2qz] filename";

extern unsigned int quiet_mode;

/*
 *  Stolen from Memory Span Unit
 */

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#ifdef __sgi__
#include <gl.h>
#endif
#include <PRimage.h>

#include <mbi.h>
#include <verify.h>

#include "rdram.h"

/*
 * The following should be in gl/image.h, but they are commented out
 * Also note that the prototype for iopen in image.h is wrong
 */
int iclose(IMAGE *image);
int putrow(IMAGE *image, unsigned short *buffer, unsigned int y,unsigned int z);

typedef struct rdram {
    unsigned char *main_addr;
    unsigned char *hidden_addr;
    long main_size;
    long hidden_size;
} rdram_t;

/* XXX I wish there was someway to get both of these */
#define MAXX_INIT	320
#define MAXY_INIT	240

/* Global data - Ok since only one instance */
static rdram_t ram;
static int ncapture;
static char outputFilename[256];
static char *infilename = "test"; /* if no outputFilename specified, use this */
static int screen_width=MAXX_INIT;
static int screen_height=MAXY_INIT;
static int zbufdump=1;

static unsigned char hiddenmask[] = {
    ~0xc0, 
    ~0x30,
    ~0x0c, 
    ~0x03 
};

static int hiddenshift[] = {
    6,
    4, 
    2, 
    0
};

/* Routine from Kevin to dump data out in his format for backend filter */
static void
WriteCVGFile(int Format, int Size, int Width, int Height, 
	     char *MainBase, char *HiddenBase, char *TmemBase)
{
    FILE *CVGFile;
    char FileName[256];
    int MainSize, HiddenSize, TmemSize;
    unsigned char F, S;
    unsigned short W, H;
    struct CVGHeader ConvHeader;

    sprintf(FileName, "OutData/%s_%d.cvg", outputFilename, ncapture);
    if (!quiet_mode)
      fprintf(stderr, "saving %s\n", FileName);

    if ((CVGFile = fopen(FileName, "w")) == NULL)
	{
	    fprintf(stderr, "Couldn't create .cvg file \n");
	    return;
	}

    if ((Format == G_IM_FMT_RGBA) && (Size == G_IM_SIZ_32b))
	{
	    MainSize   = (Width * Height) << 2;
	    HiddenSize = 0;
	    TmemSize   = 0;
	}
    else if ((Format == G_IM_FMT_IA) && (Size == G_IM_SIZ_16b))
	{
	    MainSize   = (Width * Height) << 1;
	    HiddenSize = 0;
	    TmemSize   = 0;
	}
    else if ((Format == G_IM_FMT_RGBA) && (Size == G_IM_SIZ_16b))
	{
	    MainSize   = (Width * Height) << 1;
	    HiddenSize = (MainSize >> 3) + ((MainSize % 8) ? 1 : 0);
	    TmemSize   = 0;
	}
    else if ((Format == G_IM_FMT_I) && (Size == G_IM_SIZ_8b))
	{
	    MainSize   = (Width * Height) << 0;
	    HiddenSize = 0;
	    TmemSize   = 0;
	}
    else if ((Format == G_IM_FMT_CI) && (Size == G_IM_SIZ_8b))
	{
	    MainSize   = (Width * Height) << 0;
	    HiddenSize = 0;
	    /* TmemSize   = Something; */
	}
    else
	{
	    fprintf(stderr, "Error in size/format \n");
	    return;
	}
	
    bzero(&ConvHeader, sizeof(ConvHeader));
    ConvHeader.magic  = htonl(CVGMAGIC);
    ConvHeader.format = Format;
    ConvHeader.size   = Size;
    ConvHeader.width  = htons(Width);
    ConvHeader.height = htons(Height);

    fwrite(&ConvHeader, sizeof(struct CVGHeader), 1, CVGFile);

    fwrite(MainBase,   1, MainSize,   CVGFile);
    fwrite(HiddenBase, 1, HiddenSize, CVGFile);
    fwrite(TmemBase,   1, TmemSize,   CVGFile);

    fclose(CVGFile);
}


static void
captureimage32(unsigned char *base, long xsize, long ysize)
{
    IMAGE		*img;
    char filename[100];
    unsigned char *p;
    unsigned short *rbuf, *gbuf, *bbuf;
    int i, j;

    sprintf(filename, "OutData/%s_%d.rgb", outputFilename, ncapture);
    if (!quiet_mode)
      fprintf(stderr, "saving (32 bit RGB) %s\n", filename);
    
    img = iopen(filename, "w", RLE(1), 3, xsize, ysize, 3);
    if (img == (IMAGE *)0) {
	fprintf(stderr, "couldn't create .rgb file\n");
	return;
    }
    
    rbuf = (unsigned short *)malloc(sizeof(unsigned short) * xsize);
    if (rbuf == NULL)
	goto cleanup;
    
    gbuf = (unsigned short *)malloc(sizeof(unsigned short) * xsize);
    if (gbuf == NULL)
	goto cleanup;

    
    bbuf = (unsigned short *)malloc(sizeof(unsigned short) * xsize);
    if (bbuf == NULL)
	goto cleanup;

        
    /* read data out of framebuffer into .rgb file */
    for ( i = 0; i < ysize; i++) {
	p = base + ((ysize - i - 1) * xsize * 4);
	for (j = 0; j < xsize; j++) {
	    rbuf[j] = *p++;
	    gbuf[j] = *p++;
	    bbuf[j] = *p++;
	    *p++;
	}
	putrow(img, rbuf, i, 0);
	putrow(img, gbuf, i, 1);
	putrow(img, bbuf, i, 2);
    }
    
cleanup:
    if (rbuf) free(rbuf);
    if (gbuf) free(gbuf);
    if (bbuf) free(bbuf);
    iclose(img);
}

static void
captureimage16(unsigned char *base, long xsize, long ysize)
{
    IMAGE		*img, *cvgimg = NULL;
    char filename[100];
    unsigned char *p, c1, c2;
    unsigned short *rbuf, *gbuf, *bbuf, *cbuf;
    int i, j, offset;

    sprintf(filename, "OutData/%s_%d.rgb", outputFilename, ncapture);
    if (!quiet_mode)
      fprintf(stderr, "saving (16 bit RGB) %s\n", filename);
    
    img = iopen(filename, "w", RLE(1), 3, xsize, ysize, 3);
    if (img == (IMAGE *)0) {
	fprintf(stderr, "couldn't create .rgb file\n");
	return;
    }
    if (zbufdump) {
	    sprintf(filename, "OutData/%s_%d.cvg", outputFilename, ncapture);
	    cvgimg = iopen(filename, "w", RLE(1), 2, xsize, ysize, 1);
	    if (cvgimg == (IMAGE *)0) {
		fprintf(stderr, "couldn't create .cvg file\n");
		return;
	    }
    }
    rbuf = (unsigned short *)malloc(sizeof(unsigned short) * xsize);
    if (rbuf == NULL)
	goto cleanup;
    
    gbuf = (unsigned short *)malloc(sizeof(unsigned short) * xsize);
    if (gbuf == NULL)
	goto cleanup;

    
    bbuf = (unsigned short *)malloc(sizeof(unsigned short) * xsize);
    if (bbuf == NULL)
	goto cleanup;
	
    cbuf = (unsigned short *)malloc(sizeof(unsigned short) * xsize);
    if (cbuf == NULL)
	goto cleanup;

        
    /* read data out of framebuffer into .rgb file */
    for ( i = 0; i < ysize; i++) {
	p = base + ((ysize - i - 1) * xsize * 2);
	for (j = 0; j < xsize; j++) {
	    c1 = *p++;
	    c2 = *p++;
	    rbuf[j] = c1 & 0xf8;
	    gbuf[j] = ((c1 << 5) | (c2 >> 3)) & 0xf8;
	    bbuf[j] = (c2 << 2) & 0xf8;
	    
	    /* coverage bits */
	    offset = (i * xsize) + j;
	}
	putrow(img, rbuf, i, 0);
	putrow(img, gbuf, i, 1);
	putrow(img, bbuf, i, 2);
    }
    
cleanup:
    if (rbuf) free(rbuf);
    if (gbuf) free(gbuf);
    if (bbuf) free(bbuf);
    if (cbuf) free(bbuf);
    if (img) iclose(img);
    if (cvgimg) iclose(cvgimg);
}

static void
capturezbuf(unsigned char *Mainbase, char *Hiddenbase, 
             long xsize, long ysize)
{
    IMAGE		*img;
    char filename[100];
    unsigned char *p, c1, c2;
    unsigned char *h, h1;
    unsigned short *rbuf, *gbuf, *bbuf, *cbuf;
    int i, j, offset;
    int exp,man,dz,z;

    sprintf(filename, "OutData/%s_%d.z", outputFilename, ncapture);
    if (!quiet_mode)
      fprintf(stderr, "saving (18 bit zbuffer) %s\n", filename);
    
    img = iopen(filename, "w", RLE(1), 3, xsize, ysize, 3);
    if (img == (IMAGE *)0) {
	fprintf(stderr, "couldn't create .z file\n");
	return;
    }
     
    rbuf = (unsigned short *)malloc(sizeof(unsigned short) * xsize);
    if (rbuf == NULL)
	goto cleanup;
    
    gbuf = (unsigned short *)malloc(sizeof(unsigned short) * xsize);
    if (gbuf == NULL)
	goto cleanup;

    
    bbuf = (unsigned short *)malloc(sizeof(unsigned short) * xsize);
    if (bbuf == NULL)
	goto cleanup;
	
    cbuf = (unsigned short *)malloc(sizeof(unsigned short) * xsize);
    if (cbuf == NULL)
	goto cleanup;

        
    /* read data out of zbuffer into .z file */
    assert ((xsize % 4) == 0);
    for ( i = 0; i < ysize; i++) {
	p = Mainbase + ((ysize - i - 1) * xsize * 2);
	h = Hiddenbase + (((ysize - i - 1) * xsize * 2)/HIDDEN_FACTOR);
	for (j = 0; j < xsize; j++) {
	    c1 = *p++;
	    c2 = *p++;
	    h1 = (*h >> hiddenshift[j % 4]) & 0x3;
	    if ((j % 4) == 3)
		h++;

	    exp = (c1>>5) & 0x7;
            man = (((int)c1<<6) & 0x7c0)  | ((c2 >> 2) & 0x3f);
	    dz  = ((c2<<2) & 0xc) | (h1 & 0x3);

	    if (exp > 6)
		z = 0x7f << 11;
	    else {
		man <<= (6-exp);
		z = 0x7f << (18-exp);
	    }
	    z = (z | man) & 0x3ffff;
	    z |= dz << 20;


	    bbuf[j] = (z >> 16) & 0xff;
	    gbuf[j] = (z >>  8) & 0xff;
	    rbuf[j] = (z)       & 0xff;
	    
	    /* coverage bits */
	    offset = (i * xsize) + j;
	}
	putrow(img, rbuf, i, 0);
	putrow(img, gbuf, i, 1);
	putrow(img, bbuf, i, 2);
    }
    
cleanup:
    if (rbuf) free(rbuf);
    if (gbuf) free(gbuf);
    if (bbuf) free(bbuf);
    if (cbuf) free(cbuf);
    if (img) iclose(img);
}

captureimage(unsigned long imagebase, int size, int format, unsigned long zbase)
{
    /*
     * Create the OutData directory, if it doesn't already exist.
     */
    if (access("./OutData", F_OK) == -1) {
        if (mkdir("./OutData", 06775) == -1) {
	    fprintf(stderr, "memspan: couldn't open OutData directory.\n");
	    perror(NULL);
	    exit(1);
	}
    }

    if (size == G_IM_SIZ_32b)
	captureimage32(ram.main_addr + imagebase, screen_width, screen_height);
	
    if (size == G_IM_SIZ_16b)
	captureimage16(ram.main_addr + imagebase, screen_width, screen_height);
	
    if (zbufdump) WriteCVGFile(format, size, screen_width, screen_height, ram.main_addr + imagebase,
		ram.hidden_addr + (imagebase / HIDDEN_FACTOR), NULL);
    if (zbufdump)
        capturezbuf(ram.main_addr + zbase, 
                ram.hidden_addr + (zbase / HIDDEN_FACTOR), 
                screen_width, screen_height);
    
    ncapture++;
}


static void
  print_usage( char *prog )
{
    fprintf(stderr,"usage: %s %s.\n",prog,UsageString);
    fprintf(stderr,"  -2: use second framebuffer\n");
}



int
main(int argc, char **argv)
{
    unsigned long	imagebase;
    unsigned long	zbase;
    VerifyInfo		*vp;
    int			c;
    int			read_second_fb = 0;

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

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

    while((c = getopt(argc, argv, "2quz")) != EOF)
    {
        switch(c)
        {
	    case '2':
    	      read_second_fb = 1;
	      break;
            case 'q':
              quiet_mode = 1;
              break;
	    case 'z':
	      zbufdump = 0;
              break;
	    case 'u':
	    default:
	      print_usage(argv[0]);
	      exit(-1);
        }
    }

    if (optind >= argc) {
	print_usage(argv[0]);
	exit(-1);
    }

    if (rdraminit(argv[optind], RDRAM_RDONLY, zbufdump ? RDRAM_NEEDHIDDEN : 0, 0, &ram.main_addr,
	    &ram.main_size, &ram.hidden_addr, &ram.hidden_size) == 0)
        return(1);

    vp = (VerifyInfo *)(ram.main_addr + VERIFY_INFO_PHYSADDR);
       
    if(!read_second_fb) 
      imagebase = ntohl(vp->frameBuffer0Addr);
    else
      imagebase = ntohl(vp->frameBuffer1Addr);

    zbase = zbufdump ? ntohl(vp->zBufferAddr) : 0;
    screen_width = ntohs(vp->width);
    screen_height =ntohs(vp->height);
    
    
    if (!quiet_mode)
    {
      fprintf(stderr, "cfbaddr 0x%x,  zaddr 0x%x\n", imagebase, zbase);
      fprintf(stderr, "width %d, height %d\n", screen_width, screen_height);
      fprintf(stderr, "format %d, size %d\n", 
                   ntohl(vp->frameBufferFormat), ntohl(vp->frameBufferSize));
    }
    

    strcpy(outputFilename, basename(argv[optind]));
    
    /* old programs may not set format & size */
    if (ntohl(vp->frameBufferFormat) == 0 &&  ntohl(vp->frameBufferSize == 0)) {
	captureimage(imagebase,  G_IM_SIZ_16b, G_IM_FMT_RGBA, zbase);
    } else {
	captureimage(imagebase,  ntohl(vp->frameBufferSize), 
                     ntohl(vp->frameBufferFormat), zbase);
    }

}