Fix typo
[LibreOffice.git] / include / vcl / BitmapInfoAccess.hxx
blob7ded0c9a95daeeb06cc599f303c25e3ce90ad9e4
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 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #pragma once
21 #include <vcl/dllapi.h>
22 #include <vcl/bitmap.hxx>
23 #include <vcl/Scanline.hxx>
24 #include <vcl/BitmapBuffer.hxx>
25 #include <vcl/BitmapColor.hxx>
26 #include <vcl/BitmapAccessMode.hxx>
28 bool Bitmap32IsPreMultipled();
30 typedef BitmapColor (*FncGetPixel)(ConstScanline pScanline, tools::Long nX, const ColorMask& rMask);
31 typedef void (*FncSetPixel)(Scanline pScanline, tools::Long nX, const BitmapColor& rBitmapColor,
32 const ColorMask& rMask);
34 class VCL_DLLPUBLIC BitmapInfoAccess
36 friend class BitmapReadAccess;
38 public:
39 BitmapInfoAccess(const Bitmap& rBitmap, BitmapAccessMode nMode = BitmapAccessMode::Info);
40 BitmapInfoAccess(const AlphaMask& rBitmap, BitmapAccessMode nMode = BitmapAccessMode::Info);
42 virtual ~BitmapInfoAccess();
44 bool operator!() const { return mpBuffer == nullptr; }
45 explicit operator bool() const { return mpBuffer != nullptr; }
47 tools::Long Width() const { return mpBuffer ? mpBuffer->mnWidth : 0L; }
49 tools::Long Height() const { return mpBuffer ? mpBuffer->mnHeight : 0L; }
51 bool IsTopDown() const
53 assert(mpBuffer && "Access is not valid!");
55 return mpBuffer && (mpBuffer->mnFormat & ScanlineFormat::TopDown);
58 bool IsBottomUp() const { return !IsTopDown(); }
60 ScanlineFormat GetScanlineFormat() const
62 assert(mpBuffer && "Access is not valid!");
64 return mpBuffer ? RemoveScanline(mpBuffer->mnFormat) : ScanlineFormat::NONE;
67 sal_uInt32 GetScanlineSize() const
69 assert(mpBuffer && "Access is not valid!");
71 return mpBuffer ? mpBuffer->mnScanlineSize : 0;
74 sal_uInt16 GetBitCount() const
76 assert(mpBuffer && "Access is not valid!");
78 return mpBuffer ? mpBuffer->mnBitCount : 0;
81 /// Returns the BitmapColor (i.e. palette index) that is either an exact match
82 /// of the required color, or failing that, the entry that is the closest i.e. least error
83 /// as measured by Color::GetColorError.
84 BitmapColor GetBestMatchingColor(const BitmapColor& rBitmapColor) const
86 if (HasPalette())
87 return BitmapColor(static_cast<sal_uInt8>(GetBestPaletteIndex(rBitmapColor)));
88 else
89 return rBitmapColor;
92 bool HasPalette() const
94 const BitmapBuffer* pBuffer = mpBuffer;
96 assert(pBuffer && "Access is not valid!");
98 return pBuffer && !!pBuffer->maPalette;
101 const BitmapPalette& GetPalette() const
103 const BitmapBuffer* pBuffer = mpBuffer;
105 assert(pBuffer && "Access is not valid!");
107 return pBuffer->maPalette;
110 sal_uInt16 GetPaletteEntryCount() const
112 const BitmapBuffer* pBuffer = mpBuffer;
114 assert(HasPalette() && "Bitmap has no palette!");
116 return HasPalette() ? pBuffer->maPalette.GetEntryCount() : 0;
119 const BitmapColor& GetPaletteColor(sal_uInt16 nColor) const
121 const BitmapBuffer* pBuffer = mpBuffer;
122 assert(pBuffer && "Access is not valid!");
123 assert(HasPalette() && "Bitmap has no palette!");
125 return pBuffer->maPalette[nColor];
128 /// Returns the BitmapColor (i.e. palette index) that is either an exact match
129 /// of the required color, or failing that, the entry that is the closest i.e. least error
130 /// as measured by Color::GetColorError.
131 sal_uInt16 GetBestPaletteIndex(const BitmapColor& rBitmapColor) const;
132 /// Returns the BitmapColor (i.e. palette index) that is an exact match
133 /// of the required color. Returns SAL_MAX_UINT16 if nothing found.
134 sal_uInt16 GetMatchingPaletteIndex(const BitmapColor& rBitmapColor) const;
136 const ColorMask& GetColorMask() const
138 const BitmapBuffer* pBuffer = mpBuffer;
140 assert(pBuffer && "Access is not valid!");
142 return pBuffer->maColorMask;
145 private:
146 BitmapInfoAccess(const BitmapInfoAccess&) = delete;
147 BitmapInfoAccess& operator=(const BitmapInfoAccess&) = delete;
149 protected:
150 Bitmap maBitmap;
151 BitmapBuffer* mpBuffer;
152 ColorMask maColorMask;
153 BitmapAccessMode mnAccessMode;
156 class BitmapScopedInfoAccess
158 public:
159 BitmapScopedInfoAccess(const Bitmap& rBitmap)
160 : moAccess(rBitmap)
163 BitmapScopedInfoAccess(const AlphaMask& rBitmap)
164 : moAccess(rBitmap)
167 BitmapScopedInfoAccess() {}
169 BitmapScopedInfoAccess& operator=(const Bitmap& rBitmap)
171 moAccess.emplace(rBitmap);
172 return *this;
175 BitmapScopedInfoAccess& operator=(const AlphaMask& rBitmap)
177 moAccess.emplace(rBitmap);
178 return *this;
181 bool operator!() const { return !moAccess.has_value() || !*moAccess; }
182 explicit operator bool() const { return moAccess && bool(*moAccess); }
184 void reset() { moAccess.reset(); }
186 BitmapInfoAccess* get() { return moAccess ? &*moAccess : nullptr; }
187 const BitmapInfoAccess* get() const { return moAccess ? &*moAccess : nullptr; }
189 BitmapInfoAccess* operator->() { return &*moAccess; }
190 const BitmapInfoAccess* operator->() const { return &*moAccess; }
192 BitmapInfoAccess& operator*() { return *moAccess; }
193 const BitmapInfoAccess& operator*() const { return *moAccess; }
195 private:
196 std::optional<BitmapInfoAccess> moAccess;
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */