chalk

kis_palette.cpp
1/*
2 * Copyright (c) 2005 Boudewijn Rempt <boud@valdyas.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22
23#ifdef HAVE_SYS_TYPES_H
24#include <sys/types.h>
25#endif
26
27#include <netinet/in.h>
28#include <limits.h>
29#include <stdlib.h>
30#include <cfloat>
31
32#include <tqimage.h>
33#include <tqpoint.h>
34#include <tqvaluevector.h>
35#include <tqfile.h>
36#include <tqtextstream.h>
37
38#include <kdebug.h>
39#include <tdelocale.h>
40
41#include "kis_debug_areas.h"
42#include "kis_palette.h"
43#include "kis_iterators_pixel.h"
44
45
46namespace {
47 enum enumPaletteType {
48 FORMAT_UNKNOWN,
49 FORMAT_GPL, // Gimp palette
50 FORMAT_PAL, // RIFF palette
51 FORMAT_ACT // Photoshop binary color palette
52 };
53
54}
55
56
57KisPalette::KisPalette(const TQImage * img, TQ_INT32 nColors, const TQString & name)
58 : super(TQString("")),
59 m_name(name)
60{
61 Q_ASSERT(nColors > 0);
62 Q_ASSERT(!img->isNull());
63
64 // XXX: Implement
65
66 m_columns = 0; // Set the default value that the GIMP uses...
67}
68
69KisPalette::KisPalette(const KisPaintDeviceSP device, TQ_INT32 nColors, const TQString & name)
70 : super(TQString("")),
71 m_name(name)
72{
73 Q_ASSERT(nColors > 0);
74 Q_ASSERT(device != 0);
75
76
77 // XXX: Implement
78 m_columns = 0; // Set the default value that the GIMP uses...
79}
80
81
82KisPalette::KisPalette(const KisGradient * gradient, TQ_INT32 nColors, const TQString & name)
83 : super(TQString("")),
84 m_name(name)
85{
86 Q_ASSERT(nColors > 0);
87 Q_ASSERT(gradient != 0);
88
89 double dx, cur_x;
90 TQColor c;
91 TQ_INT32 i;
92 TQ_UINT8 opacity;
93 dx = 1.0 / (nColors - 1);
94
95 KisPaletteEntry e;
96 for (i = 0, cur_x = 0; i < nColors; i++, cur_x += dx) {
97 gradient->colorAt(cur_x, &e.color, &opacity);
98 e.name = "Untitled";
99 add(e);
100 }
101
102 m_columns = 0; // Set the default value that the GIMP uses...
103}
104
105KisPalette::KisPalette(const TQString& filename)
106 : super(filename)
107{
108 // Implemented in super class
109 m_columns = 0; // Set the default value that the GIMP uses...
110}
111
113 : super("")
114{
115 m_columns = 0; // Set the default value that the GIMP uses...
116}
117
120 : super("")
121{
122 setFilename(rhs.filename());
123 m_ownData = false;
124 m_img = rhs.m_img;
125 m_name = rhs.m_name;
126 m_comment = rhs.m_comment;
127 m_columns = rhs.m_columns;
128 m_colors = rhs.m_colors;
129 setValid(true);
130}
131
132KisPalette::~KisPalette()
133{
134}
135
137{
138 TQFile file(filename());
139 file.open(IO_ReadOnly);
140 m_data = file.readAll();
141 file.close();
142 return init();
143}
144
145
147{
148 TQFile file(filename());
149 if (!file.open(IO_WriteOnly | IO_Truncate)) {
150 return false;
151 }
152
153 TQTextStream stream(&file);
154 // Header: Magic\nName: <name>\nColumns: <no idea what this means, but default = 0>
155 // In any case, we don't use Columns...
156 stream << "GIMP Palette\nName: " << name() << "\nColumns: " << m_columns << "\n#\n";
157
158 for (uint i = 0; i < m_colors.size(); i++) {
159 const KisPaletteEntry& entry = m_colors.at(i);
160 TQColor c = entry.color;
161 stream << c.red() << " " << c.green() << " " << c.blue() << "\t";
162 if (entry.name.isEmpty())
163 stream << "Untitled\n";
164 else
165 stream << entry.name << "\n";
166 }
167
168 file.close();
169 return true;
170}
171
173{
174 return m_img;
175}
176
177TQ_INT32 KisPalette::nColors()
178{
179 return m_colors.count();
180}
181
182bool KisPalette::init()
183{
184 enumPaletteType format = FORMAT_UNKNOWN;
185
186 TQString s = TQString::fromUtf8(m_data.data(), m_data.count());
187
188 if (s.isEmpty() || s.isNull() || s.length() < 50) {
189 kdWarning(DBG_AREA_FILE) << "Illegal Gimp palette file: " << filename() << "\n";
190 return false;
191 }
192
193
194 if (s.startsWith("RIFF") || s.startsWith("PAL data"))
195 {
196 format = FORMAT_PAL;
197 }
198 else if (s.startsWith("GIMP Palette"))
199 {
200 // XXX: No checks for wrong input yet!
201 TQ_UINT32 index = 0;
202
203 TQStringList lines = TQStringList::split("\n", s);
204
205 if (lines.size() < 3) {
206 return false;
207 }
208
209 TQString entry, channel, columns;
210 TQStringList c;
211 TQ_INT32 r, g, b;
212 TQColor color;
213 KisPaletteEntry e;
214
215 format = FORMAT_GPL;
216
217 // Read name
218 if (!lines[1].startsWith("Name: ") || !lines[0].startsWith("GIMP") )
219 {
220 kdWarning(DBG_AREA_FILE) << "Illegal Gimp palette file: " << filename() << "\n";
221 return false;
222 }
223
224 setName(i18n(lines[1].mid(strlen("Name: ")).stripWhiteSpace().ascii()));
225
226 index = 2;
227
228 // Read columns
229 if (lines[index].startsWith("Columns: ")) {
230 columns = lines[index].mid(strlen("Columns: ")).stripWhiteSpace();;
231 m_columns = columns.toInt();
232 index = 3;
233 }
234
235 for (TQ_UINT32 i = index; i < lines.size(); i++) {
236 if (lines[i].startsWith("#")) {
237 m_comment += lines[i].mid(1).stripWhiteSpace() + " ";
238 }
239 else if (!lines[i].isEmpty())
240 {
241 TQStringList a = TQStringList::split(" ", lines[i].replace(TQChar('\t'), " "));
242
243 if (a.count() < 3)
244 {
245 break;
246 }
247
248 r = a[0].toInt();
249 a.pop_front();
250 g = a[0].toInt();
251 a.pop_front();
252 b = a[0].toInt();
253 a.pop_front();
254
255 if (r < 0 || r > 255 ||
256 g < 0 || g > 255 ||
257 b < 0 || b > 255)
258 {
259 break;
260 }
261
262 color = TQColor(r, g, b);
263 e.color = color;
264
265 TQString name = a.join(" ");
266 e.name = name.isEmpty() ? i18n("Untitled") : name;
267
268 add(e);
269 }
270 }
271 setValid(true);
272 return true;
273 }
274 else if (s.length() == 768) {
275 kdWarning(DBG_AREA_FILE) << "Photoshop format palette file. Not implemented yet\n";
276 format = FORMAT_ACT;
277 }
278 return false;
279}
280
281
282void KisPalette::add(const KisPaletteEntry & c)
283{
284 m_colors.push_back(c);
285}
286
287void KisPalette::remove(const KisPaletteEntry & c)
288{
289 TQValueVector<KisPaletteEntry>::iterator it = m_colors.begin();
290 TQValueVector<KisPaletteEntry>::iterator end = m_colors.end();
291
292 while (it != end) {
293 if ((*it) == c) {
294 m_colors.erase(it);
295 return;
296 }
297 ++it;
298 }
299}
300
301KisPaletteEntry KisPalette::getColor(TQ_UINT32 index)
302{
303 return m_colors[index];
304}
305
306#include "kis_palette.moc"
virtual bool load()
Load this resource.
KisPalette(const TQImage *img, TQ_INT32 nColors, const TQString &name)
Create a palette from the colours in an image.
KisPalette()
Create an empty palette.
virtual TQImage img()
Returns a TQImage representing this resource.
virtual bool save()
Save this resource asynchronously.