Coverage Report

Created: 2026-10-14 09:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/vcl/source/bitmap/alpha.cxx
Line
Count
Source
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 <config_features.h>
21
22
#include <tools/color.hxx>
23
#include <vcl/alpha.hxx>
24
25
#include <vcl/BitmapWriteAccess.hxx>
26
#include <salinst.hxx>
27
#include <svdata.hxx>
28
#include <salbmp.hxx>
29
#include <sal/log.hxx>
30
#if HAVE_FEATURE_SKIA
31
#include <vcl/skia/SkiaHelper.hxx>
32
#endif
33
34
35
76.4k
AlphaMask::AlphaMask() = default;
36
37
AlphaMask::AlphaMask( const Bitmap& rBitmap ) :
38
3.42k
    maBitmap( rBitmap )
39
3.42k
{
40
3.42k
    if ( !rBitmap.IsEmpty() )
41
3.42k
        maBitmap.Convert( BmpConversion::N8BitNoConversion );
42
#if HAVE_FEATURE_SKIA
43
    // Related tdf#156866 force snapshot of alpha mask when using Skia
44
    // In release builds, tdf#156629 and tdf#156630 reappear in many
45
    // cases because a BitmapInfoAccess is in a debug block. So, instead
46
    // of relying on other code to a create a BitmapInfoAccess instance,
47
    // create one here to force the alpha mask to handle any pending
48
    // scaling and make the alpha mask immutable.
49
    else if ( SkiaHelper::isVCLSkiaEnabled() )
50
        BitmapInfoAccess aInfoAccess( maBitmap );
51
#endif
52
3.42k
    assert( (IsEmpty() || maBitmap.getPixelFormat() == vcl::PixelFormat::N8_BPP) && "alpha bitmap should be 8bpp" );
53
3.42k
    assert( (IsEmpty() || maBitmap.HasGreyPalette8Bit()) && "alpha bitmap should have greyscale palette" );
54
3.42k
}
55
56
0
AlphaMask::AlphaMask( const AlphaMask& ) = default;
57
58
6.68k
AlphaMask::AlphaMask( AlphaMask&& ) = default;
59
60
AlphaMask::AlphaMask( const Size& rSizePixel, const sal_uInt8* pEraseTransparency )
61
26.5k
    : maBitmap(rSizePixel, vcl::PixelFormat::N8_BPP, &Bitmap::GetGreyPalette(256))
62
26.5k
{
63
26.5k
    if( pEraseTransparency )
64
953
    {
65
953
        sal_uInt8 nAlpha = 255 - *pEraseTransparency;
66
953
        maBitmap.Erase( Color( nAlpha, nAlpha, nAlpha ) );
67
953
    }
68
25.5k
    else
69
25.5k
        maBitmap.Erase( COL_ALPHA_OPAQUE );
70
26.5k
}
71
72
113k
AlphaMask::~AlphaMask() = default;
73
74
AlphaMask& AlphaMask::operator=( const Bitmap& rBitmap )
75
21.9k
{
76
21.9k
    maBitmap = rBitmap;
77
78
21.9k
    if( !rBitmap.IsEmpty() )
79
21.9k
        maBitmap.Convert( BmpConversion::N8BitNoConversion );
80
81
21.9k
    assert( maBitmap.getPixelFormat() == vcl::PixelFormat::N8_BPP && "alpha bitmap should be 8bpp" );
82
21.9k
    assert( maBitmap.HasGreyPalette8Bit() && "alpha bitmap should have greyscale palette" );
83
84
21.9k
    return *this;
85
21.9k
}
86
87
void AlphaMask::Erase( sal_uInt8 cTransparency )
88
3
{
89
3
    sal_uInt8 nAlpha = 255 - cTransparency;
90
3
    maBitmap.Erase( Color( nAlpha, nAlpha, nAlpha ) );
91
3
}
92
93
void AlphaMask::BlendWith(const AlphaMask& rOther)
94
0
{
95
0
    std::shared_ptr<SalBitmap> xImpBmp(GetSalInstance()->CreateSalBitmap());
96
0
    if (xImpBmp->Create(*maBitmap.ImplGetSalBitmap()) && xImpBmp->AlphaBlendWith(*rOther.maBitmap.ImplGetSalBitmap()))
97
0
    {
98
0
        maBitmap.ImplSetSalBitmap(xImpBmp);
99
0
        assert( maBitmap.getPixelFormat() == vcl::PixelFormat::N8_BPP && "alpha bitmap should be 8bpp" );
100
0
        assert( maBitmap.HasGreyPalette8Bit() && "alpha bitmap should have greyscale palette" );
101
0
        return;
102
0
    }
103
0
    BitmapScopedReadAccess pOtherAcc(rOther);
104
0
    BitmapScopedWriteAccess pAcc(*this);
105
0
    assert (pOtherAcc && pAcc && pOtherAcc->GetBitCount() == 8 && pAcc->GetBitCount() == 8 && "cannot BlendWith this combination");
106
0
    if (!(pOtherAcc && pAcc && pOtherAcc->GetBitCount() == 8 && pAcc->GetBitCount() == 8))
107
0
    {
108
0
        SAL_WARN("vcl", "cannot BlendWith this combination");
109
0
        return;
110
0
    }
111
112
0
    const tools::Long nHeight = std::min(pOtherAcc->Height(), pAcc->Height());
113
0
    const tools::Long nWidth = std::min(pOtherAcc->Width(), pAcc->Width());
114
0
    for (tools::Long y = 0; y < nHeight; ++y)
115
0
    {
116
0
        Scanline scanline = pAcc->GetScanline( y );
117
0
        ConstScanline otherScanline = pOtherAcc->GetScanline( y );
118
0
        for (tools::Long x = 0; x < nWidth; ++x)
119
0
        {
120
            // Use sal_uInt16 for following multiplication
121
0
            const sal_uInt16 nGrey1 = *scanline;
122
0
            const sal_uInt16 nGrey2 = *otherScanline;
123
            // Awkward calculation because the original used transparency, and to replicate
124
            // the logic we need to translate into transparency, perform the original logic,
125
            // then translate back to alpha.
126
            // The original looked like:
127
            //   auto tmp = nGrey1 + nGrey2 - (nGrey1 * nGrey2 / 255)
128
            // which, when converted to using alpha looks like
129
            //   auto tmp = 255 - ((255 - nGrey1) + (255 - nGrey2) - (255 - nGrey1) * (255 - nGrey2) / 255);
130
            // which then simplifies to:
131
0
            auto tmp = nGrey1 * nGrey2 / 255;
132
0
            *scanline = static_cast<sal_uInt8>(tmp);
133
0
            ++scanline;
134
0
            ++otherScanline;
135
0
        }
136
0
    }
137
0
    pAcc.reset();
138
0
    assert( maBitmap.getPixelFormat() == vcl::PixelFormat::N8_BPP && "alpha bitmap should be 8bpp" );
139
0
    assert( maBitmap.HasGreyPalette8Bit() && "alpha bitmap should have greyscale palette" );
140
0
}
141
142
bool AlphaMask::hasAlpha() const
143
87
{
144
    // no content, no alpha
145
87
    if(IsEmpty())
146
0
        return false;
147
148
87
    BitmapScopedReadAccess pAcc(*this);
149
87
    const tools::Long nHeight(pAcc->Height());
150
87
    const tools::Long nWidth(pAcc->Width());
151
152
    // no content, no alpha
153
87
    if(0 == nHeight || 0 == nWidth)
154
0
        return false;
155
156
87
    for (tools::Long y = 0; y < nHeight; ++y)
157
87
    {
158
87
        for (tools::Long x = 0; x < nWidth; ++x)
159
87
        {
160
87
            if (255 != pAcc->GetColor(y, x).GetRed())
161
87
            {
162
87
                return true;
163
87
            }
164
87
        }
165
87
    }
166
167
0
    return false;
168
87
}
169
170
bool AlphaMask::AlphaCombineOr(const AlphaMask& rMask)
171
0
{
172
0
    BitmapScopedReadAccess pMaskAcc(rMask);
173
0
    BitmapScopedWriteAccess pAcc(*this);
174
175
0
    if (!pMaskAcc || !pAcc)
176
0
        return false;
177
178
0
    assert (pMaskAcc->GetBitCount() == 8 && pAcc->GetBitCount() == 8);
179
180
0
    const tools::Long nWidth = std::min(pMaskAcc->Width(), pAcc->Width());
181
0
    const tools::Long nHeight = std::min(pMaskAcc->Height(), pAcc->Height());
182
183
0
    for (tools::Long nY = 0; nY < nHeight; nY++)
184
0
    {
185
0
        Scanline pScanline = pAcc->GetScanline(nY);
186
0
        ConstScanline pScanlineMask = pMaskAcc->GetScanline(nY);
187
0
        for (tools::Long nX = 0; nX < nWidth; nX++)
188
0
        {
189
0
            if (*pScanlineMask != 255 || *pScanline != 255)
190
0
                *pScanline = 0;
191
0
            else
192
0
                *pScanline = 255;
193
0
            ++pScanline;
194
0
            ++pScanlineMask;
195
0
        }
196
0
    }
197
198
0
    return true;
199
0
}
200
201
bool AlphaMask::Invert()
202
2.86k
{
203
2.86k
    if (IsEmpty())
204
1.05k
        return false;
205
1.81k
    bool b = maBitmap.Invert();
206
1.81k
    assert( maBitmap.getPixelFormat() == vcl::PixelFormat::N8_BPP && "alpha bitmap should be 8bpp" );
207
    assert( maBitmap.HasGreyPalette8Bit() && "alpha bitmap should have greyscale palette" );
208
1.81k
    return b;
209
2.86k
}
210
211
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */