Coverage Report

Created: 2025-07-07 10:01

/src/libreoffice/include/vcl/BitmapInfoAccess.hxx
Line
Count
Source (jump to first uncovered line)
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
#pragma once
20
21
#include <vcl/dllapi.h>
22
#include <vcl/bitmap.hxx>
23
#include <vcl/Scanline.hxx>
24
#include <vcl/BitmapBuffer.hxx>
25
#include <vcl/BitmapColor.hxx>
26
#include <vcl/BitmapAccessMode.hxx>
27
28
bool Bitmap32IsPreMultipled();
29
30
typedef BitmapColor (*FncGetPixel)(ConstScanline pScanline, tools::Long nX);
31
typedef void (*FncSetPixel)(Scanline pScanline, tools::Long nX, const BitmapColor& rBitmapColor);
32
33
class VCL_DLLPUBLIC BitmapInfoAccess
34
{
35
    friend class BitmapReadAccess;
36
37
public:
38
    BitmapInfoAccess(const Bitmap& rBitmap, BitmapAccessMode nMode = BitmapAccessMode::Info);
39
    BitmapInfoAccess(const AlphaMask& rBitmap, BitmapAccessMode nMode = BitmapAccessMode::Info);
40
41
    virtual ~BitmapInfoAccess();
42
43
634k
    bool operator!() const { return mpBuffer == nullptr; }
44
266k
    explicit operator bool() const { return mpBuffer != nullptr; }
45
46
1.31M
    tools::Long Width() const { return mpBuffer ? mpBuffer->mnWidth : 0L; }
47
48
251k
    tools::Long Height() const { return mpBuffer ? mpBuffer->mnHeight : 0L; }
49
50
    bool IsTopDown() const
51
34.7k
    {
52
34.7k
        assert(mpBuffer && "Access is not valid!");
53
54
34.7k
        return mpBuffer && mpBuffer->meDirection == ScanlineDirection::TopDown;
55
34.7k
    }
56
57
34.7k
    bool IsBottomUp() const { return !IsTopDown(); }
58
59
    ScanlineFormat GetScanlineFormat() const
60
65.6M
    {
61
65.6M
        assert(mpBuffer && "Access is not valid!");
62
63
65.6M
        return mpBuffer ? mpBuffer->meFormat : ScanlineFormat::NONE;
64
65.6M
    }
65
66
    sal_uInt32 GetScanlineSize() const
67
81.2M
    {
68
81.2M
        assert(mpBuffer && "Access is not valid!");
69
70
81.2M
        return mpBuffer ? mpBuffer->mnScanlineSize : 0;
71
81.2M
    }
72
73
    sal_uInt16 GetBitCount() const
74
8.73k
    {
75
8.73k
        assert(mpBuffer && "Access is not valid!");
76
77
8.73k
        return mpBuffer ? mpBuffer->mnBitCount : 0;
78
8.73k
    }
79
80
    /// Returns the BitmapColor (i.e. palette index) that is either an exact match
81
    /// of the required color, or failing that, the entry that is the closest i.e. least error
82
    /// as measured by Color::GetColorError.
83
    BitmapColor GetBestMatchingColor(const BitmapColor& rBitmapColor) const
84
62.1k
    {
85
62.1k
        if (HasPalette())
86
61.4k
            return BitmapColor(static_cast<sal_uInt8>(GetBestPaletteIndex(rBitmapColor)));
87
782
        else
88
782
            return rBitmapColor;
89
62.1k
    }
90
91
    bool HasPalette() const
92
22.2M
    {
93
22.2M
        const BitmapBuffer* pBuffer = mpBuffer;
94
95
22.2M
        assert(pBuffer && "Access is not valid!");
96
97
22.2M
        return pBuffer && !!pBuffer->maPalette;
98
22.2M
    }
99
100
    const BitmapPalette& GetPalette() const
101
131k
    {
102
131k
        const BitmapBuffer* pBuffer = mpBuffer;
103
104
131k
        assert(pBuffer && "Access is not valid!");
105
106
131k
        return pBuffer->maPalette;
107
131k
    }
108
109
    sal_uInt16 GetPaletteEntryCount() const
110
54.8k
    {
111
54.8k
        const BitmapBuffer* pBuffer = mpBuffer;
112
113
54.8k
        assert(HasPalette() && "Bitmap has no palette!");
114
115
54.8k
        return HasPalette() ? pBuffer->maPalette.GetEntryCount() : 0;
116
54.8k
    }
117
118
    const BitmapColor& GetPaletteColor(sal_uInt16 nColor) const
119
156M
    {
120
156M
        const BitmapBuffer* pBuffer = mpBuffer;
121
156M
        assert(pBuffer && "Access is not valid!");
122
156M
        assert(HasPalette() && "Bitmap has no palette!");
123
124
156M
        return pBuffer->maPalette[nColor];
125
156M
    }
126
127
    /// Returns the BitmapColor (i.e. palette index) that is either an exact match
128
    /// of the required color, or failing that, the entry that is the closest i.e. least error
129
    /// as measured by Color::GetColorError.
130
    sal_uInt16 GetBestPaletteIndex(const BitmapColor& rBitmapColor) const;
131
    /// Returns the BitmapColor (i.e. palette index) that is an exact match
132
    /// of the required color. Returns SAL_MAX_UINT16 if nothing found.
133
    sal_uInt16 GetMatchingPaletteIndex(const BitmapColor& rBitmapColor) const;
134
135
private:
136
    BitmapInfoAccess(const BitmapInfoAccess&) = delete;
137
    BitmapInfoAccess& operator=(const BitmapInfoAccess&) = delete;
138
139
protected:
140
    Bitmap maBitmap;
141
    BitmapBuffer* mpBuffer;
142
    BitmapAccessMode mnAccessMode;
143
};
144
145
class BitmapScopedInfoAccess
146
{
147
public:
148
    BitmapScopedInfoAccess(const Bitmap& rBitmap)
149
44.6k
        : moAccess(rBitmap)
150
44.6k
    {
151
44.6k
    }
152
    BitmapScopedInfoAccess(const AlphaMask& rBitmap)
153
        : moAccess(rBitmap)
154
0
    {
155
0
    }
156
6
    BitmapScopedInfoAccess() {}
157
158
    BitmapScopedInfoAccess& operator=(const Bitmap& rBitmap)
159
6
    {
160
6
        moAccess.emplace(rBitmap);
161
6
        return *this;
162
6
    }
163
164
    BitmapScopedInfoAccess& operator=(const AlphaMask& rBitmap)
165
0
    {
166
0
        moAccess.emplace(rBitmap);
167
0
        return *this;
168
0
    }
169
170
6
    bool operator!() const { return !moAccess.has_value() || !*moAccess; }
171
44.5k
    explicit operator bool() const { return moAccess && bool(*moAccess); }
172
173
0
    void reset() { moAccess.reset(); }
174
175
0
    BitmapInfoAccess* get() { return moAccess ? &*moAccess : nullptr; }
176
0
    const BitmapInfoAccess* get() const { return moAccess ? &*moAccess : nullptr; }
177
178
89.2k
    BitmapInfoAccess* operator->() { return &*moAccess; }
179
0
    const BitmapInfoAccess* operator->() const { return &*moAccess; }
180
181
0
    BitmapInfoAccess& operator*() { return *moAccess; }
182
0
    const BitmapInfoAccess& operator*() const { return *moAccess; }
183
184
private:
185
    std::optional<BitmapInfoAccess> moAccess;
186
};
187
188
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */