-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpsrip_core.cpp
345 lines (283 loc) · 7.02 KB
/
psrip_core.cpp
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
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#ifndef WIN32
#include <sys/wait.h>
#endif
#include "imagesource.h"
#include "util.h"
#include "thread.h"
#include "threadutil.h"
#include "psrip_core.h"
using namespace std;
// Slight variation on the usual TempFile - because GhostScript uses a format string for output filenames
// the TempFiles don't have freedom to pick their own names.
class PSRip_TempFile : public TempFile
{
public:
PSRip_TempFile(TempFileTracker &header,const char *fname) : TempFile(&header)
{
filename=strdup(fname);
}
virtual ~PSRip_TempFile()
{
}
virtual const char *Filename()
{
return(filename);
}
protected:
};
// Subthread to RIP the file using GS.
// Uses fork()/exec() to make the process cancellable.
#define GS_ARGC 9
class Thread_PSRipProcess : public ThreadFunction, public Thread
{
public:
Thread_PSRipProcess(PSRip &rip,const char *filename,PSRipOptions &opt)
: ThreadFunction(), Thread(this), rip(rip), opt(opt), filename(filename)
{
Start();
WaitSync();
}
virtual ~Thread_PSRipProcess()
{
}
virtual int Entry(Thread &t)
{
SendSync();
Debug[TRACE] << "PSRipProcess: Sent sync to main thread" << endl;
opt.RunProgram(filename);
return(0);
}
virtual void Stop()
{
opt.StopProgram();
Thread::Stop();
}
protected:
PSRip &rip;
PSRipOptions &opt;
std::string filename;
};
// SubThread to hang around waiting for files to be rendered by GS.
// FIXME - use notification rather than polling.
class Thread_PSRipFileMonitor : public ThreadFunction, public Thread
{
public:
Thread_PSRipFileMonitor(PSRip &rip) : ThreadFunction(), Thread(this), rip(rip)
{
Start();
WaitSync();
}
virtual ~Thread_PSRipFileMonitor()
{
}
virtual int Entry(Thread &t)
{
SendSync();
try
{
Debug[TRACE] << "*** Monitor thread active" << std::endl;
// scan the output files and add them to the TempFileTracker
int page=1;
char *rfn=NULL;
while(!rip.ripthread->TestFinished())
{
// cerr << "Thread not yet finished - waiting for page " << page+1 << endl;
if(rip.TestPage(page+1))
{
Debug[TRACE] << "File monitor thread found page " << page+1 << std::endl;
if((rfn=rip.GetRippedFilename(page)))
{
Debug[TRACE] << "So now safe to add: " << rfn << std::endl;
new PSRip_TempFile(rip,rfn);
++page;
free(rfn);
cerr << "Triggering event" << endl;
rip.Event.Trigger();
}
}
else
{
// Debug[TRACE] << "File not found, so sleeping..." << std::endl;
#ifdef WIN32
Sleep(100);
#else
usleep(100000);
#endif
// Debug[TRACE] << "Woken from sleep - trying again..." << std::endl;
}
}
while(rip.TestPage(page))
{
rfn=rip.GetRippedFilename(page);
cerr << "Thread finished -- adding file: " << rfn << endl;
new PSRip_TempFile(rip,rfn);
++page;
free(rfn);
}
// FIXME - need to consider concurrency here...
}
catch(const char *err)
{
Debug[ERROR] << "Error: " << err << endl;
}
rip.Event.Trigger();
return(0);
}
protected:
PSRip &rip;
};
PSRip::PSRip()
: TempFileTracker(), ThreadEventHandler(), Event(*this,"FileReady"),
tempname(), ripthread(NULL), monitorthread(NULL)
{
}
void PSRip::Rip(const char *filename,PSRipOptions &opts)
{
if(monitorthread)
delete monitorthread;
monitorthread=NULL;
if(ripthread)
delete ripthread;
ripthread=NULL;
// Create temporary output filename
tempname=BuildFilename(filename,"","");
ripthread=new Thread_PSRipProcess(*this,filename,opts);
// Disable monitorthread until we can determine whether it's causing Ghostscript locks.
// Don't think it is - think the problem was actually with having the *input* file open from
// another thread, so re-enabling for now...
monitorthread=new Thread_PSRipFileMonitor(*this);
}
void PSRip::Stop()
{
if(ripthread)
ripthread->Stop();
}
bool PSRip::TestFinished()
{
int result=true;
if(monitorthread)
result&=monitorthread->TestFinished();
if(ripthread)
result&=ripthread->TestFinished();
return(result);
}
void PSRip::WaitFinished()
{
if(monitorthread)
monitorthread->WaitFinished();
if(ripthread)
ripthread->WaitFinished();
}
PSRip::~PSRip()
{
if(monitorthread)
delete monitorthread;
if(ripthread)
delete ripthread;
cerr << "Disposing of tempname" << endl;
if(tempname)
free(tempname);
}
char *PSRip::GetRippedFilename(int page,const char *basename)
{
if(!basename)
basename=tempname;
if(!basename)
throw "PSRip: Don't have a tempname yet!";
char *buf=(char *)malloc(strlen(basename)+10);
snprintf(buf,strlen(basename)+10,"%s_%03d.tif",basename,page);
return(buf);
}
bool PSRip::TestPage(int page)
{
char *fn=GetRippedFilename(page);
bool result=CheckFileExists(fn);
free(fn);
return(result);
}
// PSRipOptions
PSRipOptions::PSRipOptions(IS_TYPE type,int resolution,int firstpage,int lastpage,bool textantialias,bool gfxantialias)
: ExternalGhostScript(), type(type),resolution(resolution),firstpage(firstpage),lastpage(lastpage),
textantialias(textantialias),gfxantialias(gfxantialias)
{
}
PSRipOptions::~PSRipOptions()
{
}
// Build argv array ready for execv call. Returns a pointer to the argv array.
void PSRipOptions::RunProgram(std::string &filename)
{
// Build a GhostScript command according to the following format:
// %s -sDEVICE=%s -sOutputFile=%s_%%d.tif -r%dx%d %s -dBATCH -dNOPAUSE %s
ClearArgs();
// Select an appropriate GS device for output...
switch(STRIP_ALPHA(type))
{
case IS_TYPE_BW:
AddArg("-sDEVICE=tifflzw");
break;
case IS_TYPE_GREY:
AddArg("-sDEVICE=tiffgray");
break;
case IS_TYPE_RGB:
AddArg("-sDEVICE=tiff24nc");
break;
case IS_TYPE_CMYK:
AddArg("-sDEVICE=tiff32nc");
break;
case IS_TYPE_DEVICEN:
// FIXME: add support for DeviceN using the tiffsep device.
default:
throw "PSRip: type not yet supported";
break;
}
// Enable interpolation
AddArg("-dDOINTERPOLATE");
// Create anti-aliasing parameters
if(textantialias)
AddArg("-dTextAlphaBits=4");
if(gfxantialias)
AddArg("-dGraphicsAlphaBits=4");
// Create FirstPage and LastPage paramters, if needed...
if(firstpage)
{
std::stringstream tmp;
tmp << "-dFirstPage=" << firstpage;
AddArg(tmp.str());
}
if(lastpage)
{
std::stringstream tmp;
tmp << "-dLastPage=" << firstpage;
AddArg(tmp.str());
}
// FIXME - need to escape this for the shell!
// Build output name;
Debug[TRACE] << "Building filename from " << filename.c_str() << ", " << "_%03d" << " and " << "tif" << endl;
char *tmp=BuildFilename(filename.c_str(),"_%03d","tif");
char *tmp2=BuildFilename("-sOutputFile=",tmp,"");
Debug[TRACE] << "Got " << tmp2 << " by way of " << tmp << endl;
AddArg(tmp2);
free(tmp2);
free(tmp);
// Build resolution;
std::stringstream tmpss;
tmpss << "-r" << resolution << "x" << resolution << endl;
AddArg(tmpss.str());
AddArg("-dBATCH");
AddArg("-dNOPAUSE");
AddArg(filename);
ExternalGhostScript::RunProgram();
}
// Save the options into a ConfigDB.
void PSRipOptions::ToDB(ConfigDB &db)
{
}
// Retrieve the options from a ConfigDB
void PSRipOptions::FromDB(ConfigDB &db)
{
}