summaryrefslogtreecommitdiff
path: root/contrib/pscolor/test.c
blob: 5a2ee7faa07cb46a0cbe50124c16d752aed9bfa2 (plain)
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
/* using GS DLL as a ps colorpage separator.
        Carsten Hammer Océ-Deutschland GmbH
        Hammer.Carsten@oce.de			*/

#ifdef _Windows
/* Compile with:
 * cl -D_Windows -Isrc -Febin\pscolor.exe test.c lex.yy.c bin\gsdll32.lib
 */
#include <windows.h>
#define GSDLLEXPORT __declspec(dllimport)
#endif

#include <stdio.h>
#include <malloc.h>

/* prevent gp.h redefining fopen */
#define fopen fopen

#include "stdpre.h"
#include "iapi.h"    /* Ghostscript interpreter public interface */
#include "gdevdsp.h"
#include "string_.h"
#include "ierrors.h"
#include "gscdefs.h"
#include "gstypes.h"
#include "iref.h"
#include "iminst.h"
#include "imain.h"
#include "gp.h"

#include "gsdll.h"   /* old DLL public interface */

unsigned char *myimage;
int breite,hoehe,seite=0,myraster;
FILE *color_fd,*black_fd,*temp_fd,*choose;
FILE *read_fd;

//#define DISPLAY_DEBUG

int yylex(char *buf,int *mylen);

/* stdio functions */
static int GSDLLCALL
gsdll_stdin(void *instance, char *buf, int len)
{
    int c;
    int count = 0;
    char *init=buf;
    int eof;
    int hlen=len;
    eof=yylex(buf,&hlen);
//    fprintf(stderr, "eof %d\n",eof);
    if(eof==1001){
    fprintf(color_fd,"%s",buf);
    fflush(color_fd);
    fprintf(black_fd,"%s",buf);
    fflush(black_fd);
    return (strlen(buf));
    } else if(eof==1000){
    fprintf(temp_fd,"%s",buf);
        fflush(temp_fd);
        fclose(temp_fd);
        read_fd=fopen("temp.ps","rb");
//    fprintf(stderr, "Seitenende %d %d %s\n",hlen,strlen(buf),buf);
        while( (c=fgetc(read_fd)) != EOF)
        {
                fputc(c,choose);
        }
 //   fprintf(stderr, "Seitenende ende %d %d %s\n",hlen,strlen(buf),buf);
        fflush(choose);
        fclose(read_fd);
        temp_fd=fopen("temp.ps","wb");
    return (strlen(buf));
    } else if(eof!=0){
//    fprintf(stderr, "Mittendrin %s\n",buf);
    fprintf(temp_fd,"%s",buf);
    fflush(temp_fd);
    return (strlen(buf));
    } else {
    fprintf(stderr, "Dateiende\n");
        read_fd=fopen("temp.ps","rb");
        while( (c=fgetc(read_fd)) != EOF)
        {
                fputc(c,choose);
        }
        fflush(choose);
        fclose(read_fd);
        fclose(temp_fd);
        temp_fd=fopen("temp.ps","wb");
    return 0;
        }
}
#if 0
static int GSDLLCALL
gsdll_stdin(void *instance, char *buf, int len)
{
    int ch;
    int count = 0;
        size_t back;
    char *init=buf;
    while (count < len) {
        ch = fgetc(stdin);
        if (ch == EOF){
#ifdef DISPLAY_DEBUG
    fprintf(stderr, "lese %s\n",buf);
#endif
        fwrite(init,1,count,temp_fd);
        fflush(temp_fd);
            return 0;
        }
        *buf++ = ch;
        count++;
        if (ch == '\n')
            break;
    }
#ifdef DISPLAY_DEBUG
    fprintf(stderr, "lese %s\n",buf);
#endif
        fwrite(init,1,count,temp_fd);
        fflush(temp_fd);
    return count;
}
#endif

static int GSDLLCALL
gsdll_stdout(void *instance, const char *str, int len)
{
    fwrite(str, 1, len, stdout);
    fflush(stdout);
    return len;
}

static int GSDLLCALL
gsdll_stderr(void *instance, const char *str, int len)
{
    fwrite(str, 1, len, stderr);
    fflush(stderr);
    return len;
}

/* New device has been opened */
/* This is the first event from this device. */
static int display_open(void *handle, void *device)
{
#ifdef DISPLAY_DEBUG
    char buf[256];
    sprintf(buf, "display_open(0x%x, 0x%x)\n", handle, device);
    fprintf(stderr, buf);
#endif
    return 0;
}

/* Device is about to be closed. */
/* Device will not be closed until this function returns. */
static int display_preclose(void *handle, void *device)
{
#ifdef DISPLAY_DEBUG
    char buf[256];
    sprintf(buf, "display_preclose(0x%x, 0x%x)\n", handle, device);
    fprintf(stderr, buf);
#endif
    /* do nothing - no thread synchonisation needed */
    return 0;
}

/* Device has been closed. */
/* This is the last event from this device. */
static int display_close(void *handle, void *device)
{
#ifdef DISPLAY_DEBUG
    char buf[256];
    sprintf(buf, "display_close(0x%x, 0x%x)\n", handle, device);
    fprintf(stderr, buf);
#endif
    return 0;
}

