LibreOffice Module vcl (master)  1
alpha.cxx
Go to the documentation of this file.
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 <vcl/bitmapaccess.hxx>
21 #include <tools/color.hxx>
22 #include <vcl/alpha.hxx>
23 #include <bitmapwriteaccess.hxx>
24 #include <sal/log.hxx>
25 
26 AlphaMask::AlphaMask() = default;
27 
28 AlphaMask::AlphaMask( const Bitmap& rBitmap ) :
29  Bitmap( rBitmap )
30 {
31  if( !!rBitmap )
33 }
34 
35 AlphaMask::AlphaMask( const AlphaMask& ) = default;
36 
37 AlphaMask::AlphaMask( AlphaMask&& ) = default;
38 
39 AlphaMask::AlphaMask( const Size& rSizePixel, const sal_uInt8* pEraseTransparency ) :
40  Bitmap( rSizePixel, 8, &Bitmap::GetGreyPalette( 256 ) )
41 {
42  if( pEraseTransparency )
43  Bitmap::Erase( Color( *pEraseTransparency, *pEraseTransparency, *pEraseTransparency ) );
44 }
45 
46 AlphaMask::~AlphaMask() = default;
47 
49 {
50  *static_cast<Bitmap*>(this) = rBitmap;
51 
52  if( !!rBitmap )
54 
55  return *this;
56 }
57 
59 {
60  return *this;
61 }
62 
63 void AlphaMask::ImplSetBitmap( const Bitmap& rBitmap )
64 {
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;
68 }
69 
71 {
72  return ImplGetBitmap();
73 }
74 
75 void AlphaMask::Erase( sal_uInt8 cTransparency )
76 {
77  Bitmap::Erase( Color( cTransparency, cTransparency, cTransparency ) );
78 }
79 
80 void AlphaMask::Replace( const Bitmap& rMask, sal_uInt8 cReplaceTransparency )
81 {
82  Bitmap::ScopedReadAccess pMaskAcc( const_cast<Bitmap&>(rMask) );
83  AlphaScopedWriteAccess pAcc(*this);
84 
85  if( pMaskAcc && pAcc )
86  {
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 ) );
91 
92  for( long nY = 0; nY < nHeight; nY++ )
93  {
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 );
99  }
100  }
101 }
102 
103 void AlphaMask::Replace( sal_uInt8 cSearchTransparency, sal_uInt8 cReplaceTransparency )
104 {
105  AlphaScopedWriteAccess pAcc(*this);
106 
107  if( pAcc && pAcc->GetBitCount() == 8 )
108  {
109  const long nWidth = pAcc->Width(), nHeight = pAcc->Height();
110 
112  {
113  for( long nY = 0; nY < nHeight; nY++ )
114  {
115  Scanline pScan = pAcc->GetScanline( nY );
116 
117  for( long nX = 0; nX < nWidth; nX++, pScan++ )
118  {
119  if( *pScan == cSearchTransparency )
120  *pScan = cReplaceTransparency;
121  }
122  }
123  }
124  else
125  {
126  BitmapColor aReplace( cReplaceTransparency );
127 
128  for( long nY = 0; nY < nHeight; nY++ )
129  {
130  Scanline pScanline = pAcc->GetScanline(nY);
131  for( long nX = 0; nX < nWidth; nX++ )
132  {
133  if( pAcc->GetIndexFromData( pScanline, nX ) == cSearchTransparency )
134  pAcc->SetPixelOnData( pScanline, nX, aReplace );
135  }
136  }
137  }
138  }
139 }
140 
142 {
143  if( pAccess )
144  {
145  Bitmap::ReleaseAccess( pAccess );
147  }
148 }
149 
150 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void Replace(const Bitmap &rMask, sal_uInt8 rReplaceTransparency)
Definition: alpha.cxx:80
Scanline GetScanline(long nY) const
virtual ~AlphaMask() override
Bitmap const & GetBitmap() const
Definition: alpha.cxx:70
sal_uInt16 GetBitCount() const
SAL_DLLPRIVATE void ImplSetBitmap(const Bitmap &rBitmap)
Definition: alpha.cxx:63
long Width() const
void SetPixelOnData(sal_uInt8 *pData, long nX, const BitmapColor &rBitmapColor)
sal_uInt8 * Scanline
Definition: Scanline.hxx:25
void Erase(sal_uInt8 cTransparency)
Definition: alpha.cxx:75
ScanlineFormat GetScanlineFormat() const
AlphaMask & operator=(const Bitmap &rBitmap)
Definition: alpha.cxx:48
static void ReleaseAccess(BitmapInfoAccess *pAccess)
void ReleaseAccess(BitmapReadAccess *pAccess)
Definition: alpha.cxx:141
SAL_DLLPRIVATE const Bitmap & ImplGetBitmap() const
Definition: alpha.cxx:58
bool HasGreyPalette() const
long Height() const
#define SAL_WARN_IF(condition, area, stream)
unsigned char sal_uInt8
bool Convert(BmpConversion eConversion)
Convert bitmap format.
Definition: bitmap3.cxx:226
constexpr::Color COL_WHITE(0xFF, 0xFF, 0xFF)
BitmapColor GetPixelFromData(const sal_uInt8 *pData, long nX) const
bool Erase(const Color &rFillColor)
Fill the entire bitmap with the given color.
Definition: bitmappaint.cxx:34
sal_uInt16 GetBitCount() const
BitmapColor GetBestMatchingColor(const BitmapColor &rBitmapColor)
sal_uInt8 GetIndexFromData(const sal_uInt8 *pData, long nX) const