merge the formfield patch from ooo-build
[ooovba.git] / vcl / source / gdi / bitmapex.cxx
blobaaae40fbfe495a3bf3e698c729ca48df6ee1ac72
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: bitmapex.cxx,v $
10 * $Revision: 1.27 $
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_vcl.hxx"
33 #include <ctype.h>
34 #include <rtl/crc.h>
35 #include <vcl/salbtype.hxx>
36 #include <tools/stream.hxx>
37 #include <tools/debug.hxx>
38 #include <vcl/outdev.hxx>
39 #include <vcl/alpha.hxx>
40 #include <vcl/image.h>
41 #include <vcl/bitmapex.hxx>
42 #include <vcl/pngread.hxx>
43 #ifndef _SV_IMPIMAGETREE_H
44 #include <vcl/impimagetree.hxx>
45 #endif
46 #ifndef _SV_RC_H
47 #include <tools/rc.h>
48 #endif
49 #include <vcl/svapp.hxx>
50 #include <vcl/virdev.hxx>
52 // ------------
53 // - BitmapEx -
54 // ------------
56 BitmapEx::BitmapEx() :
57 eTransparent( TRANSPARENT_NONE ),
58 bAlpha ( FALSE )
62 // ------------------------------------------------------------------
64 BitmapEx::BitmapEx( const BitmapEx& rBitmapEx ) :
65 aBitmap ( rBitmapEx.aBitmap ),
66 aMask ( rBitmapEx.aMask ),
67 aBitmapSize ( rBitmapEx.aBitmapSize ),
68 aTransparentColor ( rBitmapEx.aTransparentColor ),
69 eTransparent ( rBitmapEx.eTransparent ),
70 bAlpha ( rBitmapEx.bAlpha )
74 BitmapEx::BitmapEx( const BitmapEx& rBitmapEx, Point aSrc, Size aSize ) :
75 eTransparent( TRANSPARENT_NONE ),
76 bAlpha ( FALSE )
78 if( rBitmapEx.IsEmpty() )
79 return;
81 aBitmap = Bitmap( aSize, rBitmapEx.aBitmap.GetBitCount() );
82 aBitmapSize = aSize;
83 if( rBitmapEx.IsAlpha() )
85 bAlpha = TRUE;
86 aMask = AlphaMask( aSize ).ImplGetBitmap();
88 else if( rBitmapEx.IsTransparent() )
89 aMask = Bitmap( aSize, rBitmapEx.aMask.GetBitCount() );
91 Rectangle aDestRect( Point( 0, 0 ), aSize );
92 Rectangle aSrcRect( aSrc, aSize );
93 CopyPixel( aDestRect, aSrcRect, &rBitmapEx );
96 // ------------------------------------------------------------------
98 BitmapEx::BitmapEx( const ResId& rResId ) :
99 eTransparent( TRANSPARENT_NONE ),
100 bAlpha ( FALSE )
102 static ImplImageTreeSingletonRef aImageTree;
103 ResMgr* pResMgr = NULL;
105 ResMgr::GetResourceSkipHeader( rResId.SetRT( RSC_BITMAP ), &pResMgr );
106 pResMgr->ReadLong();
107 pResMgr->ReadLong();
109 const String aFileName( pResMgr->ReadString() );
110 ::rtl::OUString aCurrentSymbolsStyle = Application::GetSettings().GetStyleSettings().GetCurrentSymbolsStyleName();
112 if( !aImageTree->loadImage( aFileName, aCurrentSymbolsStyle, *this ) )
114 #ifdef DBG_UTIL
115 ByteString aErrorStr( "BitmapEx::BitmapEx( const ResId& rResId ): could not load image <" );
116 DBG_ERROR( ( ( aErrorStr += ByteString( aFileName, RTL_TEXTENCODING_ASCII_US ) ) += '>' ).GetBuffer() );
117 #endif
121 // ------------------------------------------------------------------
123 BitmapEx::BitmapEx( const Bitmap& rBmp ) :
124 aBitmap ( rBmp ),
125 aBitmapSize ( aBitmap.GetSizePixel() ),
126 eTransparent( TRANSPARENT_NONE ),
127 bAlpha ( FALSE )
131 // ------------------------------------------------------------------
133 BitmapEx::BitmapEx( const Bitmap& rBmp, const Bitmap& rMask ) :
134 aBitmap ( rBmp ),
135 aMask ( rMask ),
136 aBitmapSize ( aBitmap.GetSizePixel() ),
137 eTransparent ( !rMask ? TRANSPARENT_NONE : TRANSPARENT_BITMAP ),
138 bAlpha ( FALSE )
140 DBG_ASSERT( !rMask || rBmp.GetSizePixel() == rMask.GetSizePixel(),
141 "BitmapEx::BitmapEx(): size mismatch for bitmap and mask." );
143 // #105489# Ensure a mask is exactly one bit deep
144 if( !!aMask && aMask.GetBitCount() != 1 )
146 OSL_TRACE("BitmapEx: forced mask to monochrome");
147 aMask.ImplMakeMono( 255 );
151 // ------------------------------------------------------------------
153 BitmapEx::BitmapEx( const Bitmap& rBmp, const AlphaMask& rAlphaMask ) :
154 aBitmap ( rBmp ),
155 aMask ( rAlphaMask.ImplGetBitmap() ),
156 aBitmapSize ( aBitmap.GetSizePixel() ),
157 eTransparent ( !rAlphaMask ? TRANSPARENT_NONE : TRANSPARENT_BITMAP ),
158 bAlpha ( !rAlphaMask ? FALSE : TRUE )
160 DBG_ASSERT( !rAlphaMask || rBmp.GetSizePixel() == rAlphaMask.GetSizePixel(),
161 "BitmapEx::BitmapEx(): size mismatch for bitmap and alpha mask." );
163 // #i75531# the workaround below can go when
164 // X11SalGraphics::drawAlphaBitmap()'s render acceleration
165 // can handle the bitmap depth mismatch directly
166 if( aBitmap.GetBitCount() < aMask.GetBitCount() )
167 aBitmap.Convert( BMP_CONVERSION_24BIT );
170 // ------------------------------------------------------------------
172 BitmapEx::BitmapEx( const Bitmap& rBmp, const Color& rTransparentColor ) :
173 aBitmap ( rBmp ),
174 aBitmapSize ( aBitmap.GetSizePixel() ),
175 aTransparentColor ( rTransparentColor ),
176 eTransparent ( TRANSPARENT_BITMAP ),
177 bAlpha ( FALSE )
179 aMask = aBitmap.CreateMask( aTransparentColor );
181 DBG_ASSERT( rBmp.GetSizePixel() == aMask.GetSizePixel(),
182 "BitmapEx::BitmapEx(): size mismatch for bitmap and alpha mask." );
185 // ------------------------------------------------------------------
187 BitmapEx::~BitmapEx()
191 // ------------------------------------------------------------------
193 // ------------------------------------------------------------------
195 BitmapEx& BitmapEx::operator=( const BitmapEx& rBitmapEx )
197 if( &rBitmapEx != this )
199 aBitmap = rBitmapEx.aBitmap;
200 aMask = rBitmapEx.aMask;
201 aBitmapSize = rBitmapEx.aBitmapSize;
202 aTransparentColor = rBitmapEx.aTransparentColor;
203 eTransparent = rBitmapEx.eTransparent;
204 bAlpha = rBitmapEx.bAlpha;
207 return *this;
210 // ------------------------------------------------------------------
212 BOOL BitmapEx::operator==( const BitmapEx& rBitmapEx ) const
214 if( eTransparent != rBitmapEx.eTransparent )
215 return FALSE;
217 if( aBitmap != rBitmapEx.aBitmap )
218 return FALSE;
220 if( aBitmapSize != rBitmapEx.aBitmapSize )
221 return FALSE;
223 if( eTransparent == TRANSPARENT_NONE )
224 return TRUE;
226 if( eTransparent == TRANSPARENT_COLOR )
227 return aTransparentColor == rBitmapEx.aTransparentColor;
229 return( ( aMask == rBitmapEx.aMask ) && ( bAlpha == rBitmapEx.bAlpha ) );
232 // ------------------------------------------------------------------
234 BOOL BitmapEx::IsEqual( const BitmapEx& rBmpEx ) const
236 return( rBmpEx.eTransparent == eTransparent &&
237 rBmpEx.bAlpha == bAlpha &&
238 rBmpEx.aBitmap.IsEqual( aBitmap ) &&
239 rBmpEx.aMask.IsEqual( aMask ) );
242 // ------------------------------------------------------------------
244 BOOL BitmapEx::IsEmpty() const
246 return( aBitmap.IsEmpty() && aMask.IsEmpty() );
249 // ------------------------------------------------------------------
251 void BitmapEx::SetEmpty()
253 aBitmap.SetEmpty();
254 aMask.SetEmpty();
255 eTransparent = TRANSPARENT_NONE;
256 bAlpha = FALSE;
259 // ------------------------------------------------------------------
261 void BitmapEx::Clear()
263 SetEmpty();
266 // ------------------------------------------------------------------
268 BOOL BitmapEx::IsTransparent() const
270 return( eTransparent != TRANSPARENT_NONE );
273 // ------------------------------------------------------------------
275 BOOL BitmapEx::IsAlpha() const
277 return( IsTransparent() && bAlpha );
280 // ------------------------------------------------------------------
282 Bitmap BitmapEx::GetBitmap( const Color* pTransReplaceColor ) const
284 Bitmap aRetBmp( aBitmap );
286 if( pTransReplaceColor && ( eTransparent != TRANSPARENT_NONE ) )
288 Bitmap aTempMask;
290 if( eTransparent == TRANSPARENT_COLOR )
291 aTempMask = aBitmap.CreateMask( aTransparentColor );
292 else
293 aTempMask = aMask;
295 if( !IsAlpha() )
296 aRetBmp.Replace( aTempMask, *pTransReplaceColor );
297 else
298 aRetBmp.Replace( GetAlpha(), *pTransReplaceColor );
301 return aRetBmp;
304 // ------------------------------------------------------------------
306 BitmapEx BitmapEx::GetColorTransformedBitmapEx( BmpColorMode eColorMode ) const
308 BitmapEx aRet;
310 if( BMP_COLOR_HIGHCONTRAST == eColorMode )
312 aRet = *this;
313 aRet.aBitmap = aBitmap.GetColorTransformedBitmap( eColorMode );
315 else if( BMP_COLOR_MONOCHROME_BLACK == eColorMode ||
316 BMP_COLOR_MONOCHROME_WHITE == eColorMode )
318 aRet = *this;
319 aRet.aBitmap = aRet.aBitmap.GetColorTransformedBitmap( eColorMode );
321 if( !aRet.aMask.IsEmpty() )
323 aRet.aMask.CombineSimple( aRet.aBitmap, BMP_COMBINE_OR );
324 aRet.aBitmap.Erase( ( BMP_COLOR_MONOCHROME_BLACK == eColorMode ) ? COL_BLACK : COL_WHITE );
326 DBG_ASSERT( aRet.aBitmap.GetSizePixel() == aRet.aMask.GetSizePixel(),
327 "BitmapEx::GetColorTransformedBitmapEx(): size mismatch for bitmap and alpha mask." );
331 return aRet;
334 // ------------------------------------------------------------------
336 Bitmap BitmapEx::GetMask() const
338 Bitmap aRet( aMask );
340 if( IsAlpha() )
341 aRet.ImplMakeMono( 255 );
343 return aRet;
346 // ------------------------------------------------------------------
348 AlphaMask BitmapEx::GetAlpha() const
350 AlphaMask aAlpha;
352 if( IsAlpha() )
353 aAlpha.ImplSetBitmap( aMask );
354 else
355 aAlpha = aMask;
357 return aAlpha;
360 // ------------------------------------------------------------------
362 ULONG BitmapEx::GetSizeBytes() const
364 ULONG nSizeBytes = aBitmap.GetSizeBytes();
366 if( eTransparent == TRANSPARENT_BITMAP )
367 nSizeBytes += aMask.GetSizeBytes();
369 return nSizeBytes;
372 // ------------------------------------------------------------------
374 ULONG BitmapEx::GetChecksum() const
376 sal_uInt32 nCrc = aBitmap.GetChecksum();
377 SVBT32 aBT32;
379 UInt32ToSVBT32( (long) eTransparent, aBT32 );
380 nCrc = rtl_crc32( nCrc, aBT32, 4 );
382 UInt32ToSVBT32( (long) bAlpha, aBT32 );
383 nCrc = rtl_crc32( nCrc, aBT32, 4 );
385 if( ( TRANSPARENT_BITMAP == eTransparent ) && !aMask.IsEmpty() )
387 UInt32ToSVBT32( aMask.GetChecksum(), aBT32 );
388 nCrc = rtl_crc32( nCrc, aBT32, 4 );
391 return nCrc;
394 // ------------------------------------------------------------------
396 void BitmapEx::SetSizePixel( const Size& rNewSize )
398 Scale( rNewSize );
401 // ------------------------------------------------------------------
403 BOOL BitmapEx::Invert()
405 BOOL bRet = FALSE;
407 if( !!aBitmap )
409 bRet = aBitmap.Invert();
411 if( bRet && ( eTransparent == TRANSPARENT_COLOR ) )
412 aTransparentColor = BitmapColor( aTransparentColor ).Invert();
415 return bRet;
418 // ------------------------------------------------------------------
420 BOOL BitmapEx::Mirror( ULONG nMirrorFlags )
422 BOOL bRet = FALSE;
424 if( !!aBitmap )
426 bRet = aBitmap.Mirror( nMirrorFlags );
428 if( bRet && ( eTransparent == TRANSPARENT_BITMAP ) && !!aMask )
429 aMask.Mirror( nMirrorFlags );
432 return bRet;
435 // ------------------------------------------------------------------
437 BOOL BitmapEx::Scale( const double& rScaleX, const double& rScaleY, ULONG nScaleFlag )
439 BOOL bRet = FALSE;
441 if( !!aBitmap )
443 bRet = aBitmap.Scale( rScaleX, rScaleY, nScaleFlag );
445 if( bRet && ( eTransparent == TRANSPARENT_BITMAP ) && !!aMask )
446 aMask.Scale( rScaleX, rScaleY, BMP_SCALE_FAST );
448 aBitmapSize = aBitmap.GetSizePixel();
450 DBG_ASSERT( !aMask || aBitmap.GetSizePixel() == aMask.GetSizePixel(),
451 "BitmapEx::Scale(): size mismatch for bitmap and alpha mask." );
454 return bRet;
457 // ------------------------------------------------------------------------
459 BOOL BitmapEx::Scale( const Size& rNewSize, ULONG nScaleFlag )
461 BOOL bRet;
463 if( aBitmapSize.Width() && aBitmapSize.Height() )
465 bRet = Scale( (double) rNewSize.Width() / aBitmapSize.Width(),
466 (double) rNewSize.Height() / aBitmapSize.Height(),
467 nScaleFlag );
469 else
470 bRet = TRUE;
472 return bRet;
475 // ------------------------------------------------------------------
477 BOOL BitmapEx::Rotate( long nAngle10, const Color& rFillColor )
479 BOOL bRet = FALSE;
481 if( !!aBitmap )
483 const BOOL bTransRotate = ( Color( COL_TRANSPARENT ) == rFillColor );
485 if( bTransRotate )
487 if( eTransparent == TRANSPARENT_COLOR )
488 bRet = aBitmap.Rotate( nAngle10, aTransparentColor );
489 else
491 bRet = aBitmap.Rotate( nAngle10, COL_BLACK );
493 if( eTransparent == TRANSPARENT_NONE )
495 aMask = Bitmap( aBitmapSize, 1 );
496 aMask.Erase( COL_BLACK );
497 eTransparent = TRANSPARENT_BITMAP;
500 if( bRet && !!aMask )
501 aMask.Rotate( nAngle10, COL_WHITE );
504 else
506 bRet = aBitmap.Rotate( nAngle10, rFillColor );
508 if( bRet && ( eTransparent == TRANSPARENT_BITMAP ) && !!aMask )
509 aMask.Rotate( nAngle10, COL_WHITE );
512 aBitmapSize = aBitmap.GetSizePixel();
514 DBG_ASSERT( !aMask || aBitmap.GetSizePixel() == aMask.GetSizePixel(),
515 "BitmapEx::Rotate(): size mismatch for bitmap and alpha mask." );
518 return bRet;
521 // ------------------------------------------------------------------
523 BOOL BitmapEx::Crop( const Rectangle& rRectPixel )
525 BOOL bRet = FALSE;
527 if( !!aBitmap )
529 bRet = aBitmap.Crop( rRectPixel );
531 if( bRet && ( eTransparent == TRANSPARENT_BITMAP ) && !!aMask )
532 aMask.Crop( rRectPixel );
534 aBitmapSize = aBitmap.GetSizePixel();
536 DBG_ASSERT( !aMask || aBitmap.GetSizePixel() == aMask.GetSizePixel(),
537 "BitmapEx::Crop(): size mismatch for bitmap and alpha mask." );
540 return bRet;
543 // ------------------------------------------------------------------
545 BOOL BitmapEx::Convert( BmpConversion eConversion )
547 return( !!aBitmap ? aBitmap.Convert( eConversion ) : FALSE );
550 // ------------------------------------------------------------------
552 BOOL BitmapEx::ReduceColors( USHORT nNewColorCount, BmpReduce eReduce )
554 return( !!aBitmap ? aBitmap.ReduceColors( nNewColorCount, eReduce ) : FALSE );
557 // ------------------------------------------------------------------
559 BOOL BitmapEx::Expand( ULONG nDX, ULONG nDY, const Color* pInitColor, BOOL bExpandTransparent )
561 BOOL bRet = FALSE;
563 if( !!aBitmap )
565 bRet = aBitmap.Expand( nDX, nDY, pInitColor );
567 if( bRet && ( eTransparent == TRANSPARENT_BITMAP ) && !!aMask )
569 Color aColor( bExpandTransparent ? COL_WHITE : COL_BLACK );
570 aMask.Expand( nDX, nDY, &aColor );
573 aBitmapSize = aBitmap.GetSizePixel();
575 DBG_ASSERT( !aMask || aBitmap.GetSizePixel() == aMask.GetSizePixel(),
576 "BitmapEx::Expand(): size mismatch for bitmap and alpha mask." );
579 return bRet;
582 // ------------------------------------------------------------------
584 BOOL BitmapEx::CopyPixel( const Rectangle& rRectDst, const Rectangle& rRectSrc,
585 const BitmapEx* pBmpExSrc )
587 BOOL bRet = FALSE;
589 if( !pBmpExSrc || pBmpExSrc->IsEmpty() )
591 if( !aBitmap.IsEmpty() )
593 bRet = aBitmap.CopyPixel( rRectDst, rRectSrc );
595 if( bRet && ( eTransparent == TRANSPARENT_BITMAP ) && !!aMask )
596 aMask.CopyPixel( rRectDst, rRectSrc );
599 else
601 if( !aBitmap.IsEmpty() )
603 bRet = aBitmap.CopyPixel( rRectDst, rRectSrc, &pBmpExSrc->aBitmap );
605 if( bRet )
607 if( pBmpExSrc->IsAlpha() )
609 if( IsAlpha() )
610 // cast to use the optimized AlphaMask::CopyPixel
611 ((AlphaMask*) &aMask)->CopyPixel( rRectDst, rRectSrc, (AlphaMask*)&pBmpExSrc->aMask );
612 else if( IsTransparent() )
614 AlphaMask* pAlpha = new AlphaMask( aMask );
616 aMask = pAlpha->ImplGetBitmap();
617 delete pAlpha;
618 bAlpha = TRUE;
619 aMask.CopyPixel( rRectDst, rRectSrc, &pBmpExSrc->aMask );
621 else
623 sal_uInt8 cBlack = 0;
624 AlphaMask* pAlpha = new AlphaMask( GetSizePixel(), &cBlack );
626 aMask = pAlpha->ImplGetBitmap();
627 delete pAlpha;
628 eTransparent = TRANSPARENT_BITMAP;
629 bAlpha = TRUE;
630 aMask.CopyPixel( rRectDst, rRectSrc, &pBmpExSrc->aMask );
633 else if( pBmpExSrc->IsTransparent() )
635 if( IsAlpha() )
637 AlphaMask aAlpha( pBmpExSrc->aMask );
638 aMask.CopyPixel( rRectDst, rRectSrc, &aAlpha.ImplGetBitmap() );
640 else if( IsTransparent() )
641 aMask.CopyPixel( rRectDst, rRectSrc, &pBmpExSrc->aMask );
642 else
644 aMask = Bitmap( GetSizePixel(), 1 );
645 aMask.Erase( Color( COL_BLACK ) );
646 eTransparent = TRANSPARENT_BITMAP;
647 aMask.CopyPixel( rRectDst, rRectSrc, &pBmpExSrc->aMask );
650 else if( IsAlpha() )
652 sal_uInt8 cBlack = 0;
653 const AlphaMask aAlphaSrc( pBmpExSrc->GetSizePixel(), &cBlack );
655 aMask.CopyPixel( rRectDst, rRectSrc, &aAlphaSrc.ImplGetBitmap() );
657 else if( IsTransparent() )
659 Bitmap aMaskSrc( pBmpExSrc->GetSizePixel(), 1 );
661 aMaskSrc.Erase( Color( COL_BLACK ) );
662 aMask.CopyPixel( rRectDst, rRectSrc, &aMaskSrc );
668 return bRet;
671 // ------------------------------------------------------------------
673 BOOL BitmapEx::Erase( const Color& rFillColor )
675 BOOL bRet = FALSE;
677 if( !!aBitmap )
679 bRet = aBitmap.Erase( rFillColor );
681 if( bRet && ( eTransparent == TRANSPARENT_BITMAP ) && !!aMask )
683 // #104416# Respect transparency on fill color
684 if( rFillColor.GetTransparency() )
686 const Color aFill( rFillColor.GetTransparency(), rFillColor.GetTransparency(), rFillColor.GetTransparency() );
687 aMask.Erase( aFill );
689 else
691 const Color aBlack( COL_BLACK );
692 aMask.Erase( aBlack );
697 return bRet;
700 // ------------------------------------------------------------------
702 BOOL BitmapEx::Dither( ULONG nDitherFlags )
704 return( !!aBitmap ? aBitmap.Dither( nDitherFlags ) : FALSE );
707 // ------------------------------------------------------------------
709 BOOL BitmapEx::Replace( const Color& rSearchColor, const Color& rReplaceColor, ULONG nTol )
711 return( !!aBitmap ? aBitmap.Replace( rSearchColor, rReplaceColor, nTol ) : FALSE );
714 // ------------------------------------------------------------------
716 BOOL BitmapEx::Replace( const Color* pSearchColors, const Color* pReplaceColors, ULONG nColorCount, const ULONG* pTols )
718 return( !!aBitmap ? aBitmap.Replace( pSearchColors, pReplaceColors, nColorCount, (ULONG*) pTols ) : FALSE );
721 // ------------------------------------------------------------------
723 BOOL BitmapEx::Adjust( short nLuminancePercent, short nContrastPercent,
724 short nChannelRPercent, short nChannelGPercent, short nChannelBPercent,
725 double fGamma, BOOL bInvert )
727 return( !!aBitmap ? aBitmap.Adjust( nLuminancePercent, nContrastPercent,
728 nChannelRPercent, nChannelGPercent, nChannelBPercent,
729 fGamma, bInvert ) : FALSE );
732 // ------------------------------------------------------------------
734 BOOL BitmapEx::Filter( BmpFilter eFilter, const BmpFilterParam* pFilterParam, const Link* pProgress )
736 return( !!aBitmap ? aBitmap.Filter( eFilter, pFilterParam, pProgress ) : FALSE );
739 // ------------------------------------------------------------------
741 void BitmapEx::Draw( OutputDevice* pOutDev, const Point& rDestPt ) const
743 pOutDev->DrawBitmapEx( rDestPt, *this );
746 // ------------------------------------------------------------------
748 void BitmapEx::Draw( OutputDevice* pOutDev,
749 const Point& rDestPt, const Size& rDestSize ) const
751 pOutDev->DrawBitmapEx( rDestPt, rDestSize, *this );
754 // ------------------------------------------------------------------
756 void BitmapEx::Draw( OutputDevice* pOutDev,
757 const Point& rDestPt, const Size& rDestSize,
758 const Point& rSrcPtPixel, const Size& rSrcSizePixel ) const
760 pOutDev->DrawBitmapEx( rDestPt, rDestSize, rSrcPtPixel, rSrcSizePixel, *this );
763 BitmapEx BitmapEx:: AutoScaleBitmap(BitmapEx & aBitmap, const long aStandardSize)
765 Point aEmptyPoint(0,0);
766 sal_Int32 imgNewWidth = 0;
767 sal_Int32 imgNewHeight = 0;
768 double imgposX = 0;
769 double imgposY = 0;
770 BitmapEx aRet = aBitmap;
771 double imgOldWidth = aRet.GetSizePixel().Width();
772 double imgOldHeight =aRet.GetSizePixel().Height();
774 Size aScaledSize;
775 if (imgOldWidth >= aStandardSize || imgOldHeight >= aStandardSize)
777 if (imgOldWidth >= imgOldHeight)
779 imgNewWidth = aStandardSize;
780 imgNewHeight = sal_Int32(imgOldHeight / (imgOldWidth / aStandardSize) + 0.5);
781 imgposX = 0;
782 imgposY = (aStandardSize - (imgOldHeight / (imgOldWidth / aStandardSize) + 0.5)) / 2 + 0.5;
784 else
786 imgNewHeight = aStandardSize;
787 imgNewWidth = sal_Int32(imgOldWidth / (imgOldHeight / aStandardSize) + 0.5);
788 imgposY = 0;
789 imgposX = (aStandardSize - (imgOldWidth / (imgOldHeight / aStandardSize) + 0.5)) / 2 + 0.5;
792 aScaledSize = Size( imgNewWidth, imgNewHeight );
793 aRet.Scale( aScaledSize, BMP_SCALE_INTERPOLATE );
795 else
797 imgposX = (aStandardSize - imgOldWidth) / 2 + 0.5;
798 imgposY = (aStandardSize - imgOldHeight) / 2 + 0.5;
801 Size aBmpSize = aRet.GetSizePixel();
802 Size aStdSize( aStandardSize, aStandardSize );
803 Rectangle aRect(aEmptyPoint, aStdSize );
805 VirtualDevice aVirDevice( *Application::GetDefaultDevice(), 0, 1 );
806 aVirDevice.SetOutputSizePixel( aStdSize );
807 aVirDevice.SetFillColor( COL_TRANSPARENT );
808 aVirDevice.SetLineColor( COL_TRANSPARENT );
810 //draw a rect into virDevice
811 aVirDevice.DrawRect( aRect );
812 Point aPointPixel( (long)imgposX, (long)imgposY );
813 aVirDevice.DrawBitmapEx( aPointPixel, aRet );
814 aRet = aVirDevice.GetBitmapEx( aEmptyPoint, aStdSize );
816 return aRet;
818 // ------------------------------------------------------------------
820 SvStream& operator<<( SvStream& rOStm, const BitmapEx& rBitmapEx )
822 rBitmapEx.aBitmap.Write( rOStm );
824 rOStm << (UINT32) 0x25091962;
825 rOStm << (UINT32) 0xACB20201;
826 rOStm << (BYTE) rBitmapEx.eTransparent;
828 if( rBitmapEx.eTransparent == TRANSPARENT_BITMAP )
829 rBitmapEx.aMask.Write( rOStm );
830 else if( rBitmapEx.eTransparent == TRANSPARENT_COLOR )
831 rOStm << rBitmapEx.aTransparentColor;
833 return rOStm;
836 // ------------------------------------------------------------------
838 SvStream& operator>>( SvStream& rIStm, BitmapEx& rBitmapEx )
840 Bitmap aBmp;
842 rIStm >> aBmp;
844 if( !rIStm.GetError() )
846 const ULONG nStmPos = rIStm.Tell();
847 UINT32 nMagic1 = 0;
848 UINT32 nMagic2 = 0;
850 rIStm >> nMagic1 >> nMagic2;
852 if( ( nMagic1 != 0x25091962 ) || ( nMagic2 != 0xACB20201 ) || rIStm.GetError() )
854 rIStm.ResetError();
855 rIStm.Seek( nStmPos );
856 rBitmapEx = aBmp;
858 else
860 BYTE bTransparent = false;
862 rIStm >> bTransparent;
864 if( bTransparent == (BYTE) TRANSPARENT_BITMAP )
866 Bitmap aMask;
868 rIStm >> aMask;
870 if( !!aMask)
872 // do we have an alpha mask?
873 if( ( 8 == aMask.GetBitCount() ) && aMask.HasGreyPalette() )
875 AlphaMask aAlpha;
877 // create alpha mask quickly (without greyscale conversion)
878 aAlpha.ImplSetBitmap( aMask );
879 rBitmapEx = BitmapEx( aBmp, aAlpha );
881 else
882 rBitmapEx = BitmapEx( aBmp, aMask );
884 else
885 rBitmapEx = aBmp;
887 else if( bTransparent == (BYTE) TRANSPARENT_COLOR )
889 Color aTransparentColor;
891 rIStm >> aTransparentColor;
892 rBitmapEx = BitmapEx( aBmp, aTransparentColor );
894 else
895 rBitmapEx = aBmp;
899 return rIStm;