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 <vcl/bitmapaccess.hxx>
21 #include <tools/color.hxx>
22 #include <vcl/alpha.hxx>
23 #include <bitmapwriteaccess.hxx>
24 #include <sal/log.hxx>
26 AlphaMask::AlphaMask() = default;
28 AlphaMask::AlphaMask( const Bitmap
& rBitmap
) :
32 Convert( BmpConversion::N8BitGreys
);
35 AlphaMask::AlphaMask( const AlphaMask
& ) = default;
37 AlphaMask::AlphaMask( AlphaMask
&& ) = default;
39 AlphaMask::AlphaMask( const Size
& rSizePixel
, const sal_uInt8
* pEraseTransparency
) :
40 Bitmap( rSizePixel
, 8, &Bitmap::GetGreyPalette( 256 ) )
42 if( pEraseTransparency
)
43 Bitmap::Erase( Color( *pEraseTransparency
, *pEraseTransparency
, *pEraseTransparency
) );
46 AlphaMask::~AlphaMask() = default;
48 AlphaMask
& AlphaMask::operator=( const Bitmap
& rBitmap
)
50 *static_cast<Bitmap
*>(this) = rBitmap
;
53 Convert( BmpConversion::N8BitGreys
);
58 const Bitmap
& AlphaMask::ImplGetBitmap() const
63 void AlphaMask::ImplSetBitmap( const Bitmap
& rBitmap
)
65 SAL_WARN_IF( 8 != rBitmap
.GetBitCount(), "vcl.gdi", "Bitmap should be 8bpp, not " << rBitmap
.GetBitCount() << "bpp" );
66 SAL_WARN_IF( !rBitmap
.HasGreyPalette(), "vcl.gdi", "Bitmap isn't greyscale" );
67 *static_cast<Bitmap
*>(this) = rBitmap
;
70 Bitmap
const & AlphaMask::GetBitmap() const
72 return ImplGetBitmap();
75 void AlphaMask::Erase( sal_uInt8 cTransparency
)
77 Bitmap::Erase( Color( cTransparency
, cTransparency
, cTransparency
) );
80 void AlphaMask::Replace( const Bitmap
& rMask
, sal_uInt8 cReplaceTransparency
)
82 Bitmap::ScopedReadAccess
pMaskAcc( const_cast<Bitmap
&>(rMask
) );
83 AlphaScopedWriteAccess
pAcc(*this);
85 if( pMaskAcc
&& pAcc
)
87 const BitmapColor
aReplace( cReplaceTransparency
);
88 const long nWidth
= std::min( pMaskAcc
->Width(), pAcc
->Width() );
89 const long nHeight
= std::min( pMaskAcc
->Height(), pAcc
->Height() );
90 const BitmapColor
aMaskWhite( pMaskAcc
->GetBestMatchingColor( COL_WHITE
) );
92 for( long nY
= 0; nY
< nHeight
; nY
++ )
94 Scanline pScanline
= pAcc
->GetScanline(nY
);
95 Scanline pScanlineMask
= pMaskAcc
->GetScanline(nY
);
96 for( long nX
= 0; nX
< nWidth
; nX
++ )
97 if( pMaskAcc
->GetPixelFromData( pScanlineMask
, nX
) == aMaskWhite
)
98 pAcc
->SetPixelOnData( pScanline
, nX
, aReplace
);
103 void AlphaMask::Replace( sal_uInt8 cSearchTransparency
, sal_uInt8 cReplaceTransparency
)
105 AlphaScopedWriteAccess
pAcc(*this);
107 if( pAcc
&& pAcc
->GetBitCount() == 8 )
109 const long nWidth
= pAcc
->Width(), nHeight
= pAcc
->Height();
111 if( pAcc
->GetScanlineFormat() == ScanlineFormat::N8BitPal
)
113 for( long nY
= 0; nY
< nHeight
; nY
++ )
115 Scanline pScan
= pAcc
->GetScanline( nY
);
117 for( long nX
= 0; nX
< nWidth
; nX
++, pScan
++ )
119 if( *pScan
== cSearchTransparency
)
120 *pScan
= cReplaceTransparency
;
126 BitmapColor
aReplace( cReplaceTransparency
);
128 for( long nY
= 0; nY
< nHeight
; nY
++ )
130 Scanline pScanline
= pAcc
->GetScanline(nY
);
131 for( long nX
= 0; nX
< nWidth
; nX
++ )
133 if( pAcc
->GetIndexFromData( pScanline
, nX
) == cSearchTransparency
)
134 pAcc
->SetPixelOnData( pScanline
, nX
, aReplace
);
141 void AlphaMask::ReleaseAccess( BitmapReadAccess
* pAccess
)
145 Bitmap::ReleaseAccess( pAccess
);
146 Convert( BmpConversion::N8BitGreys
);
150 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */