LibreOffice Module vcl (master)  1
RawBitmap.hxx
Go to the documentation of this file.
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/bitmapex.hxx>
15 
16 namespace vcl::bitmap
17 {
22 {
23  friend BitmapEx VCL_DLLPUBLIC CreateFromData(RawBitmap&& rawBitmap);
24  std::unique_ptr<sal_uInt8[]> mpData;
27 
28 public:
30  : maSize(rSize)
31  , mnBitCount(nBitCount)
32  {
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)
42  {
43  throw std::bad_alloc();
44  }
45  mpData.reset(new sal_uInt8[nDataSize]);
46  }
47  void SetPixel(tools::Long nY, tools::Long nX, Color nColor)
48  {
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();
55  }
57  {
58  assert(mnBitCount == 32);
59  tools::Long p = (nY * maSize.getWidth() + nX) * (mnBitCount / 8) + 3;
60  mpData[p] = nAlpha;
61  }
63  {
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]);
69  }
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() { return maSize.Height(); }
73  tools::Long Width() { return maSize.Width(); }
74  sal_uInt8 GetBitCount() const { return mnBitCount; }
75 };
76 
77 } // end vcl::bitmap
78 
79 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
tools::Long Height()
Definition: RawBitmap.hxx:72
sal_uInt8 GetBitCount() const
Definition: RawBitmap.hxx:74
sal_uInt8 GetAlpha() const
sal_uInt8 GetRed() const
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
long Long
Color GetPixel(tools::Long nY, tools::Long nX) const
Definition: RawBitmap.hxx:62
RawBitmap(Size const &rSize, sal_uInt8 nBitCount)
Definition: RawBitmap.hxx:29
constexpr tools::Long Width() const
constexpr tools::Long getHeight() const
sal_uInt8 GetBlue() const
void SetAlpha(tools::Long nY, tools::Long nX, sal_uInt8 nAlpha)
Definition: RawBitmap.hxx:56
tools::Long Width()
Definition: RawBitmap.hxx:73
Intended to be used to feed into CreateFromData to create a BitmapEx.
Definition: RawBitmap.hxx:21
short nBitCount
Definition: ipict.cxx:80
sal_uInt8 GetGreen() const
std::unique_ptr< sal_uInt8[]> mpData
Definition: RawBitmap.hxx:24
constexpr tools::Long Height() const
unsigned char sal_uInt8
void * p
BitmapEx CreateFromData(sal_uInt8 const *pData, sal_Int32 nWidth, sal_Int32 nHeight, sal_Int32 nStride, vcl::PixelFormat ePixelFormat, bool bReversColors, bool bReverseAlpha)
Copy block of image data into the bitmap.
constexpr tools::Long getWidth() const
void SetPixel(tools::Long nY, tools::Long nX, Color nColor)
Definition: RawBitmap.hxx:47
ColorAlpha