LibreOffice Module vcl (master) 1
bitmappalette.cxx
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 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <config_features.h>
21
22#include <sal/log.hxx>
23#include <osl/diagnose.h>
24#include <tools/helpers.hxx>
25
26#include <vcl/BitmapPalette.hxx>
27#include <vcl/bitmapex.hxx>
28#include <vcl/outdev.hxx>
29
30#include <svdata.hxx>
31#include <salinst.hxx>
32
34{
35public:
36 ImplBitmapPalette(std::initializer_list<BitmapColor> aBitmapColor)
37 : maBitmapColor(aBitmapColor)
38 {
39 }
40 ImplBitmapPalette(const BitmapColor* first, const BitmapColor* last)
42 {
43 }
45 ImplBitmapPalette(sal_uInt16 nCount)
47 {
48 }
49 std::vector<BitmapColor>& GetBitmapData() { return maBitmapColor; }
50 const std::vector<BitmapColor>& GetBitmapData() const { return maBitmapColor; }
51 bool operator==(const ImplBitmapPalette& rBitmapPalette) const
52 {
53 return maBitmapColor == rBitmapPalette.maBitmapColor;
54 }
55
56private:
57 std::vector<BitmapColor> maBitmapColor;
58};
59
60namespace
61{
62BitmapPalette::ImplType& GetGlobalDefault()
63{
64 static BitmapPalette::ImplType gDefault;
65 return gDefault;
66}
67}
68
70 : mpImpl(GetGlobalDefault())
71{
72}
73
75 : mpImpl(rOther.mpImpl)
76{
77}
78
80 : mpImpl(std::move(rOther.mpImpl))
81{
82}
83
84BitmapPalette::BitmapPalette(std::initializer_list<BitmapColor> aBitmapColor)
85 : mpImpl(aBitmapColor)
86{
87}
88
90 : mpImpl({ first, last })
91{
92}
93
95 : mpImpl(nCount)
96{
97}
98
100
102{
103 mpImpl = rOther.mpImpl;
104 return *this;
105}
106
108{
109 mpImpl = std::move(rOther.mpImpl);
110 return *this;
111}
112
114{
115 return mpImpl->GetBitmapData().data();
116}
117
118BitmapColor* BitmapPalette::ImplGetColorBuffer() { return mpImpl->GetBitmapData().data(); }
119
121{
122 auto const& rBitmapData = mpImpl->GetBitmapData();
123 return vcl_get_checksum(0, rBitmapData.data(), rBitmapData.size() * sizeof(BitmapColor));
124}
125
127{
128 return mpImpl == rOther.mpImpl;
129}
130
131bool BitmapPalette::operator!() const { return mpImpl->GetBitmapData().empty(); }
132
133sal_uInt16 BitmapPalette::GetEntryCount() const { return mpImpl->GetBitmapData().size(); }
134
135void BitmapPalette::SetEntryCount(sal_uInt16 nCount) { mpImpl->GetBitmapData().resize(nCount); }
136
137const BitmapColor& BitmapPalette::operator[](sal_uInt16 nIndex) const
138{
139 assert(nIndex < mpImpl->GetBitmapData().size() && "Palette index is out of range");
140 return mpImpl->GetBitmapData()[nIndex];
141}
142
144{
145 assert(nIndex < mpImpl->GetBitmapData().size() && "Palette index is out of range");
146 return mpImpl->GetBitmapData()[nIndex];
147}
148
149sal_uInt16 BitmapPalette::GetBestIndex(const BitmapColor& rCol) const
150{
151 auto const& rBitmapColor = mpImpl->GetBitmapData();
152 sal_uInt16 nRetIndex = 0;
153
154 if (!rBitmapColor.empty())
155 {
156 for (size_t j = 0; j < rBitmapColor.size(); ++j)
157 {
158 if (rCol == rBitmapColor[j])
159 {
160 return j;
161 }
162 }
163
164 sal_uInt16 nLastErr = SAL_MAX_UINT16;
165 for (size_t i = 0; i < rBitmapColor.size(); ++i)
166 {
167 const sal_uInt16 nActErr = rCol.GetColorError(rBitmapColor[i]);
168 if (nActErr < nLastErr)
169 {
170 nLastErr = nActErr;
171 nRetIndex = i;
172 }
173 }
174 }
175
176 return nRetIndex;
177}
178
180{
181 auto const& rBitmapColor = mpImpl->GetBitmapData();
182 const int nEntryCount = GetEntryCount();
183 if (!nEntryCount) // NOTE: an empty palette means 1:1 mapping
184 return true;
185 // See above: only certain entry values will result in a valid call to GetGreyPalette
186 if (nEntryCount == 2 || nEntryCount == 4 || nEntryCount == 16 || nEntryCount == 256)
187 {
188 const BitmapPalette& rGreyPalette = Bitmap::GetGreyPalette(nEntryCount);
189 if (rGreyPalette == *this)
190 return true;
191 }
192
193 bool bRet = false;
194 // TODO: is it worth to compare the entries for the general case?
195 if (nEntryCount == 2)
196 {
197 const BitmapColor& rCol0(rBitmapColor[0]);
198 const BitmapColor& rCol1(rBitmapColor[1]);
199 bRet = rCol0.GetRed() == rCol0.GetGreen() && rCol0.GetRed() == rCol0.GetBlue()
200 && rCol1.GetRed() == rCol1.GetGreen() && rCol1.GetRed() == rCol1.GetBlue();
201 }
202 return bRet;
203}
204
206{
207 auto const& rBitmapColor = mpImpl->GetBitmapData();
208 const int nEntryCount = GetEntryCount();
209 if (!nEntryCount) // NOTE: an empty palette means 1:1 mapping
210 return true;
211 if (nEntryCount != 256)
212 return false;
213 for (sal_uInt16 i = 0; i < 256; ++i)
214 {
215 if (rBitmapColor[i] != BitmapColor(i, i, i))
216 return false;
217 }
218 return true;
219}
220
221/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt64 BitmapChecksum
Definition: checksum.hxx:30
BitmapChecksum vcl_get_checksum(BitmapChecksum Checksum, const void *Data, sal_uInt32 DatLen)
Definition: checksum.hxx:72
BitmapChecksum GetChecksum() const
sal_uInt16 GetEntryCount() const
bool IsGreyPaletteAny() const
Returns true if the palette is a grey palette (may not be 8-bit).
void SetEntryCount(sal_uInt16 nCount)
bool operator==(const BitmapPalette &rBitmapPalette) const
bool IsGreyPalette8Bit() const
Returns true if the palette is 8-bit grey palette.
const BitmapColor & operator[](sal_uInt16 nIndex) const
bool operator!() const
sal_uInt16 GetBestIndex(const BitmapColor &rCol) const
SAL_DLLPRIVATE const BitmapColor * ImplGetColorBuffer() const
BitmapPalette & operator=(const BitmapPalette &)
static const BitmapPalette & GetGreyPalette(int nEntries)
sal_uInt8 GetBlue() const
sal_uInt8 GetRed() const
sal_uInt16 GetColorError(const Color &rCompareColor) const
sal_uInt8 GetGreen() const
const std::vector< BitmapColor > & GetBitmapData() const
std::vector< BitmapColor > & GetBitmapData()
bool operator==(const ImplBitmapPalette &rBitmapPalette) const
ImplBitmapPalette(sal_uInt16 nCount)
ImplBitmapPalette(const BitmapColor *first, const BitmapColor *last)
ImplBitmapPalette(std::initializer_list< BitmapColor > aBitmapColor)
std::vector< BitmapColor > maBitmapColor
bool empty() const
int nCount
sal_Int32 nIndex
size
int i
constexpr OUStringLiteral first
constexpr OUStringLiteral last
#define SAL_MAX_UINT16