Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <tools/debug.hxx>
21 : #include <vcl/bmpacc.hxx>
22 : #include <tools/color.hxx>
23 : #include <vcl/alpha.hxx>
24 :
25 207174 : AlphaMask::AlphaMask()
26 : {
27 207174 : }
28 :
29 19726 : AlphaMask::AlphaMask( const Bitmap& rBitmap ) :
30 19726 : Bitmap( rBitmap )
31 : {
32 19726 : if( !!rBitmap )
33 19709 : Bitmap::Convert( BMP_CONVERSION_8BIT_GREYS );
34 19726 : }
35 :
36 207116 : AlphaMask::AlphaMask( const AlphaMask& rAlphaMask ) :
37 207116 : Bitmap( rAlphaMask )
38 : {
39 207116 : }
40 :
41 23222 : AlphaMask::AlphaMask( const Size& rSizePixel, sal_uInt8* pEraseTransparency ) :
42 23222 : Bitmap( rSizePixel, 8, &Bitmap::GetGreyPalette( 256 ) )
43 : {
44 23222 : if( pEraseTransparency )
45 6731 : Bitmap::Erase( Color( *pEraseTransparency, *pEraseTransparency, *pEraseTransparency ) );
46 23222 : }
47 :
48 469701 : AlphaMask::~AlphaMask()
49 : {
50 469701 : }
51 :
52 9 : AlphaMask& AlphaMask::operator=( const Bitmap& rBitmap )
53 : {
54 9 : *static_cast<Bitmap*>(this) = rBitmap;
55 :
56 9 : if( !!rBitmap )
57 9 : Bitmap::Convert( BMP_CONVERSION_8BIT_GREYS );
58 :
59 9 : return *this;
60 : }
61 :
62 55535 : const Bitmap& AlphaMask::ImplGetBitmap() const
63 : {
64 55535 : return( (const Bitmap&) *this );
65 : }
66 :
67 207133 : void AlphaMask::ImplSetBitmap( const Bitmap& rBitmap )
68 : {
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 207133 : *static_cast<Bitmap*>(this) = rBitmap;
72 207133 : }
73 :
74 4425 : Bitmap AlphaMask::GetBitmap() const
75 : {
76 4425 : return ImplGetBitmap();
77 : }
78 :
79 10140 : bool AlphaMask::Erase( sal_uInt8 cTransparency )
80 : {
81 10140 : return Bitmap::Erase( Color( cTransparency, cTransparency, cTransparency ) );
82 : }
83 :
84 0 : bool AlphaMask::Replace( const Bitmap& rMask, sal_uInt8 cReplaceTransparency )
85 : {
86 0 : BitmapReadAccess* pMaskAcc = ( (Bitmap&) rMask ).AcquireReadAccess();
87 0 : BitmapWriteAccess* pAcc = AcquireWriteAccess();
88 0 : bool bRet = false;
89 :
90 0 : if( pMaskAcc && pAcc )
91 : {
92 0 : const BitmapColor aReplace( cReplaceTransparency );
93 0 : const long nWidth = std::min( pMaskAcc->Width(), pAcc->Width() );
94 0 : const long nHeight = std::min( pMaskAcc->Height(), pAcc->Height() );
95 0 : const BitmapColor aMaskWhite( pMaskAcc->GetBestMatchingColor( Color( COL_WHITE ) ) );
96 :
97 0 : for( long nY = 0L; nY < nHeight; nY++ )
98 0 : for( long nX = 0L; nX < nWidth; nX++ )
99 0 : if( pMaskAcc->GetPixel( nY, nX ) == aMaskWhite )
100 0 : pAcc->SetPixel( nY, nX, aReplace );
101 : }
102 :
103 0 : Bitmap::ReleaseAccess( pMaskAcc );
104 0 : ReleaseAccess( pAcc );
105 :
106 0 : return bRet;
107 : }
108 :
109 0 : bool AlphaMask::Replace( sal_uInt8 cSearchTransparency, sal_uInt8 cReplaceTransparency, sal_uLong
110 : #ifdef DBG_UTIL
111 : nTol
112 : #endif
113 : )
114 : {
115 0 : BitmapWriteAccess* pAcc = AcquireWriteAccess();
116 0 : bool bRet = false;
117 :
118 : DBG_ASSERT( !nTol, "AlphaMask::Replace: nTol not used yet" );
119 :
120 0 : if( pAcc && pAcc->GetBitCount() == 8 )
121 : {
122 0 : const long nWidth = pAcc->Width(), nHeight = pAcc->Height();
123 :
124 0 : if( pAcc->GetScanlineFormat() == BMP_FORMAT_8BIT_PAL )
125 : {
126 0 : for( long nY = 0L; nY < nHeight; nY++ )
127 : {
128 0 : Scanline pScan = pAcc->GetScanline( nY );
129 :
130 0 : for( long nX = 0L; nX < nWidth; nX++, pScan++ )
131 : {
132 0 : if( *pScan == cSearchTransparency )
133 0 : *pScan = cReplaceTransparency;
134 : }
135 : }
136 : }
137 : else
138 : {
139 0 : BitmapColor aReplace( cReplaceTransparency );
140 :
141 0 : for( long nY = 0L; nY < nHeight; nY++ )
142 : {
143 0 : for( long nX = 0L; nX < nWidth; nX++ )
144 : {
145 0 : if( pAcc->GetPixel( nY, nX ).GetIndex() == cSearchTransparency )
146 0 : pAcc->SetPixel( nY, nX, aReplace );
147 : }
148 0 : }
149 : }
150 :
151 0 : bRet = true;
152 : }
153 :
154 0 : if( pAcc )
155 0 : ReleaseAccess( pAcc );
156 :
157 0 : return bRet;
158 : }
159 :
160 158469 : void AlphaMask::ReleaseAccess( BitmapReadAccess* pAccess )
161 : {
162 158469 : if( pAccess )
163 : {
164 158468 : Bitmap::ReleaseAccess( pAccess );
165 158468 : Bitmap::Convert( BMP_CONVERSION_8BIT_GREYS );
166 : }
167 158469 : }
168 :
169 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|