Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/include/vcl/RawBitmap.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
10
#pragma once
11
12
#include <o3tl/safeint.hxx>
13
#include <vcl/BitmapColor.hxx>
14
#include <vcl/bitmap.hxx>
15
16
namespace vcl::bitmap
17
{
18
/**
19
 * Intended to be used to feed into CreateFromData to create a Bitmap. RGB data format.
20
 */
21
class VCL_DLLPUBLIC RawBitmap
22
{
23
    friend Bitmap VCL_DLLPUBLIC CreateFromData(RawBitmap&& rawBitmap);
24
    std::unique_ptr<sal_uInt8[]> mpData;
25
    Size maSize;
26
    sal_uInt8 mnBitCount;
27
28
public:
29
    RawBitmap(Size const& rSize, sal_uInt8 nBitCount)
30
657k
        : maSize(rSize)
31
657k
        , mnBitCount(nBitCount)
32
657k
    {
33
657k
        assert(nBitCount == 24 || nBitCount == 32);
34
657k
        if (rSize.getWidth() > std::numeric_limits<sal_Int32>::max() || rSize.getWidth() < 0)
35
1.57k
            throw std::bad_alloc();
36
656k
        if (rSize.getHeight() > std::numeric_limits<sal_Int32>::max() || rSize.getHeight() < 0)
37
229
            throw std::bad_alloc();
38
656k
        sal_Int32 nRowSize, nDataSize;
39
656k
        if (o3tl::checked_multiply<sal_Int32>(rSize.getWidth(), nBitCount / 8, nRowSize)
40
655k
            || o3tl::checked_multiply<sal_Int32>(nRowSize, rSize.getHeight(), nDataSize)
41
655k
            || nDataSize < 0)
42
454
        {
43
454
            throw std::bad_alloc();
44
454
        }
45
655k
        mpData.reset(new sal_uInt8[nDataSize]);
46
655k
    }
47
    void SetPixel(tools::Long nY, tools::Long nX, Color nColor)
48
482M
    {
49
482M
        tools::Long p = (nY * maSize.getWidth() + nX) * (mnBitCount / 8);
50
482M
        mpData[p++] = nColor.GetRed();
51
482M
        mpData[p++] = nColor.GetGreen();
52
482M
        mpData[p++] = nColor.GetBlue();
53
482M
        if (mnBitCount == 32)
54
1.30M
            mpData[p] = nColor.GetAlpha();
55
482M
    }
56
    void SetAlpha(tools::Long nY, tools::Long nX, sal_uInt8 nAlpha)
57
375k
    {
58
375k
        assert(mnBitCount == 32);
59
375k
        tools::Long p = (nY * maSize.getWidth() + nX) * (mnBitCount / 8) + 3;
60
375k
        mpData[p] = nAlpha;
61
375k
    }
62
    Color GetPixel(tools::Long nY, tools::Long nX) const
63
4.24M
    {
64
4.24M
        tools::Long p = (nY * maSize.getWidth() + nX) * mnBitCount / 8;
65
4.24M
        if (mnBitCount == 24)
66
3.28M
            return Color(mpData[p], mpData[p + 1], mpData[p + 2]);
67
961k
        else
68
961k
            return Color(ColorAlpha, mpData[p + 3], mpData[p], mpData[p + 1], mpData[p + 2]);
69
4.24M
    }
70
    // so we don't accidentally leave any code in that uses palette color indexes
71
    void SetPixel(tools::Long nY, tools::Long nX, BitmapColor nColor) = delete;
72
693
    tools::Long Height() const { return maSize.Height(); }
73
5.54k
    tools::Long Width() const { return maSize.Width(); }
74
637k
    sal_uInt8 GetBitCount() const { return mnBitCount; }
75
};
76
77
} // end vcl::bitmap
78
79
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */