1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <sot/factory.hxx>
22 #include <tools/poly.hxx>
23 #include <vcl/bmpacc.hxx>
24 #include <vcl/virdev.hxx>
25 #include <vcl/wrkwin.hxx>
26 #include <svl/solar.hrc>
27 #include <sfx2/docfile.hxx>
28 #include <sfx2/app.hxx>
29 #include "svx/xoutbmp.hxx"
30 #include <vcl/FilterConfigItem.hxx>
31 #include <vcl/graphicfilter.hxx>
33 #define FORMAT_BMP OUString("bmp")
34 #define FORMAT_GIF OUString("gif")
35 #define FORMAT_JPG OUString("jpg")
36 #define FORMAT_PNG OUString("png")
38 GraphicFilter
* XOutBitmap::pGrfFilter
= NULL
;
40 Animation
XOutBitmap::MirrorAnimation( const Animation
& rAnimation
, sal_Bool bHMirr
, sal_Bool bVMirr
)
42 Animation
aNewAnim( rAnimation
);
44 if( bHMirr
|| bVMirr
)
46 const Size
& rGlobalSize
= aNewAnim
.GetDisplaySizePixel();
47 sal_uIntPtr nMirrorFlags
= 0L;
50 nMirrorFlags
|= BMP_MIRROR_HORZ
;
53 nMirrorFlags
|= BMP_MIRROR_VERT
;
55 for( sal_uInt16 i
= 0, nCount
= aNewAnim
.Count(); i
< nCount
; i
++ )
57 AnimationBitmap
aAnimBmp( aNewAnim
.Get( i
) );
60 aAnimBmp
.aBmpEx
.Mirror( nMirrorFlags
);
62 // Die Positionen innerhalb der Gesamtbitmap
63 // muessen natuerlich auch angepasst werden
65 aAnimBmp
.aPosPix
.X() = rGlobalSize
.Width() - aAnimBmp
.aPosPix
.X() -
66 aAnimBmp
.aSizePix
.Width();
69 aAnimBmp
.aPosPix
.Y() = rGlobalSize
.Height() - aAnimBmp
.aPosPix
.Y() -
70 aAnimBmp
.aSizePix
.Height();
72 aNewAnim
.Replace( aAnimBmp
, i
);
79 Graphic
XOutBitmap::MirrorGraphic( const Graphic
& rGraphic
, const sal_uIntPtr nMirrorFlags
)
85 if( rGraphic
.IsAnimated() )
87 aRetGraphic
= MirrorAnimation( rGraphic
.GetAnimation(),
88 ( nMirrorFlags
& BMP_MIRROR_HORZ
) == BMP_MIRROR_HORZ
,
89 ( nMirrorFlags
& BMP_MIRROR_VERT
) == BMP_MIRROR_VERT
);
93 if( rGraphic
.IsTransparent() )
95 BitmapEx
aBmpEx( rGraphic
.GetBitmapEx() );
97 aBmpEx
.Mirror( nMirrorFlags
);
102 Bitmap
aBmp( rGraphic
.GetBitmap() );
104 aBmp
.Mirror( nMirrorFlags
);
110 aRetGraphic
= rGraphic
;
115 sal_uInt16
XOutBitmap::WriteGraphic( const Graphic
& rGraphic
, String
& rFileName
,
116 const String
& rFilterName
, const sal_uIntPtr nFlags
,
117 const Size
* pMtfSize_100TH_MM
)
119 if( rGraphic
.GetType() != GRAPHIC_NONE
)
121 INetURLObject
aURL( rFileName
);
124 GraphicFilter
& rFilter
= GraphicFilter::GetGraphicFilter();
125 sal_uInt16 nErr
= GRFILTER_FILTERERROR
, nFilter
= GRFILTER_FORMAT_NOTFOUND
;
126 sal_Bool bTransparent
= rGraphic
.IsTransparent(), bAnimated
= rGraphic
.IsAnimated();
128 DBG_ASSERT( aURL
.GetProtocol() != INET_PROT_NOT_VALID
, "XOutBitmap::WriteGraphic(...): invalid URL" );
130 // calculate correct file name
131 if( !( nFlags
& XOUTBMP_DONT_EXPAND_FILENAME
) )
133 String
aName( aURL
.getBase() );
135 aName
+= String(aURL
.getExtension());
137 String
aStr( OUString::number( rGraphic
.GetChecksum(), 16 ) );
138 if ( aStr
.GetChar(0) == '-' )
141 aURL
.setBase( aName
);
144 // #i121128# use shortcut to write SVG data in original form (if possible)
145 const SvgDataPtr
aSvgDataPtr(rGraphic
.getSvgData());
148 && aSvgDataPtr
->getSvgDataArrayLength()
149 && rFilterName
.EqualsIgnoreCaseAscii("svg"))
151 if(!(nFlags
& XOUTBMP_DONT_ADD_EXTENSION
))
153 aURL
.setExtension(rFilterName
);
156 rFileName
= aURL
.GetMainURL(INetURLObject::NO_DECODE
);
157 SfxMedium
aMedium(aURL
.GetMainURL(INetURLObject::NO_DECODE
), STREAM_WRITE
|STREAM_SHARE_DENYNONE
|STREAM_TRUNC
);
158 SvStream
* pOStm
= aMedium
.GetOutStream();
162 pOStm
->Write(aSvgDataPtr
->getSvgDataArray().get(), aSvgDataPtr
->getSvgDataArrayLength());
165 if(!aMedium
.GetError())
172 if( GRFILTER_OK
!= nErr
)
174 if( ( nFlags
& XOUTBMP_USE_NATIVE_IF_POSSIBLE
) &&
175 !( nFlags
& XOUTBMP_MIRROR_HORZ
) &&
176 !( nFlags
& XOUTBMP_MIRROR_VERT
) &&
177 ( rGraphic
.GetType() != GRAPHIC_GDIMETAFILE
) && rGraphic
.IsLink() )
179 // try to write native link
180 const GfxLink
aGfxLink( ( (Graphic
&) rGraphic
).GetLink() );
182 switch( aGfxLink
.GetType() )
184 case( GFX_LINK_TYPE_NATIVE_GIF
): aExt
= FORMAT_GIF
; break;
185 case( GFX_LINK_TYPE_NATIVE_JPG
): aExt
= FORMAT_JPG
; break;
186 case( GFX_LINK_TYPE_NATIVE_PNG
): aExt
= FORMAT_PNG
; break;
194 if( 0 == (nFlags
& XOUTBMP_DONT_ADD_EXTENSION
))
195 aURL
.setExtension( aExt
);
196 rFileName
= aURL
.GetMainURL( INetURLObject::NO_DECODE
);
198 SfxMedium
aMedium(aURL
.GetMainURL(INetURLObject::NO_DECODE
), STREAM_WRITE
| STREAM_SHARE_DENYNONE
| STREAM_TRUNC
);
199 SvStream
* pOStm
= aMedium
.GetOutStream();
201 if( pOStm
&& aGfxLink
.GetDataSize() && aGfxLink
.GetData() )
203 pOStm
->Write( aGfxLink
.GetData(), aGfxLink
.GetDataSize() );
206 if( !aMedium
.GetError() )
213 if( GRFILTER_OK
!= nErr
)
215 String
aFilter( rFilterName
);
216 bool bWriteTransGrf
= ( aFilter
.EqualsIgnoreCaseAscii( "transgrf" ) ) ||
217 ( aFilter
.EqualsIgnoreCaseAscii( "gif" ) ) ||
218 ( nFlags
& XOUTBMP_USE_GIF_IF_POSSIBLE
) ||
219 ( ( nFlags
& XOUTBMP_USE_GIF_IF_SENSIBLE
) && ( bAnimated
|| bTransparent
) );
221 // get filter and extension
223 aFilter
= FORMAT_GIF
;
225 nFilter
= rFilter
.GetExportFormatNumberForShortName( aFilter
);
227 if( GRFILTER_FORMAT_NOTFOUND
== nFilter
)
229 nFilter
= rFilter
.GetExportFormatNumberForShortName( FORMAT_JPG
);
231 if( GRFILTER_FORMAT_NOTFOUND
== nFilter
)
232 nFilter
= rFilter
.GetExportFormatNumberForShortName( FORMAT_BMP
);
235 if( GRFILTER_FORMAT_NOTFOUND
!= nFilter
)
237 aExt
= rFilter
.GetExportFormatShortName( nFilter
).ToLowerAscii();
245 if( pMtfSize_100TH_MM
&& ( rGraphic
.GetType() != GRAPHIC_BITMAP
) )
248 const Size
aSize( aVDev
.LogicToPixel( *pMtfSize_100TH_MM
, MAP_100TH_MM
) );
250 if( aVDev
.SetOutputSizePixel( aSize
) )
252 const Wallpaper
aWallpaper( aVDev
.GetBackground() );
255 aVDev
.SetBackground( Wallpaper( Color( COL_BLACK
) ) );
257 rGraphic
.Draw( &aVDev
, aPt
, aSize
);
259 const Bitmap
aBitmap( aVDev
.GetBitmap( aPt
, aSize
) );
261 aVDev
.SetBackground( aWallpaper
);
263 rGraphic
.Draw( &aVDev
, aPt
, aSize
);
265 aVDev
.SetRasterOp( ROP_XOR
);
266 aVDev
.DrawBitmap( aPt
, aSize
, aBitmap
);
267 aGraphic
= BitmapEx( aBitmap
, aVDev
.GetBitmap( aPt
, aSize
) );
270 aGraphic
= rGraphic
.GetBitmapEx();
273 aGraphic
= rGraphic
.GetBitmapEx();
278 if( pMtfSize_100TH_MM
&& ( rGraphic
.GetType() != GRAPHIC_BITMAP
) )
281 const Size
aSize( aVDev
.LogicToPixel( *pMtfSize_100TH_MM
, MAP_100TH_MM
) );
283 if( aVDev
.SetOutputSizePixel( aSize
) )
285 rGraphic
.Draw( &aVDev
, Point(), aSize
);
286 aGraphic
= aVDev
.GetBitmap( Point(), aSize
);
289 aGraphic
= rGraphic
.GetBitmap();
292 aGraphic
= rGraphic
.GetBitmap();
296 if( ( nFlags
& XOUTBMP_MIRROR_HORZ
) || ( nFlags
& XOUTBMP_MIRROR_VERT
) )
297 aGraphic
= MirrorGraphic( aGraphic
, nFlags
);
299 if( ( GRFILTER_FORMAT_NOTFOUND
!= nFilter
) && ( aGraphic
.GetType() != GRAPHIC_NONE
) )
301 if( 0 == (nFlags
& XOUTBMP_DONT_ADD_EXTENSION
))
302 aURL
.setExtension( aExt
);
303 rFileName
= aURL
.GetMainURL( INetURLObject::NO_DECODE
);
304 nErr
= ExportGraphic( aGraphic
, aURL
, rFilter
, nFilter
, NULL
);
317 sal_uInt16
XOutBitmap::ExportGraphic( const Graphic
& rGraphic
, const INetURLObject
& rURL
,
318 GraphicFilter
& rFilter
, const sal_uInt16 nFormat
,
319 const com::sun::star::uno::Sequence
< com::sun::star::beans::PropertyValue
>* pFilterData
)
321 DBG_ASSERT( rURL
.GetProtocol() != INET_PROT_NOT_VALID
, "XOutBitmap::ExportGraphic(...): invalid URL" );
323 SfxMedium
aMedium( rURL
.GetMainURL( INetURLObject::NO_DECODE
), STREAM_WRITE
| STREAM_SHARE_DENYNONE
| STREAM_TRUNC
);
324 SvStream
* pOStm
= aMedium
.GetOutStream();
325 sal_uInt16 nRet
= GRFILTER_IOERROR
;
329 pGrfFilter
= &rFilter
;
331 nRet
= rFilter
.ExportGraphic( rGraphic
, rURL
.GetMainURL( INetURLObject::NO_DECODE
), *pOStm
, nFormat
, pFilterData
);
336 if( aMedium
.GetError() && ( GRFILTER_OK
== nRet
) )
337 nRet
= GRFILTER_IOERROR
;
343 Bitmap
XOutBitmap::DetectEdges( const Bitmap
& rBmp
, const sal_uInt8 cThreshold
)
345 const Size
aSize( rBmp
.GetSizePixel() );
349 if( ( aSize
.Width() > 2L ) && ( aSize
.Height() > 2L ) )
351 Bitmap
aWorkBmp( rBmp
);
353 if( aWorkBmp
.Convert( BMP_CONVERSION_8BIT_GREYS
) )
355 Bitmap
aDstBmp( aSize
, 1 );
356 BitmapReadAccess
* pReadAcc
= aWorkBmp
.AcquireReadAccess();
357 BitmapWriteAccess
* pWriteAcc
= aDstBmp
.AcquireWriteAccess();
359 if( pReadAcc
&& pWriteAcc
)
361 const long nWidth
= aSize
.Width();
362 const long nWidth2
= nWidth
- 2L;
363 const long nHeight
= aSize
.Height();
364 const long nHeight2
= nHeight
- 2L;
365 const long lThres2
= (long) cThreshold
* cThreshold
;
366 const sal_uInt8 nWhitePalIdx
= pWriteAcc
->GetBestPaletteIndex( Color( COL_WHITE
) );
367 const sal_uInt8 nBlackPalIdx
= pWriteAcc
->GetBestPaletteIndex( Color( COL_BLACK
) );
372 // initialize border with white pixels
373 pWriteAcc
->SetLineColor( Color( COL_WHITE
) );
374 pWriteAcc
->DrawLine( Point(), Point( nWidth
- 1L, 0L ) );
375 pWriteAcc
->DrawLine( Point( nWidth
- 1L, 0L ), Point( nWidth
- 1L, nHeight
- 1L ) );
376 pWriteAcc
->DrawLine( Point( nWidth
- 1L, nHeight
- 1L ), Point( 0L, nHeight
- 1L ) );
377 pWriteAcc
->DrawLine( Point( 0, nHeight
- 1L ), Point() );
379 for( long nY
= 0L, nY1
= 1L, nY2
= 2; nY
< nHeight2
; nY
++, nY1
++, nY2
++ )
381 for( long nX
= 0L, nXDst
= 1L, nXTmp
; nX
< nWidth2
; nX
++, nXDst
++ )
385 nSum1
= -( nSum2
= lGray
= pReadAcc
->GetPixelIndex( nY
, nXTmp
++ ) );
386 nSum2
+= ( (long) pReadAcc
->GetPixelIndex( nY
, nXTmp
++ ) ) << 1;
387 nSum1
+= ( lGray
= pReadAcc
->GetPixelIndex( nY
, nXTmp
) );
390 nSum1
+= ( (long) pReadAcc
->GetPixelIndex( nY1
, nXTmp
) ) << 1;
391 nSum1
-= ( (long) pReadAcc
->GetPixelIndex( nY1
, nXTmp
-= 2 ) ) << 1;
393 nSum1
+= ( lGray
= -(long) pReadAcc
->GetPixelIndex( nY2
, nXTmp
++ ) );
395 nSum2
-= ( (long) pReadAcc
->GetPixelIndex( nY2
, nXTmp
++ ) ) << 1;
396 nSum1
+= ( lGray
= (long) pReadAcc
->GetPixelIndex( nY2
, nXTmp
) );
399 if( ( nSum1
* nSum1
+ nSum2
* nSum2
) < lThres2
)
400 pWriteAcc
->SetPixelIndex( nY1
, nXDst
, nWhitePalIdx
);
402 pWriteAcc
->SetPixelIndex( nY1
, nXDst
, nBlackPalIdx
);
409 aWorkBmp
.ReleaseAccess( pReadAcc
);
410 aDstBmp
.ReleaseAccess( pWriteAcc
);
421 aRetBmp
.SetPrefMapMode( rBmp
.GetPrefMapMode() );
422 aRetBmp
.SetPrefSize( rBmp
.GetPrefSize() );
428 Polygon
XOutBitmap::GetCountour( const Bitmap
& rBmp
, const sal_uIntPtr nFlags
,
429 const sal_uInt8 cEdgeDetectThreshold
, const Rectangle
* pWorkRectPixel
)
434 Rectangle
aWorkRect( aTmpPoint
, rBmp
.GetSizePixel() );
437 aWorkRect
.Intersection( *pWorkRectPixel
);
441 if( ( aWorkRect
.GetWidth() > 4 ) && ( aWorkRect
.GetHeight() > 4 ) )
443 // falls Flag gesetzt, muessen wir Kanten detektieren
444 if( nFlags
& XOUTBMP_CONTOUR_EDGEDETECT
)
445 aWorkBmp
= DetectEdges( rBmp
, cEdgeDetectThreshold
);
449 BitmapReadAccess
* pAcc
= aWorkBmp
.AcquireReadAccess();
453 const Size
& rPrefSize
= aWorkBmp
.GetPrefSize();
454 const long nWidth
= pAcc
->Width();
455 const long nHeight
= pAcc
->Height();
456 const double fFactorX
= (double) rPrefSize
.Width() / nWidth
;
457 const double fFactorY
= (double) rPrefSize
.Height() / nHeight
;
458 const long nStartX1
= aWorkRect
.Left() + 1L;
459 const long nEndX1
= aWorkRect
.Right();
460 const long nStartX2
= nEndX1
- 1L;
461 // const long nEndX2 = nStartX1 - 1L;
462 const long nStartY1
= aWorkRect
.Top() + 1L;
463 const long nEndY1
= aWorkRect
.Bottom();
464 const long nStartY2
= nEndY1
- 1L;
465 // const long nEndY2 = nStartY1 - 1L;
466 Point
* pPoints1
= NULL
;
467 Point
* pPoints2
= NULL
;
469 sal_uInt16 nPolyPos
= 0;
470 const BitmapColor aBlack
= pAcc
->GetBestMatchingColor( Color( COL_BLACK
) );
472 if( nFlags
& XOUTBMP_CONTOUR_VERT
)
474 pPoints1
= new Point
[ nWidth
];
475 pPoints2
= new Point
[ nWidth
];
477 for( nX
= nStartX1
; nX
< nEndX1
; nX
++ )
481 // zunaechst Zeile von Links nach Rechts durchlaufen
484 if( aBlack
== pAcc
->GetPixel( nY
, nX
) )
486 pPoints1
[ nPolyPos
] = Point( nX
, nY
);
489 // diese Schleife wird immer gebreaked da hier ja min. ein Pixel ist
492 if( aBlack
== pAcc
->GetPixel( nY
, nX
) )
494 pPoints2
[ nPolyPos
] = Point( nX
, nY
);
511 pPoints1
= new Point
[ nHeight
];
512 pPoints2
= new Point
[ nHeight
];
514 for ( nY
= nStartY1
; nY
< nEndY1
; nY
++ )
518 // zunaechst Zeile von Links nach Rechts durchlaufen
521 if( aBlack
== pAcc
->GetPixel( nY
, nX
) )
523 pPoints1
[ nPolyPos
] = Point( nX
, nY
);
526 // diese Schleife wird immer gebreaked da hier ja min. ein Pixel ist
529 if( aBlack
== pAcc
->GetPixel( nY
, nX
) )
531 pPoints2
[ nPolyPos
] = Point( nX
, nY
);
547 const sal_uInt16 nNewSize1
= nPolyPos
<< 1;
549 aRetPoly
= Polygon( nPolyPos
, pPoints1
);
550 aRetPoly
.SetSize( nNewSize1
+ 1 );
551 aRetPoly
[ nNewSize1
] = aRetPoly
[ 0 ];
553 for( sal_uInt16 j
= nPolyPos
; nPolyPos
< nNewSize1
; )
554 aRetPoly
[ nPolyPos
++ ] = pPoints2
[ --j
];
556 if( ( fFactorX
!= 0. ) && ( fFactorY
!= 0. ) )
557 aRetPoly
.Scale( fFactorX
, fFactorY
);
567 sal_Bool
DitherBitmap( Bitmap
& rBitmap
)
569 sal_Bool bRet
= sal_False
;
571 if( ( rBitmap
.GetBitCount() >= 8 ) && ( Application::GetDefaultDevice()->GetColorCount() < 257 ) )
572 bRet
= rBitmap
.Dither( BMP_DITHER_FLOYD
);
579 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */