1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <tools/debug.hxx>
21 #include <vcl/bmpacc.hxx>
22 #include <tools/color.hxx>
23 #include <vcl/alpha.hxx>
25 AlphaMask::AlphaMask()
29 AlphaMask::AlphaMask( const Bitmap
& rBitmap
) :
33 Bitmap::Convert( BMP_CONVERSION_8BIT_GREYS
);
36 AlphaMask::AlphaMask( const AlphaMask
& rAlphaMask
) :
41 AlphaMask::AlphaMask( const Size
& rSizePixel
, sal_uInt8
* pEraseTransparency
) :
42 Bitmap( rSizePixel
, 8, &Bitmap::GetGreyPalette( 256 ) )
44 if( pEraseTransparency
)
45 Bitmap::Erase( Color( *pEraseTransparency
, *pEraseTransparency
, *pEraseTransparency
) );
48 AlphaMask::~AlphaMask()
52 AlphaMask
& AlphaMask::operator=( const Bitmap
& rBitmap
)
54 *(Bitmap
*) this = rBitmap
;
57 Bitmap::Convert( BMP_CONVERSION_8BIT_GREYS
);
62 const Bitmap
& AlphaMask::ImplGetBitmap() const
64 return( (const Bitmap
&) *this );
67 void AlphaMask::ImplSetBitmap( const Bitmap
& rBitmap
)
69 SAL_WARN_IF( 8 != rBitmap
.GetBitCount(), "vcl.gdi", "Bitmap should be 8bpp, not " << rBitmap
.GetBitCount() << "bpp" );
70 SAL_WARN_IF( !rBitmap
.HasGreyPalette(), "vcl.gdi", "Bitmap isn't greyscale" );
71 *(Bitmap
*) this = rBitmap
;
74 Bitmap
AlphaMask::GetBitmap() const
76 return ImplGetBitmap();
79 bool AlphaMask::Erase( sal_uInt8 cTransparency
)
81 return Bitmap::Erase( Color( cTransparency
, cTransparency
, cTransparency
) );
84 bool AlphaMask::Replace( const Bitmap
& rMask
, sal_uInt8 cReplaceTransparency
)
86 BitmapReadAccess
* pMaskAcc
= ( (Bitmap
&) rMask
).AcquireReadAccess();
87 BitmapWriteAccess
* pAcc
= AcquireWriteAccess();
90 if( pMaskAcc
&& pAcc
)
92 const BitmapColor
aReplace( cReplaceTransparency
);
93 const long nWidth
= std::min( pMaskAcc
->Width(), pAcc
->Width() );
94 const long nHeight
= std::min( pMaskAcc
->Height(), pAcc
->Height() );
95 const BitmapColor
aMaskWhite( pMaskAcc
->GetBestMatchingColor( Color( COL_WHITE
) ) );
97 for( long nY
= 0L; nY
< nHeight
; nY
++ )
98 for( long nX
= 0L; nX
< nWidth
; nX
++ )
99 if( pMaskAcc
->GetPixel( nY
, nX
) == aMaskWhite
)
100 pAcc
->SetPixel( nY
, nX
, aReplace
);
103 Bitmap::ReleaseAccess( pMaskAcc
);
104 ReleaseAccess( pAcc
);
109 bool AlphaMask::Replace( sal_uInt8 cSearchTransparency
, sal_uInt8 cReplaceTransparency
, sal_uLong
115 BitmapWriteAccess
* pAcc
= AcquireWriteAccess();
118 DBG_ASSERT( !nTol
, "AlphaMask::Replace: nTol not used yet" );
120 if( pAcc
&& pAcc
->GetBitCount() == 8 )
122 const long nWidth
= pAcc
->Width(), nHeight
= pAcc
->Height();
124 if( pAcc
->GetScanlineFormat() == BMP_FORMAT_8BIT_PAL
)
126 for( long nY
= 0L; nY
< nHeight
; nY
++ )
128 Scanline pScan
= pAcc
->GetScanline( nY
);
130 for( long nX
= 0L; nX
< nWidth
; nX
++, pScan
++ )
132 if( *pScan
== cSearchTransparency
)
133 *pScan
= cReplaceTransparency
;
139 BitmapColor
aReplace( cReplaceTransparency
);
141 for( long nY
= 0L; nY
< nHeight
; nY
++ )
143 for( long nX
= 0L; nX
< nWidth
; nX
++ )
145 if( pAcc
->GetPixel( nY
, nX
).GetIndex() == cSearchTransparency
)
146 pAcc
->SetPixel( nY
, nX
, aReplace
);
155 ReleaseAccess( pAcc
);
160 void AlphaMask::ReleaseAccess( BitmapReadAccess
* pAccess
)
164 Bitmap::ReleaseAccess( pAccess
);
165 Bitmap::Convert( BMP_CONVERSION_8BIT_GREYS
);
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */