Help in using gsapi_set_stdio() to capture stdout into a memory buffer

"Joaquim Luis" <[email protected]>
Newsgroups gmane.comp.printing.ghostscript.devel
Organization UALG
Message-ID <op.yed8dfm6dbaoe8@macmarilu>
Hi,

I am trying to learn how to capture the raster conversion of a PS file  
into memory but it's not being easy.
The following example (adapted from one posted here before) correctly  
prints in stdout a png version of the small PS embedded in code. I know  
it's correct because I save it into a file I see it is. But my goal is to  
save it in a memory buffer so, as an intermediate step, I wanted to save  
it in file via gsapi_set_stdio.

If I compile it as is than I get this warning that I don't understand

cl gstest.c -Ighostpdl\psi -Ighostpdl\base ghostpdl\debugbin\gsdll64.lib  
/nologo
gstest.c
gstest.c(102) : warning C4047: 'function' : 'int (__cdecl *)(void *,const  
char *,int)' differs in levels of indirection from 'int'
gstest.c(102) : warning C4024: 'gsapi_set_stdio' : different types for  
formal and actual parameter 3

but It saves the PS test string in the "CRAP.txt" file. But it's not this  
that I want. What I want is to capture what is actually printed to stdout  
(because I choose to do "-sOutputFile=-") and have no idea on how to do it.
If gsapi_set_stdio() allows redirecting stdout, why is the result still  
printed to stdout?

Thanks for any help

Joaquim

_______________________________________________
gs-devel mailing list
[email protected]
http://ghostscript.com/cgi-bin/mailman/listinfo/gs-devel
gstest.c (application/octet-stream, 2.6 KB)
#include <stdio.h>
#include <stdlib.h>
#include "ierrors.h"
#include "iapi.h"
FILE *fp;

char *ps = "newpath\n"
"100 600 moveto 200 650 lineto 100 700 lineto\n"
"closepath\n"
"gsave\n"
"0.5 setgray\n"
"fill\n"
"grestore\n"
"4 setlinewidth\n"
"stroke\n"
"/Times-Roman findfont\n"
"18 scalefont\n"
"setfont\n"
"newpath\n"
"150 600 moveto\n"
"(Julia-Ghostscript in action) show\n"
"/csquare {\n"
   "newpath\n"
   "0 26 moveto\n"
   "0 1 rlineto\n"
   "1 0 rlineto\n"
   "0 -1 rlineto\n"
   "closepath\n"
   "setrgbcolor\n"
   "fill\n"
"} def\n"
"\n"
"20 20 scale\n"
"\n"
"1 5 translate\n"
"1 0 0 csquare\n"
"\n"
"1 0 translate\n"
"0 1 0 csquare\n"
"\n"
"1 0 translate\n"
"0 0 1 csquare\n"
"showpage";

/* stdio functions */
static int GSDLLCALL gsdll_stdin(void *instance, char *buf, int len) {
    int ch, count = 0;
fprintf(stderr, "MERDA in\n");
    while (count < len) {
        ch = fgetc(stdin);
        if (ch == EOF)
            return 0;
        *buf++ = ch;
        count++;
        if (ch == '\n')
            break;
    }
    return count;
}

static int GSDLLCALL gsdll_stdout(void *instance, const char *str, int len) {
fprintf(stderr, "MERDA out\n");
    fwrite(str, 1, len, fp);
    fflush(fp);
    return len;
}

static int GSDLLCALL gsdll_stderr(void *instance, const char *str, int len) {
fprintf(stderr, "MERDA err\n");
    fwrite(str, 1, len, stderr);
    fflush(stderr);
    return len;
}

void *GSinst; 
int main(int argc, char *argv[]) {
    int GScode=0;
    int GSexitCode;

    const char sPSprogram[] = "example.ps";

#define NoOfGSargs	6 
    char *GSargs[] = {
		argv[0], "-sDEVICE=pngalpha", "-dBATCH", "-dNOPAUSE", "-sOutputFile=-", "-q" 
    };
	fp = fopen ("CRAP.txt", "w");


    if ((GScode = gsapi_new_instance( &GSinst, NULL )) < 0 ) {
		fprintf( stderr, "Ghostscript gsapi_new_instance fail (%d)\n", GScode);
        exit(0);
    }
	fprintf(stderr, "Ghostscript gsapi_new_instance OK \n");

    gsapi_set_stdio(GSinst, gsdll_stdin, gsdll_stdout(GSinst, ps, strlen(ps)), gsdll_stderr);

    if ((GScode = gsapi_init_with_args(GSinst, NoOfGSargs, GSargs)) < 0 ) {
        fprintf( stderr, "Ghostscript gsapi_init_with_args fail (%d)\n", GScode);
        exit(0);
    }
	fprintf (stderr, "Ghostscript gsapi_init_with_args OK \n");

    //if (gsapi_run_file(GSinst, argv[1], 0, &GSexitCode) < 0 ) {
    if (gsapi_run_string(GSinst, ps, 0, &GSexitCode) < 0 ) {
    	fprintf (stderr, "Error: Could not load %s\n", sPSprogram );
    }

	fprintf (stderr, "Ghostscript gsapi_run_file OK\n");
	fclose(fp);
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.