main2image.c 12.4 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
/*
 * 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
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.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 int mainfd, hiddenfd;
static unsigned char *mainaddr, *hiddenaddr;

static char UsageString[] = "filename imagebase zbase";

/*
 * Usage:  $rdram_mmap_init(filename)
 */
static int
rdram_mmap_init(char *file)
{
    struct stat statbuf;
    char filename[1024];
    
fprintf(stderr, "rdram_mmap_init: file is %s\n", file);

    /* Open files, and then mmap them in */
    sprintf(filename, "%s.main", file);
    if (stat(filename, &statbuf) == -1) {
	fprintf(stderr, "Could not stat <%s>\n", filename);
	return 0;
    }
    
    if (statbuf.st_size != MAIN_MEMORY_SIZE) {
	fprintf(stderr, "Error! %s size not %d\n", file, MAIN_MEMORY_SIZE);
	return 0;
    }
    
    if ((mainfd = open(filename, O_RDONLY, 0644)) == -1) {
	fprintf(stderr, "Could not open <%s>\n", filename);
	return 0;
    }
    
    
    mainaddr = mmap(NULL, MAIN_MEMORY_SIZE, PROT_READ,
		MAP_SHARED, mainfd, 0);
		
fprintf(stderr, "rdram_mmap_init: mainaddr is 0x%x\n", mainaddr);

    if (mainaddr == (unsigned char *)-1) {
	fprintf(stderr, "Could not mmap <%s>\n", filename);
	return 0;
    }
    
    /* now for hidden part */
    sprintf(filename, "%s.hidden", file);
    if (stat(filename, &statbuf) == -1) {
	fprintf(stderr, "Could not stat <%s>\n", filename);
	return 0;
    }
    
    if (statbuf.st_size != HIDDEN_MEMORY_SIZE) {
	fprintf(stderr, "Error! %s size not %d\n", file, MAIN_MEMORY_SIZE);
	return 0;
    }
    
    if ((hiddenfd = open(filename, O_RDONLY, 0644)) == -1) {
	fprintf(stderr, "Could not open <%s>\n", filename);
	return 0;
    }
    
    hiddenaddr = mmap(NULL, HIDDEN_MEMORY_SIZE, PROT_READ,
		MAP_SHARED, hiddenfd, 0);
		
fprintf(stderr, "rdram_mmap_init: hiddenaddr is 0x%x\n", hiddenaddr);

    if (hiddenaddr == (unsigned char *)-1) {
	fprintf(stderr, "Could not mmap <%s>\n", filename);
	return 0;
    }
    
    return 1;
    
}








/**************************************************************************
 *                                                                        *
 *               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.  *
 *                                                                        *
 **************************************************************************/

/*
 *  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"

typedef struct rdram {
    unsigned char *main_addr;
    unsigned char *hidden_addr;
    unsigned long main_size;
    unsigned 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 = "\0";	
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;

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

    sprintf(FileName, "OutData/%s_%d.cvg", outputFilename, ncapture);
    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;
	}
	
    /* Hack to have fwrite work  XXX change this & filter.c to use a structure */
    F = Format;
    S = Size;
    W = Width;
    H = Height;
    fwrite(&F, 1, 1, CVGFile);
    fwrite(&S, 1, 1, CVGFile);
    fwrite(&W, 2, 1, CVGFile);
    fwrite(&H, 2, 1, CVGFile);    

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

    fclose(CVGFile);
}


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);
    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);
}

captureimage16(unsigned char *base, long xsize, long ysize)
{
    IMAGE		*img, *cvgimg;
    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);
    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;
    }
    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);
}

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

    sprintf(filename, "OutData/%s_%d.z", outputFilename, ncapture);
    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 */
    for ( i = 0; i < ysize; i++) {
	p = Mainbase + ((ysize - i - 1) * xsize * 2);
	h = Hiddenbase + (((ysize - i - 1) * xsize * 2)/HIDDEN_FACTOR);
	h_shift = 0;
	for (j = 0; j < xsize; j++) {
	    c1 = *p++;
	    c2 = *p++;
	    h1 = (*h >> h_shift) & 0x3;
	    if ((h_shift += 2) > 6) {
		h_shift=0;
		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);
	
    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++;
}



main(int argc, char **argv)
{
    unsigned long	imagebase;
    unsigned long	zbase;

    if (argc < 4) {
	fprintf(stderr,"usage: %s %s.\n",argv[0],UsageString);
	exit(-1);
    }

    imagebase = strtol(argv[2], (char **)NULL, 16);
    zbase = strtol(argv[3], (char **)NULL, 16);

    if (rdram_mmap_init(argv[1]) == 0)
        return;

    ram.main_addr = mainaddr;
    ram.hidden_addr = hiddenaddr;
    ram.main_size = MAIN_MEMORY_SIZE;
    ram.hidden_size = HIDDEN_MEMORY_SIZE;
    strcpy(outputFilename, argv[1]);

    captureimage(imagebase, G_IM_SIZ_16b, G_IM_FMT_RGBA, zbase);
}