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