8 #include <tdeapplication.h>
9 #include <tdetempfile.h>
15 #define BBOX "%%BoundingBox:"
16 #define BBOX_LEN strlen(BBOX)
18 static bool seekToCodeStart( TQIODevice * io, TQ_UINT32 & ps_offset, TQ_UINT32 & ps_size )
24 if ( io->readBlock(buf, 2)!=2 )
26 kdError(399) <<
"kimgio EPS: EPS file has less than 2 bytes." << endl;
30 if ( buf[0]==
'%' && buf[1]==
'!' )
32 kdDebug(399) <<
"kimgio EPS: normal EPS file" << endl;
34 else if ( buf[0]==
char(0xc5) && buf[1]==
char(0xd0) )
36 if ( io->readBlock(buf+2, 2)!=2 )
38 kdError(399) <<
"kimgio EPS: potential MS-DOS EPS file has less than 4 bytes." << endl;
41 if ( buf[2]==
char(0xd3) && buf[3]==
char(0xc6) )
43 if (io->readBlock(buf, 4)!=4)
45 kdError(399) <<
"kimgio EPS: cannot read offset of MS-DOS EPS file" << endl;
49 = ((
unsigned char) buf[0])
50 + ((
unsigned char) buf[1] << 8)
51 + ((
unsigned char) buf[2] << 16)
52 + ((
unsigned char) buf[3] << 24);
53 if (io->readBlock(buf, 4)!=4)
55 kdError(399) <<
"kimgio EPS: cannot read size of MS-DOS EPS file" << endl;
59 = ((
unsigned char) buf[0])
60 + ((
unsigned char) buf[1] << 8)
61 + ((
unsigned char) buf[2] << 16)
62 + ((
unsigned char) buf[3] << 24);
63 kdDebug(399) <<
"kimgio EPS: Offset: " << ps_offset <<
" Size: " << ps_size << endl;
64 if ( !io->at(ps_offset) )
66 kdError(399) <<
"kimgio EPS: cannot seek in MS-DOS EPS file" << endl;
69 if ( io->readBlock(buf, 2)!=2 )
71 kdError(399) <<
"kimgio EPS: PostScript code has less than 2 bytes." << endl;
74 if ( buf[0]==
'%' && buf[1]==
'!' )
76 kdDebug(399) <<
"kimgio EPS: MS-DOS EPS file" << endl;
80 kdError(399) <<
"kimgio EPS: supposed Postscript code of a MS-DOS EPS file doe not start with %!." << endl;
86 kdError(399) <<
"kimgio EPS: wrong magic for potential MS-DOS EPS file!" << endl;
92 kdError(399) <<
"kimgio EPS: not an EPS file!" << endl;
98 static bool bbox ( TQIODevice *io,
int *x1,
int *y1,
int *x2,
int *y2)
104 while (io->readLine(buf, BUFLEN) > 0)
106 if (strncmp (buf, BBOX, BBOX_LEN) == 0)
110 float _x1, _y1, _x2, _y2;
111 if ( sscanf (buf,
"%*s %f %f %f %f",
112 &_x1, &_y1, &_x2, &_y2) == 4) {
113 kdDebug(399) <<
"kimgio EPS BBOX: " << _x1 <<
" " << _y1 <<
" " << _x2 <<
" " << _y2 << endl;
114 *x1=(int)_x1; *y1=(int)_y1; *x2=(int)_x2; *y2=(int)_y2;
124 TDE_EXPORT
void kimgio_eps_read (TQImageIO *image)
126 kdDebug(399) <<
"kimgio EPS: starting..." << endl;
136 TQIODevice* io = image->ioDevice();
137 TQ_UINT32 ps_offset, ps_size;
140 if ( !seekToCodeStart(io, ps_offset, ps_size) )
144 if ( !bbox (io, &x1, &y1, &x2, &y2)) {
145 kdError(399) <<
"kimgio EPS: no bounding box found!" << endl;
150 tmpFile.setAutoDelete(
true);
152 if( tmpFile.status() != 0 ) {
153 kdError(399) <<
"kimgio EPS: no temp file!" << endl;
166 bool needsScaling =
false;
167 int wantedWidth = x2;
168 int wantedHeight = y2;
170 if (image->parameters())
173 TQStringList params = TQStringList::split(
':', image->parameters());
174 if (params.count() >= 2 && x2 != 0.0 && y2 != 0.0)
176 wantedWidth = params[0].toInt();
177 xScale = (double)wantedWidth / (
double)x2;
178 wantedHeight = params[1].toInt();
179 yScale = (double)wantedHeight / (
double)y2;
188 cmdBuf =
"gs -sOutputFile=";
189 cmdBuf += tmpFile.name();
191 tmp.setNum( wantedWidth );
193 tmp.setNum( wantedHeight );
196 cmdBuf +=
" -dSAFER -dPARANOIDSAFER -dNOPAUSE -sDEVICE=ppm -c "
201 "1 1 254 255 div setrgbcolor fill "
202 "0 0 0 setrgbcolor - -c showpage quit";
206 ghostfd = popen (TQFile::encodeName(cmdBuf),
"w");
208 if ( ghostfd == 0 ) {
209 kdError(399) <<
"kimgio EPS: no GhostScript?" << endl;
213 fprintf (ghostfd,
"\n%d %d translate\n", -tqRound(x1*xScale), -tqRound(y1*yScale));
215 fprintf (ghostfd,
"%g %g scale\n", xScale, yScale);
222 TQByteArray buffer ( io->readAll() );
225 if (ps_size<=0L || ps_size>buffer.size())
226 ps_size=buffer.size();
228 fwrite(buffer.data(),
sizeof(
char), ps_size, ghostfd);
235 if( myimage.load (tmpFile.name()) ) {
236 image->setImage (myimage);
237 image->setStatus (0);
238 kdDebug(399) <<
"kimgio EPS: success!" << endl;
241 kdError(399) <<
"kimgio EPS: no image!" << endl;
248 TDE_EXPORT
void kimgio_eps_write( TQImageIO *imageio )
250 TQPrinter psOut(TQPrinter::PrinterResolution);
254 psOut.setCreator(
"KDE " TDE_VERSION_STRING );
255 psOut.setOutputToFile(
true );
258 KTempFile tmpFile(TQString::null,
".eps");
259 tmpFile.setAutoDelete(
true);
260 if ( tmpFile.status() != 0)
264 psOut.setOutputFileName(tmpFile.name());
265 psOut.setFullPage(
true);
270 p.setClipRect( 0, 0, imageio->image().width(), imageio->image().height(), TQPainter::CoordPainter);
271 p.drawImage( TQPoint( 0, 0 ), imageio->image() );
275 TQFile inFile(tmpFile.name());
276 inFile.open( IO_ReadOnly );
278 TQTextStream in( &inFile );
279 in.setEncoding( TQTextStream::Latin1 );
280 TQTextStream out( imageio->ioDevice() );
281 out.setEncoding( TQTextStream::Latin1 );
283 TQString szInLine = in.readLine();
284 out << szInLine <<
'\n';
286 while( !in.atEnd() ){
287 szInLine = in.readLine();
288 out << szInLine <<
'\n';
293 imageio->setStatus(0);