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
29AlphaMask::AlphaMask() = default;
30
31AlphaMask::AlphaMask( const Bitmap& rBitmap ) :
32 Bitmap( rBitmap )
33{
34 if( !rBitmap.IsEmpty() )
36}
37
38AlphaMask::AlphaMask( const AlphaMask& ) = default;
39
40AlphaMask::AlphaMask( AlphaMask&& ) = default;
41
42AlphaMask::AlphaMask( const Size& rSizePixel, const sal_uInt8* pEraseTransparency )
43 : Bitmap(rSizePixel, vcl::PixelFormat::N8_BPP, &Bitmap::GetGreyPalette(256))
44{
45 if( pEraseTransparency )
46 Bitmap::Erase( Color( *pEraseTransparency, *pEraseTransparency, *pEraseTransparency ) );
47}
48
49AlphaMask::~AlphaMask() = default;
50
52{
53 *static_cast<Bitmap*>(this) = rBitmap;
54
55 if( !rBitmap.IsEmpty() )
57
58 return *this;
59}
60
62{
63 return *this;
64}
65
66void AlphaMask::ImplSetBitmap( const Bitmap& rBitmap )
67{
68 SAL_WARN_IF(rBitmap.getPixelFormat() != vcl::PixelFormat::N8_BPP, "vcl.gdi", "Bitmap should be 8bpp, not " << vcl::pixelFormatBitCount(rBitmap.getPixelFormat()) << "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
78void AlphaMask::Erase( sal_uInt8 cTransparency )
79{
80 Bitmap::Erase( Color( cTransparency, cTransparency, cTransparency ) );
81}
82
83void AlphaMask::BlendWith(const Bitmap& rOther)
84{
85 std::shared_ptr<SalBitmap> xImpBmp(ImplGetSVData()->mpDefInst->CreateSalBitmap());
86 if (xImpBmp->Create(*ImplGetSalBitmap()) && xImpBmp->AlphaBlendWith(*rOther.ImplGetSalBitmap()))
87 {
88 ImplSetSalBitmap(xImpBmp);
89 return;
90 }
91 AlphaMask aOther(rOther); // to 8 bits
92 Bitmap::ScopedReadAccess pOtherAcc(aOther);
93 AlphaScopedWriteAccess pAcc(*this);
94 if (!(pOtherAcc && pAcc && pOtherAcc->GetBitCount() == 8 && pAcc->GetBitCount() == 8))
95 return;
96
97 const tools::Long nHeight = std::min(pOtherAcc->Height(), pAcc->Height());
98 const tools::Long nWidth = std::min(pOtherAcc->Width(), pAcc->Width());
99 for (tools::Long y = 0; y < nHeight; ++y)
100 {
101 Scanline scanline = pAcc->GetScanline( y );
102 ConstScanline otherScanline = pOtherAcc->GetScanline( y );
103 for (tools::Long x = 0; x < nWidth; ++x)
104 {
105 // Use sal_uInt16 for following multiplication
106 const sal_uInt16 nGrey1 = *scanline;
107 const sal_uInt16 nGrey2 = *otherScanline;
108 *scanline = static_cast<sal_uInt8>(nGrey1 + nGrey2 - nGrey1 * nGrey2 / 255);
109 ++scanline;
110 ++otherScanline;
111 }
112 }
113}
114
116{
117 // no content, no alpha
118 if(IsEmpty())
119 return false;
120
121 ScopedReadAccess pAcc(const_cast<AlphaMask&>(*this));
122 const tools::Long nHeight(pAcc->Height());
123 const tools::Long nWidth(pAcc->Width());
124
125 // no content, no alpha
126 if(0 == nHeight || 0 == nWidth)
127 return false;
128
129 for (tools::Long y = 0; y < nHeight; ++y)
130 {
131 for (tools::Long x = 0; x < nWidth; ++x)
132 {
133 if (0 != pAcc->GetColor(y, x).GetRed())
134 {
135 return true;
136 }
137 }
138 }
139
140 return false;
141}
142
144{
145 if( pAccess )
146 {
147 Bitmap::ReleaseAccess( pAccess );
149 }
150}
151
152/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const sal_uInt8 * ConstScanline
Definition: Scanline.hxx:27
sal_uInt8 * Scanline
Definition: Scanline.hxx:26
AlphaMask & operator=(const Bitmap &rBitmap)
Definition: alpha.cxx:51
Bitmap const & GetBitmap() const
Definition: alpha.cxx:73
void BlendWith(const Bitmap &rOther)
Definition: alpha.cxx:83
bool IsEmpty() const
void ReleaseAccess(BitmapReadAccess *pAccess)
Definition: alpha.cxx:143
virtual ~AlphaMask() override
SAL_DLLPRIVATE const Bitmap & ImplGetBitmap() const
Definition: alpha.cxx:61
SAL_DLLPRIVATE void ImplSetBitmap(const Bitmap &rBitmap)
Definition: alpha.cxx:66
void Erase(sal_uInt8 cTransparency)
Definition: alpha.cxx:78
bool hasAlpha() const
Definition: alpha.cxx:115
tools::Long Height() const
tools::Long Width() const
sal_uInt16 GetBitCount() const
Scanline GetScanline(tools::Long nY) const
const std::shared_ptr< SalBitmap > & ImplGetSalBitmap() const
bool HasGreyPalette8Bit() const
SAL_DLLPRIVATE void ImplSetSalBitmap(const std::shared_ptr< SalBitmap > &xImpBmp)
bool Convert(BmpConversion eConversion)
Convert bitmap format.
static void ReleaseAccess(BitmapInfoAccess *pAccess)
bool IsEmpty() const
bool Erase(const Color &rFillColor)
Fill the entire bitmap with the given color.
Definition: bitmappaint.cxx:34
vcl::PixelFormat getPixelFormat() const
float y
float x
#define SAL_WARN_IF(condition, area, stream)
long Long
PixelFormat
Pixel format of the bitmap in bits per pixel.
Definition: BitmapTypes.hxx:20
constexpr sal_uInt16 pixelFormatBitCount(PixelFormat ePixelFormat)
Definition: BitmapTypes.hxx:35
ImplSVData * ImplGetSVData()
Definition: svdata.cxx:76
unsigned char sal_uInt8