Avoid potential negative array index access to cached text.
[LibreOffice.git] / include / vcl / BitmapWriteAccess.hxx
blob9027210adb50d9d1546200af7998edcefe9ac779
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/.
9 */
10 #pragma once
12 #include <vcl/alpha.hxx>
13 #include <vcl/bitmap.hxx>
14 #include <vcl/BitmapReadAccess.hxx>
15 #include <optional>
17 class VCL_DLLPUBLIC BitmapWriteAccess final : public BitmapReadAccess
19 public:
20 BitmapWriteAccess(Bitmap& rBitmap);
21 BitmapWriteAccess(AlphaMask& rBitmap);
22 virtual ~BitmapWriteAccess() override;
24 void CopyScanline(tools::Long nY, const BitmapReadAccess& rReadAcc);
25 void CopyScanline(tools::Long nY, ConstScanline aSrcScanline, ScanlineFormat nSrcScanlineFormat,
26 sal_uInt32 nSrcScanlineSize);
28 void SetPalette(const BitmapPalette& rPalette)
30 assert(mpBuffer && "Access is not valid!");
32 mpBuffer->maPalette = rPalette;
35 void SetPaletteEntryCount(sal_uInt16 nCount)
37 assert(mpBuffer && "Access is not valid!");
39 mpBuffer->maPalette.SetEntryCount(nCount);
42 void SetPaletteColor(sal_uInt16 nColor, const BitmapColor& rBitmapColor)
44 assert(mpBuffer && "Access is not valid!");
45 assert(HasPalette() && "Bitmap has no palette!");
47 mpBuffer->maPalette[nColor] = rBitmapColor;
50 void SetPixel(tools::Long nY, tools::Long nX, const BitmapColor& rBitmapColor)
52 assert(mpBuffer && "Access is not valid!");
53 assert(nX < mpBuffer->mnWidth && "x-coordinate out of range!");
54 assert(nY < mpBuffer->mnHeight && "y-coordinate out of range!");
56 mFncSetPixel(GetScanline(nY), nX, rBitmapColor, maColorMask);
59 void SetPixelIndex(tools::Long nY, tools::Long nX, sal_uInt8 cIndex)
61 SetPixel(nY, nX, BitmapColor(cIndex));
64 void SetLineColor(const Color& rColor);
66 void SetFillColor();
67 void SetFillColor(const Color& rColor);
69 void Erase(const Color& rColor);
71 void DrawLine(const Point& rStart, const Point& rEnd);
73 void FillRect(const tools::Rectangle& rRect);
74 void DrawRect(const tools::Rectangle& rRect);
76 private:
77 std::optional<BitmapColor> mpLineColor;
78 std::optional<BitmapColor> mpFillColor;
80 BitmapWriteAccess() = delete;
81 BitmapWriteAccess(const BitmapWriteAccess&) = delete;
82 BitmapWriteAccess& operator=(const BitmapWriteAccess&) = delete;
85 class BitmapScopedWriteAccess
87 public:
88 BitmapScopedWriteAccess(Bitmap& rBitmap)
89 : moAccess(rBitmap)
92 BitmapScopedWriteAccess(AlphaMask& rBitmap)
93 : moAccess(rBitmap)
96 BitmapScopedWriteAccess() {}
98 BitmapScopedWriteAccess& operator=(Bitmap& rBitmap)
100 moAccess.emplace(rBitmap);
101 return *this;
104 BitmapScopedWriteAccess& operator=(AlphaMask& rBitmap)
106 moAccess.emplace(rBitmap);
107 return *this;
110 bool operator!() const { return !moAccess.has_value() || !*moAccess; }
111 explicit operator bool() const { return moAccess && bool(*moAccess); }
113 void reset() { moAccess.reset(); }
115 BitmapWriteAccess* get() { return moAccess ? &*moAccess : nullptr; }
116 const BitmapWriteAccess* get() const { return moAccess ? &*moAccess : nullptr; }
118 BitmapWriteAccess* operator->() { return &*moAccess; }
119 const BitmapWriteAccess* operator->() const { return &*moAccess; }
121 BitmapWriteAccess& operator*() { return *moAccess; }
122 const BitmapWriteAccess& operator*() const { return *moAccess; }
124 private:
125 std::optional<BitmapWriteAccess> moAccess;
128 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */