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