LibreOffice Module svx (master)  1
Palette.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 <svx/Palette.hxx>
21 #include <tools/stream.hxx>
22 
23 #include <palettes.hxx>
24 
26 {
27 }
28 
30 {
31 }
32 
33 PaletteASE::PaletteASE( const OUString &rFPath, const OUString &rFName ) :
34  mbValidPalette( false ),
35  maFPath ( rFPath ),
36  maASEPaletteName ( rFName )
37 {
38  LoadPalette();
39 }
40 
42 {
43  rColorSet.Clear();
44  int nIx = 1;
45  for (const auto& rColor : maColors)
46  {
47  rColorSet.InsertItem(nIx, rColor.first, rColor.second);
48  ++nIx;
49  }
50 }
51 
53 {
54  rColorSet.Clear();
55  int nIx = 1;
56  for (const auto& rColor : maColors)
57  {
58  rColorSet.InsertItem(nIx, rColor.first, rColor.second);
59  ++nIx;
60  }
61 }
62 
63 const OUString& PaletteASE::GetName()
64 {
65  return maASEPaletteName;
66 }
67 
68 const OUString& PaletteASE::GetPath()
69 {
70  return maFPath;
71 }
72 
74 {
75  return mbValidPalette;
76 }
77 
78 // CMYK values from 0 to 1
79 // TODO: Deduplicate me (taken from core/cui/source/dialogs/colorpicker.cxx)
80 static void lcl_CMYKtoRGB( float fCyan, float fMagenta, float fYellow, float fKey, float& dR, float& dG, float& dB )
81 {
82  fCyan = (fCyan * ( 1.0 - fKey )) + fKey;
83  fMagenta = (fMagenta * ( 1.0 - fKey )) + fKey;
84  fYellow = (fYellow * ( 1.0 - fKey )) + fKey;
85 
86  dR = std::max( std::min( ( 1.0 - fCyan ), 1.0), 0.0 );
87  dG = std::max( std::min( ( 1.0 - fMagenta ), 1.0), 0.0 );
88  dB = std::max( std::min( ( 1.0 - fYellow ), 1.0), 0.0 );
89 }
90 
92 {
93  SvFileStream aFile(maFPath, StreamMode::READ);
94  aFile.SetEndian(SvStreamEndian::BIG);
95 
96  // Verify magic first 4 characters
97  char cMagic[5] = {0};
98  if ((aFile.ReadBytes(cMagic, 4) != 4) || (strncmp(cMagic, "ASEF", 4) != 0))
99  {
100  mbValidPalette = false;
101  return;
102  }
103 
104  // Ignore the version number
105  aFile.SeekRel(4);
106 
107  sal_uInt32 nBlocks = 0;
108  aFile.ReadUInt32(nBlocks);
109  for (sal_uInt32 nI = 0; nI < nBlocks; nI++) {
110  sal_uInt32 nChunkType = 0;
111  aFile.ReadUInt32(nChunkType);
112  // End chunk
113  if (nChunkType == 0)
114  break;
115 
116  // Grab chunk size, name length
117  sal_uInt16 nChunkSize = 0;
118  sal_uInt16 nChars = 0;
119  aFile.ReadUInt16(nChunkSize);
120  aFile.ReadUInt16(nChars);
121 
122  OUString aPaletteName("");
123  if (nChars > 1)
124  aPaletteName = read_uInt16s_ToOUString(aFile, nChars);
125  else
126  aFile.SeekRel(2);
127 
128  if (nChunkType == 0xC0010000)
129  {
130  // Got a start chunk, so set palette name
131  maASEPaletteName = aPaletteName;
132  // Is there color data? (shouldn't happen in a start block, but check anyway)
133  if (nChunkSize > ((nChars * 2) + 2))
134  aPaletteName.clear();
135  else
136  continue;
137  }
138 
139  char cColorModel[5] = {0};
140  aFile.ReadBytes(cColorModel, 4);
141  OString aColorModel(cColorModel);
142  // r, g, and b are floats ranging from 0 to 1
143  float r = 0, g = 0, b = 0;
144 
145  if (aColorModel.equalsIgnoreAsciiCase("cmyk"))
146  {
147  float c = 0, m = 0, y = 0, k = 0;
148  aFile.ReadFloat(c);
149  aFile.ReadFloat(m);
150  aFile.ReadFloat(y);
151  aFile.ReadFloat(k);
152  lcl_CMYKtoRGB(c, m, y, k, r, g, b);
153  }
154  else if (aColorModel.equalsIgnoreAsciiCase("rgb "))
155  {
156  aFile.ReadFloat(r);
157  aFile.ReadFloat(g);
158  aFile.ReadFloat(b);
159  }
160  else if (aColorModel.equalsIgnoreAsciiCase("gray"))
161  {
162  float nVal = 0;
163  aFile.ReadFloat(nVal);
164  r = g = b = nVal;
165  }
166  else
167  {
168  float nL = 0, nA = 0, nB = 0;
169  aFile.ReadFloat(nL);
170  aFile.ReadFloat(nA);
171  aFile.ReadFloat(nB);
172  // TODO: How to convert LAB to RGB?
173  r = g = b = 0;
174  }
175 
176  // Ignore color type
177  aFile.SeekRel(2);
178  maColors.emplace_back(Color(r * 255, g * 255, b * 255), aPaletteName);
179  }
180 
181  mbValidPalette = true;
182 }
183 
184 // PaletteGPL ------------------------------------------------------------------
185 
186 static OString lcl_getToken(const OString& rStr, sal_Int32& index);
187 
188 PaletteGPL::PaletteGPL( const OUString &rFPath, const OUString &rFName ) :
189  mbLoadedPalette( false ),
190  mbValidPalette( false ),
191  maFName( rFName ),
192  maFPath( rFPath )
193 {
195 }
196 
198 {
199 }
200 
201 const OUString& PaletteGPL::GetName()
202 {
203  return maGPLPaletteName;
204 }
205 
206 const OUString& PaletteGPL::GetPath()
207 {
208  return maFPath;
209 }
210 
212 {
213  LoadPalette();
214 
215  rColorSet.Clear();
216  int nIx = 1;
217  for (const auto& rColor : maColors)
218  {
219  rColorSet.InsertItem(nIx, rColor.first, rColor.second);
220  ++nIx;
221  }
222 }
223 
225 {
226  LoadPalette();
227 
228  rColorSet.Clear();
229  int nIx = 1;
230  for (const auto& rColor : maColors)
231  {
232  rColorSet.InsertItem(nIx, rColor.first, rColor.second);
233  ++nIx;
234  }
235 }
236 
238 {
239  return mbValidPalette;
240 }
241 
243 {
244  OString aLine;
245  OString aPaletteName;
246 
247  rFileStream.ReadLine(aLine);
248  if( !aLine.startsWith("GIMP Palette") ) return false;
249  rFileStream.ReadLine(aLine);
250  if( aLine.startsWith("Name: ", &aPaletteName) )
251  {
252  maGPLPaletteName = OStringToOUString(aPaletteName, RTL_TEXTENCODING_ASCII_US);
253  rFileStream.ReadLine(aLine);
254  if( aLine.startsWith("Columns: "))
255  rFileStream.ReadLine(aLine); // we can ignore this
256  }
257  else
258  {
260  }
261  return true;
262 }
263 
265 {
266  SvFileStream aFile(maFPath, StreamMode::READ);
268 }
269 
271 {
272  if( mbLoadedPalette ) return;
273  mbLoadedPalette = true;
274 
275  // TODO add error handling!!!
276  SvFileStream aFile(maFPath, StreamMode::READ);
278 
279  if( !mbValidPalette ) return;
280 
281  OString aLine;
282  do {
283  if (aLine[0] != '#' && aLine[0] != '\n')
284  {
285  // TODO check if r,g,b are 0<= x <=255, or just clamp?
286  sal_Int32 nIndex = 0;
287  OString token;
288 
289  token = lcl_getToken(aLine, nIndex);
290  if(token.isEmpty() || nIndex == -1) continue;
291  sal_Int32 r = token.toInt32();
292 
293  token = lcl_getToken(aLine, nIndex);
294  if(token.isEmpty() || nIndex == -1) continue;
295  sal_Int32 g = token.toInt32();
296 
297  token = lcl_getToken(aLine, nIndex);
298  if(token.isEmpty()) continue;
299  sal_Int32 b = token.toInt32();
300 
301  OString name;
302  if(nIndex != -1)
303  name = aLine.copy(nIndex);
304 
305  maColors.emplace_back(
306  Color(r, g, b),
307  OStringToOUString(name, RTL_TEXTENCODING_ASCII_US));
308  }
309  } while (aFile.ReadLine(aLine));
310 }
311 
312 // finds first token in rStr from index, separated by whitespace
313 // returns position of next token in index
314 static OString lcl_getToken(const OString& rStr, sal_Int32& index)
315 {
316  sal_Int32 substart, toklen = 0;
317  OUString aWhitespaceChars( " \n\t" );
318 
319  while(index < rStr.getLength() &&
320  aWhitespaceChars.indexOf( rStr[index] ) != -1)
321  ++index;
322  if(index == rStr.getLength())
323  {
324  index = -1;
325  return OString();
326  }
327  substart = index;
328 
329  //counts length of token
330  while(index < rStr.getLength() &&
331  aWhitespaceChars.indexOf( rStr[index] ) == -1 )
332  {
333  ++index;
334  ++toklen;
335  }
336 
337  //counts to position of next token
338  while(index < rStr.getLength() &&
339  aWhitespaceChars.indexOf( rStr[index] ) != -1 )
340  ++index;
341  if(index == rStr.getLength())
342  index = -1;
343 
344  return rStr.copy(substart, toklen);
345 }
346 
347 // PaletteSOC ------------------------------------------------------------------
348 
349 PaletteSOC::PaletteSOC( const OUString &rFPath, const OUString &rFName ) :
350  mbLoadedPalette( false ),
351  maFPath( rFPath ),
352  maSOCPaletteName( rFName )
353 {
354 }
355 
357 {
358 }
359 
360 const OUString& PaletteSOC::GetName()
361 {
362  return maSOCPaletteName;
363 }
364 
365 const OUString& PaletteSOC::GetPath()
366 {
367  return maFPath;
368 }
369 
371 {
372  if( !mbLoadedPalette )
373  {
374  mbLoadedPalette = true;
376  (void)mpColorList->Load();
377  }
378  rColorSet.Clear();
379  if( mpColorList.is() )
380  rColorSet.addEntriesForXColorList( *mpColorList );
381 }
382 
384 {
385  if( !mbLoadedPalette )
386  {
387  mbLoadedPalette = true;
389  (void)mpColorList->Load();
390  }
391  rColorSet.Clear();
392  if( mpColorList.is() )
393  rColorSet.addEntriesForXColorList( *mpColorList );
394 }
395 
397 {
398  return true;
399 }
400 
401 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void LoadColorSet(SvxColorValueSet &rColorSet) override
Definition: Palette.cxx:211
sal_Int32 nIndex
void LoadPalette()
Definition: Palette.cxx:91
virtual ~Palette()
Definition: Palette.cxx:25
OUString read_uInt16s_ToOUString(SvStream &rStrm, std::size_t nLen)
bool mbLoadedPalette
Definition: palettes.hxx:56
SvStream & ReadUInt16(sal_uInt16 &rUInt16)
virtual const OUString & GetName() override
Definition: Palette.cxx:360
virtual bool IsValid() override
Definition: Palette.cxx:396
PaletteGPL(const OUString &rFPath, const OUString &rFName)
Definition: Palette.cxx:188
OUString maASEPaletteName
Definition: palettes.hxx:36
OUString maFPath
Definition: palettes.hxx:83
void LoadPaletteHeader()
Definition: Palette.cxx:264
OUString maFPath
Definition: palettes.hxx:35
bool mbValidPalette
Definition: palettes.hxx:57
sal_uInt64 SeekRel(sal_Int64 nPos)
void Clear()
virtual bool IsValid() override
Definition: Palette.cxx:73
virtual const OUString & GetName() override
Definition: Palette.cxx:201
void addEntriesForXColorList(const XColorList &rXColorList, sal_uInt32 nStartIndex=1)
virtual void LoadColorSet(SvxColorValueSet &rColorSet) override
Definition: Palette.cxx:370
bool ReadLine(OString &rStr, sal_Int32 nMaxBytesToRead=0xFFFE)
void Clear()
bool ReadPaletteHeader(SvFileStream &rFileStream)
Definition: Palette.cxx:242
virtual const OUString & GetPath() override
Definition: Palette.cxx:365
float y
PaletteASE(const OUString &rFPath, const OUString &rFName)
Definition: Palette.cxx:33
virtual const OUString & GetPath() override
Definition: Palette.cxx:206
static XColorListRef AsColorList(rtl::Reference< XPropertyList > const &plist)
Definition: xtable.hxx:386
SvStream & ReadUInt32(sal_uInt32 &rUInt32)
virtual const OUString & GetPath() override
Definition: Palette.cxx:68
static OString lcl_getToken(const OString &rStr, sal_Int32 &index)
Definition: Palette.cxx:314
void LoadPalette()
Definition: Palette.cxx:270
virtual ~PaletteASE() override
Definition: Palette.cxx:29
OUString maSOCPaletteName
Definition: palettes.hxx:84
SvStream & ReadFloat(float &rFloat)
bool mbValidPalette
Definition: palettes.hxx:34
ColorList maColors
Definition: palettes.hxx:61
bool mbLoadedPalette
Definition: palettes.hxx:82
virtual const OUString & GetName() override
Definition: Palette.cxx:63
void InsertItem(sal_uInt16 nItemId, const Image &rImage, const OUString &rStr, size_t nPos=VALUESET_APPEND)
virtual ~PaletteSOC() override
Definition: Palette.cxx:356
virtual ~PaletteGPL() override
Definition: Palette.cxx:197
OUString maFName
Definition: palettes.hxx:58
std::size_t ReadBytes(void *pData, std::size_t nSize)
virtual bool IsValid() override
Definition: Palette.cxx:237
OUString maGPLPaletteName
Definition: palettes.hxx:60
virtual void LoadColorSet(SvxColorValueSet &rColorSet) override
Definition: Palette.cxx:41
void SetEndian(SvStreamEndian SvStreamEndian)
PaletteSOC(const OUString &rFPath, const OUString &rFName)
Definition: Palette.cxx:349
const char * name
void addEntriesForXColorList(const XColorList &rXColorList, sal_uInt32 nStartIndex=1)
XColorListRef mpColorList
Definition: palettes.hxx:85
void InsertItem(sal_uInt16 nItemId, const Image &rImage)
OUString maFPath
Definition: palettes.hxx:59
tuple m
ColorList maColors
Definition: palettes.hxx:37
static void lcl_CMYKtoRGB(float fCyan, float fMagenta, float fYellow, float fKey, float &dR, float &dG, float &dB)
Definition: Palette.cxx:80
static XPropertyListRef CreatePropertyListFromURL(XPropertyListType t, const OUString &rUrl)
Definition: xtable.cxx:353
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo