1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <svx/Palette.hxx>
#include <tools/stream.hxx>
#include "palettes.hxx"
Palette::~Palette()
{
}
PaletteASE::~PaletteASE()
{
}
PaletteASE::PaletteASE( const OUString &rFPath, const OUString &rFName ) :
mbValidPalette( false ),
maFPath ( rFPath ),
maASEPaletteName ( rFName )
{
LoadPalette();
}
void PaletteASE::LoadColorSet( SvxColorValueSet& rColorSet )
{
rColorSet.Clear();
int nIx = 1;
for (ColorList::const_iterator it = maColors.begin(); it != maColors.end(); ++it)
{
rColorSet.InsertItem(nIx, it->first, it->second);
++nIx;
}
}
const OUString& PaletteASE::GetName()
{
return maASEPaletteName;
}
const OUString& PaletteASE::GetPath()
{
return maFPath;
}
bool PaletteASE::IsValid()
{
return mbValidPalette;
}
// CMYK values from 0 to 1
// TODO: Deduplicate me (taken from core/cui/source/dialogs/colorpicker.cxx)
static void lcl_CMYKtoRGB( float fCyan, float fMagenta, float fYellow, float fKey, float& dR, float& dG, float& dB )
{
fCyan = (fCyan * ( 1.0 - fKey )) + fKey;
fMagenta = (fMagenta * ( 1.0 - fKey )) + fKey;
fYellow = (fYellow * ( 1.0 - fKey )) + fKey;
dR = std::max( std::min( ( 1.0 - fCyan ), 1.0), 0.0 );
dG = std::max( std::min( ( 1.0 - fMagenta ), 1.0), 0.0 );
dB = std::max( std::min( ( 1.0 - fYellow ), 1.0), 0.0 );
}
void PaletteASE::LoadPalette()
{
SvFileStream aFile(maFPath, StreamMode::READ);
aFile.SetEndian(SvStreamEndian::BIG);
// Verify magic first 4 characters
sal_Char cMagic[5] = {0};
if ((aFile.ReadBytes(cMagic, 4) != 4) || (strncmp(cMagic, "ASEF", 4) != 0))
{
mbValidPalette = false;
return;
}
// Ignore the version number
aFile.SeekRel(4);
sal_uInt32 nBlocks = 0;
aFile.ReadUInt32(nBlocks);
for (sal_uInt32 nI = 0; nI < nBlocks; nI++) {
sal_uInt32 nChunkType = 0;
aFile.ReadUInt32(nChunkType);
// End chunk
if (nChunkType == 0)
break;
// Grab chunk size, name length
sal_uInt16 nChunkSize = 0;
sal_uInt16 nChars = 0;
aFile.ReadUInt16(nChunkSize);
aFile.ReadUInt16(nChars);
OUString aPaletteName("");
if (nChars > 1)
aPaletteName = read_uInt16s_ToOUString(aFile, nChars);
else
aFile.SeekRel(2);
if (nChunkType == 0xC0010000)
{
// Got a start chunk, so set palette name
maASEPaletteName = aPaletteName;
// Is there color data? (shouldn't happen in a start block, but check anyway)
if (nChunkSize > ((nChars * 2) + 2))
aPaletteName.clear();
else
continue;
}
sal_Char cColorModel[5] = {0};
aFile.ReadBytes(cColorModel, 4);
OString aColorModel(cColorModel);
// r, g, and b are floats ranging from 0 to 1
float r = 0, g = 0, b = 0;
if (aColorModel.equalsIgnoreAsciiCase("cmyk"))
{
float c = 0, m = 0, y = 0, k = 0;
aFile.ReadFloat(c);
aFile.ReadFloat(m);
aFile.ReadFloat(y);
aFile.ReadFloat(k);
lcl_CMYKtoRGB(c, m, y, k, r, g, b);
}
else if (aColorModel.equalsIgnoreAsciiCase("rgb "))
{
aFile.ReadFloat(r);
aFile.ReadFloat(g);
aFile.ReadFloat(b);
}
else if (aColorModel.equalsIgnoreAsciiCase("gray"))
{
float nVal = 0;
aFile.ReadFloat(nVal);
r = g = b = nVal;
}
else
{
float nL = 0, nA = 0, nB = 0;
aFile.ReadFloat(nL);
aFile.ReadFloat(nA);
aFile.ReadFloat(nB);
// TODO: How to convert LAB to RGB?
r = g = b = 0;
}
// Ignore color type
aFile.SeekRel(2);
maColors.emplace_back(Color(r * 255, g * 255, b * 255), aPaletteName);
}
mbValidPalette = true;
}
// PaletteGPL ------------------------------------------------------------------
static OString lcl_getToken(const OString& rStr, sal_Int32& index);
PaletteGPL::PaletteGPL( const OUString &rFPath, const OUString &rFName ) :
mbLoadedPalette( false ),
mbValidPalette( false ),
maFName( rFName ),
maFPath( rFPath )
{
LoadPaletteHeader();
}
PaletteGPL::~PaletteGPL()
{
}
const OUString& PaletteGPL::GetName()
{
return maGPLPaletteName;
}
const OUString& PaletteGPL::GetPath()
{
return maFPath;
}
void PaletteGPL::LoadColorSet( SvxColorValueSet& rColorSet )
{
LoadPalette();
rColorSet.Clear();
int nIx = 1;
for (ColorList::const_iterator it = maColors.begin(); it != maColors.end(); ++it)
{
rColorSet.InsertItem(nIx, it->first, it->second);
++nIx;
}
}
bool PaletteGPL::IsValid()
{
return mbValidPalette;
}
bool PaletteGPL::ReadPaletteHeader(SvFileStream& rFileStream)
{
OString aLine;
OString aPaletteName;
rFileStream.ReadLine(aLine);
if( !aLine.startsWith("GIMP Palette") ) return false;
rFileStream.ReadLine(aLine);
if( aLine.startsWith("Name: ", &aPaletteName) )
{
maGPLPaletteName = OStringToOUString(aPaletteName, RTL_TEXTENCODING_ASCII_US);
rFileStream.ReadLine(aLine);
if( aLine.startsWith("Columns: "))
rFileStream.ReadLine(aLine); // we can ignore this
}
else
{
maGPLPaletteName = maFName;
}
return true;
}
void PaletteGPL::LoadPaletteHeader()
{
SvFileStream aFile(maFPath, StreamMode::READ);
mbValidPalette = ReadPaletteHeader( aFile );
}
void PaletteGPL::LoadPalette()
{
if( mbLoadedPalette ) return;
mbLoadedPalette = true;
// TODO add error handling!!!
SvFileStream aFile(maFPath, StreamMode::READ);
mbValidPalette = ReadPaletteHeader( aFile );
if( !mbValidPalette ) return;
OString aLine;
do {
if (aLine[0] != '#' && aLine[0] != '\n')
{
// TODO check if r,g,b are 0<= x <=255, or just clamp?
sal_Int32 nIndex = 0;
OString token;
token = lcl_getToken(aLine, nIndex);
if(token.isEmpty() || nIndex == -1) continue;
sal_Int32 r = token.toInt32();
token = lcl_getToken(aLine, nIndex);
if(token.isEmpty() || nIndex == -1) continue;
sal_Int32 g = token.toInt32();
token = lcl_getToken(aLine, nIndex);
if(token.isEmpty()) continue;
sal_Int32 b = token.toInt32();
OString name;
if(nIndex != -1)
name = aLine.copy(nIndex);
maColors.emplace_back(
Color(r, g, b),
OStringToOUString(name, RTL_TEXTENCODING_ASCII_US));
}
} while (aFile.ReadLine(aLine));
}
// finds first token in rStr from index, separated by whitespace
// returns position of next token in index
static OString lcl_getToken(const OString& rStr, sal_Int32& index)
{
sal_Int32 substart, toklen = 0;
OUString aWhitespaceChars( " \n\t" );
while(index < rStr.getLength() &&
aWhitespaceChars.indexOf( rStr[index] ) != -1)
++index;
if(index == rStr.getLength())
{
index = -1;
return OString();
}
substart = index;
//counts length of token
while(index < rStr.getLength() &&
aWhitespaceChars.indexOf( rStr[index] ) == -1 )
{
++index;
++toklen;
}
//counts to position of next token
while(index < rStr.getLength() &&
aWhitespaceChars.indexOf( rStr[index] ) != -1 )
++index;
if(index == rStr.getLength())
index = -1;
return rStr.copy(substart, toklen);
}
// PaletteSOC ------------------------------------------------------------------
PaletteSOC::PaletteSOC( const OUString &rFPath, const OUString &rFName ) :
mbLoadedPalette( false ),
maFPath( rFPath ),
maSOCPaletteName( rFName )
{
}
PaletteSOC::~PaletteSOC()
{
}
const OUString& PaletteSOC::GetName()
{
return maSOCPaletteName;
}
const OUString& PaletteSOC::GetPath()
{
return maFPath;
}
void PaletteSOC::LoadColorSet( SvxColorValueSet& rColorSet )
{
if( !mbLoadedPalette )
{
mbLoadedPalette = true;
mpColorList = XPropertyList::AsColorList(XPropertyList::CreatePropertyListFromURL(XPropertyListType::Color, maFPath));
(void)mpColorList->Load();
}
rColorSet.Clear();
if( mpColorList.is() )
rColorSet.addEntriesForXColorList( *mpColorList );
}
bool PaletteSOC::IsValid()
{
return true;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|