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 
105 {
106  return mpImpl->GetBitmapData().data();
107 }
108 
110 
112 {
113  auto const& rBitmapData = mpImpl->GetBitmapData();
114  return vcl_get_checksum(0, rBitmapData.data(), rBitmapData.size() * sizeof(BitmapColor));
115 }
116 
117 bool BitmapPalette::operator==(const BitmapPalette& rOther) const
118 {
119  return mpImpl == rOther.mpImpl;
120 }
121 
122 bool BitmapPalette::operator!() const { return mpImpl->GetBitmapData().empty(); }
123 
124 sal_uInt16 BitmapPalette::GetEntryCount() const { return mpImpl->GetBitmapData().size(); }
125 
126 void BitmapPalette::SetEntryCount(sal_uInt16 nCount) { mpImpl->GetBitmapData().resize(nCount); }
127 
128 const 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 
140 sal_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: */
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()
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).
SAL_DLLPRIVATE const BitmapColor * ImplGetColorBuffer() const
bool operator==(const BitmapPalette &rBitmapPalette) const