/* Device is about to be resized. */
/* Resize will only occur if this function returns 0. */
static int display_presize(void *handle, void *device, int width, int height,
        int raster, unsigned int format)
{
#ifdef DISPLAY_DEBUG
    char buf[256];
    sprintf(buf, "display_presize(0x%x, 0x%x, width=%d height=%d raster=%d\n\
  format=%d)\n",
       handle, device, width, height, raster, format);
    fprintf(stderr, buf);
#endif
    return 0;
}

/* Device has been resized. */
/* New pointer to raster returned in pimage */
static int display_size(void *handle, void *device, int width, int height,
        int raster, unsigned int format, unsigned char *pimage)
{

#ifdef DISPLAY_DEBUG
    char buf[256];
    sprintf(buf, "display_size(0x%x, 0x%x, width=%d height=%d raster=%d\n\
  format=%d image=0x%x)\n",
       handle, device, width, height, raster, format, pimage);
    fprintf(stderr, buf);
#endif
        myimage=pimage;
        breite=width;
        hoehe=height;
        myraster=raster;
           return 0;
}

/* flushpage */
static int display_sync(void *handle, void *device)
{

#ifdef DISPLAY_DEBUG
    char buf[256];
    sprintf(buf, "display_sync(0x%x, 0x%x)\n", handle, device);
    fprintf(stderr, buf);
#endif

    return 0;
}

/* showpage */
/* If you want to pause on showpage, then don't return immediately */
static int display_page(void *handle, void *device, int copies, int flush)
{
        int i,t,color=0;
#ifdef DISPLAY_DEBUG
    char buf[256];
    sprintf(buf, "display_page(0x%x, 0x%x, copies=%d flush=%d)\n",
        handle, device, copies, flush);
    fprintf(stderr, buf);
#endif
        seite++;
        for(i=hoehe-1;i>=0;i=i-1){
                for(t=breite-1;t>=0;t=t-1){
                        if((myimage[i*myraster+t*3+0]!=myimage[i*myraster+t*3+1])||(myimage[i*myraster+t*3+1]!=myimage[i*myraster+t*3+2])){
                                color=1;
                                fprintf(stderr,"Ungleich %d,%d %x,%x,%x\n",i,t,myimage[i*myraster+t*3+0],myimage[i*myraster+t*3+1],myimage[i*myraster+t*3+2]);
                                goto out;
                        }
                }
        }
out:
        if(color){
        fprintf(stderr,"[%d]Color\n",seite);
        choose=color_fd;
        } else {
        fprintf(stderr,"[%d]Grey\n",seite);
        choose=black_fd;
        }
/*
        read_fd=fopen("temp.ps","rb");
        while( (c=fgetc(read_fd)) != EOF)
        {
                fputc(c,choose);
        }
        fflush(choose);
        fclose(read_fd);
        fclose(temp_fd);
        temp_fd=fopen("temp.ps","wb");
*/
    return 0;
}

/* Poll the caller for cooperative multitasking. */
/* If this function is NULL, polling is not needed */
static int display_update(void *handle, void *device,
    int x, int y, int w, int h)
{
    return 0;
}

display_callback display = {
    sizeof(display_callback),
    DISPLAY_VERSION_MAJOR,
    DISPLAY_VERSION_MINOR,
    display_open,
    display_preclose,
    display_close,
    display_presize,
    display_size,
    display_sync,
    display_page,
    display_update,
    NULL,	/* memalloc */
    NULL	/* memfree */
};

static gs_main_instance *minst;
const char start_string[] = "systemdict /start get exec\n";

int main(int argc, char *argv[])
{
    int code;
    const char * gsargv[12];
    char arg[64];
    int gsargc;
    gsargv[0] = "ps2colordetect";	/* actual value doesn't matter */
    gsargv[1] = "-dNOPAUSE";
    gsargv[2] = "-dBATCH";
    gsargv[3] = "-dSAFER";
//    gsargv[4] = "-sDEVICE=pdfwrite";
    gsargv[4] = "-sDEVICE=display";
    gsargv[5] = "-dDisplayHandle=0";
    sprintf(arg,"-dDisplayFormat=%d",DISPLAY_COLORS_RGB|DISPLAY_ALPHA_NONE|DISPLAY_DEPTH_8|DISPLAY_LITTLEENDIAN|DISPLAY_BOTTOMFIRST);
    gsargv[6] = arg;
    gsargv[7] = "-sOutputFile=out.pdf";
    gsargv[8] = "-c";
    gsargv[9] = ".setpdfwrite";
    gsargv[10] = "-f";
//    gsargv[11] = "input.ps";
    gsargv[11] = "-";
    gsargc=12;

    code = gsapi_new_instance(&minst, NULL);
    if (code < 0)
        return 1;
    gsapi_set_stdio(minst, gsdll_stdin, gsdll_stdout, gsdll_stderr);
    gsapi_set_display_callback(minst, &display);

        color_fd=fopen("color.ps","wb");
        black_fd=fopen("black.ps","wb");
        temp_fd=fopen("temp.ps","wb");
        choose=black_fd;

    code = gsapi_init_with_args(minst, gsargc, gsargv);
    gsapi_exit(minst);

    gsapi_delete_instance(minst);

        fclose(color_fd);
        fclose(black_fd);
        fclose(temp_fd);

    if ((code == 0) || (code == gs_error_Quit))
        return 0;
    return 1;
}