Update ooo320-m1
[ooovba.git] / goodies / source / filter.vcl / ieps / ieps.cxx
blob178f247f6e22e21e5b993605afc11b839b792fa5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ieps.cxx,v $
10 * $Revision: 1.20 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_goodies.hxx"
34 #include <stdio.h>
37 #include <vcl/sv.h>
38 #include <vcl/svapp.hxx>
39 #include <vcl/bitmap.hxx>
40 #include <vcl/bitmapex.hxx>
41 #include <vcl/animate.hxx>
42 #include <vcl/gdimtf.hxx>
43 #include <vcl/graph.h>
44 #include <vcl/window.hxx>
45 #include <vcl/graph.hxx>
46 #include <vcl/metaact.hxx>
47 #include <vcl/gdimtf.hxx>
48 #include <vcl/virdev.hxx>
49 #include <vcl/cvtgrf.hxx>
50 #include <vcl/bmpacc.hxx>
51 #include <svtools/fltcall.hxx>
52 #include <tools/urlobj.hxx>
53 #include <tools/tempfile.hxx>
54 #include <osl/process.h>
55 #include <osl/file.hxx>
57 /*************************************************************************
59 |* ImpSearchEntry()
61 |* Beschreibung Prueft ob im Speicherbereich pSource der nComp Bytes
62 |* gross ist eine Zeichenkette(pDest) mit der l�nge nSize
63 |* liegt. Geprueft wird NON-CASE-SENSITIVE und der Rueck-
64 |* gabewert ist die Adresse an der die Zeichekette gefunden
65 |* wurde oder NULL
67 |* Ersterstellung SJ 04.03.98 ( und das an meinem Geburtstag )
68 |* Letzte Aenderung SJ 04.03.98
70 *************************************************************************/
72 static BYTE* ImplSearchEntry( BYTE* pSource, BYTE* pDest, ULONG nComp, ULONG nSize )
74 while ( nComp-- >= nSize )
76 ULONG i;
77 for ( i = 0; i < nSize; i++ )
79 if ( ( pSource[i]&~0x20 ) != ( pDest[i]&~0x20 ) )
80 break;
82 if ( i == nSize )
83 return pSource;
84 pSource++;
86 return NULL;
89 //--------------------------------------------------------------------------
90 // SecurityCount is the buffersize of the buffer in which we will parse for a number
91 static long ImplGetNumber( BYTE **pBuf, int& nSecurityCount )
93 BOOL bValid = TRUE;
94 BOOL bNegative = FALSE;
95 long nRetValue = 0;
96 while ( ( --nSecurityCount ) && ( ( **pBuf == ' ' ) || ( **pBuf == 0x9 ) ) )
97 (*pBuf)++;
98 BYTE nByte = **pBuf;
99 while ( nSecurityCount && ( nByte != ' ' ) && ( nByte != 0x9 ) && ( nByte != 0xd ) && ( nByte != 0xa ) )
101 switch ( nByte )
103 case '.' :
104 // we'll only use the integer format
105 bValid = FALSE;
106 break;
107 case '-' :
108 bNegative = TRUE;
109 break;
110 default :
111 if ( ( nByte < '0' ) || ( nByte > '9' ) )
112 nSecurityCount = 1; // error parsing the bounding box values
113 else if ( bValid )
115 nRetValue *= 10;
116 nRetValue += nByte - '0';
118 break;
120 nSecurityCount--;
121 nByte = *(++(*pBuf));
123 if ( bNegative )
124 nRetValue = -nRetValue;
125 return nRetValue;
128 //--------------------------------------------------------------------------
130 static int ImplGetLen( BYTE* pBuf, int nMax )
132 int nLen = 0;
133 while( nLen != nMax )
135 BYTE nDat = *pBuf++;
136 if ( nDat == 0x0a || nDat == 0x25 )
137 break;
138 nLen++;
140 return nLen;
143 static void MakeAsMeta(Graphic &rGraphic)
145 VirtualDevice aVDev;
146 GDIMetaFile aMtf;
147 Bitmap aBmp( rGraphic.GetBitmap() );
148 Size aSize = aBmp.GetPrefSize();
150 if( !aSize.Width() || !aSize.Height() )
151 aSize = Application::GetDefaultDevice()->PixelToLogic(
152 aBmp.GetSizePixel(), MAP_100TH_MM );
153 else
154 aSize = Application::GetDefaultDevice()->LogicToLogic( aSize,
155 aBmp.GetPrefMapMode(), MAP_100TH_MM );
157 aVDev.EnableOutput( FALSE );
158 aMtf.Record( &aVDev );
159 aVDev.DrawBitmap( Point(), aSize, rGraphic.GetBitmap() );
160 aMtf.Stop();
161 aMtf.WindStart();
162 aMtf.SetPrefMapMode( MAP_100TH_MM );
163 aMtf.SetPrefSize( aSize );
164 rGraphic = aMtf;
167 static bool RenderAsEMF(const sal_uInt8* pBuf, sal_uInt32 nBytesRead, Graphic &rGraphic)
169 TempFile aTemp;
170 aTemp.EnableKillingFile();
171 rtl::OUString fileName =
172 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("pstoedit"));
173 rtl::OUString arg1 =
174 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-f"));
175 rtl::OUString arg2 =
176 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("emf:-OO"));
177 rtl::OUString arg3 =
178 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-"));
179 rtl::OUString output;
180 osl::FileBase::getSystemPathFromFileURL(aTemp.GetName(), output);
181 rtl_uString *args[] =
183 arg1.pData, arg2.pData, arg3.pData, output.pData
185 oslProcess aProcess;
186 oslFileHandle pIn = NULL;
187 oslFileHandle pOut = NULL;
188 oslFileHandle pErr = NULL;
189 oslProcessError eErr = osl_executeProcess_WithRedirectedIO(fileName.pData,
190 args, sizeof(args)/sizeof(rtl_uString *),
191 osl_Process_SEARCHPATH | osl_Process_HIDDEN,
192 osl_getCurrentSecurity(), 0, 0, 0, &aProcess, &pIn, &pOut, &pErr);
193 if (eErr!=osl_Process_E_None)
194 return false;
196 bool bRet = false;
197 sal_uInt64 nCount;
198 osl_writeFile(pIn, pBuf, nBytesRead, &nCount);
199 if (pIn) osl_closeFile(pIn);
200 bool bEMFSupported=true;
201 if (pOut)
203 rtl::ByteSequence seq;
204 if (osl_File_E_None == osl_readLine(pOut, (sal_Sequence **)&seq))
206 rtl::OString line( (const sal_Char *) seq.getConstArray(), seq.getLength() );
207 if (line.indexOf(rtl::OString("Unsupported output format")) == 0)
208 bEMFSupported=false;
210 osl_closeFile(pOut);
212 if (pErr) osl_closeFile(pErr);
213 if (nCount == nBytesRead && bEMFSupported)
215 SvFileStream aFile(output, STREAM_READ);
216 if (GraphicConverter::Import(aFile, rGraphic, CVT_EMF) == ERRCODE_NONE)
217 bRet = true;
219 osl_joinProcess(aProcess);
220 osl_freeProcessHandle(aProcess);
221 return bRet;
224 static bool RenderAsPNGThroughHelper(const sal_uInt8* pBuf, sal_uInt32 nBytesRead,
225 Graphic &rGraphic, rtl::OUString &rProgName, rtl_uString **pArgs, size_t nArgs)
227 oslProcess aProcess;
228 oslFileHandle pIn = NULL;
229 oslFileHandle pOut = NULL;
230 oslFileHandle pErr = NULL;
231 oslProcessError eErr = osl_executeProcess_WithRedirectedIO(rProgName.pData,
232 pArgs, nArgs, osl_Process_SEARCHPATH | osl_Process_HIDDEN,
233 osl_getCurrentSecurity(), 0, 0, 0, &aProcess, &pIn, &pOut, &pErr);
234 if (eErr!=osl_Process_E_None)
235 return false;
237 bool bRet = false;
238 sal_uInt64 nCount;
239 osl_writeFile(pIn, pBuf, nBytesRead, &nCount);
240 if (pIn) osl_closeFile(pIn);
241 if (nCount == nBytesRead)
243 SvMemoryStream aMemStm;
244 sal_uInt8 aBuf[32000];
245 oslFileError eFileErr = osl_readFile(pOut, aBuf, 32000, &nCount);
246 while (eFileErr == osl_File_E_None && nCount)
248 aMemStm.Write(aBuf, sal::static_int_cast< sal_Size >(nCount));
249 eFileErr = osl_readFile(pOut, aBuf, 32000, &nCount);
252 aMemStm.Seek(0);
253 if (
254 eFileErr == osl_File_E_None &&
255 GraphicConverter::Import(aMemStm, rGraphic, CVT_PNG) == ERRCODE_NONE
258 MakeAsMeta(rGraphic);
259 bRet = true;
262 if (pOut) osl_closeFile(pOut);
263 if (pErr) osl_closeFile(pErr);
264 osl_joinProcess(aProcess);
265 osl_freeProcessHandle(aProcess);
266 return bRet;
269 static bool RenderAsPNGThroughConvert(const sal_uInt8* pBuf, sal_uInt32 nBytesRead,
270 Graphic &rGraphic)
272 rtl::OUString fileName =
273 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("convert"));
274 // density in pixel/inch
275 rtl::OUString arg1 = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-density"));
276 // since the preview is also used for PDF-Export & printing on non-PS-printers,
277 // use some better quality - 300x300 should allow some resizing as well
278 rtl::OUString arg2 = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("300x300"));
279 // read eps from STDIN
280 rtl::OUString arg3 = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("eps:-"));
281 // write png to STDOUT
282 rtl::OUString arg4 = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("png:-"));
283 rtl_uString *args[] =
285 arg1.pData, arg2.pData, arg3.pData, arg4.pData
287 return RenderAsPNGThroughHelper(pBuf, nBytesRead, rGraphic, fileName, args,
288 sizeof(args)/sizeof(rtl_uString *));
291 static bool RenderAsPNGThroughGS(const sal_uInt8* pBuf, sal_uInt32 nBytesRead,
292 Graphic &rGraphic)
294 #ifdef WNT
295 rtl::OUString fileName =
296 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("gswin32c"));
297 #else
298 rtl::OUString fileName =
299 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("gs"));
300 #endif
301 rtl::OUString arg1 =
302 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-q"));
303 rtl::OUString arg2 =
304 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-dBATCH"));
305 rtl::OUString arg3 =
306 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-dNOPAUSE"));
307 rtl::OUString arg4 =
308 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-dPARANOIDSAFER"));
309 rtl::OUString arg5 =
310 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-dEPSCrop"));
311 rtl::OUString arg6 =
312 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-dTextAlphaBits=4"));
313 rtl::OUString arg7 =
314 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-dGraphicsAlphaBits=4"));
315 rtl::OUString arg8 =
316 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-r300x300"));
317 rtl::OUString arg9 =
318 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-sDEVICE=png256"));
319 rtl::OUString arg10 =
320 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-sOutputFile=-"));
321 rtl::OUString arg11 =
322 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-"));
323 rtl_uString *args[] =
325 arg1.pData, arg2.pData, arg3.pData, arg4.pData, arg5.pData,
326 arg6.pData, arg7.pData, arg8.pData, arg9.pData, arg10.pData,
327 arg11.pData
329 return RenderAsPNGThroughHelper(pBuf, nBytesRead, rGraphic, fileName, args,
330 sizeof(args)/sizeof(rtl_uString *));
333 static bool RenderAsPNG(const sal_uInt8* pBuf, sal_uInt32 nBytesRead, Graphic &rGraphic)
335 if (RenderAsPNGThroughConvert(pBuf, nBytesRead, rGraphic))
336 return true;
337 else
338 return RenderAsPNGThroughGS(pBuf, nBytesRead, rGraphic);
341 // this method adds a replacement action containing the original wmf or tiff replacement,
342 // so the original eps can be written when storing to ODF.
343 void CreateMtfReplacementAction( GDIMetaFile& rMtf, SvStream& rStrm, sal_uInt32 nOrigPos, sal_uInt32 nPSSize,
344 sal_uInt32 nPosWMF, sal_uInt32 nSizeWMF, sal_uInt32 nPosTIFF, sal_uInt32 nSizeTIFF )
346 ByteString aComment( (const sal_Char*)"EPSReplacementGraphic" );
347 if ( nSizeWMF || nSizeTIFF )
349 SvMemoryStream aReplacement( nSizeWMF + nSizeTIFF + 28 );
350 sal_uInt32 nMagic = 0xc6d3d0c5;
351 sal_uInt32 nPPos = 28 + nSizeWMF + nSizeTIFF;
352 sal_uInt32 nWPos = nSizeWMF ? 28 : 0;
353 sal_uInt32 nTPos = nSizeTIFF ? 28 + nSizeWMF : 0;
355 aReplacement << nMagic << nPPos << nPSSize
356 << nWPos << nSizeWMF
357 << nTPos << nSizeTIFF;
358 if ( nSizeWMF )
360 sal_uInt8* pBuf = new sal_uInt8[ nSizeWMF ];
361 rStrm.Seek( nOrigPos + nPosWMF );
362 rStrm.Read( pBuf, nSizeWMF );
363 aReplacement.Write( pBuf, nSizeWMF );
364 delete[] pBuf;
366 if ( nSizeTIFF )
368 sal_uInt8* pBuf = new sal_uInt8[ nSizeTIFF ];
369 rStrm.Seek( nOrigPos + nPosTIFF );
370 rStrm.Read( pBuf, nSizeTIFF );
371 aReplacement.Write( pBuf, nSizeTIFF );
372 delete[] pBuf;
374 rMtf.AddAction( (MetaAction*)( new MetaCommentAction( aComment, 0, (const BYTE*)aReplacement.GetData(), aReplacement.Tell() ) ) );
376 else
377 rMtf.AddAction( (MetaAction*)( new MetaCommentAction( aComment, 0, NULL, 0 ) ) );
380 //there is no preview -> make a red box
381 void MakePreview(sal_uInt8* pBuf, sal_uInt32 nBytesRead,
382 long nWidth, long nHeight, Graphic &rGraphic)
384 GDIMetaFile aMtf;
385 VirtualDevice aVDev;
386 Font aFont;
388 aVDev.EnableOutput( FALSE );
389 aMtf.Record( &aVDev );
390 aVDev.SetLineColor( Color( COL_RED ) );
391 aVDev.SetFillColor();
393 aFont.SetColor( COL_LIGHTRED );
394 // aFont.SetSize( Size( 0, 32 ) );
396 aVDev.Push( PUSH_FONT );
397 aVDev.SetFont( aFont );
399 Rectangle aRect( Point( 1, 1 ), Size( nWidth - 2, nHeight - 2 ) );
400 aVDev.DrawRect( aRect );
402 String aString;
403 int nLen;
404 BYTE* pDest = ImplSearchEntry( pBuf, (BYTE*)"%%Title:", nBytesRead - 32, 8 );
405 if ( pDest )
407 pDest += 8;
408 if ( *pDest == ' ' )
409 pDest++;
410 nLen = ImplGetLen( pDest, 32 );
411 BYTE aOldValue(pDest[ nLen ]); pDest[ nLen ] = 0;
412 if ( strcmp( (const char*)pDest, "none" ) != 0 )
414 aString.AppendAscii( " Title:" );
415 aString.AppendAscii( (char*)pDest );
416 aString.AppendAscii( "\n" );
418 pDest[ nLen ] = aOldValue;
420 pDest = ImplSearchEntry( pBuf, (BYTE*)"%%Creator:", nBytesRead - 32, 10 );
421 if ( pDest )
423 pDest += 10;
424 if ( *pDest == ' ' )
425 pDest++;
426 nLen = ImplGetLen( pDest, 32 );
427 BYTE aOldValue(pDest[ nLen ]); pDest[ nLen ] = 0;
428 aString.AppendAscii( " Creator:" );
429 aString.AppendAscii( (char*)pDest );
430 aString.AppendAscii( "\n" );
431 pDest[ nLen ] = aOldValue;
433 pDest = ImplSearchEntry( pBuf, (BYTE*)"%%CreationDate:", nBytesRead - 32, 15 );
434 if ( pDest )
436 pDest += 15;
437 if ( *pDest == ' ' )
438 pDest++;
439 nLen = ImplGetLen( pDest, 32 );
440 BYTE aOldValue(pDest[ nLen ]); pDest[ nLen ] = 0;
441 if ( strcmp( (const char*)pDest, "none" ) != 0 )
443 aString.AppendAscii( " CreationDate:" );
444 aString.AppendAscii( (char*)pDest );
445 aString.AppendAscii( "\n" );
447 pDest[ nLen ] = aOldValue;
449 pDest = ImplSearchEntry( pBuf, (BYTE*)"%%LanguageLevel:", nBytesRead - 4, 16 );
450 if ( pDest )
452 pDest += 16;
453 int nCount = 4;
454 long nNumber = ImplGetNumber( &pDest, nCount );
455 if ( nCount && ( (UINT32)nNumber < 10 ) )
457 aString.AppendAscii( " LanguageLevel:" );
458 aString.Append( UniString::CreateFromInt32( nNumber ) );
461 aVDev.DrawText( aRect, aString, TEXT_DRAW_CLIP | TEXT_DRAW_MULTILINE );
462 aVDev.Pop();
463 aMtf.Stop();
464 aMtf.WindStart();
465 aMtf.SetPrefMapMode( MAP_POINT );
466 aMtf.SetPrefSize( Size( nWidth, nHeight ) );
467 rGraphic = aMtf;
471 //================== GraphicImport - die exportierte Funktion ================
473 #ifdef WNT
474 extern "C" BOOL _cdecl GraphicImport(SvStream & rStream, Graphic & rGraphic, FilterConfigItem*, BOOL)
475 #else
476 extern "C" BOOL GraphicImport(SvStream & rStream, Graphic & rGraphic, FilterConfigItem*, BOOL)
477 #endif
479 if ( rStream.GetError() )
480 return FALSE;
482 Graphic aGraphic;
483 sal_Bool bRetValue = sal_False;
484 sal_Bool bHasPreview = sal_False;
485 sal_Bool bGraphicLinkCreated = sal_False;
486 sal_uInt32 nSignature, nPSStreamPos, nPSSize;
487 sal_uInt32 nSizeWMF = 0;
488 sal_uInt32 nPosWMF = 0;
489 sal_uInt32 nSizeTIFF = 0;
490 sal_uInt32 nPosTIFF = 0;
491 sal_uInt32 nOrigPos = nPSStreamPos = rStream.Tell();
492 sal_uInt16 nOldFormat = rStream.GetNumberFormatInt();
493 rStream.SetNumberFormatInt( NUMBERFORMAT_INT_LITTLEENDIAN );
494 rStream >> nSignature;
495 if ( nSignature == 0xc6d3d0c5 )
497 rStream >> nPSStreamPos >> nPSSize >> nPosWMF >> nSizeWMF;
499 // first we try to get the metafile grafix
501 if ( nSizeWMF )
503 if ( nPosWMF != 0 )
505 rStream.Seek( nOrigPos + nPosWMF );
506 if ( GraphicConverter::Import( rStream, aGraphic, CVT_WMF ) == ERRCODE_NONE )
507 bHasPreview = bRetValue = TRUE;
510 else
512 rStream >> nPosTIFF >> nSizeTIFF;
514 // else we have to get the tiff grafix
516 if ( nPosTIFF && nSizeTIFF )
518 rStream.Seek( nOrigPos + nPosTIFF );
519 if ( GraphicConverter::Import( rStream, aGraphic, CVT_TIF ) == ERRCODE_NONE )
521 MakeAsMeta(aGraphic);
522 rStream.Seek( nOrigPos + nPosTIFF );
523 bHasPreview = bRetValue = TRUE;
528 else
530 nPSStreamPos = nOrigPos; // no preview available _>so we must get the size manually
531 nPSSize = rStream.Seek( STREAM_SEEK_TO_END ) - nOrigPos;
533 sal_uInt8* pHeader = new sal_uInt8[ 22 ];
534 rStream.Seek( nPSStreamPos );
535 rStream.Read( pHeader, 22 ); // check PostScript header
536 if ( ImplSearchEntry( pHeader, (BYTE*)"%!PS-Adobe", 10, 10 ) &&
537 ImplSearchEntry( &pHeader[ 15 ], (BYTE*)"EPS", 3, 3 ) )
539 rStream.Seek( nPSStreamPos );
540 sal_uInt8* pBuf = new sal_uInt8[ nPSSize ];
541 if ( pBuf )
543 sal_uInt32 nBufStartPos = rStream.Tell();
544 sal_uInt32 nBytesRead = rStream.Read( pBuf, nPSSize );
545 if ( nBytesRead == nPSSize )
547 int nSecurityCount = 32;
548 if ( !bHasPreview ) // if there is no tiff/wmf preview, we will parse for an preview in the eps prolog
550 BYTE* pDest = ImplSearchEntry( pBuf, (BYTE*)"%%BeginPreview:", nBytesRead - 32, 15 );
551 if ( pDest )
553 pDest += 15;
554 long nWidth = ImplGetNumber( &pDest, nSecurityCount );
555 long nHeight = ImplGetNumber( &pDest, nSecurityCount );
556 long nBitDepth = ImplGetNumber( &pDest, nSecurityCount );
557 long nScanLines = ImplGetNumber( &pDest, nSecurityCount );
558 pDest = ImplSearchEntry( pDest, (BYTE*)"%", 16, 1 ); // go to the first Scanline
559 if ( nSecurityCount && pDest && nWidth && nHeight && ( ( nBitDepth == 1 ) || ( nBitDepth == 8 ) ) && nScanLines )
561 rStream.Seek( nBufStartPos + ( pDest - pBuf ) );
563 Bitmap aBitmap( Size( nWidth, nHeight ), 1 );
564 BitmapWriteAccess* pAcc = aBitmap.AcquireWriteAccess();
565 if ( pAcc )
567 int nBitsLeft;
568 BOOL bIsValid = TRUE;
569 BYTE nDat = 0;
570 char nByte;
571 for ( long y = 0; bIsValid && ( y < nHeight ); y++ )
573 nBitsLeft = 0;
574 for ( long x = 0; x < nWidth; x++ )
576 if ( --nBitsLeft < 0 )
578 while ( bIsValid && ( nBitsLeft != 7 ) )
580 rStream >> nByte;
581 switch ( nByte )
583 case 0x0a :
584 if ( --nScanLines < 0 )
585 bIsValid = FALSE;
586 case 0x09 :
587 case 0x0d :
588 case 0x20 :
589 case 0x25 :
590 break;
591 default:
593 if ( nByte >= '0' )
595 if ( nByte > '9' )
597 nByte &=~0x20; // case none sensitive for hexadezimal values
598 nByte -= ( 'A' - 10 );
599 if ( nByte > 15 )
600 bIsValid = FALSE;
602 else
603 nByte -= '0';
604 nBitsLeft += 4;
605 nDat <<= 4;
606 nDat |= ( nByte ^ 0xf ); // in epsi a zero bit represents white color
608 else
609 bIsValid = FALSE;
611 break;
615 if ( nBitDepth == 1 )
616 pAcc->SetPixel( y, x, sal::static_int_cast< BYTE >(( nDat >> nBitsLeft ) & 1) );
617 else
619 pAcc->SetPixel( y, x, ( nDat ) ? 1 : 0 ); // nBitDepth == 8
620 nBitsLeft = 0;
624 if ( bIsValid )
626 VirtualDevice aVDev;
627 GDIMetaFile aMtf;
628 Size aSize;
629 aVDev.EnableOutput( FALSE );
630 aMtf.Record( &aVDev );
631 aSize = aBitmap.GetPrefSize();
632 if( !aSize.Width() || !aSize.Height() )
633 aSize = Application::GetDefaultDevice()->PixelToLogic( aBitmap.GetSizePixel(), MAP_100TH_MM );
634 else
635 aSize = Application::GetDefaultDevice()->LogicToLogic( aSize, aBitmap.GetPrefMapMode(), MAP_100TH_MM );
636 aVDev.DrawBitmap( Point(), aSize, aBitmap );
637 aMtf.Stop();
638 aMtf.WindStart();
639 aMtf.SetPrefMapMode( MAP_100TH_MM );
640 aMtf.SetPrefSize( aSize );
641 aGraphic = aMtf;
642 bHasPreview = bRetValue = TRUE;
644 aBitmap.ReleaseAccess( pAcc );
650 BYTE* pDest = ImplSearchEntry( pBuf, (BYTE*)"%%BoundingBox:", nBytesRead, 14 );
651 if ( pDest )
653 nSecurityCount = 100;
654 long nNumb[4];
655 nNumb[0] = nNumb[1] = nNumb[2] = nNumb[3] = 0;
656 pDest += 14;
657 for ( int i = 0; ( i < 4 ) && nSecurityCount; i++ )
659 nNumb[ i ] = ImplGetNumber( &pDest, nSecurityCount );
661 if ( nSecurityCount)
663 bGraphicLinkCreated = sal_True;
664 GfxLink aGfxLink( pBuf, nPSSize, GFX_LINK_TYPE_EPS_BUFFER, TRUE ) ;
665 GDIMetaFile aMtf;
667 long nWidth = nNumb[2] - nNumb[0] + 1;
668 long nHeight = nNumb[3] - nNumb[1] + 1;
670 // if there is no preview -> try with gs to make one
671 if( !bHasPreview )
673 bHasPreview = RenderAsEMF(pBuf, nBytesRead, aGraphic);
674 if (!bHasPreview)
675 bHasPreview = RenderAsPNG(pBuf, nBytesRead, aGraphic);
678 // if there is no preview -> make a red box
679 if( !bHasPreview )
681 MakePreview(pBuf, nBytesRead, nWidth, nHeight,
682 aGraphic);
685 aMtf.AddAction( (MetaAction*)( new MetaEPSAction( Point(), Size( nWidth, nHeight ),
686 aGfxLink, aGraphic.GetGDIMetaFile() ) ) );
687 CreateMtfReplacementAction( aMtf, rStream, nOrigPos, nPSSize, nPosWMF, nSizeWMF, nPosTIFF, nSizeTIFF );
688 aMtf.WindStart();
689 aMtf.SetPrefMapMode( MAP_POINT );
690 aMtf.SetPrefSize( Size( nWidth, nHeight ) );
691 rGraphic = aMtf;
692 bRetValue = sal_True;
697 if ( !bGraphicLinkCreated )
698 delete[] pBuf;
700 delete[] pHeader;
701 rStream.SetNumberFormatInt(nOldFormat);
702 rStream.Seek( nOrigPos );
703 return ( bRetValue );
706 //================== ein bischen Muell fuer Windows ==========================
707 #ifndef GCC
708 #endif
710 #ifdef WIN
712 static HINSTANCE hDLLInst = 0; // HANDLE der DLL
714 extern "C" int CALLBACK LibMain( HINSTANCE hDLL, WORD, WORD nHeap, LPSTR )
716 #ifndef WNT
717 if ( nHeap )
718 UnlockData( 0 );
719 #endif
721 hDLLInst = hDLL;
723 return TRUE;
726 extern "C" int CALLBACK WEP( int )
728 return 1;
731 #endif