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.HasGreyPalette8Bit(), "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  return;
87 
88  const BitmapColor aReplace( cReplaceTransparency );
89  const long nWidth = std::min( pMaskAcc->Width(), pAcc->Width() );
90  const long nHeight = std::min( pMaskAcc->Height(), pAcc->Height() );
91  const BitmapColor aMaskWhite( pMaskAcc->GetBestMatchingColor( COL_WHITE ) );
92 
93  for( long nY = 0; nY < nHeight; nY++ )
94  {
95  Scanline pScanline = pAcc->GetScanline(nY);
96  Scanline pScanlineMask = pMaskAcc->GetScanline(nY);
97  for( long nX = 0; nX < nWidth; nX++ )
98  if( pMaskAcc->GetPixelFromData( pScanlineMask, nX ) == aMaskWhite )
99  pAcc->SetPixelOnData( pScanline, nX, aReplace );
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  return;
109 
110  const long nWidth = pAcc->Width(), nHeight = pAcc->Height();
111 
113  {
114  for( long nY = 0; nY < nHeight; nY++ )
115  {
116  Scanline pScan = pAcc->GetScanline( nY );
117 
118  for( long nX = 0; nX < nWidth; nX++, pScan++ )
119  {
120  if( *pScan == cSearchTransparency )
121  *pScan = cReplaceTransparency;
122  }
123  }
124  }
125  else
126  {
127  BitmapColor aReplace( cReplaceTransparency );
128 
129  for( long nY = 0; nY < nHeight; nY++ )
130  {
131  Scanline pScanline = pAcc->GetScanline(nY);
132  for( long nX = 0; nX < nWidth; nX++ )
133  {
134  if( pAcc->GetIndexFromData( pScanline, nX ) == cSearchTransparency )
135  pAcc->SetPixelOnData( pScanline, nX, aReplace );
136  }
137  }
138  }
139 }
140 
141 void AlphaMask::BlendWith(const Bitmap& rOther)
142 {
143  AlphaMask aOther(rOther); // to 8 bits
144  Bitmap::ScopedReadAccess pOtherAcc(aOther);
145  AlphaScopedWriteAccess pAcc(*this);
146  if (!(pOtherAcc && pAcc && pOtherAcc->GetBitCount() == 8 && pAcc->GetBitCount() == 8))
147  return;
148 
149  const long nHeight = std::min(pOtherAcc->Height(), pAcc->Height());
150  const long nWidth = std::min(pOtherAcc->Width(), pAcc->Width());
151  for (long y = 0; y < nHeight; ++y)
152  {
153  Scanline scanline = pAcc->GetScanline( y );
154  ConstScanline otherScanline = pOtherAcc->GetScanline( y );
155  for (long x = 0; x < nWidth; ++x)
156  {
157  // Use sal_uInt16 for following multiplication
158  const sal_uInt16 nGrey1 = *scanline;
159  const sal_uInt16 nGrey2 = *otherScanline;
160  *scanline = static_cast<sal_uInt8>(nGrey1 + nGrey2 - nGrey1 * nGrey2 / 255);
161  ++scanline;
162  ++otherScanline;
163  }
164  }
165 }
166 
168 {
169  if( pAccess )
170  {
171  Bitmap::ReleaseAccess( pAccess );
173  }
174 }
175 
176 /* 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
float x
bool HasGreyPalette8Bit() const
void SetPixelOnData(sal_uInt8 *pData, long nX, const BitmapColor &rBitmapColor)
float y
void BlendWith(const Bitmap &rOther)
Definition: alpha.cxx:141
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:167
const sal_uInt8 * ConstScanline
Definition: Scanline.hxx:26
SAL_DLLPRIVATE const Bitmap & ImplGetBitmap() const
Definition: alpha.cxx:58
long Height() const
#define SAL_WARN_IF(condition, area, stream)
unsigned char sal_uInt8
bool Convert(BmpConversion eConversion)
Convert bitmap format.
Definition: bitmap3.cxx:229
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_WHITE
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