Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / vcl / source / gdi / alpha.cxx
blobfab58d0b0b520d043663296cd985f48d4635f6c0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <vcl/bitmapaccess.hxx>
21 #include <tools/color.hxx>
22 #include <vcl/alpha.hxx>
24 AlphaMask::AlphaMask()
28 AlphaMask::AlphaMask( const Bitmap& rBitmap ) :
29 Bitmap( rBitmap )
31 if( !!rBitmap )
32 Convert( BmpConversion::N8BitGreys );
35 AlphaMask::AlphaMask( const AlphaMask& rAlphaMask ) :
36 Bitmap( rAlphaMask )
40 AlphaMask::AlphaMask( AlphaMask&& rAlphaMask ) :
41 Bitmap( std::move(rAlphaMask) )
45 AlphaMask::AlphaMask( const Size& rSizePixel, sal_uInt8* pEraseTransparency ) :
46 Bitmap( rSizePixel, 8, &Bitmap::GetGreyPalette( 256 ) )
48 if( pEraseTransparency )
49 Bitmap::Erase( Color( *pEraseTransparency, *pEraseTransparency, *pEraseTransparency ) );
52 AlphaMask::~AlphaMask()
56 AlphaMask& AlphaMask::operator=( const Bitmap& rBitmap )
58 *static_cast<Bitmap*>(this) = rBitmap;
60 if( !!rBitmap )
61 Convert( BmpConversion::N8BitGreys );
63 return *this;
66 const Bitmap& AlphaMask::ImplGetBitmap() const
68 return( (const Bitmap&) *this );
71 void AlphaMask::ImplSetBitmap( const Bitmap& rBitmap )
73 SAL_WARN_IF( 8 != rBitmap.GetBitCount(), "vcl.gdi", "Bitmap should be 8bpp, not " << rBitmap.GetBitCount() << "bpp" );
74 SAL_WARN_IF( !rBitmap.HasGreyPalette(), "vcl.gdi", "Bitmap isn't greyscale" );
75 *static_cast<Bitmap*>(this) = rBitmap;
78 Bitmap AlphaMask::GetBitmap() const
80 return ImplGetBitmap();
83 bool AlphaMask::Erase( sal_uInt8 cTransparency )
85 return Bitmap::Erase( Color( cTransparency, cTransparency, cTransparency ) );
88 bool AlphaMask::Replace( const Bitmap& rMask, sal_uInt8 cReplaceTransparency )
90 Bitmap::ScopedReadAccess pMaskAcc( const_cast<Bitmap&>(rMask) );
91 AlphaMask::ScopedWriteAccess pAcc(*this);
92 bool bRet = false;
94 if( pMaskAcc && pAcc )
96 const BitmapColor aReplace( cReplaceTransparency );
97 const long nWidth = std::min( pMaskAcc->Width(), pAcc->Width() );
98 const long nHeight = std::min( pMaskAcc->Height(), pAcc->Height() );
99 const BitmapColor aMaskWhite( pMaskAcc->GetBestMatchingColor( Color( COL_WHITE ) ) );
101 for( long nY = 0L; nY < nHeight; nY++ )
102 for( long nX = 0L; nX < nWidth; nX++ )
103 if( pMaskAcc->GetPixel( nY, nX ) == aMaskWhite )
104 pAcc->SetPixel( nY, nX, aReplace );
106 return bRet;
109 bool AlphaMask::Replace( sal_uInt8 cSearchTransparency, sal_uInt8 cReplaceTransparency )
111 AlphaMask::ScopedWriteAccess pAcc(*this);
112 bool bRet = false;
114 if( pAcc && pAcc->GetBitCount() == 8 )
116 const long nWidth = pAcc->Width(), nHeight = pAcc->Height();
118 if( pAcc->GetScanlineFormat() == ScanlineFormat::N8BitPal )
120 for( long nY = 0L; nY < nHeight; nY++ )
122 Scanline pScan = pAcc->GetScanline( nY );
124 for( long nX = 0L; nX < nWidth; nX++, pScan++ )
126 if( *pScan == cSearchTransparency )
127 *pScan = cReplaceTransparency;
131 else
133 BitmapColor aReplace( cReplaceTransparency );
135 for( long nY = 0L; nY < nHeight; nY++ )
137 for( long nX = 0L; nX < nWidth; nX++ )
139 if( pAcc->GetPixel( nY, nX ).GetIndex() == cSearchTransparency )
140 pAcc->SetPixel( nY, nX, aReplace );
145 bRet = true;
148 return bRet;
151 void AlphaMask::ReleaseAccess( BitmapReadAccess* pAccess )
153 if( pAccess )
155 Bitmap::ReleaseAccess( pAccess );
156 Convert( BmpConversion::N8BitGreys );
160 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */