xref: /aoo42x/main/vcl/source/gdi/alpha.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_vcl.hxx"
30*cdf0e10cSrcweir #include <tools/debug.hxx>
31*cdf0e10cSrcweir #include <vcl/bmpacc.hxx>
32*cdf0e10cSrcweir #include <tools/color.hxx>
33*cdf0e10cSrcweir #include <vcl/alpha.hxx>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir // -------------
36*cdf0e10cSrcweir // - AlphaMask -
37*cdf0e10cSrcweir // -------------
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir AlphaMask::AlphaMask()
40*cdf0e10cSrcweir {
41*cdf0e10cSrcweir }
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir // -----------------------------------------------------------------------------
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir AlphaMask::AlphaMask( const Bitmap& rBitmap ) :
46*cdf0e10cSrcweir 	Bitmap( rBitmap )
47*cdf0e10cSrcweir {
48*cdf0e10cSrcweir 	if( !!rBitmap )
49*cdf0e10cSrcweir 		Bitmap::Convert( BMP_CONVERSION_8BIT_GREYS );
50*cdf0e10cSrcweir }
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir // -----------------------------------------------------------------------------
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir AlphaMask::AlphaMask( const AlphaMask& rAlphaMask ) :
55*cdf0e10cSrcweir 	Bitmap( rAlphaMask )
56*cdf0e10cSrcweir {
57*cdf0e10cSrcweir }
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir // -----------------------------------------------------------------------------
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir AlphaMask::AlphaMask( const Size& rSizePixel, sal_uInt8* pEraseTransparency ) :
62*cdf0e10cSrcweir 	Bitmap( rSizePixel, 8, &Bitmap::GetGreyPalette( 256 ) )
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir 	if( pEraseTransparency )
65*cdf0e10cSrcweir 		Bitmap::Erase( Color( *pEraseTransparency, *pEraseTransparency, *pEraseTransparency ) );
66*cdf0e10cSrcweir }
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir // -----------------------------------------------------------------------------
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir AlphaMask::~AlphaMask()
71*cdf0e10cSrcweir {
72*cdf0e10cSrcweir }
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir // -----------------------------------------------------------------------------
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir AlphaMask& AlphaMask::operator=( const Bitmap& rBitmap )
77*cdf0e10cSrcweir {
78*cdf0e10cSrcweir 	*(Bitmap*) this = rBitmap;
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir 	if( !!rBitmap )
81*cdf0e10cSrcweir 		Bitmap::Convert( BMP_CONVERSION_8BIT_GREYS );
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir 	return *this;
84*cdf0e10cSrcweir }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir // -----------------------------------------------------------------------------
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir const Bitmap& AlphaMask::ImplGetBitmap() const
89*cdf0e10cSrcweir {
90*cdf0e10cSrcweir 	return( (const Bitmap&) *this );
91*cdf0e10cSrcweir }
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir // -----------------------------------------------------------------------------
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir void AlphaMask::ImplSetBitmap( const Bitmap& rBitmap )
96*cdf0e10cSrcweir {
97*cdf0e10cSrcweir     DBG_ASSERT( ( 8 == rBitmap.GetBitCount() ) && rBitmap.HasGreyPalette(), "AlphaMask::ImplSetBitmap: invalid bitmap" );
98*cdf0e10cSrcweir 	*(Bitmap*) this = rBitmap;
99*cdf0e10cSrcweir }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir // -----------------------------------------------------------------------------
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir Bitmap AlphaMask::GetBitmap() const
104*cdf0e10cSrcweir {
105*cdf0e10cSrcweir 	return ImplGetBitmap();
106*cdf0e10cSrcweir }
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir // -----------------------------------------------------------------------------
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir sal_Bool AlphaMask::Crop( const Rectangle& rRectPixel )
111*cdf0e10cSrcweir {
112*cdf0e10cSrcweir 	return Bitmap::Crop( rRectPixel );
113*cdf0e10cSrcweir }
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir // -----------------------------------------------------------------------------
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir sal_Bool AlphaMask::Expand( sal_uLong nDX, sal_uLong nDY, sal_uInt8* pInitTransparency )
118*cdf0e10cSrcweir {
119*cdf0e10cSrcweir 	Color aColor;
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 	if( pInitTransparency )
122*cdf0e10cSrcweir 		aColor = Color( *pInitTransparency, *pInitTransparency, *pInitTransparency );
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 	return Bitmap::Expand( nDX, nDY, pInitTransparency ? &aColor : NULL );
125*cdf0e10cSrcweir }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir // -----------------------------------------------------------------------------
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir sal_Bool AlphaMask::CopyPixel( const Rectangle& rRectDst, const Rectangle& rRectSrc,
130*cdf0e10cSrcweir 						   const AlphaMask* pAlphaSrc )
131*cdf0e10cSrcweir {
132*cdf0e10cSrcweir     // Note: this code is copied from Bitmap::CopyPixel but avoids any palette lookups
133*cdf0e10cSrcweir     // this optimization is possible because the palettes of AlphaMasks are always identical (8bit GreyPalette, see ctor)
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir 	const Size	aSizePix( GetSizePixel() );
136*cdf0e10cSrcweir 	Rectangle	aRectDst( rRectDst );
137*cdf0e10cSrcweir 	sal_Bool		bRet = sal_False;
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir 	aRectDst.Intersection( Rectangle( Point(), aSizePix ) );
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir 	if( !aRectDst.IsEmpty() )
142*cdf0e10cSrcweir 	{
143*cdf0e10cSrcweir 		if( pAlphaSrc && ( *pAlphaSrc != *this ) )
144*cdf0e10cSrcweir 		{
145*cdf0e10cSrcweir 			Bitmap* 		pSrc = (Bitmap*) pAlphaSrc;
146*cdf0e10cSrcweir 			const Size		aCopySizePix( pSrc->GetSizePixel() );
147*cdf0e10cSrcweir 			Rectangle		aRectSrc( rRectSrc );
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir 			aRectSrc.Intersection( Rectangle( Point(), aCopySizePix ) );
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 			if( !aRectSrc.IsEmpty() )
152*cdf0e10cSrcweir 			{
153*cdf0e10cSrcweir 				BitmapReadAccess* pReadAcc = pSrc->AcquireReadAccess();
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir 				if( pReadAcc )
156*cdf0e10cSrcweir 				{
157*cdf0e10cSrcweir 					BitmapWriteAccess* pWriteAcc = AcquireWriteAccess();
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 					if( pWriteAcc )
160*cdf0e10cSrcweir 					{
161*cdf0e10cSrcweir 						const long	nWidth = Min( aRectSrc.GetWidth(), aRectDst.GetWidth() );
162*cdf0e10cSrcweir 						const long	nHeight = Min( aRectSrc.GetHeight(), aRectDst.GetHeight() );
163*cdf0e10cSrcweir 						const long	nSrcEndX = aRectSrc.Left() + nWidth;
164*cdf0e10cSrcweir 						const long	nSrcEndY = aRectSrc.Top() + nHeight;
165*cdf0e10cSrcweir 						long		nDstY = aRectDst.Top();
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 						for( long nSrcY = aRectSrc.Top(); nSrcY < nSrcEndY; nSrcY++, nDstY++ )
168*cdf0e10cSrcweir 							for( long nSrcX = aRectSrc.Left(), nDstX = aRectDst.Left(); nSrcX < nSrcEndX; nSrcX++, nDstX++ )
169*cdf0e10cSrcweir 								pWriteAcc->SetPixel( nDstY, nDstX, pReadAcc->GetPixel( nSrcY, nSrcX ) );
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir 						ReleaseAccess( pWriteAcc );
172*cdf0e10cSrcweir 						bRet = ( nWidth > 0L ) && ( nHeight > 0L );
173*cdf0e10cSrcweir 					}
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir 					pSrc->ReleaseAccess( pReadAcc );
176*cdf0e10cSrcweir 				}
177*cdf0e10cSrcweir 			}
178*cdf0e10cSrcweir 		}
179*cdf0e10cSrcweir 		else
180*cdf0e10cSrcweir 		{
181*cdf0e10cSrcweir 			Rectangle aRectSrc( rRectSrc );
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir 			aRectSrc.Intersection( Rectangle( Point(), aSizePix ) );
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 			if( !aRectSrc.IsEmpty() && ( aRectSrc != aRectDst ) )
186*cdf0e10cSrcweir 			{
187*cdf0e10cSrcweir 				BitmapWriteAccess*	pWriteAcc = AcquireWriteAccess();
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir 				if( pWriteAcc )
190*cdf0e10cSrcweir 				{
191*cdf0e10cSrcweir 					const long	nWidth = Min( aRectSrc.GetWidth(), aRectDst.GetWidth() );
192*cdf0e10cSrcweir 					const long	nHeight = Min( aRectSrc.GetHeight(), aRectDst.GetHeight() );
193*cdf0e10cSrcweir 					const long	nSrcX = aRectSrc.Left();
194*cdf0e10cSrcweir 					const long	nSrcY = aRectSrc.Top();
195*cdf0e10cSrcweir 					const long	nSrcEndX1 = nSrcX + nWidth - 1L;
196*cdf0e10cSrcweir 					const long	nSrcEndY1 = nSrcY + nHeight - 1L;
197*cdf0e10cSrcweir 					const long	nDstX = aRectDst.Left();
198*cdf0e10cSrcweir 					const long	nDstY = aRectDst.Top();
199*cdf0e10cSrcweir 					const long	nDstEndX1 = nDstX + nWidth - 1L;
200*cdf0e10cSrcweir 					const long	nDstEndY1 = nDstY + nHeight - 1L;
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir 					if( ( nDstX <= nSrcX ) && ( nDstY <= nSrcY ) )
203*cdf0e10cSrcweir 					{
204*cdf0e10cSrcweir 						for( long nY = nSrcY, nYN = nDstY; nY <= nSrcEndY1; nY++, nYN++ )
205*cdf0e10cSrcweir 							for( long nX = nSrcX, nXN = nDstX; nX <= nSrcEndX1; nX++, nXN++ )
206*cdf0e10cSrcweir 								pWriteAcc->SetPixel( nYN, nXN, pWriteAcc->GetPixel( nY, nX ) );
207*cdf0e10cSrcweir 					}
208*cdf0e10cSrcweir 					else if( ( nDstX <= nSrcX ) && ( nDstY >= nSrcY ) )
209*cdf0e10cSrcweir 					{
210*cdf0e10cSrcweir 						for( long nY = nSrcEndY1, nYN = nDstEndY1; nY >= nSrcY; nY--, nYN-- )
211*cdf0e10cSrcweir 							for( long nX = nSrcX, nXN = nDstX; nX <= nSrcEndX1; nX++, nXN++ )
212*cdf0e10cSrcweir 								pWriteAcc->SetPixel( nYN, nXN, pWriteAcc->GetPixel( nY, nX ) );
213*cdf0e10cSrcweir 					}
214*cdf0e10cSrcweir 					else if( ( nDstX >= nSrcX ) && ( nDstY <= nSrcY ) )
215*cdf0e10cSrcweir 					{
216*cdf0e10cSrcweir 						for( long nY = nSrcY, nYN = nDstY; nY <= nSrcEndY1; nY++, nYN++ )
217*cdf0e10cSrcweir 							for( long nX = nSrcEndX1, nXN = nDstEndX1; nX >= nSrcX; nX--, nXN-- )
218*cdf0e10cSrcweir 								pWriteAcc->SetPixel( nYN, nXN, pWriteAcc->GetPixel( nY, nX ) );
219*cdf0e10cSrcweir 					}
220*cdf0e10cSrcweir 					else
221*cdf0e10cSrcweir 					{
222*cdf0e10cSrcweir 						for( long nY = nSrcEndY1, nYN = nDstEndY1; nY >= nSrcY; nY--, nYN-- )
223*cdf0e10cSrcweir 							for( long nX = nSrcEndX1, nXN = nDstEndX1; nX >= nSrcX; nX--, nXN-- )
224*cdf0e10cSrcweir 								pWriteAcc->SetPixel( nYN, nXN, pWriteAcc->GetPixel( nY, nX ) );
225*cdf0e10cSrcweir 					}
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir 					ReleaseAccess( pWriteAcc );
228*cdf0e10cSrcweir 					bRet = sal_True;
229*cdf0e10cSrcweir 				}
230*cdf0e10cSrcweir 			}
231*cdf0e10cSrcweir 		}
232*cdf0e10cSrcweir 	}
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir 	return bRet;
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir }
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir // -----------------------------------------------------------------------------
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir sal_Bool AlphaMask::Erase( sal_uInt8 cTransparency )
241*cdf0e10cSrcweir {
242*cdf0e10cSrcweir 	return Bitmap::Erase( Color( cTransparency, cTransparency, cTransparency ) );
243*cdf0e10cSrcweir }
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir // -----------------------------------------------------------------------------
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir sal_Bool AlphaMask::Invert()
248*cdf0e10cSrcweir {
249*cdf0e10cSrcweir 	BitmapWriteAccess*	pAcc = AcquireWriteAccess();
250*cdf0e10cSrcweir 	sal_Bool				bRet = sal_False;
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir 	if( pAcc && pAcc->GetBitCount() == 8 )
253*cdf0e10cSrcweir 	{
254*cdf0e10cSrcweir 		BitmapColor	aCol( 0 );
255*cdf0e10cSrcweir 		const long	nWidth = pAcc->Width(),	nHeight = pAcc->Height();
256*cdf0e10cSrcweir 		sal_uInt8*		pMap = new sal_uInt8[ 256 ];
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir 		for( long i = 0; i < 256; i++ )
259*cdf0e10cSrcweir 			pMap[ i ] = ~(sal_uInt8) i;
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir 		for( long nY = 0L; nY < nHeight; nY++ )
262*cdf0e10cSrcweir 		{
263*cdf0e10cSrcweir 			for( long nX = 0L; nX < nWidth; nX++ )
264*cdf0e10cSrcweir 			{
265*cdf0e10cSrcweir 				aCol.SetIndex( pMap[ pAcc->GetPixel( nY, nX ).GetIndex() ] );
266*cdf0e10cSrcweir 				pAcc->SetPixel( nY, nX, aCol );
267*cdf0e10cSrcweir 			}
268*cdf0e10cSrcweir 		}
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir 		delete[] pMap;
271*cdf0e10cSrcweir 		bRet = sal_True;
272*cdf0e10cSrcweir 	}
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir 	if( pAcc )
275*cdf0e10cSrcweir 		ReleaseAccess( pAcc );
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir 	return bRet;
278*cdf0e10cSrcweir }
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir // -----------------------------------------------------------------------------
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir sal_Bool AlphaMask::Mirror( sal_uLong nMirrorFlags )
283*cdf0e10cSrcweir {
284*cdf0e10cSrcweir 	return Bitmap::Mirror( nMirrorFlags );
285*cdf0e10cSrcweir }
286*cdf0e10cSrcweir 
287*cdf0e10cSrcweir // -----------------------------------------------------------------------------
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir sal_Bool AlphaMask::Scale( const Size& rNewSize, sal_uLong nScaleFlag )
290*cdf0e10cSrcweir {
291*cdf0e10cSrcweir 	sal_Bool bRet = Bitmap::Scale( rNewSize, nScaleFlag );
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir 	if( bRet && ( nScaleFlag == BMP_SCALE_INTERPOLATE ) )
294*cdf0e10cSrcweir 		Bitmap::Convert( BMP_CONVERSION_8BIT_GREYS );
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir 	return bRet;
297*cdf0e10cSrcweir }
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir // -----------------------------------------------------------------------------
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir sal_Bool AlphaMask::Scale( const double& rScaleX, const double& rScaleY, sal_uLong nScaleFlag )
302*cdf0e10cSrcweir {
303*cdf0e10cSrcweir 	sal_Bool bRet = Bitmap::Scale( rScaleX, rScaleY, nScaleFlag );
304*cdf0e10cSrcweir 
305*cdf0e10cSrcweir 	if( bRet && ( nScaleFlag == BMP_SCALE_INTERPOLATE ) )
306*cdf0e10cSrcweir 		Bitmap::Convert( BMP_CONVERSION_8BIT_GREYS );
307*cdf0e10cSrcweir 
308*cdf0e10cSrcweir 	return bRet;
309*cdf0e10cSrcweir }
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir // -----------------------------------------------------------------------------
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir sal_Bool AlphaMask::Rotate( long nAngle10, sal_uInt8 cFillTransparency )
314*cdf0e10cSrcweir {
315*cdf0e10cSrcweir 	return Bitmap::Rotate( nAngle10, Color( cFillTransparency, cFillTransparency, cFillTransparency ) );
316*cdf0e10cSrcweir }
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir // -----------------------------------------------------------------------------
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir sal_Bool AlphaMask::Replace( const Bitmap& rMask, sal_uInt8 cReplaceTransparency )
321*cdf0e10cSrcweir {
322*cdf0e10cSrcweir 	BitmapReadAccess*	pMaskAcc = ( (Bitmap&) rMask ).AcquireReadAccess();
323*cdf0e10cSrcweir 	BitmapWriteAccess*	pAcc = AcquireWriteAccess();
324*cdf0e10cSrcweir 	sal_Bool				bRet = sal_False;
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir 	if( pMaskAcc && pAcc )
327*cdf0e10cSrcweir 	{
328*cdf0e10cSrcweir 		const BitmapColor	aReplace( cReplaceTransparency );
329*cdf0e10cSrcweir 		const long			nWidth = Min( pMaskAcc->Width(), pAcc->Width() );
330*cdf0e10cSrcweir 		const long			nHeight = Min( pMaskAcc->Height(), pAcc->Height() );
331*cdf0e10cSrcweir 		const BitmapColor	aMaskWhite( pMaskAcc->GetBestMatchingColor( Color( COL_WHITE ) ) );
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir 		for( long nY = 0L; nY < nHeight; nY++ )
334*cdf0e10cSrcweir 			for( long nX = 0L; nX < nWidth; nX++ )
335*cdf0e10cSrcweir 				if( pMaskAcc->GetPixel( nY, nX ) == aMaskWhite )
336*cdf0e10cSrcweir 					pAcc->SetPixel( nY, nX, aReplace );
337*cdf0e10cSrcweir 	}
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir 	( (Bitmap&) rMask ).ReleaseAccess( pMaskAcc );
340*cdf0e10cSrcweir 	ReleaseAccess( pAcc );
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir 	return bRet;
343*cdf0e10cSrcweir }
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir // -----------------------------------------------------------------------------
346*cdf0e10cSrcweir 
347*cdf0e10cSrcweir sal_Bool AlphaMask::Replace( sal_uInt8 cSearchTransparency, sal_uInt8 cReplaceTransparency, sal_uLong
348*cdf0e10cSrcweir #ifdef DBG_UTIL
349*cdf0e10cSrcweir nTol
350*cdf0e10cSrcweir #endif
351*cdf0e10cSrcweir )
352*cdf0e10cSrcweir {
353*cdf0e10cSrcweir 	BitmapWriteAccess*	pAcc = AcquireWriteAccess();
354*cdf0e10cSrcweir 	sal_Bool				bRet = sal_False;
355*cdf0e10cSrcweir 
356*cdf0e10cSrcweir 	DBG_ASSERT( !nTol, "AlphaMask::Replace: nTol not used yet" );
357*cdf0e10cSrcweir 
358*cdf0e10cSrcweir 	if( pAcc && pAcc->GetBitCount() == 8 )
359*cdf0e10cSrcweir 	{
360*cdf0e10cSrcweir 		const long nWidth = pAcc->Width(), nHeight = pAcc->Height();
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir 		if( pAcc->GetScanlineFormat() == BMP_FORMAT_8BIT_PAL )
363*cdf0e10cSrcweir 		{
364*cdf0e10cSrcweir 			for( long nY = 0L; nY < nHeight; nY++ )
365*cdf0e10cSrcweir 			{
366*cdf0e10cSrcweir 				Scanline pScan = pAcc->GetScanline( nY );
367*cdf0e10cSrcweir 
368*cdf0e10cSrcweir 				for( long nX = 0L; nX < nWidth; nX++, pScan++ )
369*cdf0e10cSrcweir 				{
370*cdf0e10cSrcweir 					if( *pScan == cSearchTransparency )
371*cdf0e10cSrcweir 						*pScan = cReplaceTransparency;
372*cdf0e10cSrcweir 				}
373*cdf0e10cSrcweir 			}
374*cdf0e10cSrcweir 		}
375*cdf0e10cSrcweir 		else
376*cdf0e10cSrcweir 		{
377*cdf0e10cSrcweir 			BitmapColor	aReplace( cReplaceTransparency );
378*cdf0e10cSrcweir 
379*cdf0e10cSrcweir 			for( long nY = 0L; nY < nHeight; nY++ )
380*cdf0e10cSrcweir 			{
381*cdf0e10cSrcweir 				for( long nX = 0L; nX < nWidth; nX++ )
382*cdf0e10cSrcweir 				{
383*cdf0e10cSrcweir 					if( pAcc->GetPixel( nY, nX ).GetIndex() == cSearchTransparency )
384*cdf0e10cSrcweir 						pAcc->SetPixel( nY, nX, aReplace );
385*cdf0e10cSrcweir 				}
386*cdf0e10cSrcweir 			}
387*cdf0e10cSrcweir 		}
388*cdf0e10cSrcweir 
389*cdf0e10cSrcweir 		bRet = sal_True;
390*cdf0e10cSrcweir 	}
391*cdf0e10cSrcweir 
392*cdf0e10cSrcweir 	if( pAcc )
393*cdf0e10cSrcweir 		ReleaseAccess( pAcc );
394*cdf0e10cSrcweir 
395*cdf0e10cSrcweir 	return bRet;
396*cdf0e10cSrcweir }
397*cdf0e10cSrcweir 
398*cdf0e10cSrcweir // -----------------------------------------------------------------------------
399*cdf0e10cSrcweir 
400*cdf0e10cSrcweir sal_Bool AlphaMask::Replace( sal_uInt8* pSearchTransparencies, sal_uInt8* pReplaceTransparencies,
401*cdf0e10cSrcweir 						 sal_uLong nColorCount, sal_uLong* pTols )
402*cdf0e10cSrcweir {
403*cdf0e10cSrcweir 	Color*	pSearchColors = new Color[ nColorCount ];
404*cdf0e10cSrcweir 	Color*	pReplaceColors = new Color[ nColorCount ];
405*cdf0e10cSrcweir 	sal_Bool	bRet;
406*cdf0e10cSrcweir 
407*cdf0e10cSrcweir 	for( sal_uLong i = 0; i < nColorCount; i++ )
408*cdf0e10cSrcweir 	{
409*cdf0e10cSrcweir 		const sal_uInt8 cSearchTransparency = pSearchTransparencies[ i ];
410*cdf0e10cSrcweir 		const sal_uInt8 cReplaceTransparency = pReplaceTransparencies[ i ];
411*cdf0e10cSrcweir 
412*cdf0e10cSrcweir 		pSearchColors[ i ] = Color( cSearchTransparency, cSearchTransparency, cSearchTransparency );
413*cdf0e10cSrcweir 		pReplaceColors[ i ] = Color( cReplaceTransparency, cReplaceTransparency, cReplaceTransparency );
414*cdf0e10cSrcweir 	}
415*cdf0e10cSrcweir 
416*cdf0e10cSrcweir 	bRet = Bitmap::Replace( pSearchColors, pReplaceColors, nColorCount, pTols ) &&
417*cdf0e10cSrcweir 		   Bitmap::Convert( BMP_CONVERSION_8BIT_GREYS );
418*cdf0e10cSrcweir 
419*cdf0e10cSrcweir 	delete[] pSearchColors;
420*cdf0e10cSrcweir 	delete[] pReplaceColors;
421*cdf0e10cSrcweir 
422*cdf0e10cSrcweir 	return bRet;
423*cdf0e10cSrcweir }
424*cdf0e10cSrcweir 
425*cdf0e10cSrcweir // -----------------------------------------------------------------------------
426*cdf0e10cSrcweir 
427*cdf0e10cSrcweir void AlphaMask::ReleaseAccess( BitmapReadAccess* pAccess )
428*cdf0e10cSrcweir {
429*cdf0e10cSrcweir 	if( pAccess )
430*cdf0e10cSrcweir 	{
431*cdf0e10cSrcweir 		Bitmap::ReleaseAccess( pAccess );
432*cdf0e10cSrcweir 		Bitmap::Convert( BMP_CONVERSION_8BIT_GREYS );
433*cdf0e10cSrcweir 	}
434*cdf0e10cSrcweir }
435