bump product version to 7.6.3.2-android
[LibreOffice.git] / include / vcl / RawBitmap.hxx
blob12c924dbb8ba88d0c9e8c6cb87519200229128bc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #pragma once
12 #include <o3tl/safeint.hxx>
13 #include <vcl/BitmapColor.hxx>
14 #include <vcl/bitmapex.hxx>
16 namespace vcl::bitmap
18 /**
19 * Intended to be used to feed into CreateFromData to create a BitmapEx. RGB data format.
21 class VCL_DLLPUBLIC RawBitmap
23 friend BitmapEx VCL_DLLPUBLIC CreateFromData(RawBitmap&& rawBitmap);
24 std::unique_ptr<sal_uInt8[]> mpData;
25 Size maSize;
26 sal_uInt8 mnBitCount;
28 public:
29 RawBitmap(Size const& rSize, sal_uInt8 nBitCount)
30 : maSize(rSize)
31 , mnBitCount(nBitCount)
33 assert(nBitCount == 24 || nBitCount == 32);
34 if (rSize.getWidth() > std::numeric_limits<sal_Int32>::max() || rSize.getWidth() < 0)
35 throw std::bad_alloc();
36 if (rSize.getHeight() > std::numeric_limits<sal_Int32>::max() || rSize.getHeight() < 0)
37 throw std::bad_alloc();
38 sal_Int32 nRowSize, nDataSize;
39 if (o3tl::checked_multiply<sal_Int32>(rSize.getWidth(), nBitCount / 8, nRowSize)
40 || o3tl::checked_multiply<sal_Int32>(nRowSize, rSize.getHeight(), nDataSize)
41 || nDataSize < 0)
43 throw std::bad_alloc();
45 mpData.reset(new sal_uInt8[nDataSize]);
47 void SetPixel(tools::Long nY, tools::Long nX, Color nColor)
49 tools::Long p = (nY * maSize.getWidth() + nX) * (mnBitCount / 8);
50 mpData[p++] = nColor.GetRed();
51 mpData[p++] = nColor.GetGreen();
52 mpData[p++] = nColor.GetBlue();
53 if (mnBitCount == 32)
54 mpData[p] = nColor.GetAlpha();
56 void SetAlpha(tools::Long nY, tools::Long nX, sal_uInt8 nAlpha)
58 assert(mnBitCount == 32);
59 tools::Long p = (nY * maSize.getWidth() + nX) * (mnBitCount / 8) + 3;
60 mpData[p] = nAlpha;
62 Color GetPixel(tools::Long nY, tools::Long nX) const
64 tools::Long p = (nY * maSize.getWidth() + nX) * mnBitCount / 8;
65 if (mnBitCount == 24)
66 return Color(mpData[p], mpData[p + 1], mpData[p + 2]);
67 else
68 return Color(ColorAlpha, mpData[p + 3], mpData[p], mpData[p + 1], mpData[p + 2]);
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 tools::Long Height() const { return maSize.Height(); }
73 tools::Long Width() const { return maSize.Width(); }
74 sal_uInt8 GetBitCount() const { return mnBitCount; }
77 } // end vcl::bitmap
79 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */