Coverage Report

Created: 2026-11-06 11:29

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