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 <tools/color.hxx>
21 #include <vcl/alpha.hxx>
22 
24 #include <salinst.hxx>
25 #include <svdata.hxx>
26 #include <salbmp.hxx>
27 #include <sal/log.hxx>
28 
29 AlphaMask::AlphaMask() = default;
30 
31 AlphaMask::AlphaMask( const Bitmap& rBitmap ) :
32  Bitmap( rBitmap )
33 {
34  if( !!rBitmap )
36 }
37 
38 AlphaMask::AlphaMask( const AlphaMask& ) = default;
39 
40 AlphaMask::AlphaMask( AlphaMask&& ) = default;
41 
42 AlphaMask::AlphaMask( const Size& rSizePixel, const sal_uInt8* pEraseTransparency ) :
43  Bitmap( rSizePixel, 8, &Bitmap::GetGreyPalette( 256 ) )
44 {
45  if( pEraseTransparency )
46  Bitmap::Erase( Color( *pEraseTransparency, *pEraseTransparency, *pEraseTransparency ) );
47 }
48 
49 AlphaMask::~AlphaMask() = default;
50 
52 {
53  *static_cast<Bitmap*>(this) = rBitmap;
54 
55  if( !!rBitmap )
57 
58  return *this;
59 }
60 
62 {
63  return *this;
64 }
65 
66 void AlphaMask::ImplSetBitmap( const Bitmap& rBitmap )
67 {
68  SAL_WARN_IF( 8 != rBitmap.GetBitCount(), "vcl.gdi", "Bitmap should be 8bpp, not " << rBitmap.GetBitCount() << "bpp" );
69  SAL_WARN_IF( !rBitmap.HasGreyPalette8Bit(), "vcl.gdi", "Bitmap isn't greyscale" );
70  *static_cast<Bitmap*>(this) = rBitmap;
71 }
72 
74 {
75  return ImplGetBitmap();
76 }
77 
78 void AlphaMask::Erase( sal_uInt8 cTransparency )
79 {
80  Bitmap::Erase( Color( cTransparency, cTransparency, cTransparency ) );
81 }
82 
83 void AlphaMask::Replace( const Bitmap& rMask, sal_uInt8 cReplaceTransparency )
84 {
85  Bitmap::ScopedReadAccess pMaskAcc( const_cast<Bitmap&>(rMask) );
86  AlphaScopedWriteAccess pAcc(*this);
87 
88  if( !(pMaskAcc && pAcc) )
89  return;
90 
91  const BitmapColor aReplace( cReplaceTransparency );
92  const tools::Long nWidth = std::min( pMaskAcc->Width(), pAcc->Width() );
93  const tools::Long nHeight = std::min( pMaskAcc->Height(), pAcc->Height() );
94  const BitmapColor aMaskWhite( pMaskAcc->GetBestMatchingColor( COL_WHITE ) );
95 
96  for( tools::Long nY = 0; nY < nHeight; nY++ )
97  {
98  Scanline pScanline = pAcc->GetScanline(nY);
99  Scanline pScanlineMask = pMaskAcc->GetScanline(nY);
100  for( tools::Long nX = 0; nX < nWidth; nX++ )
101  if( pMaskAcc->GetPixelFromData( pScanlineMask, nX ) == aMaskWhite )
102  pAcc->SetPixelOnData( pScanline, nX, aReplace );
103  }
104 }
105 
106 void AlphaMask::Replace( sal_uInt8 cSearchTransparency, sal_uInt8 cReplaceTransparency )
107 {
108  AlphaScopedWriteAccess pAcc(*this);
109 
110  if( !(pAcc && pAcc->GetBitCount() == 8) )
111  return;
112 
113  const tools::Long nWidth = pAcc->Width(), nHeight = pAcc->Height();
114 
116  {
117  for( tools::Long nY = 0; nY < nHeight; nY++ )
118  {
119  Scanline pScan = pAcc->GetScanline( nY );
120 
121  for( tools::Long nX = 0; nX < nWidth; nX++, pScan++ )
122  {
123  if( *pScan == cSearchTransparency )
124  *pScan = cReplaceTransparency;
125  }
126  }
127  }
128  else
129  {
130  BitmapColor aReplace( cReplaceTransparency );
131 
132  for( tools::Long nY = 0; nY < nHeight; nY++ )
133  {
134  Scanline pScanline = pAcc->GetScanline(nY);
135  for( tools::Long nX = 0; nX < nWidth; nX++ )
136  {
137  if( pAcc->GetIndexFromData( pScanline, nX ) == cSearchTransparency )
138  pAcc->SetPixelOnData( pScanline, nX, aReplace );
139  }
140  }
141  }
142 }
143 
144 void AlphaMask::BlendWith(const Bitmap& rOther)
145 {
146  std::shared_ptr<SalBitmap> xImpBmp(ImplGetSVData()->mpDefInst->CreateSalBitmap());
147  if (xImpBmp->Create(*ImplGetSalBitmap()) && xImpBmp->AlphaBlendWith(*rOther.ImplGetSalBitmap()))
148  {
149  ImplSetSalBitmap(xImpBmp);
150  return;
151  }
152  AlphaMask aOther(rOther); // to 8 bits
153  Bitmap::ScopedReadAccess pOtherAcc(aOther);
154  AlphaScopedWriteAccess pAcc(*this);
155  if (!(pOtherAcc && pAcc && pOtherAcc->GetBitCount() == 8 && pAcc->GetBitCount() == 8))
156  return;
157 
158  const tools::Long nHeight = std::min(pOtherAcc->Height(), pAcc->Height());
159  const tools::Long nWidth = std::min(pOtherAcc->Width(), pAcc->Width());
160  for (tools::Long y = 0; y < nHeight; ++y)
161  {
162  Scanline scanline = pAcc->GetScanline( y );
163  ConstScanline otherScanline = pOtherAcc->GetScanline( y );
164  for (tools::Long x = 0; x < nWidth; ++x)
165  {
166  // Use sal_uInt16 for following multiplication
167  const sal_uInt16 nGrey1 = *scanline;
168  const sal_uInt16 nGrey2 = *otherScanline;
169  *scanline = static_cast<sal_uInt8>(nGrey1 + nGrey2 - nGrey1 * nGrey2 / 255);
170  ++scanline;
171  ++otherScanline;
172  }
173  }
174 }
175 
177 {
178  if( pAccess )
179  {
180  Bitmap::ReleaseAccess( pAccess );
182  }
183 }
184 
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void Replace(const Bitmap &rMask, sal_uInt8 rReplaceTransparency)
Definition: alpha.cxx:83
sal_uInt8 GetIndexFromData(const sal_uInt8 *pData, tools::Long nX) const
tools::Long Height() const
virtual ~AlphaMask() override
const std::shared_ptr< SalBitmap > & ImplGetSalBitmap() const
Definition: bitmap.hxx:525
Bitmap const & GetBitmap() const
Definition: alpha.cxx:73
long Long
sal_uInt16 GetBitCount() const
SAL_DLLPRIVATE void ImplSetBitmap(const Bitmap &rBitmap)
Definition: alpha.cxx:66
float x
void SetPixelOnData(sal_uInt8 *pData, tools::Long nX, const BitmapColor &rBitmapColor)
bool HasGreyPalette8Bit() const
Scanline GetScanline(tools::Long nY) const
float y
ImplSVData * ImplGetSVData()
Definition: svdata.cxx:74
void BlendWith(const Bitmap &rOther)
Definition: alpha.cxx:144
sal_uInt8 * Scanline
Definition: Scanline.hxx:25
void Erase(sal_uInt8 cTransparency)
Definition: alpha.cxx:78
ScanlineFormat GetScanlineFormat() const
tools::Long Width() const
AlphaMask & operator=(const Bitmap &rBitmap)
Definition: alpha.cxx:51
static void ReleaseAccess(BitmapInfoAccess *pAccess)
void ReleaseAccess(BitmapReadAccess *pAccess)
Definition: alpha.cxx:176
const sal_uInt8 * ConstScanline
Definition: Scanline.hxx:26
SAL_DLLPRIVATE const Bitmap & ImplGetBitmap() const
Definition: alpha.cxx:61
#define SAL_WARN_IF(condition, area, stream)
unsigned char sal_uInt8
bool Convert(BmpConversion eConversion)
Convert bitmap format.
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_WHITE
BitmapColor GetPixelFromData(const sal_uInt8 *pData, tools::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_DLLPRIVATE void ImplSetSalBitmap(const std::shared_ptr< SalBitmap > &xImpBmp)