fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / svx / source / xoutdev / _xoutbmp.cxx
blob33e36ee0fd10ef0310a49c7174f6bf321c978854
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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;
49 if( bHMirr )
50 nMirrorFlags |= BMP_MIRROR_HORZ;
52 if( bVMirr )
53 nMirrorFlags |= BMP_MIRROR_VERT;
55 for( sal_uInt16 i = 0, nCount = aNewAnim.Count(); i < nCount; i++ )
57 AnimationBitmap aAnimBmp( aNewAnim.Get( i ) );
59 // BitmapEx spiegeln
60 aAnimBmp.aBmpEx.Mirror( nMirrorFlags );
62 // Die Positionen innerhalb der Gesamtbitmap
63 // muessen natuerlich auch angepasst werden
64 if( bHMirr )
65 aAnimBmp.aPosPix.X() = rGlobalSize.Width() - aAnimBmp.aPosPix.X() -
66 aAnimBmp.aSizePix.Width();
68 if( bVMirr )
69 aAnimBmp.aPosPix.Y() = rGlobalSize.Height() - aAnimBmp.aPosPix.Y() -
70 aAnimBmp.aSizePix.Height();
72 aNewAnim.Replace( aAnimBmp, i );
76 return aNewAnim;
79 Graphic XOutBitmap::MirrorGraphic( const Graphic& rGraphic, const sal_uIntPtr nMirrorFlags )
81 Graphic aRetGraphic;
83 if( 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 );
91 else
93 if( rGraphic.IsTransparent() )
95 BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
97 aBmpEx.Mirror( nMirrorFlags );
98 aRetGraphic = aBmpEx;
100 else
102 Bitmap aBmp( rGraphic.GetBitmap() );
104 aBmp.Mirror( nMirrorFlags );
105 aRetGraphic = aBmp;
109 else
110 aRetGraphic = rGraphic;
112 return aRetGraphic;
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 );
122 Graphic aGraphic;
123 String aExt;
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() );
134 aName += '_';
135 aName += String(aURL.getExtension());
136 aName += '_';
137 String aStr( OUString::number( rGraphic.GetChecksum(), 16 ) );
138 if ( aStr.GetChar(0) == '-' )
139 aStr.SetChar(0,'m');
140 aName += aStr;
141 aURL.setBase( aName );
144 // #i121128# use shortcut to write SVG data in original form (if possible)
145 const SvgDataPtr aSvgDataPtr(rGraphic.getSvgData());
147 if(aSvgDataPtr.get()
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();
160 if(pOStm)
162 pOStm->Write(aSvgDataPtr->getSvgDataArray().get(), aSvgDataPtr->getSvgDataArrayLength());
163 aMedium.Commit();
165 if(!aMedium.GetError())
167 nErr = GRFILTER_OK;
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;
188 default:
189 break;
192 if( aExt.Len() )
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() );
204 aMedium.Commit();
206 if( !aMedium.GetError() )
207 nErr = GRFILTER_OK;
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
222 if( bWriteTransGrf )
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();
239 if( bWriteTransGrf )
241 if( bAnimated )
242 aGraphic = rGraphic;
243 else
245 if( pMtfSize_100TH_MM && ( rGraphic.GetType() != GRAPHIC_BITMAP ) )
247 VirtualDevice aVDev;
248 const Size aSize( aVDev.LogicToPixel( *pMtfSize_100TH_MM, MAP_100TH_MM ) );
250 if( aVDev.SetOutputSizePixel( aSize ) )
252 const Wallpaper aWallpaper( aVDev.GetBackground() );
253 const Point aPt;
255 aVDev.SetBackground( Wallpaper( Color( COL_BLACK ) ) );
256 aVDev.Erase();
257 rGraphic.Draw( &aVDev, aPt, aSize );
259 const Bitmap aBitmap( aVDev.GetBitmap( aPt, aSize ) );
261 aVDev.SetBackground( aWallpaper );
262 aVDev.Erase();
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 ) );
269 else
270 aGraphic = rGraphic.GetBitmapEx();
272 else
273 aGraphic = rGraphic.GetBitmapEx();
276 else
278 if( pMtfSize_100TH_MM && ( rGraphic.GetType() != GRAPHIC_BITMAP ) )
280 VirtualDevice aVDev;
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 );
288 else
289 aGraphic = rGraphic.GetBitmap();
291 else
292 aGraphic = rGraphic.GetBitmap();
295 // mirror?
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 );
309 return nErr;
311 else
313 return GRFILTER_OK;
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;
327 if( pOStm )
329 pGrfFilter = &rFilter;
331 nRet = rFilter.ExportGraphic( rGraphic, rURL.GetMainURL( INetURLObject::NO_DECODE ), *pOStm, nFormat, pFilterData );
333 pGrfFilter = NULL;
334 aMedium.Commit();
336 if( aMedium.GetError() && ( GRFILTER_OK == nRet ) )
337 nRet = GRFILTER_IOERROR;
340 return nRet;
343 Bitmap XOutBitmap::DetectEdges( const Bitmap& rBmp, const sal_uInt8 cThreshold )
345 const Size aSize( rBmp.GetSizePixel() );
346 Bitmap aRetBmp;
347 bool bRet = false;
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 ) );
368 long nSum1;
369 long nSum2;
370 long lGray;
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++ )
383 nXTmp = nX;
385 nSum1 = -( nSum2 = lGray = pReadAcc->GetPixelIndex( nY, nXTmp++ ) );
386 nSum2 += ( (long) pReadAcc->GetPixelIndex( nY, nXTmp++ ) ) << 1;
387 nSum1 += ( lGray = pReadAcc->GetPixelIndex( nY, nXTmp ) );
388 nSum2 += lGray;
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++ ) );
394 nSum2 += lGray;
395 nSum2 -= ( (long) pReadAcc->GetPixelIndex( nY2, nXTmp++ ) ) << 1;
396 nSum1 += ( lGray = (long) pReadAcc->GetPixelIndex( nY2, nXTmp ) );
397 nSum2 -= lGray;
399 if( ( nSum1 * nSum1 + nSum2 * nSum2 ) < lThres2 )
400 pWriteAcc->SetPixelIndex( nY1, nXDst, nWhitePalIdx );
401 else
402 pWriteAcc->SetPixelIndex( nY1, nXDst, nBlackPalIdx );
406 bRet = true;
409 aWorkBmp.ReleaseAccess( pReadAcc );
410 aDstBmp.ReleaseAccess( pWriteAcc );
412 if( bRet )
413 aRetBmp = aDstBmp;
417 if( !aRetBmp )
418 aRetBmp = rBmp;
419 else
421 aRetBmp.SetPrefMapMode( rBmp.GetPrefMapMode() );
422 aRetBmp.SetPrefSize( rBmp.GetPrefSize() );
425 return aRetBmp;
428 Polygon XOutBitmap::GetCountour( const Bitmap& rBmp, const sal_uIntPtr nFlags,
429 const sal_uInt8 cEdgeDetectThreshold, const Rectangle* pWorkRectPixel )
431 Bitmap aWorkBmp;
432 Polygon aRetPoly;
433 Point aTmpPoint;
434 Rectangle aWorkRect( aTmpPoint, rBmp.GetSizePixel() );
436 if( pWorkRectPixel )
437 aWorkRect.Intersection( *pWorkRectPixel );
439 aWorkRect.Justify();
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 );
446 else
447 aWorkBmp = rBmp;
449 BitmapReadAccess* pAcc = aWorkBmp.AcquireReadAccess();
451 if( pAcc )
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;
468 long nX, nY;
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++ )
479 nY = nStartY1;
481 // zunaechst Zeile von Links nach Rechts durchlaufen
482 while( nY < nEndY1 )
484 if( aBlack == pAcc->GetPixel( nY, nX ) )
486 pPoints1[ nPolyPos ] = Point( nX, nY );
487 nY = nStartY2;
489 // diese Schleife wird immer gebreaked da hier ja min. ein Pixel ist
490 while( true )
492 if( aBlack == pAcc->GetPixel( nY, nX ) )
494 pPoints2[ nPolyPos ] = Point( nX, nY );
495 break;
498 nY--;
501 nPolyPos++;
502 break;
505 nY++;
509 else
511 pPoints1 = new Point[ nHeight ];
512 pPoints2 = new Point[ nHeight ];
514 for ( nY = nStartY1; nY < nEndY1; nY++ )
516 nX = nStartX1;
518 // zunaechst Zeile von Links nach Rechts durchlaufen
519 while( nX < nEndX1 )
521 if( aBlack == pAcc->GetPixel( nY, nX ) )
523 pPoints1[ nPolyPos ] = Point( nX, nY );
524 nX = nStartX2;
526 // diese Schleife wird immer gebreaked da hier ja min. ein Pixel ist
527 while( true )
529 if( aBlack == pAcc->GetPixel( nY, nX ) )
531 pPoints2[ nPolyPos ] = Point( nX, nY );
532 break;
535 nX--;
538 nPolyPos++;
539 break;
542 nX++;
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 );
559 delete[] pPoints1;
560 delete[] pPoints2;
564 return aRetPoly;
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 );
573 else
574 bRet = sal_False;
576 return bRet;
579 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */