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 }
41 ImplBitmapPalette(sal_uInt16 nCount)
43 {
44 }
45 std::vector<BitmapColor>& GetBitmapData() { return maBitmapColor; }
46 const std::vector<BitmapColor>& GetBitmapData() const { return maBitmapColor; }
47 bool operator==(const ImplBitmapPalette& rBitmapPalette) const
48 {
49 return maBitmapColor == rBitmapPalette.maBitmapColor;
50 }
51
52private:
53 std::vector<BitmapColor> maBitmapColor;
54};
55
56namespace
57{
58BitmapPalette::ImplType& GetGlobalDefault()
59{
60 static BitmapPalette::ImplType gDefault;
61 return gDefault;
62}
63}
64
66 : mpImpl(GetGlobalDefault())
67{
68}
69
71 : mpImpl(rOther.mpImpl)
72{
73}
74
76 : mpImpl(std::move(rOther.mpImpl))
77{
78}
79
80BitmapPalette::BitmapPalette(std::initializer_list<BitmapColor> aBitmapColor)
81 : mpImpl(aBitmapColor)
82{
83}
84
86 : mpImpl(nCount)
87{
88}
89
91
93{
94 mpImpl = rOther.mpImpl;
95 return *this;
96}
97
99{
100 mpImpl = std::move(rOther.mpImpl);
101 return *this;
102}
103
105{
106 return mpImpl->GetBitmapData().data();
107}
108
109BitmapColor* BitmapPalette::ImplGetColorBuffer() { return mpImpl->GetBitmapData().data(); }
110
112{
113 auto const& rBitmapData = mpImpl->GetBitmapData();
114 return vcl_get_checksum(0, rBitmapData.data(), rBitmapData.size() * sizeof(BitmapColor));
115}
116
118{
119 return mpImpl == rOther.mpImpl;
120}
121
122bool BitmapPalette::operator!() const { return mpImpl->GetBitmapData().empty(); }
123
124sal_uInt16 BitmapPalette::GetEntryCount() const { return mpImpl->GetBitmapData().size(); }
125
126void BitmapPalette::SetEntryCount(sal_uInt16 nCount) { mpImpl->GetBitmapData().resize(nCount); }
127
128const BitmapColor& BitmapPalette::operator[](sal_uInt16 nIndex) const
129{
130 assert(nIndex < mpImpl->GetBitmapData().size() && "Palette index is out of range");
131 return mpImpl->GetBitmapData()[nIndex];
132}
133
135{
136 assert(nIndex < mpImpl->GetBitmapData().size() && "Palette index is out of range");
137 return mpImpl->GetBitmapData()[nIndex];
138}
139
140sal_uInt16 BitmapPalette::GetBestIndex(const BitmapColor& rCol) const
141{
142 auto const& rBitmapColor = mpImpl->GetBitmapData();
143 sal_uInt16 nRetIndex = 0;
144
145 if (!rBitmapColor.empty())
146 {
147 for (size_t j = 0; j < rBitmapColor.size(); ++j)
148 {
149 if (rCol == rBitmapColor[j])
150 {
151 return j;
152 }
153 }
154
155 sal_uInt16 nLastErr = SAL_MAX_UINT16;
156 for (size_t i = 0; i < rBitmapColor.size(); ++i)
157 {
158 const sal_uInt16 nActErr = rCol.GetColorError(rBitmapColor[i]);
159 if (nActErr < nLastErr)
160 {
161 nLastErr = nActErr;
162 nRetIndex = i;
163 }
164 }
165 }
166
167 return nRetIndex;
168}
169
171{
172 auto const& rBitmapColor = mpImpl->GetBitmapData();
173 const int nEntryCount = GetEntryCount();
174 if (!nEntryCount) // NOTE: an empty palette means 1:1 mapping
175 return true;
176 // See above: only certain entry values will result in a valid call to GetGreyPalette
177 if (nEntryCount == 2 || nEntryCount == 4 || nEntryCount == 16 || nEntryCount == 256)
178 {
179 const BitmapPalette& rGreyPalette = Bitmap::GetGreyPalette(nEntryCount);
180 if (rGreyPalette == *this)
181 return true;
182 }
183
184 bool bRet = false;
185 // TODO: is it worth to compare the entries for the general case?
186 if (nEntryCount == 2)
187 {
188 const BitmapColor& rCol0(rBitmapColor[0]);
189 const BitmapColor& rCol1(rBitmapColor[1]);
190 bRet = rCol0.GetRed() == rCol0.GetGreen() && rCol0.GetRed() == rCol0.GetBlue()
191 && rCol1.GetRed() == rCol1.GetGreen() && rCol1.GetRed() == rCol1.GetBlue();
192 }
193 return bRet;
194}
195
197{
198 auto const& rBitmapColor = mpImpl->GetBitmapData();
199 const int nEntryCount = GetEntryCount();
200 if (!nEntryCount) // NOTE: an empty palette means 1:1 mapping
201 return true;
202 if (nEntryCount != 256)
203 return false;
204 for (sal_uInt16 i = 0; i < 256; ++i)
205 {
206 if (rBitmapColor[i] != BitmapColor(i, i, i))
207 return false;
208 }
209 return true;
210}
211
212/* 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(std::initializer_list< BitmapColor > aBitmapColor)
std::vector< BitmapColor > maBitmapColor
bool empty() const
int nCount
sal_Int32 nIndex
size
int i
#define SAL_MAX_UINT16