/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 <tools/gen.hxx> |
14 | | #include <tools/long.hxx> |
15 | | #include <vcl/BitmapColor.hxx> |
16 | | |
17 | | class Bitmap; |
18 | | |
19 | | namespace vcl::bitmap |
20 | | { |
21 | | /** |
22 | | * Intended to be used to feed into CreateFromData to create a Bitmap. RGB data format. |
23 | | */ |
24 | | class VCL_DLLPUBLIC RawBitmap |
25 | | { |
26 | | friend Bitmap VCL_DLLPUBLIC CreateFromData(RawBitmap&& rawBitmap); |
27 | | std::unique_ptr<sal_uInt8[]> mpData; |
28 | | Size maSize; |
29 | | sal_uInt8 mnBitCount; |
30 | | |
31 | | public: |
32 | | RawBitmap(Size const& rSize, sal_uInt8 nBitCount) |
33 | 310k | : maSize(rSize) |
34 | 310k | , mnBitCount(nBitCount) |
35 | 310k | { |
36 | 310k | assert(nBitCount == 24 || nBitCount == 32); |
37 | 310k | if (rSize.getWidth() > std::numeric_limits<sal_Int32>::max() || rSize.getWidth() < 0) |
38 | 1.57k | throw std::bad_alloc(); |
39 | 308k | if (rSize.getHeight() > std::numeric_limits<sal_Int32>::max() || rSize.getHeight() < 0) |
40 | 204 | throw std::bad_alloc(); |
41 | 308k | sal_Int32 nRowSize, nDataSize; |
42 | 308k | if (o3tl::checked_multiply<sal_Int32>(rSize.getWidth(), nBitCount / 8, nRowSize) |
43 | 307k | || o3tl::checked_multiply<sal_Int32>(nRowSize, rSize.getHeight(), nDataSize) |
44 | 307k | || nDataSize < 0) |
45 | 527 | { |
46 | 527 | throw std::bad_alloc(); |
47 | 527 | } |
48 | 307k | mpData.reset(new sal_uInt8[nDataSize]); |
49 | 307k | } |
50 | | void SetPixel(tools::Long nY, tools::Long nX, Color nColor) |
51 | 599M | { |
52 | 599M | tools::Long p = (nY * maSize.getWidth() + nX) * (mnBitCount / 8); |
53 | 599M | mpData[p++] = nColor.GetRed(); |
54 | 599M | mpData[p++] = nColor.GetGreen(); |
55 | 599M | mpData[p++] = nColor.GetBlue(); |
56 | 599M | if (mnBitCount == 32) |
57 | 3.52M | mpData[p] = nColor.GetAlpha(); |
58 | 599M | } |
59 | | void SetAlpha(tools::Long nY, tools::Long nX, sal_uInt8 nAlpha) |
60 | 1.00M | { |
61 | 1.00M | assert(mnBitCount == 32); |
62 | 1.00M | tools::Long p = (nY * maSize.getWidth() + nX) * (mnBitCount / 8) + 3; |
63 | 1.00M | mpData[p] = nAlpha; |
64 | 1.00M | } |
65 | | Color GetPixel(tools::Long nY, tools::Long nX) const |
66 | 5.32M | { |
67 | 5.32M | tools::Long p = (nY * maSize.getWidth() + nX) * mnBitCount / 8; |
68 | 5.32M | if (mnBitCount == 24) |
69 | 2.66M | return Color(mpData[p], mpData[p + 1], mpData[p + 2]); |
70 | 2.66M | else |
71 | 2.66M | return Color(ColorAlpha, mpData[p + 3], mpData[p], mpData[p + 1], mpData[p + 2]); |
72 | 5.32M | } |
73 | | // so we don't accidentally leave any code in that uses palette color indexes |
74 | | void SetPixel(tools::Long nY, tools::Long nX, BitmapColor nColor) = delete; |
75 | 603 | tools::Long Height() const { return maSize.Height(); } |
76 | 4.82k | tools::Long Width() const { return maSize.Width(); } |
77 | 288k | sal_uInt8 GetBitCount() const { return mnBitCount; } |
78 | | }; |
79 | | |
80 | | } // end vcl::bitmap |
81 | | |
82 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |