// ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//
void GfxTexture::SaveTGA( const char* fname )
{
	FILE *file_TGA = fopen( fname, "wb");

	if (file_TGA != NULL)
		{
		int nSize = Width * Height * (IsRGBA ? 4 : 1);
		void *buf_image = malloc( nSize );
		
		if (buf_image != NULL)
			{
			glBindFramebuffer(GL_FRAMEBUFFER,FramebufferId);
			check_gl();
			glReadPixels(0,0,Width,Height,IsRGBA ? GL_RGBA : GL_LUMINANCE, GL_UNSIGNED_BYTE, buf_image);
			check_gl();
			glBindFramebuffer(GL_FRAMEBUFFER,0);

			raspitexutil_brga_to_rgba( (uint8_t*) buf_image, nSize );
			write_tga( file_TGA, Width, Height, (uint8_t*) buf_image, nSize);

			free(buf_image);
			}
		
		fclose( file_TGA );
		}
}
Exemple #2
0
/**
 * Writes the next GL frame-buffer to a RAW .ppm formatted file
 * using the specified file-handle.
 * @param state Pointer to the GL preview state.
 * @param outpt_file Output file handle for the ppm image.
 * @return Zero on success.
 */
int raspitex_capture(RASPITEX_STATE *state, FILE *output_file)
{
    int rc = 0;
    uint8_t *buffer = NULL;
    size_t size = 0;

    vcos_log_trace("%s: state %p file %p", VCOS_FUNCTION,
                   state, output_file);

    if (state && output_file)
    {
        /* Only request one capture at a time */
        vcos_semaphore_wait(&state->capture.start_sem);
        state->capture.request = 1;

        /* Wait for capture to start */
        vcos_semaphore_wait(&state->capture.completed_sem);

        /* Take ownership of the captured buffer */
        buffer = state->capture.buffer;
        size = state->capture.size;

        state->capture.request = 0;
        state->capture.buffer = 0;
        state->capture.size = 0;

        /* Allow another capture to be requested */
        vcos_semaphore_post(&state->capture.start_sem);
    }
    if (size == 0 || ! buffer)
    {
        vcos_log_error("%s: capture failed", VCOS_FUNCTION);
        rc = -1;
        goto end;
    }

    raspitexutil_brga_to_rgba(buffer, size);
    rc = write_tga(output_file, state->width, state->height, buffer, size);
    fflush(output_file);

end:
    free(buffer);
    return rc;
}
Exemple #3
0
extern int main(int argc, char * argv[])
{
    char const * outdir=NULL;
    char const * bnifile=NULL;
    FILE *       fbni;
    struct stat  s;
    int          a;
    int          forcefile=0;
    char         dash[]="-"; /* unique address used as flag */
    
    if (argc<1 || !argv || !argv[0])
    {
	fprintf(stderr,"bad arguments\n");
	return STATUS_FAILURE;
    }
    
    for (a=1; a<argc; a++)
        if (forcefile && !bnifile)
            bnifile = argv[a];
        else if (strcmp(argv[a],"-")==0 && !bnifile)
            bnifile = dash;
        else if (argv[a][0]!='-' && !bnifile)
            bnifile = argv[a];
        else if (forcefile && !outdir)
            outdir = argv[a];
        else if (strcmp(argv[a],"-")==0 && !outdir)
            outdir = dash;
        else if (argv[a][0]!='-' && !outdir)
            outdir = argv[a];
        else if (forcefile || argv[a][0]!='-' || strcmp(argv[a],"-")==0)
        {
            fprintf(stderr,"%s: extra file argument \"%s\"\n",argv[0],argv[a]);
            usage(argv[0]);
        }
        else if (strcmp(argv[a],"--")==0)
            forcefile = 1;
        else if (strcmp(argv[a],"-v")==0 || strcmp(argv[a],"--version")==0)
        {
            printf("version "BNETD_VERSION"\n");
            return STATUS_SUCCESS;
        }
        else if (strcmp(argv[a],"-h")==0 || strcmp(argv[a],"--help")==0 || strcmp(argv[a],"--usage")
==0)
            usage(argv[0]);
        else
        {
            fprintf(stderr,"%s: unknown option \"%s\"\n",argv[0],argv[a]);
            usage(argv[0]);
        }
    
    if (!bnifile)
    {
	fprintf(stderr,"%s: BNI file not specified\n",argv[0]);
	usage(argv[0]);
    }
    if (!outdir)
    {
	fprintf(stderr,"%s: output directory not specified\n",argv[0]);
	usage(argv[0]);
    }
    
    if (bnifile==dash)
        fbni = stdin;
    else
	if (!(fbni = fopen(bnifile,"r")))
	{
	    fprintf(stderr,"%s: could not open BNI file \"%s\" for reading (fopen: %s)\n",argv[0],bnifile,strerror(errno));
	    return STATUS_FAILURE;
	}
    
    if (outdir==dash)
    {
	fprintf(stderr,"%s: can not write directory to <stdout>\n",argv[0]);
	if (bnifile!=dash && fclose(fbni)<0)
	    fprintf(stderr,"%s: could not close BNI file \"%s\" after reading (fclose: %s)\n",argv[0],bnifile,strerror(errno));
	return STATUS_FAILURE;
    }
    if (stat(outdir,&s)<0) {
	if (errno == ENOENT) {
	    fprintf(stderr,"Info: Creating directory \"%s\" ...\n",outdir);
	    if (p_mkdir(outdir,S_IRWXU+S_IRWXG+S_IRWXO)<0) {
		fprintf(stderr,"%s: could not create output directory \"%s\" (mkdir: %s)",argv[0],outdir,strerror(errno));
		if (bnifile!=dash && fclose(fbni)<0)
		    fprintf(stderr,"%s: could not close BNI file \"%s\" after reading (fclose: %s)\n",argv[0],bnifile,strerror(errno));
		return STATUS_FAILURE;
	    }
	} else {
	    fprintf(stderr,"%s: could not stat output directory \"%s\" (stat: %s)\n",argv[0],outdir,strerror(errno));
	    if (bnifile!=dash && fclose(fbni)<0)
		fprintf(stderr,"%s: could not close BNI file \"%s\" after reading (fclose: %s)\n",argv[0],bnifile,strerror(errno));
	    return STATUS_FAILURE;
	}
    }
    else
	if (S_ISDIR(s.st_mode) == 0) {
	    fprintf(stderr,"%s: \"%s\" is not a directory\n",argv[0],outdir);
	    if (bnifile!=dash && fclose(fbni)<0)
		fprintf(stderr,"%s: could not close BNI file \"%s\" after reading (fclose: %s)\n",argv[0],bnifile,strerror(errno));
	    return STATUS_FAILURE;
	}
    
    {
	unsigned int i;
	int          curry;
	t_tgaimg *   iconimg;
	t_bnifile *  bni;
	FILE *       indexfile;
	char *       indexfilename;
	
	fprintf(stderr,"Info: Loading \"%s\" ...\n",bnifile);
	bni = load_bni(fbni);
	if (bni == NULL) return STATUS_FAILURE;
	if (fseek(fbni,bni->dataoffset,SEEK_SET)<0) {
		fprintf(stderr,"%s: could not seek to TGA data offset %lu (fseek: %s)\n",argv[0],(unsigned long int)bni->dataoffset,strerror(errno));
		if (bnifile!=dash && fclose(fbni)<0)
			fprintf(stderr,"%s: could not close BNI file \"%s\" after reading (fclose: %s)\n",argv[0],bnifile,strerror(errno));
		return STATUS_FAILURE;
	}
	fprintf(stderr,"Info: Loading image ...\n");
	iconimg = load_tga(fbni);
	if (iconimg == NULL) return STATUS_FAILURE;
	
	fprintf(stderr,"Info: Extracting icons ...\n");
	indexfilename = malloc(strlen(outdir)+14);
	sprintf(indexfilename,"%s/bniindex.lst",outdir);
	fprintf(stderr,"Info: Writing Index to \"%s\" ... \n",indexfilename);
	indexfile = fopen(indexfilename , "w");
	if (indexfile == NULL) {
		fprintf(stderr,"%s: could not open index file \"%s\" for writing (fopen: %s)\n",argv[0],indexfilename,strerror(errno));
		if (bnifile!=dash && fclose(fbni)<0)
			fprintf(stderr,"%s: could not close BNI file \"%s\" after reading (fclose: %s)\n",argv[0],bnifile,strerror(errno));
		return STATUS_FAILURE;
	}
	fprintf(indexfile,"unknown1 %08x\n",bni->unknown1);
	fprintf(indexfile,"unknown2 %08x\n",bni->unknown2);
	curry = 0;
	for (i=0; i < bni->numicons; i++) {
		FILE *dsttga;
		char *name;
		t_tgaimg *icn;
		icn = area2img(iconimg,0,curry,bni->icons->icon[i].x,bni->icons->icon[i].y,tgaimgtype_uncompressed_truecolor);
		if (icn == NULL) {
			fprintf(stderr,"Error: area2img failed!\n");
			return STATUS_FAILURE;
		}
		if (bni->icons->icon[i].id == 0) {
			int tag = bni->icons->icon[i].tag;
			name = malloc(strlen(outdir)+10);
			sprintf(name,"%s/%c%c%c%c.tga",outdir,((tag >> 24) & 0xff),((tag >> 16) & 0xff),((tag >> 8) & 0xff),((tag >> 0) & 0xff));
		} else {
			name = malloc(strlen(outdir)+16);
			sprintf(name,"%s/%08x.tga",outdir,bni->icons->icon[i].id);
		}
		fprintf(stderr,"Info: Writing icon %u(%ux%u) to file \"%s\" ... \n",i+1,icn->width,icn->height,name);
		curry += icn->height;
		dsttga = fopen(name,"w");
		if (dsttga == NULL) {
			fprintf(stderr,"%s: could not open ouptut TGA file \"%s\" for writing (fopen: %s)\n",argv[0],name,strerror(errno));
		} else {
			if (write_tga(dsttga,icn) < 0) {
				fprintf(stderr,"Error: Writing to TGA failed.\n");
			} else {
				int tag = bni->icons->icon[i].tag;
				if (bni->icons->icon[i].id == 0) {
					fprintf(indexfile,"icon !%c%c%c%c %d %d %08x\n",((tag >> 24) & 0xff),((tag >> 16) & 0xff),((tag >> 8) & 0xff),((tag >> 0) & 0xff),bni->icons->icon[i].x,bni->icons->icon[i].y,bni->icons->icon[i].unknown);
				} else {
					fprintf(indexfile,"icon #%08x %d %d %08x\n",bni->icons->icon[i].id,bni->icons->icon[i].x,bni->icons->icon[i].y,bni->icons->icon[i].unknown);
				}
			}