getimages.c 2.93 KB
/*
 * getimages.c: 
 *
 * opens the debug port to the rdpvector app, then reads image data provided 
 * by the app (which has a command line flag to "dump image data".
 *
 * We assume that users will have started up rdpvector and given it the "dump
 * image" command line flag, so that it will jive with this application.
 */
#include <stdio.h>
#include <errno.h>
#include <PRimage.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);

#define DUMP_REQUEST	0xdeadbeef
#define DUMP_REPLY	0xbaaddeed

main(int argc, char **argv)     /* Host side code */
{
    int		fd, status;
    unsigned int hostBuffer[2];
    unsigned char *imageBuf;
    unsigned char *p;
    unsigned short *rbuf, *gbuf, *bbuf;
    IMAGE		*img;
    int		xsize = 320;
    int		ysize = 240;
    int i, j;
	
    /*
     * XXX add support for 16 bit framebuffers
     * XXX add support for arbitrary x, y size framebuffers
     */
    if (argc != 2) {
	fprintf(stderr, "usage: %s <.rgb filename>\n", argv[0]);
	exit(1);
    }
    if ((fd = uhOpenGame("/dev/u64")) == -1) {
	perror("uhOpenGame");
    	exit(1);
    }

    hostBuffer[0] = DUMP_REQUEST;

    if (uhWriteGame(fd, hostBuffer, 4) == -1) {
	fprintf(stderr, "uhWriteGame %s\n", sys_errlist[errno]);
	exit(1);
    }

    /*
     * Read the reply, blocking if necessary.
     */
    if (uhReadGame(fd, hostBuffer, 8) == -1) {
	fprintf(stderr, "uhReadGame %s\n", sys_errlist[errno]);
	exit(1);
    }

    if (hostBuffer[0] != DUMP_REPLY) {
	fprintf(stderr, "unexpected response from game: 0x%x\n", hostBuffer[0]);
	exit(1);
    }

    imageBuf = (unsigned char *)malloc(hostBuffer[1]);
    if (imageBuf == NULL) {
	fprintf(stderr, "%s: unable to malloc %d bytes.\n",
	    argv[0], hostBuffer[1]);
    }

    uhReadGame(fd, imageBuf, hostBuffer[1]);

    img = iopen(argv[1], "w", RLE(1), 3, xsize, ysize, 3);
    if (img == (IMAGE *)0) {
	fprintf(stderr, "%s: couldn't create .rgb file\n", argv[0]);
	exit(1);
    }
    
    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 = imageBuf + ((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);

    if (uhCloseGame(fd) != 0) {
	perror("uhCloseGame");
	exit(1);
    }
    exit(0);
}