summaryrefslogtreecommitdiff
path: root/vcl/source/gdi/alpha.cxx
blob: 284314e28f98a5ea0cc2e6fa9aa5086032fc9387 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <vcl/bitmapaccess.hxx>
#include <tools/color.hxx>
#include <vcl/alpha.hxx>
#include <bitmapwriteaccess.hxx>
#include <salinst.hxx>
#include <svdata.hxx>
#include <salbmp.hxx>
#include <sal/log.hxx>

AlphaMask::AlphaMask() = default;

AlphaMask::AlphaMask( const Bitmap& rBitmap ) :
    Bitmap( rBitmap )
{
    if( !!rBitmap )
        Convert( BmpConversion::N8BitNoConversion );
}

AlphaMask::AlphaMask( const AlphaMask& ) = default;

AlphaMask::AlphaMask( AlphaMask&& ) = default;

AlphaMask::AlphaMask( const Size& rSizePixel, const sal_uInt8* pEraseTransparency ) :
    Bitmap( rSizePixel, 8, &Bitmap::GetGreyPalette( 256 ) )
{
    if( pEraseTransparency )
        Bitmap::Erase( Color( *pEraseTransparency, *pEraseTransparency, *pEraseTransparency ) );
}

AlphaMask::~AlphaMask() = default;

AlphaMask& AlphaMask::operator=( const Bitmap& rBitmap )
{
    *static_cast<Bitmap*>(this) = rBitmap;

    if( !!rBitmap )
        Convert( BmpConversion::N8BitNoConversion );

    return *this;
}

const Bitmap& AlphaMask::ImplGetBitmap() const
{
    return *this;
}

void AlphaMask::ImplSetBitmap( const Bitmap& rBitmap )
{
    SAL_WARN_IF( 8 != rBitmap.GetBitCount(), "vcl.gdi", "Bitmap should be 8bpp, not " << rBitmap.GetBitCount() << "bpp" );
    SAL_WARN_IF( !rBitmap.HasGreyPalette8Bit(), "vcl.gdi", "Bitmap isn't greyscale" );
    *static_cast<Bitmap*>(this) = rBitmap;
}

Bitmap const & AlphaMask::GetBitmap() const
{
    return ImplGetBitmap();
}

void AlphaMask::Erase( sal_uInt8 cTransparency )
{
    Bitmap::Erase( Color( cTransparency, cTransparency, cTransparency ) );
}

void AlphaMask::Replace( const Bitmap& rMask, sal_uInt8 cReplaceTransparency )
{
    Bitmap::ScopedReadAccess pMaskAcc( const_cast<Bitmap&>(rMask) );
    AlphaScopedWriteAccess pAcc(*this);

    if( !(pMaskAcc && pAcc) )
        return;

    const BitmapColor   aReplace( cReplaceTransparency );
    const long          nWidth = std::min( pMaskAcc->Width(), pAcc->Width() );
    const long          nHeight = std::min( pMaskAcc->Height(), pAcc->Height() );
    const BitmapColor   aMaskWhite( pMaskAcc->GetBestMatchingColor( COL_WHITE ) );

    for( long nY = 0; nY < nHeight; nY++ )
    {
        Scanline pScanline = pAcc->GetScanline(nY);
        Scanline pScanlineMask = pMaskAcc->GetScanline(nY);
        for( long nX = 0; nX < nWidth; nX++ )
            if( pMaskAcc->GetPixelFromData( pScanlineMask, nX ) == aMaskWhite )
                pAcc->SetPixelOnData( pScanline, nX, aReplace );
    }
}

void AlphaMask::Replace( sal_uInt8 cSearchTransparency, sal_uInt8 cReplaceTransparency )
{
    AlphaScopedWriteAccess pAcc(*this);

    if( !(pAcc && pAcc->GetBitCount() == 8) )
        return;

    const long nWidth = pAcc->Width(), nHeight = pAcc->Height();

    if( pAcc->GetScanlineFormat() == ScanlineFormat::N8BitPal )
    {
        for( long nY = 0; nY < nHeight; nY++ )
        {
            Scanline pScan = pAcc->GetScanline( nY );

            for( long nX = 0; nX < nWidth; nX++, pScan++ )
            {
                if( *pScan == cSearchTransparency )
                    *pScan = cReplaceTransparency;
            }
        }
    }
    else
    {
        BitmapColor aReplace( cReplaceTransparency );

        for( long nY = 0; nY < nHeight; nY++ )
        {
            Scanline pScanline = pAcc->GetScanline(nY);
            for( long nX = 0; nX < nWidth; nX++ )
            {
                if( pAcc->GetIndexFromData( pScanline, nX ) == cSearchTransparency )
                    pAcc->SetPixelOnData( pScanline, nX, aReplace );
            }
        }
    }
}

void AlphaMask::BlendWith(const Bitmap& rOther)
{
    std::shared_ptr<SalBitmap> xImpBmp(ImplGetSVData()->mpDefInst->CreateSalBitmap());
    if (xImpBmp->Create(*ImplGetSalBitmap()) && xImpBmp->AlphaBlendWith(*rOther.ImplGetSalBitmap()))
    {
        ImplSetSalBitmap(xImpBmp);
        return;
    }
    AlphaMask aOther(rOther); // to 8 bits
    Bitmap::ScopedReadAccess pOtherAcc(aOther);
    AlphaScopedWriteAccess pAcc(*this);
    if (!(pOtherAcc && pAcc && pOtherAcc->GetBitCount() == 8 && pAcc->GetBitCount() == 8))
        return;

    const long nHeight = std::min(pOtherAcc->Height(), pAcc->Height());
    const long nWidth = std::min(pOtherAcc->Width(), pAcc->Width());
    for (long y = 0; y < nHeight; ++y)
    {
        Scanline scanline = pAcc->GetScanline( y );
        ConstScanline otherScanline = pOtherAcc->GetScanline( y );
        for (long x = 0; x < nWidth; ++x)
        {
            // Use sal_uInt16 for following multiplication
            const sal_uInt16 nGrey1 = *scanline;
            const sal_uInt16 nGrey2 = *otherScanline;
            *scanline = static_cast<sal_uInt8>(nGrey1 + nGrey2 - nGrey1 * nGrey2 / 255);
            ++scanline;
            ++otherScanline;
        }
    }
}

void AlphaMask::ReleaseAccess( BitmapReadAccess* pAccess )
{
    if( pAccess )
    {
        Bitmap::ReleaseAccess( pAccess );
        Convert( BmpConversion::N8BitNoConversion );
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */