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 assert (pOtherAcc && pAcc && pOtherAcc->GetBitCount() == 8 && pAcc->GetBitCount() == 8 && "cannot BlendWith this combination");
95 if (!(pOtherAcc && pAcc && pOtherAcc->GetBitCount() == 8 && pAcc->GetBitCount() == 8))
96 {
97 SAL_WARN("vcl", "cannot BlendWith this combination");
98 return;
99 }
100
101 const tools::Long nHeight = std::min(pOtherAcc->Height(), pAcc->Height());
102 const tools::Long nWidth = std::min(pOtherAcc->Width(), pAcc->Width());
103 for (tools::Long y = 0; y < nHeight; ++y)
104 {
105 Scanline scanline = pAcc->GetScanline( y );
106 ConstScanline otherScanline = pOtherAcc->GetScanline( y );
107 for (tools::Long x = 0; x < nWidth; ++x)
108 {
109 // Use sal_uInt16 for following multiplication
110 const sal_uInt16 nGrey1 = *scanline;
111 const sal_uInt16 nGrey2 = *otherScanline;
112 *scanline = static_cast<sal_uInt8>(nGrey1 + nGrey2 - nGrey1 * nGrey2 / 255);
113 ++scanline;
114 ++otherScanline;
115 }
116 }
117}
118
120{
121 // no content, no alpha
122 if(IsEmpty())
123 return false;
124
125 ScopedReadAccess pAcc(const_cast<AlphaMask&>(*this));
126 const tools::Long nHeight(pAcc->Height());
127 const tools::Long nWidth(pAcc->Width());
128
129 // no content, no alpha
130 if(0 == nHeight || 0 == nWidth)
131 return false;
132
133 for (tools::Long y = 0; y < nHeight; ++y)
134 {
135 for (tools::Long x = 0; x < nWidth; ++x)
136 {
137 if (0 != pAcc->GetColor(y, x).GetRed())
138 {
139 return true;
140 }
141 }
142 }
143
144 return false;
145}
146
148{
149 if( pAccess )
150 {
151 Bitmap::ReleaseAccess( pAccess );
153 }
154}
155
156/* 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:147
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:119
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)
#define SAL_WARN(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:34
ImplSVData * ImplGetSVData()
Definition: svdata.cxx:76
unsigned char sal_uInt8