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 {
35 public:
36  ImplBitmapPalette(std::initializer_list<BitmapColor> aBitmapColor)
37  : maBitmapColor(aBitmapColor)
38  {
39  }
41  ImplBitmapPalette(sal_uInt16 nCount)
42  : maBitmapColor(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 
52 private:
53  std::vector<BitmapColor> maBitmapColor;
54 };
55 
56 namespace
57 {
58 BitmapPalette::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 
80 BitmapPalette::BitmapPalette(std::initializer_list<BitmapColor> aBitmapColor)
81  : mpImpl(aBitmapColor)
82 {
83 }
84 
85 BitmapPalette::BitmapPalette(sal_uInt16 nCount)
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 
104 SAL_DLLPRIVATE const BitmapColor* BitmapPalette::ImplGetColorBuffer() const
105 {
106  return mpImpl->GetBitmapData().data();
107 }
108 
110 {
111  return mpImpl->GetBitmapData().data();
112 }
113 
115 {
116  auto const& rBitmapData = mpImpl->GetBitmapData();
117  return vcl_get_checksum(0, rBitmapData.data(), rBitmapData.size() * sizeof(BitmapColor));
118 }
119 
120 bool BitmapPalette::operator==(const BitmapPalette& rOther) const
121 {
122  return mpImpl == rOther.mpImpl;
123 }
124 
125 bool BitmapPalette::operator!() const { return mpImpl->GetBitmapData().empty(); }
126 
127 sal_uInt16 BitmapPalette::GetEntryCount() const { return mpImpl->GetBitmapData().size(); }
128 
129 void BitmapPalette::SetEntryCount(sal_uInt16 nCount) { mpImpl->GetBitmapData().resize(nCount); }
130 
131 const BitmapColor& BitmapPalette::operator[](sal_uInt16 nIndex) const
132 {
133  assert(nIndex < mpImpl->GetBitmapData().size() && "Palette index is out of range");
134  return mpImpl->GetBitmapData()[nIndex];
135 }
136 
138 {
139  assert(nIndex < mpImpl->GetBitmapData().size() && "Palette index is out of range");
140  return mpImpl->GetBitmapData()[nIndex];
141 }
142 
143 sal_uInt16 BitmapPalette::GetBestIndex(const BitmapColor& rCol) const
144 {
145  auto const& rBitmapColor = mpImpl->GetBitmapData();
146  sal_uInt16 nRetIndex = 0;
147 
148  if (!rBitmapColor.empty())
149  {
150  for (size_t j = 0; j < rBitmapColor.size(); ++j)
151  {
152  if (rCol == rBitmapColor[j])
153  {
154  return j;
155  }
156  }
157 
158  sal_uInt16 nLastErr = SAL_MAX_UINT16;
159  for (size_t i = 0; i < rBitmapColor.size(); ++i)
160  {
161  const sal_uInt16 nActErr = rCol.GetColorError(rBitmapColor[i]);
162  if (nActErr < nLastErr)
163  {
164  nLastErr = nActErr;
165  nRetIndex = i;
166  }
167  }
168  }
169 
170  return nRetIndex;
171 }
172 
174 {
175  auto const& rBitmapColor = mpImpl->GetBitmapData();
176  const int nEntryCount = GetEntryCount();
177  if (!nEntryCount) // NOTE: an empty palette means 1:1 mapping
178  return true;
179  // See above: only certain entry values will result in a valid call to GetGreyPalette
180  if (nEntryCount == 2 || nEntryCount == 4 || nEntryCount == 16 || nEntryCount == 256)
181  {
182  const BitmapPalette& rGreyPalette = Bitmap::GetGreyPalette(nEntryCount);
183  if (rGreyPalette == *this)
184  return true;
185  }
186 
187  bool bRet = false;
188  // TODO: is it worth to compare the entries for the general case?
189  if (nEntryCount == 2)
190  {
191  const BitmapColor& rCol0(rBitmapColor[0]);
192  const BitmapColor& rCol1(rBitmapColor[1]);
193  bRet = rCol0.GetRed() == rCol0.GetGreen() && rCol0.GetRed() == rCol0.GetBlue()
194  && rCol1.GetRed() == rCol1.GetGreen() && rCol1.GetRed() == rCol1.GetBlue();
195  }
196  return bRet;
197 }
198 
200 {
201  auto const& rBitmapColor = mpImpl->GetBitmapData();
202  const int nEntryCount = GetEntryCount();
203  if (!nEntryCount) // NOTE: an empty palette means 1:1 mapping
204  return true;
205  if (nEntryCount != 256)
206  return false;
207  for (sal_uInt16 i = 0; i < 256; ++i)
208  {
209  if (rBitmapColor[i] != BitmapColor(i, i, i))
210  return false;
211  }
212  return true;
213 }
214 
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool operator==(const ImplBitmapPalette &rBitmapPalette) const
sal_uInt64 BitmapChecksum
Definition: checksum.hxx:30
bool IsGreyPalette8Bit() const
Returns true if the palette is 8-bit grey palette.
sal_uInt8 GetRed() const
BitmapChecksum GetChecksum() const
void SetEntryCount(sal_uInt16 nCount)
BitmapPalette & operator=(const BitmapPalette &)
bool operator!() const
std::vector< BitmapColor > & GetBitmapData()
SAL_DLLPRIVATE const BitmapColor * ImplGetColorBuffer() const
BitmapChecksum vcl_get_checksum(BitmapChecksum Checksum, const void *Data, sal_uInt32 DatLen)
Definition: checksum.hxx:72
const BitmapColor & operator[](sal_uInt16 nIndex) const
#define SAL_MAX_UINT16
sal_uInt16 GetColorError(const Color &rCompareColor) const
sal_uInt8 GetBlue() const
const std::vector< BitmapColor > & GetBitmapData() const
sal_uInt16 GetEntryCount() const
int i
ImplBitmapPalette(sal_uInt16 nCount)
size
ImplBitmapPalette(std::initializer_list< BitmapColor > aBitmapColor)
sal_uInt16 GetBestIndex(const BitmapColor &rCol) const
sal_uInt8 GetGreen() const
std::vector< BitmapColor > maBitmapColor
static const BitmapPalette & GetGreyPalette(int nEntries)
bool IsGreyPaletteAny() const
Returns true if the palette is a grey palette (may not be 8-bit).
bool operator==(const BitmapPalette &rBitmapPalette) const