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 0 : AlphaMask::AlphaMask()
26 : {
27 0 : }
28 :
29 0 : AlphaMask::AlphaMask( const Bitmap& rBitmap ) :
30 0 : Bitmap( rBitmap )
31 : {
32 0 : if( !!rBitmap )
33 0 : Bitmap::Convert( BMP_CONVERSION_8BIT_GREYS );
34 0 : }
35 :
36 0 : AlphaMask::AlphaMask( const AlphaMask& rAlphaMask ) :
37 0 : Bitmap( rAlphaMask )
38 : {
39 0 : }
40 :
41 0 : AlphaMask::AlphaMask( const Size& rSizePixel, sal_uInt8* pEraseTransparency ) :
42 0 : Bitmap( rSizePixel, 8, &Bitmap::GetGreyPalette( 256 ) )
43 : {
44 0 : if( pEraseTransparency )
45 0 : Bitmap::Erase( Color( *pEraseTransparency, *pEraseTransparency, *pEraseTransparency ) );
46 0 : }
47 :
48 0 : AlphaMask::~AlphaMask()
49 : {
50 0 : }
51 :
52 0 : AlphaMask& AlphaMask::operator=( const Bitmap& rBitmap )
53 : {
54 0 : *(Bitmap*) this = rBitmap;
55 :
56 0 : if( !!rBitmap )
57 0 : Bitmap::Convert( BMP_CONVERSION_8BIT_GREYS );
58 :
59 0 : return *this;
60 : }
61 :
62 0 : const Bitmap& AlphaMask::ImplGetBitmap() const
63 : {
64 0 : return( (const Bitmap&) *this );
65 : }
66 :
67 0 : 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 0 : *(Bitmap*) this = rBitmap;
72 0 : }
73 :
74 0 : Bitmap AlphaMask::GetBitmap() const
75 : {
76 0 : return ImplGetBitmap();
77 : }
78 :
79 0 : bool AlphaMask::CopyPixel( const Rectangle& rRectDst, const Rectangle& rRectSrc,
80 : const AlphaMask* pAlphaSrc )
81 : {
82 : // Note: this code is copied from Bitmap::CopyPixel but avoids any palette lookups
83 : // This optimization is possible because the palettes of AlphaMasks are always identical (8bit GreyPalette, see ctor)
84 0 : const Size aSizePix( GetSizePixel() );
85 0 : Rectangle aRectDst( rRectDst );
86 0 : bool bRet = false;
87 :
88 0 : aRectDst.Intersection( Rectangle( Point(), aSizePix ) );
89 :
90 0 : if( !aRectDst.IsEmpty() )
91 : {
92 0 : if( pAlphaSrc && ( *pAlphaSrc != *this ) )
93 : {
94 0 : Bitmap* pSrc = (Bitmap*) pAlphaSrc;
95 0 : const Size aCopySizePix( pSrc->GetSizePixel() );
96 0 : Rectangle aRectSrc( rRectSrc );
97 :
98 0 : aRectSrc.Intersection( Rectangle( Point(), aCopySizePix ) );
99 :
100 0 : if( !aRectSrc.IsEmpty() )
101 : {
102 0 : BitmapReadAccess* pReadAcc = pSrc->AcquireReadAccess();
103 :
104 0 : if( pReadAcc )
105 : {
106 0 : BitmapWriteAccess* pWriteAcc = AcquireWriteAccess();
107 :
108 0 : if( pWriteAcc )
109 : {
110 0 : const long nWidth = std::min( aRectSrc.GetWidth(), aRectDst.GetWidth() );
111 0 : const long nHeight = std::min( aRectSrc.GetHeight(), aRectDst.GetHeight() );
112 0 : const long nSrcEndX = aRectSrc.Left() + nWidth;
113 0 : const long nSrcEndY = aRectSrc.Top() + nHeight;
114 0 : long nDstY = aRectDst.Top();
115 :
116 0 : for( long nSrcY = aRectSrc.Top(); nSrcY < nSrcEndY; nSrcY++, nDstY++ )
117 0 : for( long nSrcX = aRectSrc.Left(), nDstX = aRectDst.Left(); nSrcX < nSrcEndX; nSrcX++, nDstX++ )
118 0 : pWriteAcc->SetPixel( nDstY, nDstX, pReadAcc->GetPixel( nSrcY, nSrcX ) );
119 :
120 0 : ReleaseAccess( pWriteAcc );
121 0 : bRet = ( nWidth > 0L ) && ( nHeight > 0L );
122 : }
123 :
124 0 : pSrc->ReleaseAccess( pReadAcc );
125 : }
126 : }
127 : }
128 : else
129 : {
130 0 : Rectangle aRectSrc( rRectSrc );
131 :
132 0 : aRectSrc.Intersection( Rectangle( Point(), aSizePix ) );
133 :
134 0 : if( !aRectSrc.IsEmpty() && ( aRectSrc != aRectDst ) )
135 : {
136 0 : BitmapWriteAccess* pWriteAcc = AcquireWriteAccess();
137 :
138 0 : if( pWriteAcc )
139 : {
140 0 : const long nWidth = std::min( aRectSrc.GetWidth(), aRectDst.GetWidth() );
141 0 : const long nHeight = std::min( aRectSrc.GetHeight(), aRectDst.GetHeight() );
142 0 : const long nSrcX = aRectSrc.Left();
143 0 : const long nSrcY = aRectSrc.Top();
144 0 : const long nSrcEndX1 = nSrcX + nWidth - 1L;
145 0 : const long nSrcEndY1 = nSrcY + nHeight - 1L;
146 0 : const long nDstX = aRectDst.Left();
147 0 : const long nDstY = aRectDst.Top();
148 0 : const long nDstEndX1 = nDstX + nWidth - 1L;
149 0 : const long nDstEndY1 = nDstY + nHeight - 1L;
150 :
151 0 : if( ( nDstX <= nSrcX ) && ( nDstY <= nSrcY ) )
152 : {
153 0 : for( long nY = nSrcY, nYN = nDstY; nY <= nSrcEndY1; nY++, nYN++ )
154 0 : for( long nX = nSrcX, nXN = nDstX; nX <= nSrcEndX1; nX++, nXN++ )
155 0 : pWriteAcc->SetPixel( nYN, nXN, pWriteAcc->GetPixel( nY, nX ) );
156 : }
157 0 : else if( ( nDstX <= nSrcX ) && ( nDstY >= nSrcY ) )
158 : {
159 0 : for( long nY = nSrcEndY1, nYN = nDstEndY1; nY >= nSrcY; nY--, nYN-- )
160 0 : for( long nX = nSrcX, nXN = nDstX; nX <= nSrcEndX1; nX++, nXN++ )
161 0 : pWriteAcc->SetPixel( nYN, nXN, pWriteAcc->GetPixel( nY, nX ) );
162 : }
163 0 : else if( ( nDstX >= nSrcX ) && ( nDstY <= nSrcY ) )
164 : {
165 0 : for( long nY = nSrcY, nYN = nDstY; nY <= nSrcEndY1; nY++, nYN++ )
166 0 : for( long nX = nSrcEndX1, nXN = nDstEndX1; nX >= nSrcX; nX--, nXN-- )
167 0 : pWriteAcc->SetPixel( nYN, nXN, pWriteAcc->GetPixel( nY, nX ) );
168 : }
169 : else
170 : {
171 0 : for( long nY = nSrcEndY1, nYN = nDstEndY1; nY >= nSrcY; nY--, nYN-- )
172 0 : for( long nX = nSrcEndX1, nXN = nDstEndX1; nX >= nSrcX; nX--, nXN-- )
173 0 : pWriteAcc->SetPixel( nYN, nXN, pWriteAcc->GetPixel( nY, nX ) );
174 : }
175 :
176 0 : ReleaseAccess( pWriteAcc );
177 0 : bRet = true;
178 : }
179 : }
180 : }
181 : }
182 :
183 0 : return bRet;
184 :
185 : }
186 :
187 0 : bool AlphaMask::Erase( sal_uInt8 cTransparency )
188 : {
189 0 : return Bitmap::Erase( Color( cTransparency, cTransparency, cTransparency ) );
190 : }
191 :
192 0 : bool AlphaMask::Replace( const Bitmap& rMask, sal_uInt8 cReplaceTransparency )
193 : {
194 0 : BitmapReadAccess* pMaskAcc = ( (Bitmap&) rMask ).AcquireReadAccess();
195 0 : BitmapWriteAccess* pAcc = AcquireWriteAccess();
196 0 : bool bRet = false;
197 :
198 0 : if( pMaskAcc && pAcc )
199 : {
200 0 : const BitmapColor aReplace( cReplaceTransparency );
201 0 : const long nWidth = std::min( pMaskAcc->Width(), pAcc->Width() );
202 0 : const long nHeight = std::min( pMaskAcc->Height(), pAcc->Height() );
203 0 : const BitmapColor aMaskWhite( pMaskAcc->GetBestMatchingColor( Color( COL_WHITE ) ) );
204 :
205 0 : for( long nY = 0L; nY < nHeight; nY++ )
206 0 : for( long nX = 0L; nX < nWidth; nX++ )
207 0 : if( pMaskAcc->GetPixel( nY, nX ) == aMaskWhite )
208 0 : pAcc->SetPixel( nY, nX, aReplace );
209 : }
210 :
211 0 : ( (Bitmap&) rMask ).ReleaseAccess( pMaskAcc );
212 0 : ReleaseAccess( pAcc );
213 :
214 0 : return bRet;
215 : }
216 :
217 0 : bool AlphaMask::Replace( sal_uInt8 cSearchTransparency, sal_uInt8 cReplaceTransparency, sal_uLong
218 : #ifdef DBG_UTIL
219 : nTol
220 : #endif
221 : )
222 : {
223 0 : BitmapWriteAccess* pAcc = AcquireWriteAccess();
224 0 : bool bRet = false;
225 :
226 : DBG_ASSERT( !nTol, "AlphaMask::Replace: nTol not used yet" );
227 :
228 0 : if( pAcc && pAcc->GetBitCount() == 8 )
229 : {
230 0 : const long nWidth = pAcc->Width(), nHeight = pAcc->Height();
231 :
232 0 : if( pAcc->GetScanlineFormat() == BMP_FORMAT_8BIT_PAL )
233 : {
234 0 : for( long nY = 0L; nY < nHeight; nY++ )
235 : {
236 0 : Scanline pScan = pAcc->GetScanline( nY );
237 :
238 0 : for( long nX = 0L; nX < nWidth; nX++, pScan++ )
239 : {
240 0 : if( *pScan == cSearchTransparency )
241 0 : *pScan = cReplaceTransparency;
242 : }
243 : }
244 : }
245 : else
246 : {
247 0 : BitmapColor aReplace( cReplaceTransparency );
248 :
249 0 : for( long nY = 0L; nY < nHeight; nY++ )
250 : {
251 0 : for( long nX = 0L; nX < nWidth; nX++ )
252 : {
253 0 : if( pAcc->GetPixel( nY, nX ).GetIndex() == cSearchTransparency )
254 0 : pAcc->SetPixel( nY, nX, aReplace );
255 : }
256 0 : }
257 : }
258 :
259 0 : bRet = true;
260 : }
261 :
262 0 : if( pAcc )
263 0 : ReleaseAccess( pAcc );
264 :
265 0 : return bRet;
266 : }
267 :
268 0 : void AlphaMask::ReleaseAccess( BitmapReadAccess* pAccess )
269 : {
270 0 : if( pAccess )
271 : {
272 0 : Bitmap::ReleaseAccess( pAccess );
273 0 : Bitmap::Convert( BMP_CONVERSION_8BIT_GREYS );
274 : }
275 0 : }
276 :
277 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|