xref: /aoo42x/main/vcl/source/gdi/bitmapex.cxx (revision 54628ca4)
19f62ea84SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
39f62ea84SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
49f62ea84SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
59f62ea84SAndrew Rist  * distributed with this work for additional information
69f62ea84SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
79f62ea84SAndrew Rist  * to you under the Apache License, Version 2.0 (the
89f62ea84SAndrew Rist  * "License"); you may not use this file except in compliance
99f62ea84SAndrew Rist  * with the License.  You may obtain a copy of the License at
109f62ea84SAndrew Rist  *
119f62ea84SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
129f62ea84SAndrew Rist  *
139f62ea84SAndrew Rist  * Unless required by applicable law or agreed to in writing,
149f62ea84SAndrew Rist  * software distributed under the License is distributed on an
159f62ea84SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169f62ea84SAndrew Rist  * KIND, either express or implied.  See the License for the
179f62ea84SAndrew Rist  * specific language governing permissions and limitations
189f62ea84SAndrew Rist  * under the License.
199f62ea84SAndrew Rist  *
209f62ea84SAndrew Rist  *************************************************************/
219f62ea84SAndrew Rist 
229f62ea84SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_vcl.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <ctype.h>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <rtl/crc.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <tools/stream.hxx>
32cdf0e10cSrcweir #include <tools/debug.hxx>
33cdf0e10cSrcweir #include <tools/rc.h>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <vcl/salbtype.hxx>
36cdf0e10cSrcweir #include <vcl/outdev.hxx>
37cdf0e10cSrcweir #include <vcl/alpha.hxx>
38cdf0e10cSrcweir #include <vcl/bitmapex.hxx>
39cdf0e10cSrcweir #include <vcl/pngread.hxx>
40cdf0e10cSrcweir #include <vcl/svapp.hxx>
41cdf0e10cSrcweir #include <vcl/bmpacc.hxx>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #include <image.h>
44cdf0e10cSrcweir #include <impimagetree.hxx>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir // ------------
47cdf0e10cSrcweir // - BitmapEx -
48cdf0e10cSrcweir // ------------
49cdf0e10cSrcweir 
50cdf0e10cSrcweir BitmapEx::BitmapEx() :
51cdf0e10cSrcweir 		eTransparent( TRANSPARENT_NONE ),
52cdf0e10cSrcweir 		bAlpha		( sal_False )
53cdf0e10cSrcweir {
54cdf0e10cSrcweir }
55cdf0e10cSrcweir 
56cdf0e10cSrcweir // ------------------------------------------------------------------
57cdf0e10cSrcweir 
58cdf0e10cSrcweir BitmapEx::BitmapEx( const BitmapEx& rBitmapEx ) :
59cdf0e10cSrcweir 		aBitmap				( rBitmapEx.aBitmap ),
60cdf0e10cSrcweir 		aMask				( rBitmapEx.aMask ),
61cdf0e10cSrcweir 		aBitmapSize			( rBitmapEx.aBitmapSize ),
62cdf0e10cSrcweir 		aTransparentColor	( rBitmapEx.aTransparentColor ),
63cdf0e10cSrcweir 		eTransparent		( rBitmapEx.eTransparent ),
64cdf0e10cSrcweir 		bAlpha				( rBitmapEx.bAlpha )
65cdf0e10cSrcweir {
66cdf0e10cSrcweir }
67cdf0e10cSrcweir 
68cdf0e10cSrcweir BitmapEx::BitmapEx( const BitmapEx& rBitmapEx, Point aSrc, Size aSize ) :
69cdf0e10cSrcweir 		eTransparent( TRANSPARENT_NONE ),
70cdf0e10cSrcweir 		bAlpha		( sal_False )
71cdf0e10cSrcweir {
72cdf0e10cSrcweir 	if( rBitmapEx.IsEmpty() )
73cdf0e10cSrcweir 		return;
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 	aBitmap = Bitmap( aSize, rBitmapEx.aBitmap.GetBitCount() );
76cdf0e10cSrcweir 	aBitmapSize = aSize;
77cdf0e10cSrcweir 	if( rBitmapEx.IsAlpha() )
78cdf0e10cSrcweir 	{
79cdf0e10cSrcweir 		bAlpha = sal_True;
80cdf0e10cSrcweir 		aMask = AlphaMask( aSize ).ImplGetBitmap();
81cdf0e10cSrcweir 	}
82cdf0e10cSrcweir 	else if( rBitmapEx.IsTransparent() )
83cdf0e10cSrcweir 		aMask = Bitmap( aSize, rBitmapEx.aMask.GetBitCount() );
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 	Rectangle aDestRect( Point( 0, 0 ), aSize );
86cdf0e10cSrcweir 	Rectangle aSrcRect( aSrc, aSize );
87cdf0e10cSrcweir 	CopyPixel( aDestRect, aSrcRect, &rBitmapEx );
88cdf0e10cSrcweir }
89cdf0e10cSrcweir 
90cdf0e10cSrcweir // ------------------------------------------------------------------
91cdf0e10cSrcweir 
92cdf0e10cSrcweir BitmapEx::BitmapEx( const ResId& rResId ) :
93cdf0e10cSrcweir 		eTransparent( TRANSPARENT_NONE ),
94cdf0e10cSrcweir 		bAlpha		( sal_False )
95cdf0e10cSrcweir {
96cdf0e10cSrcweir 	static ImplImageTreeSingletonRef	aImageTree;
97cdf0e10cSrcweir 	ResMgr* 							pResMgr = NULL;
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 	ResMgr::GetResourceSkipHeader( rResId.SetRT( RSC_BITMAP ), &pResMgr );
100cdf0e10cSrcweir 	pResMgr->ReadLong();
101cdf0e10cSrcweir 	pResMgr->ReadLong();
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 	const String aFileName( pResMgr->ReadString() );
104cdf0e10cSrcweir 	::rtl::OUString aCurrentSymbolsStyle = Application::GetSettings().GetStyleSettings().GetCurrentSymbolsStyleName();
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 	if( !aImageTree->loadImage( aFileName, aCurrentSymbolsStyle, *this ) )
107cdf0e10cSrcweir 	{
108cdf0e10cSrcweir #ifdef DBG_UTIL
109cdf0e10cSrcweir 		ByteString aErrorStr( "BitmapEx::BitmapEx( const ResId& rResId ): could not load image <" );
110cdf0e10cSrcweir 		DBG_ERROR( ( ( aErrorStr += ByteString( aFileName, RTL_TEXTENCODING_ASCII_US ) ) += '>' ).GetBuffer() );
111cdf0e10cSrcweir #endif
112cdf0e10cSrcweir 	}
113cdf0e10cSrcweir }
114cdf0e10cSrcweir 
115cdf0e10cSrcweir // ------------------------------------------------------------------
116cdf0e10cSrcweir 
117cdf0e10cSrcweir BitmapEx::BitmapEx( const Bitmap& rBmp ) :
118cdf0e10cSrcweir 		aBitmap		( rBmp ),
119cdf0e10cSrcweir 		aBitmapSize	( aBitmap.GetSizePixel() ),
120cdf0e10cSrcweir 		eTransparent( TRANSPARENT_NONE ),
121cdf0e10cSrcweir 		bAlpha		( sal_False )
122cdf0e10cSrcweir {
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir // ------------------------------------------------------------------
126cdf0e10cSrcweir 
127cdf0e10cSrcweir BitmapEx::BitmapEx( const Bitmap& rBmp, const Bitmap& rMask ) :
128cdf0e10cSrcweir 		aBitmap			( rBmp ),
129cdf0e10cSrcweir 		aMask			( rMask ),
130cdf0e10cSrcweir 		aBitmapSize		( aBitmap.GetSizePixel() ),
131cdf0e10cSrcweir 		eTransparent	( !rMask ? TRANSPARENT_NONE : TRANSPARENT_BITMAP ),
132cdf0e10cSrcweir 		bAlpha			( sal_False )
133cdf0e10cSrcweir {
134ba5b0517SArmin Le Grand     if(!!aBitmap && !!aMask && aBitmap.GetSizePixel() != aMask.GetSizePixel())
135479f2b27SArmin Le Grand     {
136479f2b27SArmin Le Grand         OSL_ENSURE(false, "Mask size differs from Bitmap size, corrected Mask (!)");
137ba5b0517SArmin Le Grand         aMask.Scale(aBitmap.GetSizePixel());
138479f2b27SArmin Le Grand     }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir     // #105489# Ensure a mask is exactly one bit deep
141cdf0e10cSrcweir     if( !!aMask && aMask.GetBitCount() != 1 )
142cdf0e10cSrcweir     {
143cdf0e10cSrcweir         OSL_TRACE("BitmapEx: forced mask to monochrome");
144cdf0e10cSrcweir         aMask.ImplMakeMono( 255 );
145cdf0e10cSrcweir     }
146cdf0e10cSrcweir }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir // ------------------------------------------------------------------
149cdf0e10cSrcweir 
150cdf0e10cSrcweir BitmapEx::BitmapEx( const Bitmap& rBmp, const AlphaMask& rAlphaMask ) :
151cdf0e10cSrcweir 		aBitmap			( rBmp ),
152cdf0e10cSrcweir 		aMask			( rAlphaMask.ImplGetBitmap() ),
153cdf0e10cSrcweir 		aBitmapSize		( aBitmap.GetSizePixel() ),
154cdf0e10cSrcweir 		eTransparent	( !rAlphaMask ? TRANSPARENT_NONE : TRANSPARENT_BITMAP ),
155cdf0e10cSrcweir 		bAlpha			( !rAlphaMask ? sal_False : sal_True )
156cdf0e10cSrcweir {
157ba5b0517SArmin Le Grand     if(!!aBitmap && !!aMask && aBitmap.GetSizePixel() != aMask.GetSizePixel())
158479f2b27SArmin Le Grand     {
159479f2b27SArmin Le Grand         OSL_ENSURE(false, "Alpha size differs from Bitmap size, corrected Mask (!)");
160479f2b27SArmin Le Grand         aMask.Scale(rBmp.GetSizePixel());
161479f2b27SArmin Le Grand     }
162cdf0e10cSrcweir 
163cdf0e10cSrcweir     // #i75531# the workaround below can go when
164cdf0e10cSrcweir     // X11SalGraphics::drawAlphaBitmap()'s render acceleration
165cdf0e10cSrcweir     // can handle the bitmap depth mismatch directly
166cdf0e10cSrcweir     if( aBitmap.GetBitCount() < aMask.GetBitCount() )
167cdf0e10cSrcweir         aBitmap.Convert( BMP_CONVERSION_24BIT );
168cdf0e10cSrcweir }
169cdf0e10cSrcweir 
170cdf0e10cSrcweir // ------------------------------------------------------------------
171cdf0e10cSrcweir 
172cdf0e10cSrcweir BitmapEx::BitmapEx( const Bitmap& rBmp, const Color& rTransparentColor ) :
173cdf0e10cSrcweir 		aBitmap				( rBmp ),
174cdf0e10cSrcweir 		aBitmapSize			( aBitmap.GetSizePixel() ),
175cdf0e10cSrcweir 		aTransparentColor	( rTransparentColor ),
176cdf0e10cSrcweir 		eTransparent		( TRANSPARENT_BITMAP ),
177cdf0e10cSrcweir 		bAlpha				( sal_False )
178cdf0e10cSrcweir {
179cdf0e10cSrcweir 	aMask = aBitmap.CreateMask( aTransparentColor );
180cdf0e10cSrcweir 
181cdf0e10cSrcweir     DBG_ASSERT( rBmp.GetSizePixel() == aMask.GetSizePixel(),
182cdf0e10cSrcweir                 "BitmapEx::BitmapEx(): size mismatch for bitmap and alpha mask." );
183cdf0e10cSrcweir }
184cdf0e10cSrcweir 
185cdf0e10cSrcweir // ------------------------------------------------------------------
186cdf0e10cSrcweir 
187cdf0e10cSrcweir BitmapEx::~BitmapEx()
188cdf0e10cSrcweir {
189cdf0e10cSrcweir }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir // ------------------------------------------------------------------
192cdf0e10cSrcweir 
193cdf0e10cSrcweir // ------------------------------------------------------------------
194cdf0e10cSrcweir 
195cdf0e10cSrcweir BitmapEx& BitmapEx::operator=( const BitmapEx& rBitmapEx )
196cdf0e10cSrcweir {
197cdf0e10cSrcweir 	if( &rBitmapEx != this )
198cdf0e10cSrcweir 	{
199cdf0e10cSrcweir 		aBitmap = rBitmapEx.aBitmap;
200cdf0e10cSrcweir 		aMask = rBitmapEx.aMask;
201cdf0e10cSrcweir 		aBitmapSize = rBitmapEx.aBitmapSize;
202cdf0e10cSrcweir 		aTransparentColor = rBitmapEx.aTransparentColor;
203cdf0e10cSrcweir 		eTransparent = rBitmapEx.eTransparent;
204cdf0e10cSrcweir 		bAlpha = rBitmapEx.bAlpha;
205cdf0e10cSrcweir 	}
206cdf0e10cSrcweir 
207cdf0e10cSrcweir 	return *this;
208cdf0e10cSrcweir }
209cdf0e10cSrcweir 
210cdf0e10cSrcweir // ------------------------------------------------------------------
211cdf0e10cSrcweir 
212cdf0e10cSrcweir sal_Bool BitmapEx::operator==( const BitmapEx& rBitmapEx ) const
213cdf0e10cSrcweir {
214cdf0e10cSrcweir 	if( eTransparent != rBitmapEx.eTransparent )
215cdf0e10cSrcweir 		return sal_False;
216cdf0e10cSrcweir 
217cdf0e10cSrcweir 	if( aBitmap != rBitmapEx.aBitmap )
218cdf0e10cSrcweir 		return sal_False;
219cdf0e10cSrcweir 
220cdf0e10cSrcweir 	if( aBitmapSize != rBitmapEx.aBitmapSize )
221cdf0e10cSrcweir 		return sal_False;
222cdf0e10cSrcweir 
223cdf0e10cSrcweir 	if( eTransparent == TRANSPARENT_NONE )
224cdf0e10cSrcweir 		return sal_True;
225cdf0e10cSrcweir 
226cdf0e10cSrcweir 	if( eTransparent == TRANSPARENT_COLOR )
227cdf0e10cSrcweir 		return aTransparentColor == rBitmapEx.aTransparentColor;
228cdf0e10cSrcweir 
229cdf0e10cSrcweir 	return( ( aMask == rBitmapEx.aMask ) && ( bAlpha == rBitmapEx.bAlpha ) );
230cdf0e10cSrcweir }
231cdf0e10cSrcweir 
232cdf0e10cSrcweir // ------------------------------------------------------------------
233cdf0e10cSrcweir 
234cdf0e10cSrcweir sal_Bool BitmapEx::IsEqual( const BitmapEx& rBmpEx ) const
235cdf0e10cSrcweir {
236cdf0e10cSrcweir 	return( rBmpEx.eTransparent == eTransparent &&
237cdf0e10cSrcweir 			rBmpEx.bAlpha == bAlpha &&
238cdf0e10cSrcweir 			rBmpEx.aBitmap.IsEqual( aBitmap ) &&
239cdf0e10cSrcweir 			rBmpEx.aMask.IsEqual( aMask ) );
240cdf0e10cSrcweir }
241cdf0e10cSrcweir 
242cdf0e10cSrcweir // ------------------------------------------------------------------
243cdf0e10cSrcweir 
244cdf0e10cSrcweir sal_Bool BitmapEx::IsEmpty() const
245cdf0e10cSrcweir {
246cdf0e10cSrcweir 	return( aBitmap.IsEmpty() && aMask.IsEmpty() );
247cdf0e10cSrcweir }
248cdf0e10cSrcweir 
249cdf0e10cSrcweir // ------------------------------------------------------------------
250cdf0e10cSrcweir 
251cdf0e10cSrcweir void BitmapEx::SetEmpty()
252cdf0e10cSrcweir {
253cdf0e10cSrcweir 	aBitmap.SetEmpty();
254cdf0e10cSrcweir 	aMask.SetEmpty();
255cdf0e10cSrcweir 	eTransparent = TRANSPARENT_NONE;
256cdf0e10cSrcweir 	bAlpha = sal_False;
257cdf0e10cSrcweir }
258cdf0e10cSrcweir 
259cdf0e10cSrcweir // ------------------------------------------------------------------
260cdf0e10cSrcweir 
261cdf0e10cSrcweir void BitmapEx::Clear()
262cdf0e10cSrcweir {
263cdf0e10cSrcweir 	SetEmpty();
264cdf0e10cSrcweir }
265cdf0e10cSrcweir 
266cdf0e10cSrcweir // ------------------------------------------------------------------
267cdf0e10cSrcweir 
268cdf0e10cSrcweir sal_Bool BitmapEx::IsTransparent() const
269cdf0e10cSrcweir {
270cdf0e10cSrcweir 	return( eTransparent != TRANSPARENT_NONE );
271cdf0e10cSrcweir }
272cdf0e10cSrcweir 
273cdf0e10cSrcweir // ------------------------------------------------------------------
274cdf0e10cSrcweir 
275cdf0e10cSrcweir sal_Bool BitmapEx::IsAlpha() const
276cdf0e10cSrcweir {
277cdf0e10cSrcweir 	return( IsTransparent() && bAlpha );
278cdf0e10cSrcweir }
279cdf0e10cSrcweir 
280cdf0e10cSrcweir // ------------------------------------------------------------------
281cdf0e10cSrcweir 
282cdf0e10cSrcweir Bitmap BitmapEx::GetBitmap( const Color* pTransReplaceColor ) const
283cdf0e10cSrcweir {
284cdf0e10cSrcweir 	Bitmap aRetBmp( aBitmap );
285cdf0e10cSrcweir 
286cdf0e10cSrcweir 	if( pTransReplaceColor && ( eTransparent != TRANSPARENT_NONE ) )
287cdf0e10cSrcweir 	{
288cdf0e10cSrcweir 		Bitmap aTempMask;
289cdf0e10cSrcweir 
290cdf0e10cSrcweir 		if( eTransparent == TRANSPARENT_COLOR )
291cdf0e10cSrcweir 			aTempMask = aBitmap.CreateMask( aTransparentColor );
292cdf0e10cSrcweir 		else
293cdf0e10cSrcweir 			aTempMask = aMask;
294cdf0e10cSrcweir 
295cdf0e10cSrcweir 		if( !IsAlpha() )
296cdf0e10cSrcweir 			aRetBmp.Replace( aTempMask, *pTransReplaceColor );
297cdf0e10cSrcweir 		else
298cdf0e10cSrcweir 			aRetBmp.Replace( GetAlpha(), *pTransReplaceColor );
299cdf0e10cSrcweir 	}
300cdf0e10cSrcweir 
301cdf0e10cSrcweir 	return aRetBmp;
302cdf0e10cSrcweir }
303cdf0e10cSrcweir 
304cdf0e10cSrcweir // ------------------------------------------------------------------
305cdf0e10cSrcweir 
306cdf0e10cSrcweir BitmapEx BitmapEx::GetColorTransformedBitmapEx( BmpColorMode eColorMode ) const
307cdf0e10cSrcweir {
308cdf0e10cSrcweir 	BitmapEx aRet;
309cdf0e10cSrcweir 
310cdf0e10cSrcweir 	if( BMP_COLOR_HIGHCONTRAST == eColorMode )
311cdf0e10cSrcweir 	{
312cdf0e10cSrcweir 		aRet = *this;
313cdf0e10cSrcweir 		aRet.aBitmap = aBitmap.GetColorTransformedBitmap( eColorMode );
314cdf0e10cSrcweir 	}
315cdf0e10cSrcweir 	else if( BMP_COLOR_MONOCHROME_BLACK == eColorMode ||
316cdf0e10cSrcweir 			 BMP_COLOR_MONOCHROME_WHITE == eColorMode )
317cdf0e10cSrcweir 	{
318cdf0e10cSrcweir 		aRet = *this;
319cdf0e10cSrcweir 		aRet.aBitmap = aRet.aBitmap.GetColorTransformedBitmap( eColorMode );
320cdf0e10cSrcweir 
321cdf0e10cSrcweir 		if( !aRet.aMask.IsEmpty() )
322cdf0e10cSrcweir 		{
323cdf0e10cSrcweir 			aRet.aMask.CombineSimple( aRet.aBitmap, BMP_COMBINE_OR );
324cdf0e10cSrcweir 			aRet.aBitmap.Erase( ( BMP_COLOR_MONOCHROME_BLACK == eColorMode ) ? COL_BLACK : COL_WHITE );
325cdf0e10cSrcweir 
326cdf0e10cSrcweir             DBG_ASSERT( aRet.aBitmap.GetSizePixel() == aRet.aMask.GetSizePixel(),
327cdf0e10cSrcweir                         "BitmapEx::GetColorTransformedBitmapEx(): size mismatch for bitmap and alpha mask." );
328cdf0e10cSrcweir 		}
329cdf0e10cSrcweir 	}
330cdf0e10cSrcweir 
331cdf0e10cSrcweir 	return aRet;
332cdf0e10cSrcweir }
333cdf0e10cSrcweir 
334cdf0e10cSrcweir // ------------------------------------------------------------------
335cdf0e10cSrcweir 
336cdf0e10cSrcweir Bitmap BitmapEx::GetMask() const
337cdf0e10cSrcweir {
338cdf0e10cSrcweir 	Bitmap aRet( aMask );
339cdf0e10cSrcweir 
340cdf0e10cSrcweir 	if( IsAlpha() )
341cdf0e10cSrcweir 		aRet.ImplMakeMono( 255 );
342cdf0e10cSrcweir 
343cdf0e10cSrcweir 	return aRet;
344cdf0e10cSrcweir }
345cdf0e10cSrcweir 
346cdf0e10cSrcweir // ------------------------------------------------------------------
347cdf0e10cSrcweir 
348cdf0e10cSrcweir AlphaMask BitmapEx::GetAlpha() const
349cdf0e10cSrcweir {
350cdf0e10cSrcweir 	AlphaMask aAlpha;
351cdf0e10cSrcweir 
352cdf0e10cSrcweir 	if( IsAlpha() )
353cdf0e10cSrcweir 		aAlpha.ImplSetBitmap( aMask );
354cdf0e10cSrcweir 	else
355cdf0e10cSrcweir 		aAlpha = aMask;
356cdf0e10cSrcweir 
357cdf0e10cSrcweir 	return aAlpha;
358cdf0e10cSrcweir }
359cdf0e10cSrcweir 
360cdf0e10cSrcweir // ------------------------------------------------------------------
361cdf0e10cSrcweir 
362cdf0e10cSrcweir sal_uLong BitmapEx::GetSizeBytes() const
363cdf0e10cSrcweir {
364cdf0e10cSrcweir 	sal_uLong nSizeBytes = aBitmap.GetSizeBytes();
365cdf0e10cSrcweir 
366cdf0e10cSrcweir 	if( eTransparent == TRANSPARENT_BITMAP )
367cdf0e10cSrcweir 		nSizeBytes += aMask.GetSizeBytes();
368cdf0e10cSrcweir 
369cdf0e10cSrcweir 	return nSizeBytes;
370cdf0e10cSrcweir }
371cdf0e10cSrcweir 
372cdf0e10cSrcweir // ------------------------------------------------------------------
373cdf0e10cSrcweir 
374cdf0e10cSrcweir sal_uLong BitmapEx::GetChecksum() const
375cdf0e10cSrcweir {
376cdf0e10cSrcweir 	sal_uInt32	nCrc = aBitmap.GetChecksum();
377cdf0e10cSrcweir 	SVBT32		aBT32;
378cdf0e10cSrcweir 
379cdf0e10cSrcweir 	UInt32ToSVBT32( (long) eTransparent, aBT32 );
380cdf0e10cSrcweir 	nCrc = rtl_crc32( nCrc, aBT32, 4 );
381cdf0e10cSrcweir 
382cdf0e10cSrcweir 	UInt32ToSVBT32( (long) bAlpha, aBT32 );
383cdf0e10cSrcweir 	nCrc = rtl_crc32( nCrc, aBT32, 4 );
384cdf0e10cSrcweir 
385cdf0e10cSrcweir 	if( ( TRANSPARENT_BITMAP == eTransparent ) && !aMask.IsEmpty() )
386cdf0e10cSrcweir 	{
387cdf0e10cSrcweir 		UInt32ToSVBT32( aMask.GetChecksum(), aBT32 );
388cdf0e10cSrcweir 		nCrc = rtl_crc32( nCrc, aBT32, 4 );
389cdf0e10cSrcweir 	}
390cdf0e10cSrcweir 
391cdf0e10cSrcweir 	return nCrc;
392cdf0e10cSrcweir }
393cdf0e10cSrcweir 
394cdf0e10cSrcweir // ------------------------------------------------------------------
395cdf0e10cSrcweir 
396*54628ca4SArmin Le Grand void BitmapEx::SetSizePixel( const Size& rNewSize, sal_uInt32 nScaleFlag )
397cdf0e10cSrcweir {
398*54628ca4SArmin Le Grand     if(GetSizePixel() != rNewSize)
399*54628ca4SArmin Le Grand     {
400*54628ca4SArmin Le Grand         Scale( rNewSize, nScaleFlag );
401*54628ca4SArmin Le Grand     }
402cdf0e10cSrcweir }
403cdf0e10cSrcweir 
404cdf0e10cSrcweir // ------------------------------------------------------------------
405cdf0e10cSrcweir 
406cdf0e10cSrcweir sal_Bool BitmapEx::Invert()
407cdf0e10cSrcweir {
408cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
409cdf0e10cSrcweir 
410cdf0e10cSrcweir 	if( !!aBitmap )
411cdf0e10cSrcweir 	{
412cdf0e10cSrcweir 		bRet = aBitmap.Invert();
413cdf0e10cSrcweir 
414cdf0e10cSrcweir 		if( bRet && ( eTransparent == TRANSPARENT_COLOR ) )
415cdf0e10cSrcweir 			aTransparentColor = BitmapColor( aTransparentColor ).Invert();
416cdf0e10cSrcweir 	}
417cdf0e10cSrcweir 
418cdf0e10cSrcweir 	return bRet;
419cdf0e10cSrcweir }
420cdf0e10cSrcweir 
421cdf0e10cSrcweir // ------------------------------------------------------------------
422cdf0e10cSrcweir 
423cdf0e10cSrcweir sal_Bool BitmapEx::Mirror( sal_uLong nMirrorFlags )
424cdf0e10cSrcweir {
425cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
426cdf0e10cSrcweir 
427cdf0e10cSrcweir 	if( !!aBitmap )
428cdf0e10cSrcweir 	{
429cdf0e10cSrcweir 		bRet = aBitmap.Mirror( nMirrorFlags );
430cdf0e10cSrcweir 
431cdf0e10cSrcweir 		if( bRet && ( eTransparent == TRANSPARENT_BITMAP ) && !!aMask )
432cdf0e10cSrcweir 			aMask.Mirror( nMirrorFlags );
433cdf0e10cSrcweir 	}
434cdf0e10cSrcweir 
435cdf0e10cSrcweir 	return bRet;
436cdf0e10cSrcweir }
437cdf0e10cSrcweir 
438cdf0e10cSrcweir // ------------------------------------------------------------------
439cdf0e10cSrcweir 
440*54628ca4SArmin Le Grand sal_Bool BitmapEx::Scale( const double& rScaleX, const double& rScaleY, sal_uInt32 nScaleFlag )
441cdf0e10cSrcweir {
442cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
443cdf0e10cSrcweir 
444cdf0e10cSrcweir 	if( !!aBitmap )
445cdf0e10cSrcweir 	{
446cdf0e10cSrcweir 		bRet = aBitmap.Scale( rScaleX, rScaleY, nScaleFlag );
447cdf0e10cSrcweir 
448cdf0e10cSrcweir 		if( bRet && ( eTransparent == TRANSPARENT_BITMAP ) && !!aMask )
44937ab0f2dSArmin Le Grand         {
45037ab0f2dSArmin Le Grand 			aMask.Scale( rScaleX, rScaleY, nScaleFlag );
45137ab0f2dSArmin Le Grand         }
452cdf0e10cSrcweir 
453cdf0e10cSrcweir 		aBitmapSize = aBitmap.GetSizePixel();
454cdf0e10cSrcweir 
455cdf0e10cSrcweir         DBG_ASSERT( !aMask || aBitmap.GetSizePixel() == aMask.GetSizePixel(),
456cdf0e10cSrcweir                     "BitmapEx::Scale(): size mismatch for bitmap and alpha mask." );
457cdf0e10cSrcweir 	}
458cdf0e10cSrcweir 
459cdf0e10cSrcweir 	return bRet;
460cdf0e10cSrcweir }
461cdf0e10cSrcweir 
462cdf0e10cSrcweir // ------------------------------------------------------------------------
463cdf0e10cSrcweir 
464*54628ca4SArmin Le Grand sal_Bool BitmapEx::Scale( const Size& rNewSize, sal_uInt32 nScaleFlag )
465cdf0e10cSrcweir {
466cdf0e10cSrcweir 	sal_Bool bRet;
467cdf0e10cSrcweir 
468cdf0e10cSrcweir 	if( aBitmapSize.Width() && aBitmapSize.Height() )
469cdf0e10cSrcweir 	{
470cdf0e10cSrcweir 		bRet = Scale( (double) rNewSize.Width() / aBitmapSize.Width(),
471cdf0e10cSrcweir 					  (double) rNewSize.Height() / aBitmapSize.Height(),
472cdf0e10cSrcweir 					  nScaleFlag );
473cdf0e10cSrcweir 	}
474cdf0e10cSrcweir 	else
475cdf0e10cSrcweir 		bRet = sal_True;
476cdf0e10cSrcweir 
477cdf0e10cSrcweir 	return bRet;
478cdf0e10cSrcweir }
479cdf0e10cSrcweir 
480cdf0e10cSrcweir // ------------------------------------------------------------------
481cdf0e10cSrcweir 
482cdf0e10cSrcweir sal_Bool BitmapEx::Rotate( long nAngle10, const Color& rFillColor )
483cdf0e10cSrcweir {
484cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
485cdf0e10cSrcweir 
486cdf0e10cSrcweir 	if( !!aBitmap )
487cdf0e10cSrcweir 	{
488cdf0e10cSrcweir 		const sal_Bool bTransRotate = ( Color( COL_TRANSPARENT ) == rFillColor );
489cdf0e10cSrcweir 
490cdf0e10cSrcweir 		if( bTransRotate )
491cdf0e10cSrcweir 		{
492cdf0e10cSrcweir 			if( eTransparent == TRANSPARENT_COLOR )
493cdf0e10cSrcweir 				bRet = aBitmap.Rotate( nAngle10, aTransparentColor );
494cdf0e10cSrcweir 			else
495cdf0e10cSrcweir 			{
496cdf0e10cSrcweir 				bRet = aBitmap.Rotate( nAngle10, COL_BLACK );
497cdf0e10cSrcweir 
498cdf0e10cSrcweir 				if( eTransparent == TRANSPARENT_NONE )
499cdf0e10cSrcweir 				{
500cdf0e10cSrcweir 					aMask = Bitmap( aBitmapSize, 1 );
501cdf0e10cSrcweir 					aMask.Erase( COL_BLACK );
502cdf0e10cSrcweir 					eTransparent = TRANSPARENT_BITMAP;
503cdf0e10cSrcweir 				}
504cdf0e10cSrcweir 
505cdf0e10cSrcweir 				if( bRet && !!aMask )
506cdf0e10cSrcweir 					aMask.Rotate( nAngle10, COL_WHITE );
507cdf0e10cSrcweir 			}
508cdf0e10cSrcweir 		}
509cdf0e10cSrcweir 		else
510cdf0e10cSrcweir 		{
511cdf0e10cSrcweir 			bRet = aBitmap.Rotate( nAngle10, rFillColor );
512cdf0e10cSrcweir 
513cdf0e10cSrcweir 			if( bRet && ( eTransparent == TRANSPARENT_BITMAP ) && !!aMask )
514cdf0e10cSrcweir 				aMask.Rotate( nAngle10, COL_WHITE );
515cdf0e10cSrcweir 		}
516cdf0e10cSrcweir 
517cdf0e10cSrcweir 		aBitmapSize = aBitmap.GetSizePixel();
518cdf0e10cSrcweir 
519cdf0e10cSrcweir         DBG_ASSERT( !aMask || aBitmap.GetSizePixel() == aMask.GetSizePixel(),
520cdf0e10cSrcweir                     "BitmapEx::Rotate(): size mismatch for bitmap and alpha mask." );
521cdf0e10cSrcweir 	}
522cdf0e10cSrcweir 
523cdf0e10cSrcweir 	return bRet;
524cdf0e10cSrcweir }
525cdf0e10cSrcweir 
526cdf0e10cSrcweir // ------------------------------------------------------------------
527cdf0e10cSrcweir 
528cdf0e10cSrcweir sal_Bool BitmapEx::Crop( const Rectangle& rRectPixel )
529cdf0e10cSrcweir {
530cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
531cdf0e10cSrcweir 
532cdf0e10cSrcweir 	if( !!aBitmap )
533cdf0e10cSrcweir 	{
534cdf0e10cSrcweir 		bRet = aBitmap.Crop( rRectPixel );
535cdf0e10cSrcweir 
536cdf0e10cSrcweir 		if( bRet && ( eTransparent == TRANSPARENT_BITMAP ) && !!aMask )
537cdf0e10cSrcweir 			aMask.Crop( rRectPixel );
538cdf0e10cSrcweir 
539cdf0e10cSrcweir 		aBitmapSize = aBitmap.GetSizePixel();
540cdf0e10cSrcweir 
541cdf0e10cSrcweir         DBG_ASSERT( !aMask || aBitmap.GetSizePixel() == aMask.GetSizePixel(),
542cdf0e10cSrcweir                     "BitmapEx::Crop(): size mismatch for bitmap and alpha mask." );
543cdf0e10cSrcweir 	}
544cdf0e10cSrcweir 
545cdf0e10cSrcweir 	return bRet;
546cdf0e10cSrcweir }
547cdf0e10cSrcweir 
548cdf0e10cSrcweir // ------------------------------------------------------------------
549cdf0e10cSrcweir 
550cdf0e10cSrcweir sal_Bool BitmapEx::Convert( BmpConversion eConversion )
551cdf0e10cSrcweir {
552cdf0e10cSrcweir 	return( !!aBitmap ? aBitmap.Convert( eConversion ) : sal_False );
553cdf0e10cSrcweir }
554cdf0e10cSrcweir 
555cdf0e10cSrcweir // ------------------------------------------------------------------
556cdf0e10cSrcweir 
557cdf0e10cSrcweir sal_Bool BitmapEx::ReduceColors( sal_uInt16 nNewColorCount, BmpReduce eReduce )
558cdf0e10cSrcweir {
559cdf0e10cSrcweir 	return( !!aBitmap ? aBitmap.ReduceColors( nNewColorCount, eReduce ) : sal_False );
560cdf0e10cSrcweir }
561cdf0e10cSrcweir 
562cdf0e10cSrcweir // ------------------------------------------------------------------
563cdf0e10cSrcweir 
564cdf0e10cSrcweir sal_Bool BitmapEx::Expand( sal_uLong nDX, sal_uLong nDY, const Color* pInitColor, sal_Bool bExpandTransparent )
565cdf0e10cSrcweir {
566cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
567cdf0e10cSrcweir 
568cdf0e10cSrcweir 	if( !!aBitmap )
569cdf0e10cSrcweir 	{
570cdf0e10cSrcweir 		bRet = aBitmap.Expand( nDX, nDY, pInitColor );
571cdf0e10cSrcweir 
572cdf0e10cSrcweir 		if( bRet && ( eTransparent == TRANSPARENT_BITMAP ) && !!aMask )
573cdf0e10cSrcweir 		{
574cdf0e10cSrcweir 			Color aColor( bExpandTransparent ? COL_WHITE : COL_BLACK );
575cdf0e10cSrcweir 			aMask.Expand( nDX, nDY, &aColor );
576cdf0e10cSrcweir 		}
577cdf0e10cSrcweir 
578cdf0e10cSrcweir 		aBitmapSize = aBitmap.GetSizePixel();
579cdf0e10cSrcweir 
580cdf0e10cSrcweir         DBG_ASSERT( !aMask || aBitmap.GetSizePixel() == aMask.GetSizePixel(),
581cdf0e10cSrcweir                     "BitmapEx::Expand(): size mismatch for bitmap and alpha mask." );
582cdf0e10cSrcweir 	}
583cdf0e10cSrcweir 
584cdf0e10cSrcweir 	return bRet;
585cdf0e10cSrcweir }
586cdf0e10cSrcweir 
587cdf0e10cSrcweir // ------------------------------------------------------------------
588cdf0e10cSrcweir 
589cdf0e10cSrcweir sal_Bool BitmapEx::CopyPixel( const Rectangle& rRectDst, const Rectangle& rRectSrc,
590cdf0e10cSrcweir 						  const BitmapEx* pBmpExSrc )
591cdf0e10cSrcweir {
592cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
593cdf0e10cSrcweir 
594cdf0e10cSrcweir 	if( !pBmpExSrc || pBmpExSrc->IsEmpty() )
595cdf0e10cSrcweir 	{
596cdf0e10cSrcweir 		if( !aBitmap.IsEmpty() )
597cdf0e10cSrcweir 		{
598cdf0e10cSrcweir 			bRet = aBitmap.CopyPixel( rRectDst, rRectSrc );
599cdf0e10cSrcweir 
600cdf0e10cSrcweir 			if( bRet && ( eTransparent == TRANSPARENT_BITMAP ) && !!aMask )
601cdf0e10cSrcweir 				aMask.CopyPixel( rRectDst, rRectSrc );
602cdf0e10cSrcweir 		}
603cdf0e10cSrcweir 	}
604cdf0e10cSrcweir 	else
605cdf0e10cSrcweir 	{
606cdf0e10cSrcweir 		if( !aBitmap.IsEmpty() )
607cdf0e10cSrcweir 		{
608cdf0e10cSrcweir 			bRet = aBitmap.CopyPixel( rRectDst, rRectSrc, &pBmpExSrc->aBitmap );
609cdf0e10cSrcweir 
610cdf0e10cSrcweir 			if( bRet )
611cdf0e10cSrcweir 			{
612cdf0e10cSrcweir 				if( pBmpExSrc->IsAlpha() )
613cdf0e10cSrcweir 				{
614cdf0e10cSrcweir 					if( IsAlpha() )
615cdf0e10cSrcweir                         // cast to use the optimized AlphaMask::CopyPixel
616cdf0e10cSrcweir 						((AlphaMask*) &aMask)->CopyPixel( rRectDst, rRectSrc, (AlphaMask*)&pBmpExSrc->aMask );
617cdf0e10cSrcweir 					else if( IsTransparent() )
618cdf0e10cSrcweir 					{
619cdf0e10cSrcweir 						AlphaMask* pAlpha = new AlphaMask( aMask );
620cdf0e10cSrcweir 
621cdf0e10cSrcweir 						aMask = pAlpha->ImplGetBitmap();
622cdf0e10cSrcweir 						delete pAlpha;
623cdf0e10cSrcweir 						bAlpha = sal_True;
624cdf0e10cSrcweir 						aMask.CopyPixel( rRectDst, rRectSrc, &pBmpExSrc->aMask );
625cdf0e10cSrcweir 					}
626cdf0e10cSrcweir 					else
627cdf0e10cSrcweir 					{
628cdf0e10cSrcweir 						sal_uInt8	cBlack = 0;
629cdf0e10cSrcweir 						AlphaMask*	pAlpha = new AlphaMask( GetSizePixel(), &cBlack );
630cdf0e10cSrcweir 
631cdf0e10cSrcweir 						aMask = pAlpha->ImplGetBitmap();
632cdf0e10cSrcweir 						delete pAlpha;
633cdf0e10cSrcweir 						eTransparent = TRANSPARENT_BITMAP;
634cdf0e10cSrcweir 						bAlpha = sal_True;
635cdf0e10cSrcweir 						aMask.CopyPixel( rRectDst, rRectSrc, &pBmpExSrc->aMask );
636cdf0e10cSrcweir 					}
637cdf0e10cSrcweir 				}
638cdf0e10cSrcweir 				else if( pBmpExSrc->IsTransparent() )
639cdf0e10cSrcweir 				{
640cdf0e10cSrcweir 					if( IsAlpha() )
641cdf0e10cSrcweir 					{
642cdf0e10cSrcweir 						AlphaMask aAlpha( pBmpExSrc->aMask );
643cdf0e10cSrcweir 						aMask.CopyPixel( rRectDst, rRectSrc, &aAlpha.ImplGetBitmap() );
644cdf0e10cSrcweir 					}
645cdf0e10cSrcweir 					else if( IsTransparent() )
646cdf0e10cSrcweir 						aMask.CopyPixel( rRectDst, rRectSrc, &pBmpExSrc->aMask );
647cdf0e10cSrcweir 					else
648cdf0e10cSrcweir 					{
649cdf0e10cSrcweir 						aMask = Bitmap( GetSizePixel(), 1 );
650cdf0e10cSrcweir 						aMask.Erase( Color( COL_BLACK ) );
651cdf0e10cSrcweir 						eTransparent = TRANSPARENT_BITMAP;
652cdf0e10cSrcweir 						aMask.CopyPixel( rRectDst, rRectSrc, &pBmpExSrc->aMask );
653cdf0e10cSrcweir 					}
654cdf0e10cSrcweir 				}
655cdf0e10cSrcweir                 else if( IsAlpha() )
656cdf0e10cSrcweir                 {
657cdf0e10cSrcweir                     sal_uInt8	      cBlack = 0;
658cdf0e10cSrcweir                     const AlphaMask   aAlphaSrc( pBmpExSrc->GetSizePixel(), &cBlack );
659cdf0e10cSrcweir 
660cdf0e10cSrcweir                     aMask.CopyPixel( rRectDst, rRectSrc, &aAlphaSrc.ImplGetBitmap() );
661cdf0e10cSrcweir                 }
662cdf0e10cSrcweir                 else if( IsTransparent() )
663cdf0e10cSrcweir                 {
664cdf0e10cSrcweir                     Bitmap aMaskSrc( pBmpExSrc->GetSizePixel(), 1 );
665cdf0e10cSrcweir 
666cdf0e10cSrcweir                     aMaskSrc.Erase( Color( COL_BLACK ) );
667cdf0e10cSrcweir                     aMask.CopyPixel( rRectDst, rRectSrc, &aMaskSrc );
668cdf0e10cSrcweir                 }
669cdf0e10cSrcweir 			}
670cdf0e10cSrcweir 		}
671cdf0e10cSrcweir 	}
672cdf0e10cSrcweir 
673cdf0e10cSrcweir 	return bRet;
674cdf0e10cSrcweir }
675cdf0e10cSrcweir 
676cdf0e10cSrcweir // ------------------------------------------------------------------
677cdf0e10cSrcweir 
678cdf0e10cSrcweir sal_Bool BitmapEx::Erase( const Color& rFillColor )
679cdf0e10cSrcweir {
680cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
681cdf0e10cSrcweir 
682cdf0e10cSrcweir 	if( !!aBitmap )
683cdf0e10cSrcweir 	{
684cdf0e10cSrcweir 		bRet = aBitmap.Erase( rFillColor );
685cdf0e10cSrcweir 
686cdf0e10cSrcweir 		if( bRet && ( eTransparent == TRANSPARENT_BITMAP ) && !!aMask )
687cdf0e10cSrcweir 		{
688cdf0e10cSrcweir             // #104416# Respect transparency on fill color
689cdf0e10cSrcweir             if( rFillColor.GetTransparency() )
690cdf0e10cSrcweir             {
691cdf0e10cSrcweir                 const Color aFill( rFillColor.GetTransparency(), rFillColor.GetTransparency(), rFillColor.GetTransparency() );
692cdf0e10cSrcweir                 aMask.Erase( aFill );
693cdf0e10cSrcweir             }
694cdf0e10cSrcweir             else
695cdf0e10cSrcweir             {
696cdf0e10cSrcweir                 const Color aBlack( COL_BLACK );
697cdf0e10cSrcweir                 aMask.Erase( aBlack );
698cdf0e10cSrcweir             }
699cdf0e10cSrcweir 		}
700cdf0e10cSrcweir 	}
701cdf0e10cSrcweir 
702cdf0e10cSrcweir 	return bRet;
703cdf0e10cSrcweir }
704cdf0e10cSrcweir 
705cdf0e10cSrcweir // ------------------------------------------------------------------
706cdf0e10cSrcweir 
707cdf0e10cSrcweir sal_Bool BitmapEx::Dither( sal_uLong nDitherFlags )
708cdf0e10cSrcweir {
709cdf0e10cSrcweir 	return( !!aBitmap ? aBitmap.Dither( nDitherFlags ) : sal_False );
710cdf0e10cSrcweir }
711cdf0e10cSrcweir 
712cdf0e10cSrcweir // ------------------------------------------------------------------
713cdf0e10cSrcweir 
714cdf0e10cSrcweir sal_Bool BitmapEx::Replace( const Color& rSearchColor, const Color& rReplaceColor, sal_uLong nTol )
715cdf0e10cSrcweir {
716cdf0e10cSrcweir 	return( !!aBitmap ? aBitmap.Replace( rSearchColor, rReplaceColor, nTol ) : sal_False );
717cdf0e10cSrcweir }
718cdf0e10cSrcweir 
719cdf0e10cSrcweir // ------------------------------------------------------------------
720cdf0e10cSrcweir 
721cdf0e10cSrcweir sal_Bool BitmapEx::Replace( const Color* pSearchColors, const Color* pReplaceColors, sal_uLong nColorCount, const sal_uLong* pTols )
722cdf0e10cSrcweir {
723cdf0e10cSrcweir 	return( !!aBitmap ? aBitmap.Replace( pSearchColors, pReplaceColors, nColorCount, (sal_uLong*) pTols ) : sal_False );
724cdf0e10cSrcweir }
725cdf0e10cSrcweir 
726cdf0e10cSrcweir // ------------------------------------------------------------------
727cdf0e10cSrcweir 
728cdf0e10cSrcweir sal_Bool BitmapEx::Adjust( short nLuminancePercent, short nContrastPercent,
729cdf0e10cSrcweir 					   short nChannelRPercent, short nChannelGPercent, short nChannelBPercent,
730cdf0e10cSrcweir 					   double fGamma, sal_Bool bInvert )
731cdf0e10cSrcweir {
732cdf0e10cSrcweir 	return( !!aBitmap ? aBitmap.Adjust( nLuminancePercent, nContrastPercent,
733cdf0e10cSrcweir 										nChannelRPercent, nChannelGPercent, nChannelBPercent,
734cdf0e10cSrcweir 										fGamma, bInvert ) : sal_False );
735cdf0e10cSrcweir }
736cdf0e10cSrcweir 
737cdf0e10cSrcweir // ------------------------------------------------------------------
738cdf0e10cSrcweir 
739cdf0e10cSrcweir sal_Bool BitmapEx::Filter( BmpFilter eFilter, const BmpFilterParam* pFilterParam, const Link* pProgress )
740cdf0e10cSrcweir {
741cdf0e10cSrcweir 	return( !!aBitmap ? aBitmap.Filter( eFilter, pFilterParam, pProgress ) : sal_False );
742cdf0e10cSrcweir }
743cdf0e10cSrcweir 
744cdf0e10cSrcweir // ------------------------------------------------------------------
745cdf0e10cSrcweir 
746cdf0e10cSrcweir void BitmapEx::Draw( OutputDevice* pOutDev, const Point& rDestPt ) const
747cdf0e10cSrcweir {
748cdf0e10cSrcweir 	pOutDev->DrawBitmapEx( rDestPt, *this );
749cdf0e10cSrcweir }
750cdf0e10cSrcweir 
751cdf0e10cSrcweir // ------------------------------------------------------------------
752cdf0e10cSrcweir 
753cdf0e10cSrcweir void BitmapEx::Draw( OutputDevice* pOutDev,
754cdf0e10cSrcweir 					 const Point& rDestPt, const Size& rDestSize ) const
755cdf0e10cSrcweir {
756cdf0e10cSrcweir 	pOutDev->DrawBitmapEx( rDestPt, rDestSize, *this );
757cdf0e10cSrcweir }
758cdf0e10cSrcweir 
759cdf0e10cSrcweir // ------------------------------------------------------------------
760cdf0e10cSrcweir 
761cdf0e10cSrcweir void BitmapEx::Draw( OutputDevice* pOutDev,
762cdf0e10cSrcweir 					 const Point& rDestPt, const Size& rDestSize,
763cdf0e10cSrcweir 					 const Point& rSrcPtPixel, const Size& rSrcSizePixel ) const
764cdf0e10cSrcweir {
765cdf0e10cSrcweir 	pOutDev->DrawBitmapEx( rDestPt, rDestSize, rSrcPtPixel, rSrcSizePixel, *this );
766cdf0e10cSrcweir }
767cdf0e10cSrcweir 
768cdf0e10cSrcweir // ------------------------------------------------------------------
769cdf0e10cSrcweir 
770cdf0e10cSrcweir sal_uInt8 BitmapEx::GetTransparency(sal_Int32 nX, sal_Int32 nY) const
771cdf0e10cSrcweir {
772cdf0e10cSrcweir     sal_uInt8 nTransparency(0xff);
773cdf0e10cSrcweir 
774cdf0e10cSrcweir     if(!aBitmap.IsEmpty())
775cdf0e10cSrcweir     {
776cdf0e10cSrcweir         if(nX >= 0 && nX < aBitmapSize.Width() && nY >= 0 && nY < aBitmapSize.Height())
777cdf0e10cSrcweir         {
778cdf0e10cSrcweir             switch(eTransparent)
779cdf0e10cSrcweir             {
780cdf0e10cSrcweir                 case TRANSPARENT_NONE:
781cdf0e10cSrcweir                 {
782cdf0e10cSrcweir                     // not transparent, ergo all covered
783cdf0e10cSrcweir                     nTransparency = 0x00;
784cdf0e10cSrcweir                     break;
785cdf0e10cSrcweir                 }
786cdf0e10cSrcweir                 case TRANSPARENT_COLOR:
787cdf0e10cSrcweir                 {
788cdf0e10cSrcweir                     Bitmap aTestBitmap(aBitmap);
789cdf0e10cSrcweir                     BitmapReadAccess* pRead = aTestBitmap.AcquireReadAccess();
790cdf0e10cSrcweir 
791cdf0e10cSrcweir                     if(pRead)
792cdf0e10cSrcweir                     {
793cdf0e10cSrcweir                         const Color aColor = pRead->GetColor(nY, nX);
794cdf0e10cSrcweir 
795cdf0e10cSrcweir                         // if color is not equal to TransparentColor, we are not transparent
796cdf0e10cSrcweir                         if(aColor != aTransparentColor)
797cdf0e10cSrcweir                         {
798cdf0e10cSrcweir                             nTransparency = 0x00;
799cdf0e10cSrcweir                         }
800cdf0e10cSrcweir 
801cdf0e10cSrcweir                         aTestBitmap.ReleaseAccess(pRead);
802cdf0e10cSrcweir                     }
803cdf0e10cSrcweir                     break;
804cdf0e10cSrcweir                 }
805cdf0e10cSrcweir                 case TRANSPARENT_BITMAP:
806cdf0e10cSrcweir                 {
807cdf0e10cSrcweir                     if(!aMask.IsEmpty())
808cdf0e10cSrcweir                     {
809cdf0e10cSrcweir                         Bitmap aTestBitmap(aMask);
810cdf0e10cSrcweir                         BitmapReadAccess* pRead = aTestBitmap.AcquireReadAccess();
811cdf0e10cSrcweir 
812cdf0e10cSrcweir                         if(pRead)
813cdf0e10cSrcweir                         {
814cdf0e10cSrcweir                             const BitmapColor aBitmapColor(pRead->GetPixel(nY, nX));
815cdf0e10cSrcweir 
816cdf0e10cSrcweir                             if(bAlpha)
817cdf0e10cSrcweir                             {
818cdf0e10cSrcweir                                 nTransparency = aBitmapColor.GetIndex();
819cdf0e10cSrcweir                             }
820cdf0e10cSrcweir                             else
821cdf0e10cSrcweir                             {
822cdf0e10cSrcweir                                 if(0x00 == aBitmapColor.GetIndex())
823cdf0e10cSrcweir                                 {
824cdf0e10cSrcweir                                     nTransparency = 0x00;
825cdf0e10cSrcweir                                 }
826cdf0e10cSrcweir                             }
827cdf0e10cSrcweir 
828cdf0e10cSrcweir                             aTestBitmap.ReleaseAccess(pRead);
829cdf0e10cSrcweir                         }
830cdf0e10cSrcweir                     }
831cdf0e10cSrcweir                     break;
832cdf0e10cSrcweir                 }
833cdf0e10cSrcweir             }
834cdf0e10cSrcweir         }
835cdf0e10cSrcweir     }
836cdf0e10cSrcweir 
837cdf0e10cSrcweir     return nTransparency;
838cdf0e10cSrcweir }
839cdf0e10cSrcweir 
840cdf0e10cSrcweir // ------------------------------------------------------------------
841cdf0e10cSrcweir 
842cdf0e10cSrcweir SvStream& operator<<( SvStream& rOStm, const BitmapEx& rBitmapEx )
843cdf0e10cSrcweir {
844cdf0e10cSrcweir 	rBitmapEx.aBitmap.Write( rOStm );
845cdf0e10cSrcweir 
846cdf0e10cSrcweir 	rOStm << (sal_uInt32) 0x25091962;
847cdf0e10cSrcweir 	rOStm << (sal_uInt32) 0xACB20201;
848cdf0e10cSrcweir 	rOStm << (sal_uInt8) rBitmapEx.eTransparent;
849cdf0e10cSrcweir 
850cdf0e10cSrcweir 	if( rBitmapEx.eTransparent == TRANSPARENT_BITMAP )
851cdf0e10cSrcweir 		rBitmapEx.aMask.Write( rOStm );
852cdf0e10cSrcweir 	else if( rBitmapEx.eTransparent == TRANSPARENT_COLOR )
853cdf0e10cSrcweir 		rOStm << rBitmapEx.aTransparentColor;
854cdf0e10cSrcweir 
855cdf0e10cSrcweir 	return rOStm;
856cdf0e10cSrcweir }
857cdf0e10cSrcweir 
858cdf0e10cSrcweir // ------------------------------------------------------------------
859cdf0e10cSrcweir 
860cdf0e10cSrcweir SvStream& operator>>( SvStream& rIStm, BitmapEx& rBitmapEx )
861cdf0e10cSrcweir {
862cdf0e10cSrcweir 	Bitmap aBmp;
863cdf0e10cSrcweir 
864cdf0e10cSrcweir 	rIStm >> aBmp;
865cdf0e10cSrcweir 
866cdf0e10cSrcweir 	if( !rIStm.GetError() )
867cdf0e10cSrcweir 	{
868cdf0e10cSrcweir 		const sal_uLong nStmPos = rIStm.Tell();
869cdf0e10cSrcweir 		sal_uInt32		nMagic1 = 0;
870cdf0e10cSrcweir 		sal_uInt32		nMagic2 = 0;
871cdf0e10cSrcweir 
872cdf0e10cSrcweir 		rIStm >> nMagic1 >> nMagic2;
873cdf0e10cSrcweir 
874cdf0e10cSrcweir 		if( ( nMagic1 != 0x25091962 ) || ( nMagic2 != 0xACB20201 ) || rIStm.GetError() )
875cdf0e10cSrcweir 		{
876cdf0e10cSrcweir 			rIStm.ResetError();
877cdf0e10cSrcweir 			rIStm.Seek( nStmPos );
878cdf0e10cSrcweir 			rBitmapEx = aBmp;
879cdf0e10cSrcweir 		}
880cdf0e10cSrcweir 		else
881cdf0e10cSrcweir 		{
882cdf0e10cSrcweir 			sal_uInt8 bTransparent = false;
883cdf0e10cSrcweir 
884cdf0e10cSrcweir 			rIStm >> bTransparent;
885cdf0e10cSrcweir 
886cdf0e10cSrcweir 			if( bTransparent == (sal_uInt8) TRANSPARENT_BITMAP )
887cdf0e10cSrcweir 			{
888cdf0e10cSrcweir 				Bitmap aMask;
889cdf0e10cSrcweir 
890cdf0e10cSrcweir 				rIStm >> aMask;
891cdf0e10cSrcweir 
892cdf0e10cSrcweir 				if( !!aMask)
893cdf0e10cSrcweir 				{
894cdf0e10cSrcweir 					// do we have an alpha mask?
895cdf0e10cSrcweir 					if( ( 8 == aMask.GetBitCount() ) && aMask.HasGreyPalette() )
896cdf0e10cSrcweir 					{
897cdf0e10cSrcweir 						AlphaMask aAlpha;
898cdf0e10cSrcweir 
899cdf0e10cSrcweir 						// create alpha mask quickly (without greyscale conversion)
900cdf0e10cSrcweir 						aAlpha.ImplSetBitmap( aMask );
901cdf0e10cSrcweir 						rBitmapEx = BitmapEx( aBmp, aAlpha );
902cdf0e10cSrcweir 					}
903cdf0e10cSrcweir 					else
904cdf0e10cSrcweir 						rBitmapEx = BitmapEx( aBmp, aMask );
905cdf0e10cSrcweir 				}
906cdf0e10cSrcweir 				else
907cdf0e10cSrcweir 					rBitmapEx = aBmp;
908cdf0e10cSrcweir 			}
909cdf0e10cSrcweir 			else if( bTransparent == (sal_uInt8) TRANSPARENT_COLOR )
910cdf0e10cSrcweir 			{
911cdf0e10cSrcweir 				Color aTransparentColor;
912cdf0e10cSrcweir 
913cdf0e10cSrcweir 				rIStm >> aTransparentColor;
914cdf0e10cSrcweir 				rBitmapEx = BitmapEx( aBmp, aTransparentColor );
915cdf0e10cSrcweir 			}
916cdf0e10cSrcweir 			else
917cdf0e10cSrcweir 				rBitmapEx = aBmp;
918cdf0e10cSrcweir 		}
919cdf0e10cSrcweir 	}
920cdf0e10cSrcweir 
921cdf0e10cSrcweir 	return rIStm;
922cdf0e10cSrcweir }
923