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