xref: /trunk/main/svx/source/xoutdev/_xoutbmp.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1f6e50924SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3f6e50924SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4f6e50924SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5f6e50924SAndrew Rist  * distributed with this work for additional information
6f6e50924SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7f6e50924SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8f6e50924SAndrew Rist  * "License"); you may not use this file except in compliance
9f6e50924SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11f6e50924SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13f6e50924SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14f6e50924SAndrew Rist  * software distributed under the License is distributed on an
15f6e50924SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16f6e50924SAndrew Rist  * KIND, either express or implied.  See the License for the
17f6e50924SAndrew Rist  * specific language governing permissions and limitations
18f6e50924SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20f6e50924SAndrew Rist  *************************************************************/
21f6e50924SAndrew Rist 
22cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
23cdf0e10cSrcweir #include "precompiled_svx.hxx"
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include <sot/factory.hxx>
26cdf0e10cSrcweir #include <tools/urlobj.hxx>
27cdf0e10cSrcweir #include <unotools/ucbstreamhelper.hxx>
28cdf0e10cSrcweir #include <vcl/bmpacc.hxx>
29cdf0e10cSrcweir #include <tools/poly.hxx>
30cdf0e10cSrcweir #include <vcl/virdev.hxx>
31cdf0e10cSrcweir #include <vcl/wrkwin.hxx>
32cdf0e10cSrcweir #include <svl/solar.hrc>
33cdf0e10cSrcweir #include <sfx2/docfile.hxx>
34cdf0e10cSrcweir #include <sfx2/app.hxx>
35cdf0e10cSrcweir #include "svx/xoutbmp.hxx"
36cdf0e10cSrcweir #include <svtools/FilterConfigItem.hxx>
37cdf0e10cSrcweir #include <svtools/filter.hxx>
3845fd3b9aSArmin Le Grand #include <vcl/dibtools.hxx>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir // -----------
41cdf0e10cSrcweir // - Defines -
42cdf0e10cSrcweir // -----------
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #define FORMAT_BMP  String(RTL_CONSTASCII_USTRINGPARAM("bmp"))
45cdf0e10cSrcweir #define FORMAT_GIF  String(RTL_CONSTASCII_USTRINGPARAM("gif"))
46cdf0e10cSrcweir #define FORMAT_JPG  String(RTL_CONSTASCII_USTRINGPARAM("jpg"))
47cdf0e10cSrcweir #define FORMAT_PNG  String(RTL_CONSTASCII_USTRINGPARAM("png"))
48cdf0e10cSrcweir 
49cdf0e10cSrcweir // --------------
50cdf0e10cSrcweir // - XOutBitmap -
51cdf0e10cSrcweir // --------------
52cdf0e10cSrcweir 
53cdf0e10cSrcweir GraphicFilter* XOutBitmap::pGrfFilter = NULL;
54cdf0e10cSrcweir 
55cdf0e10cSrcweir // ------------------------------------------------------------------------
56cdf0e10cSrcweir 
MirrorAnimation(const Animation & rAnimation,sal_Bool bHMirr,sal_Bool bVMirr)57cdf0e10cSrcweir Animation XOutBitmap::MirrorAnimation( const Animation& rAnimation, sal_Bool bHMirr, sal_Bool bVMirr )
58cdf0e10cSrcweir {
59cdf0e10cSrcweir     Animation aNewAnim( rAnimation );
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     if( bHMirr || bVMirr )
62cdf0e10cSrcweir     {
63cdf0e10cSrcweir         const Size& rGlobalSize = aNewAnim.GetDisplaySizePixel();
64cdf0e10cSrcweir         sal_uIntPtr     nMirrorFlags = 0L;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir         if( bHMirr )
67cdf0e10cSrcweir             nMirrorFlags |= BMP_MIRROR_HORZ;
68cdf0e10cSrcweir 
69cdf0e10cSrcweir         if( bVMirr )
70cdf0e10cSrcweir             nMirrorFlags |= BMP_MIRROR_VERT;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir         for( sal_uInt16 i = 0, nCount = aNewAnim.Count(); i < nCount; i++ )
73cdf0e10cSrcweir         {
74cdf0e10cSrcweir             AnimationBitmap aAnimBmp( aNewAnim.Get( i ) );
75cdf0e10cSrcweir 
76cdf0e10cSrcweir             // BitmapEx spiegeln
77cdf0e10cSrcweir             aAnimBmp.aBmpEx.Mirror( nMirrorFlags );
78cdf0e10cSrcweir 
79cdf0e10cSrcweir             // Die Positionen innerhalb der Gesamtbitmap
80cdf0e10cSrcweir             // muessen natuerlich auch angepasst werden
81cdf0e10cSrcweir             if( bHMirr )
82cdf0e10cSrcweir                 aAnimBmp.aPosPix.X() = rGlobalSize.Width() - aAnimBmp.aPosPix.X() -
83cdf0e10cSrcweir                                        aAnimBmp.aSizePix.Width();
84cdf0e10cSrcweir 
85cdf0e10cSrcweir             if( bVMirr )
86cdf0e10cSrcweir                 aAnimBmp.aPosPix.Y() = rGlobalSize.Height() - aAnimBmp.aPosPix.Y() -
87cdf0e10cSrcweir                                        aAnimBmp.aSizePix.Height();
88cdf0e10cSrcweir 
89cdf0e10cSrcweir             aNewAnim.Replace( aAnimBmp, i );
90cdf0e10cSrcweir         }
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     return aNewAnim;
94cdf0e10cSrcweir }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir // ------------------------------------------------------------------------
97cdf0e10cSrcweir 
MirrorGraphic(const Graphic & rGraphic,const sal_uIntPtr nMirrorFlags)98cdf0e10cSrcweir Graphic XOutBitmap::MirrorGraphic( const Graphic& rGraphic, const sal_uIntPtr nMirrorFlags )
99cdf0e10cSrcweir {
100cdf0e10cSrcweir     Graphic aRetGraphic;
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     if( nMirrorFlags )
103cdf0e10cSrcweir     {
104cdf0e10cSrcweir         if( rGraphic.IsAnimated() )
105cdf0e10cSrcweir         {
106cdf0e10cSrcweir             aRetGraphic = MirrorAnimation( rGraphic.GetAnimation(),
107cdf0e10cSrcweir                                            ( nMirrorFlags & BMP_MIRROR_HORZ ) == BMP_MIRROR_HORZ,
108cdf0e10cSrcweir                                            ( nMirrorFlags & BMP_MIRROR_VERT ) == BMP_MIRROR_VERT );
109cdf0e10cSrcweir         }
110cdf0e10cSrcweir         else
111cdf0e10cSrcweir         {
112cdf0e10cSrcweir             if( rGraphic.IsTransparent() )
113cdf0e10cSrcweir             {
114cdf0e10cSrcweir                 BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
115cdf0e10cSrcweir 
116cdf0e10cSrcweir                 aBmpEx.Mirror( nMirrorFlags );
117cdf0e10cSrcweir                 aRetGraphic = aBmpEx;
118cdf0e10cSrcweir             }
119cdf0e10cSrcweir             else
120cdf0e10cSrcweir             {
121cdf0e10cSrcweir                 Bitmap aBmp( rGraphic.GetBitmap() );
122cdf0e10cSrcweir 
123cdf0e10cSrcweir                 aBmp.Mirror( nMirrorFlags );
124cdf0e10cSrcweir                 aRetGraphic = aBmp;
125cdf0e10cSrcweir             }
126cdf0e10cSrcweir         }
127cdf0e10cSrcweir     }
128cdf0e10cSrcweir     else
129cdf0e10cSrcweir         aRetGraphic = rGraphic;
130cdf0e10cSrcweir 
131cdf0e10cSrcweir     return aRetGraphic;
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir // ------------------------------------------------------------------------
135cdf0e10cSrcweir 
WriteGraphic(const Graphic & rGraphic,String & rFileName,const String & rFilterName,const sal_uIntPtr nFlags,const Size * pMtfSize_100TH_MM)136cdf0e10cSrcweir sal_uInt16 XOutBitmap::WriteGraphic( const Graphic& rGraphic, String& rFileName,
137cdf0e10cSrcweir                                  const String& rFilterName, const sal_uIntPtr nFlags,
138cdf0e10cSrcweir                                  const Size* pMtfSize_100TH_MM )
139cdf0e10cSrcweir {
140cdf0e10cSrcweir     if( rGraphic.GetType() != GRAPHIC_NONE )
141cdf0e10cSrcweir     {
142cdf0e10cSrcweir         INetURLObject   aURL( rFileName );
143cdf0e10cSrcweir         Graphic         aGraphic;
144cdf0e10cSrcweir         String          aExt;
145cdf0e10cSrcweir         GraphicFilter*  pFilter = GraphicFilter::GetGraphicFilter();
146cdf0e10cSrcweir         sal_uInt16          nErr = GRFILTER_FILTERERROR, nFilter = GRFILTER_FORMAT_NOTFOUND;
147cdf0e10cSrcweir         sal_Bool            bTransparent = rGraphic.IsTransparent(), bAnimated = rGraphic.IsAnimated();
148cdf0e10cSrcweir 
149cdf0e10cSrcweir         DBG_ASSERT( aURL.GetProtocol() != INET_PROT_NOT_VALID, "XOutBitmap::WriteGraphic(...): invalid URL" );
150cdf0e10cSrcweir 
151cdf0e10cSrcweir         // calculate correct file name
152cdf0e10cSrcweir         if( !( nFlags & XOUTBMP_DONT_EXPAND_FILENAME ) )
153cdf0e10cSrcweir         {
154cdf0e10cSrcweir             String aName( aURL.getBase() );
155cdf0e10cSrcweir             aName += '_';
156cdf0e10cSrcweir             aName += String(aURL.getExtension());
157cdf0e10cSrcweir             aName += '_';
158cdf0e10cSrcweir             String aStr( String::CreateFromInt32( rGraphic.GetChecksum(), 16 ) );
159cdf0e10cSrcweir             if ( aStr.GetChar(0) == '-' )
160cdf0e10cSrcweir                 aStr.SetChar(0,'m');
161cdf0e10cSrcweir             aName += aStr;
162cdf0e10cSrcweir             aURL.setBase( aName );
163cdf0e10cSrcweir         }
164cdf0e10cSrcweir 
16561472d26SArmin Le Grand         // #121128# use shortcut to write SVG data in original form (if possible)
16661472d26SArmin Le Grand         const SvgDataPtr aSvgDataPtr(rGraphic.getSvgData());
16761472d26SArmin Le Grand 
16861472d26SArmin Le Grand         if(aSvgDataPtr.get()
16961472d26SArmin Le Grand             && aSvgDataPtr->getSvgDataArrayLength()
17061472d26SArmin Le Grand             && rFilterName.EqualsIgnoreCaseAscii("svg"))
17161472d26SArmin Le Grand         {
17261472d26SArmin Le Grand             if(!(nFlags & XOUTBMP_DONT_ADD_EXTENSION))
17361472d26SArmin Le Grand             {
17461472d26SArmin Le Grand                 aURL.setExtension(rFilterName);
17561472d26SArmin Le Grand             }
17661472d26SArmin Le Grand 
17761472d26SArmin Le Grand             rFileName = aURL.GetMainURL(INetURLObject::NO_DECODE);
17861472d26SArmin Le Grand             SfxMedium aMedium(aURL.GetMainURL(INetURLObject::NO_DECODE), STREAM_WRITE|STREAM_SHARE_DENYNONE|STREAM_TRUNC, true);
17961472d26SArmin Le Grand             SvStream* pOStm = aMedium.GetOutStream();
18061472d26SArmin Le Grand 
18161472d26SArmin Le Grand             if(pOStm)
18261472d26SArmin Le Grand             {
18361472d26SArmin Le Grand                 pOStm->Write(aSvgDataPtr->getSvgDataArray().get(), aSvgDataPtr->getSvgDataArrayLength());
18461472d26SArmin Le Grand                 aMedium.Commit();
18561472d26SArmin Le Grand 
18661472d26SArmin Le Grand                 if(!aMedium.GetError())
18761472d26SArmin Le Grand                 {
18861472d26SArmin Le Grand                     nErr = GRFILTER_OK;
18961472d26SArmin Le Grand                 }
19061472d26SArmin Le Grand             }
19161472d26SArmin Le Grand         }
19261472d26SArmin Le Grand 
19361472d26SArmin Le Grand         if( GRFILTER_OK != nErr )
19461472d26SArmin Le Grand         {
195cdf0e10cSrcweir             if( ( nFlags & XOUTBMP_USE_NATIVE_IF_POSSIBLE ) &&
196cdf0e10cSrcweir                 !( nFlags & XOUTBMP_MIRROR_HORZ ) &&
197cdf0e10cSrcweir                 !( nFlags & XOUTBMP_MIRROR_VERT ) &&
198cdf0e10cSrcweir                 ( rGraphic.GetType() != GRAPHIC_GDIMETAFILE ) && rGraphic.IsLink() )
199cdf0e10cSrcweir             {
200cdf0e10cSrcweir                 // try to write native link
201cdf0e10cSrcweir                 const GfxLink aGfxLink( ( (Graphic&) rGraphic ).GetLink() );
202cdf0e10cSrcweir 
203cdf0e10cSrcweir                 switch( aGfxLink.GetType() )
204cdf0e10cSrcweir                 {
205cdf0e10cSrcweir                     case( GFX_LINK_TYPE_NATIVE_GIF ): aExt = FORMAT_GIF; break;
206*72583341SArmin Le Grand 
207*72583341SArmin Le Grand                     // #15508# added BMP type for better exports (no call/trigger found, prob used in HTML export)
208*72583341SArmin Le Grand                     case( GFX_LINK_TYPE_NATIVE_BMP ): aExt = FORMAT_BMP; break;
209*72583341SArmin Le Grand 
210cdf0e10cSrcweir                     case( GFX_LINK_TYPE_NATIVE_JPG ): aExt = FORMAT_JPG; break;
211cdf0e10cSrcweir                     case( GFX_LINK_TYPE_NATIVE_PNG ): aExt = FORMAT_PNG; break;
212cdf0e10cSrcweir 
213cdf0e10cSrcweir                     default:
214cdf0e10cSrcweir                     break;
215cdf0e10cSrcweir                 }
216cdf0e10cSrcweir 
217cdf0e10cSrcweir                 if( aExt.Len() )
218cdf0e10cSrcweir                 {
219cdf0e10cSrcweir                     if( 0 == (nFlags & XOUTBMP_DONT_ADD_EXTENSION))
220cdf0e10cSrcweir                         aURL.setExtension( aExt );
221cdf0e10cSrcweir                     rFileName = aURL.GetMainURL( INetURLObject::NO_DECODE );
222cdf0e10cSrcweir 
223cdf0e10cSrcweir                     SfxMedium   aMedium( aURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_WRITE | STREAM_SHARE_DENYNONE | STREAM_TRUNC, sal_True );
224cdf0e10cSrcweir                     SvStream*   pOStm = aMedium.GetOutStream();
225cdf0e10cSrcweir 
226cdf0e10cSrcweir                     if( pOStm && aGfxLink.GetDataSize() && aGfxLink.GetData() )
227cdf0e10cSrcweir                     {
228cdf0e10cSrcweir                         pOStm->Write( aGfxLink.GetData(), aGfxLink.GetDataSize() );
229cdf0e10cSrcweir                         aMedium.Commit();
230cdf0e10cSrcweir 
231cdf0e10cSrcweir                         if( !aMedium.GetError() )
232cdf0e10cSrcweir                             nErr = GRFILTER_OK;
233cdf0e10cSrcweir                     }
234cdf0e10cSrcweir                 }
235cdf0e10cSrcweir             }
23661472d26SArmin Le Grand         }
237cdf0e10cSrcweir 
238cdf0e10cSrcweir         if( GRFILTER_OK != nErr )
239cdf0e10cSrcweir         {
240cdf0e10cSrcweir             String  aFilter( rFilterName );
241cdf0e10cSrcweir             sal_Bool    bWriteTransGrf = ( aFilter.EqualsIgnoreCaseAscii( "transgrf" ) ) ||
242cdf0e10cSrcweir                                      ( aFilter.EqualsIgnoreCaseAscii( "gif" ) ) ||
243cdf0e10cSrcweir                                      ( nFlags & XOUTBMP_USE_GIF_IF_POSSIBLE ) ||
244cdf0e10cSrcweir                                      ( ( nFlags & XOUTBMP_USE_GIF_IF_SENSIBLE ) && ( bAnimated || bTransparent ) );
245cdf0e10cSrcweir 
246cdf0e10cSrcweir             // get filter and extension
247cdf0e10cSrcweir             if( bWriteTransGrf )
248cdf0e10cSrcweir                 aFilter = FORMAT_GIF;
249cdf0e10cSrcweir 
250cdf0e10cSrcweir             nFilter = pFilter->GetExportFormatNumberForShortName( aFilter );
251cdf0e10cSrcweir 
252cdf0e10cSrcweir             if( GRFILTER_FORMAT_NOTFOUND == nFilter )
253cdf0e10cSrcweir             {
254cdf0e10cSrcweir                 nFilter = pFilter->GetExportFormatNumberForShortName( FORMAT_JPG );
255cdf0e10cSrcweir 
256cdf0e10cSrcweir                 if( GRFILTER_FORMAT_NOTFOUND == nFilter )
257cdf0e10cSrcweir                     nFilter = pFilter->GetExportFormatNumberForShortName( FORMAT_BMP );
258cdf0e10cSrcweir             }
259cdf0e10cSrcweir 
260cdf0e10cSrcweir             if( GRFILTER_FORMAT_NOTFOUND != nFilter )
261cdf0e10cSrcweir             {
262cdf0e10cSrcweir                 aExt = pFilter->GetExportFormatShortName( nFilter ).ToLowerAscii();
263cdf0e10cSrcweir 
264cdf0e10cSrcweir                 if( bWriteTransGrf )
265cdf0e10cSrcweir                 {
266cdf0e10cSrcweir                     if( bAnimated  )
267cdf0e10cSrcweir                         aGraphic = rGraphic;
268cdf0e10cSrcweir                     else
269cdf0e10cSrcweir                     {
270cdf0e10cSrcweir                         if( pMtfSize_100TH_MM && ( rGraphic.GetType() != GRAPHIC_BITMAP ) )
271cdf0e10cSrcweir                         {
272cdf0e10cSrcweir                             VirtualDevice aVDev;
273cdf0e10cSrcweir                             const Size    aSize( aVDev.LogicToPixel( *pMtfSize_100TH_MM, MAP_100TH_MM ) );
274cdf0e10cSrcweir 
275cdf0e10cSrcweir                             if( aVDev.SetOutputSizePixel( aSize ) )
276cdf0e10cSrcweir                             {
277cdf0e10cSrcweir                                 const Wallpaper aWallpaper( aVDev.GetBackground() );
278cdf0e10cSrcweir                                 const Point     aPt;
279cdf0e10cSrcweir 
280cdf0e10cSrcweir                                 aVDev.SetBackground( Wallpaper( Color( COL_BLACK ) ) );
281cdf0e10cSrcweir                                 aVDev.Erase();
282cdf0e10cSrcweir                                 rGraphic.Draw( &aVDev, aPt, aSize );
283cdf0e10cSrcweir 
284cdf0e10cSrcweir                                 const Bitmap aBitmap( aVDev.GetBitmap( aPt, aSize ) );
285cdf0e10cSrcweir 
286cdf0e10cSrcweir                                 aVDev.SetBackground( aWallpaper );
287cdf0e10cSrcweir                                 aVDev.Erase();
288cdf0e10cSrcweir                                 rGraphic.Draw( &aVDev, aPt, aSize );
289cdf0e10cSrcweir 
290cdf0e10cSrcweir                                 aVDev.SetRasterOp( ROP_XOR );
291cdf0e10cSrcweir                                 aVDev.DrawBitmap( aPt, aSize, aBitmap );
292cdf0e10cSrcweir                                 aGraphic = BitmapEx( aBitmap, aVDev.GetBitmap( aPt, aSize ) );
293cdf0e10cSrcweir                             }
294cdf0e10cSrcweir                             else
295cdf0e10cSrcweir                                 aGraphic = rGraphic.GetBitmapEx();
296cdf0e10cSrcweir                         }
297cdf0e10cSrcweir                         else
298cdf0e10cSrcweir                             aGraphic = rGraphic.GetBitmapEx();
299cdf0e10cSrcweir                     }
300cdf0e10cSrcweir                 }
301cdf0e10cSrcweir                 else
302cdf0e10cSrcweir                 {
303cdf0e10cSrcweir                     if( pMtfSize_100TH_MM && ( rGraphic.GetType() != GRAPHIC_BITMAP ) )
304cdf0e10cSrcweir                     {
305cdf0e10cSrcweir                         VirtualDevice   aVDev;
306cdf0e10cSrcweir                         const Size      aSize( aVDev.LogicToPixel( *pMtfSize_100TH_MM, MAP_100TH_MM ) );
307cdf0e10cSrcweir 
308cdf0e10cSrcweir                         if( aVDev.SetOutputSizePixel( aSize ) )
309cdf0e10cSrcweir                         {
310cdf0e10cSrcweir                             rGraphic.Draw( &aVDev, Point(), aSize );
311cdf0e10cSrcweir                             aGraphic =  aVDev.GetBitmap( Point(), aSize );
312cdf0e10cSrcweir                         }
313cdf0e10cSrcweir                         else
314cdf0e10cSrcweir                             aGraphic = rGraphic.GetBitmap();
315cdf0e10cSrcweir                     }
316cdf0e10cSrcweir                     else
317cdf0e10cSrcweir                         aGraphic = rGraphic.GetBitmap();
318cdf0e10cSrcweir                 }
319cdf0e10cSrcweir 
320cdf0e10cSrcweir                 // mirror?
321cdf0e10cSrcweir                 if( ( nFlags & XOUTBMP_MIRROR_HORZ ) || ( nFlags & XOUTBMP_MIRROR_VERT ) )
322cdf0e10cSrcweir                     aGraphic = MirrorGraphic( aGraphic, nFlags );
323cdf0e10cSrcweir 
324cdf0e10cSrcweir                 if( ( GRFILTER_FORMAT_NOTFOUND != nFilter ) && ( aGraphic.GetType() != GRAPHIC_NONE ) )
325cdf0e10cSrcweir                 {
326cdf0e10cSrcweir                     if( 0 == (nFlags & XOUTBMP_DONT_ADD_EXTENSION))
327cdf0e10cSrcweir                         aURL.setExtension( aExt );
328cdf0e10cSrcweir                     rFileName = aURL.GetMainURL( INetURLObject::NO_DECODE );
329cdf0e10cSrcweir                     nErr = ExportGraphic( aGraphic, aURL, *pFilter, nFilter, NULL );
330cdf0e10cSrcweir                 }
331cdf0e10cSrcweir             }
332cdf0e10cSrcweir         }
333cdf0e10cSrcweir 
334cdf0e10cSrcweir         return nErr;
335cdf0e10cSrcweir     }
336cdf0e10cSrcweir     else
337cdf0e10cSrcweir     {
338cdf0e10cSrcweir         return GRFILTER_OK;
339cdf0e10cSrcweir     }
340cdf0e10cSrcweir }
341cdf0e10cSrcweir 
342cdf0e10cSrcweir // ------------------------------------------------------------------------
343cdf0e10cSrcweir 
344cdf0e10cSrcweir #ifdef _MSC_VER
345cdf0e10cSrcweir #pragma optimize ( "", off )
346cdf0e10cSrcweir #endif
347cdf0e10cSrcweir 
ExportGraphic(const Graphic & rGraphic,const INetURLObject & rURL,GraphicFilter & rFilter,const sal_uInt16 nFormat,const com::sun::star::uno::Sequence<com::sun::star::beans::PropertyValue> * pFilterData)348cdf0e10cSrcweir sal_uInt16 XOutBitmap::ExportGraphic( const Graphic& rGraphic, const INetURLObject& rURL,
349cdf0e10cSrcweir                                   GraphicFilter& rFilter, const sal_uInt16 nFormat,
350cdf0e10cSrcweir                                   const com::sun::star::uno::Sequence< com::sun::star::beans::PropertyValue >* pFilterData )
351cdf0e10cSrcweir {
352cdf0e10cSrcweir     DBG_ASSERT( rURL.GetProtocol() != INET_PROT_NOT_VALID, "XOutBitmap::ExportGraphic(...): invalid URL" );
353cdf0e10cSrcweir 
354cdf0e10cSrcweir     SfxMedium   aMedium( rURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_WRITE | STREAM_SHARE_DENYNONE | STREAM_TRUNC, sal_True );
355cdf0e10cSrcweir     SvStream*   pOStm = aMedium.GetOutStream();
356cdf0e10cSrcweir     sal_uInt16      nRet = GRFILTER_IOERROR;
357cdf0e10cSrcweir 
358cdf0e10cSrcweir     if( pOStm )
359cdf0e10cSrcweir     {
360cdf0e10cSrcweir         pGrfFilter = &rFilter;
361cdf0e10cSrcweir 
362cdf0e10cSrcweir         nRet = rFilter.ExportGraphic( rGraphic, rURL.GetMainURL( INetURLObject::NO_DECODE ), *pOStm, nFormat, pFilterData );
363cdf0e10cSrcweir 
364cdf0e10cSrcweir         pGrfFilter = NULL;
365cdf0e10cSrcweir         aMedium.Commit();
366cdf0e10cSrcweir 
367cdf0e10cSrcweir         if( aMedium.GetError() && ( GRFILTER_OK == nRet  ) )
368cdf0e10cSrcweir             nRet = GRFILTER_IOERROR;
369cdf0e10cSrcweir     }
370cdf0e10cSrcweir 
371cdf0e10cSrcweir     return nRet;
372cdf0e10cSrcweir }
373cdf0e10cSrcweir 
374cdf0e10cSrcweir #ifdef _MSC_VER
375cdf0e10cSrcweir #pragma optimize ( "", on )
376cdf0e10cSrcweir #endif
377cdf0e10cSrcweir 
378cdf0e10cSrcweir // ------------------------------------------------------------------------
379cdf0e10cSrcweir 
DetectEdges(const Bitmap & rBmp,const sal_uInt8 cThreshold)380cdf0e10cSrcweir Bitmap XOutBitmap::DetectEdges( const Bitmap& rBmp, const sal_uInt8 cThreshold )
381cdf0e10cSrcweir {
382cdf0e10cSrcweir     const Size  aSize( rBmp.GetSizePixel() );
383cdf0e10cSrcweir     Bitmap      aRetBmp;
384cdf0e10cSrcweir     sal_Bool        bRet = sal_False;
385cdf0e10cSrcweir 
386cdf0e10cSrcweir     if( ( aSize.Width() > 2L ) && ( aSize.Height() > 2L ) )
387cdf0e10cSrcweir     {
388cdf0e10cSrcweir         Bitmap aWorkBmp( rBmp );
389cdf0e10cSrcweir 
390cdf0e10cSrcweir         if( aWorkBmp.Convert( BMP_CONVERSION_8BIT_GREYS ) )
391cdf0e10cSrcweir         {
392cdf0e10cSrcweir             Bitmap              aDstBmp( aSize, 1 );
393cdf0e10cSrcweir             BitmapReadAccess*   pReadAcc = aWorkBmp.AcquireReadAccess();
394cdf0e10cSrcweir             BitmapWriteAccess*  pWriteAcc = aDstBmp.AcquireWriteAccess();
395cdf0e10cSrcweir 
396cdf0e10cSrcweir             if( pReadAcc && pWriteAcc )
397cdf0e10cSrcweir             {
398cdf0e10cSrcweir                 const long          nWidth = aSize.Width();
399cdf0e10cSrcweir                 const long          nWidth2 = nWidth - 2L;
400cdf0e10cSrcweir                 const long          nHeight = aSize.Height();
401cdf0e10cSrcweir                 const long          nHeight2 = nHeight - 2L;
402cdf0e10cSrcweir                 const long          lThres2 = (long) cThreshold * cThreshold;
40345fd3b9aSArmin Le Grand                 const sal_uInt8 nWhitePalIdx(static_cast< sal_uInt8 >(pWriteAcc->GetBestPaletteIndex(Color(COL_WHITE))));
40445fd3b9aSArmin Le Grand                 const sal_uInt8 nBlackPalIdx(static_cast< sal_uInt8 >(pWriteAcc->GetBestPaletteIndex(Color(COL_BLACK))));
405cdf0e10cSrcweir                 long                nSum1;
406cdf0e10cSrcweir                 long                nSum2;
407cdf0e10cSrcweir                 long                lGray;
408cdf0e10cSrcweir 
40987bc88d3SHerbert Dürr                 // initialize border with white pixels
410cdf0e10cSrcweir                 pWriteAcc->SetLineColor( Color( COL_WHITE) );
411cdf0e10cSrcweir                 pWriteAcc->DrawLine( Point(), Point( nWidth - 1L, 0L ) );
412cdf0e10cSrcweir                 pWriteAcc->DrawLine( Point( nWidth - 1L, 0L ), Point( nWidth - 1L, nHeight - 1L ) );
413cdf0e10cSrcweir                 pWriteAcc->DrawLine( Point( nWidth - 1L, nHeight - 1L ), Point( 0L, nHeight - 1L ) );
414cdf0e10cSrcweir                 pWriteAcc->DrawLine( Point( 0, nHeight - 1L ), Point() );
415cdf0e10cSrcweir 
416cdf0e10cSrcweir                 for( long nY = 0L, nY1 = 1L, nY2 = 2; nY < nHeight2; nY++, nY1++, nY2++ )
417cdf0e10cSrcweir                 {
418cdf0e10cSrcweir                     for( long nX = 0L, nXDst = 1L, nXTmp; nX < nWidth2; nX++, nXDst++ )
419cdf0e10cSrcweir                     {
420cdf0e10cSrcweir                         nXTmp = nX;
421cdf0e10cSrcweir 
42287bc88d3SHerbert Dürr                         nSum1 = -( nSum2 = lGray = pReadAcc->GetPixelIndex( nY, nXTmp++ ) );
42387bc88d3SHerbert Dürr                         nSum2 += ( (long) pReadAcc->GetPixelIndex( nY, nXTmp++ ) ) << 1;
42487bc88d3SHerbert Dürr                         nSum1 += ( lGray = pReadAcc->GetPixelIndex( nY, nXTmp ) );
425cdf0e10cSrcweir                         nSum2 += lGray;
426cdf0e10cSrcweir 
42787bc88d3SHerbert Dürr                         nSum1 += ( (long) pReadAcc->GetPixelIndex( nY1, nXTmp ) ) << 1;
42887bc88d3SHerbert Dürr                         nSum1 -= ( (long) pReadAcc->GetPixelIndex( nY1, nXTmp -= 2 ) ) << 1;
429cdf0e10cSrcweir 
43087bc88d3SHerbert Dürr                         nSum1 += ( lGray = -(long) pReadAcc->GetPixelIndex( nY2, nXTmp++ ) );
431cdf0e10cSrcweir                         nSum2 += lGray;
43287bc88d3SHerbert Dürr                         nSum2 -= ( (long) pReadAcc->GetPixelIndex( nY2, nXTmp++ ) ) << 1;
43387bc88d3SHerbert Dürr                         nSum1 += ( lGray = (long) pReadAcc->GetPixelIndex( nY2, nXTmp ) );
434cdf0e10cSrcweir                         nSum2 -= lGray;
435cdf0e10cSrcweir 
436cdf0e10cSrcweir                         if( ( nSum1 * nSum1 + nSum2 * nSum2 ) < lThres2 )
437825c776bSPavel Janík                             pWriteAcc->SetPixelIndex( nY1, nXDst, nWhitePalIdx );
438cdf0e10cSrcweir                         else
439825c776bSPavel Janík                             pWriteAcc->SetPixelIndex( nY1, nXDst, nBlackPalIdx );
440cdf0e10cSrcweir                     }
441cdf0e10cSrcweir                 }
442cdf0e10cSrcweir 
443cdf0e10cSrcweir                 bRet = sal_True;
444cdf0e10cSrcweir             }
445cdf0e10cSrcweir 
446cdf0e10cSrcweir             aWorkBmp.ReleaseAccess( pReadAcc );
447cdf0e10cSrcweir             aDstBmp.ReleaseAccess( pWriteAcc );
448cdf0e10cSrcweir 
449cdf0e10cSrcweir             if( bRet )
450cdf0e10cSrcweir                 aRetBmp = aDstBmp;
451cdf0e10cSrcweir         }
452cdf0e10cSrcweir     }
453cdf0e10cSrcweir 
454cdf0e10cSrcweir     if( !aRetBmp )
455cdf0e10cSrcweir         aRetBmp = rBmp;
456cdf0e10cSrcweir     else
457cdf0e10cSrcweir     {
458cdf0e10cSrcweir         aRetBmp.SetPrefMapMode( rBmp.GetPrefMapMode() );
459cdf0e10cSrcweir         aRetBmp.SetPrefSize( rBmp.GetPrefSize() );
460cdf0e10cSrcweir     }
461cdf0e10cSrcweir 
462cdf0e10cSrcweir     return aRetBmp;
463cdf0e10cSrcweir };
464cdf0e10cSrcweir 
465cdf0e10cSrcweir // ------------------------------------------------------------------------
466cdf0e10cSrcweir 
GetCountour(const Bitmap & rBmp,const sal_uIntPtr nFlags,const sal_uInt8 cEdgeDetectThreshold,const Rectangle * pWorkRectPixel)467cdf0e10cSrcweir Polygon XOutBitmap::GetCountour( const Bitmap& rBmp, const sal_uIntPtr nFlags,
468cdf0e10cSrcweir                                  const sal_uInt8 cEdgeDetectThreshold, const Rectangle* pWorkRectPixel )
469cdf0e10cSrcweir {
470cdf0e10cSrcweir     Bitmap      aWorkBmp;
471cdf0e10cSrcweir     Polygon     aRetPoly;
472cdf0e10cSrcweir     Point       aTmpPoint;
473cdf0e10cSrcweir     Rectangle   aWorkRect( aTmpPoint, rBmp.GetSizePixel() );
474cdf0e10cSrcweir 
475cdf0e10cSrcweir     if( pWorkRectPixel )
476cdf0e10cSrcweir         aWorkRect.Intersection( *pWorkRectPixel );
477cdf0e10cSrcweir 
478cdf0e10cSrcweir     aWorkRect.Justify();
479cdf0e10cSrcweir 
480cdf0e10cSrcweir     if( ( aWorkRect.GetWidth() > 4 ) && ( aWorkRect.GetHeight() > 4 ) )
481cdf0e10cSrcweir     {
482cdf0e10cSrcweir         // falls Flag gesetzt, muessen wir Kanten detektieren
483cdf0e10cSrcweir         if( nFlags & XOUTBMP_CONTOUR_EDGEDETECT )
484cdf0e10cSrcweir             aWorkBmp = DetectEdges( rBmp, cEdgeDetectThreshold );
485cdf0e10cSrcweir         else
486cdf0e10cSrcweir             aWorkBmp = rBmp;
487cdf0e10cSrcweir 
488cdf0e10cSrcweir         BitmapReadAccess* pAcc = aWorkBmp.AcquireReadAccess();
489cdf0e10cSrcweir 
490cdf0e10cSrcweir         if( pAcc )
491cdf0e10cSrcweir         {
492cdf0e10cSrcweir             const Size&         rPrefSize = aWorkBmp.GetPrefSize();
493cdf0e10cSrcweir             const long          nWidth = pAcc->Width();
494cdf0e10cSrcweir             const long          nHeight = pAcc->Height();
495cdf0e10cSrcweir             const double        fFactorX = (double) rPrefSize.Width() / nWidth;
496cdf0e10cSrcweir             const double        fFactorY = (double) rPrefSize.Height() / nHeight;
497cdf0e10cSrcweir             const long          nStartX1 = aWorkRect.Left() + 1L;
498cdf0e10cSrcweir             const long          nEndX1 = aWorkRect.Right();
499cdf0e10cSrcweir             const long          nStartX2 = nEndX1 - 1L;
500cdf0e10cSrcweir //          const long          nEndX2 = nStartX1 - 1L;
501cdf0e10cSrcweir             const long          nStartY1 = aWorkRect.Top() + 1L;
502cdf0e10cSrcweir             const long          nEndY1 = aWorkRect.Bottom();
503cdf0e10cSrcweir             const long          nStartY2 = nEndY1 - 1L;
504cdf0e10cSrcweir //          const long          nEndY2 = nStartY1 - 1L;
505cdf0e10cSrcweir             Point*              pPoints1 = NULL;
506cdf0e10cSrcweir             Point*              pPoints2 = NULL;
507cdf0e10cSrcweir             long                nX, nY;
508cdf0e10cSrcweir             sal_uInt16              nPolyPos = 0;
509cdf0e10cSrcweir             const BitmapColor   aBlack = pAcc->GetBestMatchingColor( Color( COL_BLACK ) );
510cdf0e10cSrcweir 
511cdf0e10cSrcweir             if( nFlags & XOUTBMP_CONTOUR_VERT )
512cdf0e10cSrcweir             {
513cdf0e10cSrcweir                 pPoints1 = new Point[ nWidth ];
514cdf0e10cSrcweir                 pPoints2 = new Point[ nWidth ];
515cdf0e10cSrcweir 
516cdf0e10cSrcweir                 for( nX = nStartX1; nX < nEndX1; nX++ )
517cdf0e10cSrcweir                 {
518cdf0e10cSrcweir                     nY = nStartY1;
519cdf0e10cSrcweir 
520cdf0e10cSrcweir                     // zunaechst Zeile von Links nach Rechts durchlaufen
521cdf0e10cSrcweir                     while( nY < nEndY1 )
522cdf0e10cSrcweir                     {
523cdf0e10cSrcweir                         if( aBlack == pAcc->GetPixel( nY, nX ) )
524cdf0e10cSrcweir                         {
525cdf0e10cSrcweir                             pPoints1[ nPolyPos ] = Point( nX, nY );
526cdf0e10cSrcweir                             nY = nStartY2;
527cdf0e10cSrcweir 
528cdf0e10cSrcweir                             // diese Schleife wird immer gebreaked da hier ja min. ein Pixel ist
529cdf0e10cSrcweir                             while( sal_True )
530cdf0e10cSrcweir                             {
531cdf0e10cSrcweir                                 if( aBlack == pAcc->GetPixel( nY, nX ) )
532cdf0e10cSrcweir                                 {
533cdf0e10cSrcweir                                     pPoints2[ nPolyPos ] = Point( nX, nY );
534cdf0e10cSrcweir                                     break;
535cdf0e10cSrcweir                                 }
536cdf0e10cSrcweir 
537cdf0e10cSrcweir                                 nY--;
538cdf0e10cSrcweir                             }
539cdf0e10cSrcweir 
540cdf0e10cSrcweir                             nPolyPos++;
541cdf0e10cSrcweir                             break;
542cdf0e10cSrcweir                         }
543cdf0e10cSrcweir 
544cdf0e10cSrcweir                         nY++;
545cdf0e10cSrcweir                     }
546cdf0e10cSrcweir                 }
547cdf0e10cSrcweir             }
548cdf0e10cSrcweir             else
549cdf0e10cSrcweir             {
550cdf0e10cSrcweir                 pPoints1 = new Point[ nHeight ];
551cdf0e10cSrcweir                 pPoints2 = new Point[ nHeight ];
552cdf0e10cSrcweir 
553cdf0e10cSrcweir                 for ( nY = nStartY1; nY < nEndY1; nY++ )
554cdf0e10cSrcweir                 {
555cdf0e10cSrcweir                     nX = nStartX1;
556cdf0e10cSrcweir 
557cdf0e10cSrcweir                     // zunaechst Zeile von Links nach Rechts durchlaufen
558cdf0e10cSrcweir                     while( nX < nEndX1 )
559cdf0e10cSrcweir                     {
560cdf0e10cSrcweir                         if( aBlack == pAcc->GetPixel( nY, nX ) )
561cdf0e10cSrcweir                         {
562cdf0e10cSrcweir                             pPoints1[ nPolyPos ] = Point( nX, nY );
563cdf0e10cSrcweir                             nX = nStartX2;
564cdf0e10cSrcweir 
565cdf0e10cSrcweir                             // diese Schleife wird immer gebreaked da hier ja min. ein Pixel ist
566cdf0e10cSrcweir                             while( sal_True )
567cdf0e10cSrcweir                             {
568cdf0e10cSrcweir                                 if( aBlack == pAcc->GetPixel( nY, nX ) )
569cdf0e10cSrcweir                                 {
570cdf0e10cSrcweir                                     pPoints2[ nPolyPos ] = Point( nX, nY );
571cdf0e10cSrcweir                                     break;
572cdf0e10cSrcweir                                 }
573cdf0e10cSrcweir 
574cdf0e10cSrcweir                                 nX--;
575cdf0e10cSrcweir                             }
576cdf0e10cSrcweir 
577cdf0e10cSrcweir                             nPolyPos++;
578cdf0e10cSrcweir                             break;
579cdf0e10cSrcweir                         }
580cdf0e10cSrcweir 
581cdf0e10cSrcweir                         nX++;
582cdf0e10cSrcweir                     }
583cdf0e10cSrcweir                 }
584cdf0e10cSrcweir             }
585cdf0e10cSrcweir 
586cdf0e10cSrcweir             const sal_uInt16 nNewSize1 = nPolyPos << 1;
587cdf0e10cSrcweir 
588cdf0e10cSrcweir             aRetPoly = Polygon( nPolyPos, pPoints1 );
589cdf0e10cSrcweir             aRetPoly.SetSize( nNewSize1 + 1 );
590cdf0e10cSrcweir             aRetPoly[ nNewSize1 ] = aRetPoly[ 0 ];
591cdf0e10cSrcweir 
592cdf0e10cSrcweir             for( sal_uInt16 j = nPolyPos; nPolyPos < nNewSize1; )
593cdf0e10cSrcweir                 aRetPoly[ nPolyPos++ ] = pPoints2[ --j ];
594cdf0e10cSrcweir 
595cdf0e10cSrcweir             if( ( fFactorX != 0. ) && ( fFactorY != 0. ) )
596cdf0e10cSrcweir                 aRetPoly.Scale( fFactorX, fFactorY );
597cdf0e10cSrcweir 
598cdf0e10cSrcweir             delete[] pPoints1;
599cdf0e10cSrcweir             delete[] pPoints2;
600cdf0e10cSrcweir         }
601cdf0e10cSrcweir     }
602cdf0e10cSrcweir 
603cdf0e10cSrcweir     return aRetPoly;
604cdf0e10cSrcweir };
605cdf0e10cSrcweir 
606cdf0e10cSrcweir // ----------------
607cdf0e10cSrcweir // - DitherBitmap -
608cdf0e10cSrcweir // ----------------
609cdf0e10cSrcweir 
DitherBitmap(Bitmap & rBitmap)610cdf0e10cSrcweir sal_Bool DitherBitmap( Bitmap& rBitmap )
611cdf0e10cSrcweir {
612cdf0e10cSrcweir     sal_Bool bRet = sal_False;
613cdf0e10cSrcweir 
614cdf0e10cSrcweir     if( ( rBitmap.GetBitCount() >= 8 ) && ( Application::GetDefaultDevice()->GetColorCount() < 257 ) )
615cdf0e10cSrcweir         bRet = rBitmap.Dither( BMP_DITHER_FLOYD );
616cdf0e10cSrcweir     else
617cdf0e10cSrcweir         bRet = sal_False;
618cdf0e10cSrcweir 
619cdf0e10cSrcweir     return bRet;
620cdf0e10cSrcweir }
621