krita
kis_palette.cc00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifdef HAVE_CONFIG_H
00020 #include <config.h>
00021 #endif
00022
00023 #ifdef HAVE_SYS_TYPES_H
00024 #include <sys/types.h>
00025 #endif
00026
00027 #include <netinet/in.h>
00028 #include <limits.h>
00029 #include <stdlib.h>
00030 #include <cfloat>
00031
00032 #include <qimage.h>
00033 #include <qpoint.h>
00034 #include <qvaluevector.h>
00035 #include <qfile.h>
00036 #include <qtextstream.h>
00037
00038 #include <kdebug.h>
00039 #include <klocale.h>
00040
00041 #include "kis_debug_areas.h"
00042 #include "kis_palette.h"
00043 #include "kis_iterators_pixel.h"
00044
00045
00046 namespace {
00047 enum enumPaletteType {
00048 FORMAT_UNKNOWN,
00049 FORMAT_GPL,
00050 FORMAT_PAL,
00051 FORMAT_ACT
00052 };
00053
00054 }
00055
00056
00057 KisPalette::KisPalette(const QImage * img, Q_INT32 nColors, const QString & name)
00058 : super(QString("")),
00059 m_name(name)
00060 {
00061 Q_ASSERT(nColors > 0);
00062 Q_ASSERT(!img->isNull());
00063
00064
00065
00066 m_columns = 0;
00067 }
00068
00069 KisPalette::KisPalette(const KisPaintDeviceSP device, Q_INT32 nColors, const QString & name)
00070 : super(QString("")),
00071 m_name(name)
00072 {
00073 Q_ASSERT(nColors > 0);
00074 Q_ASSERT(device != 0);
00075
00076
00077
00078 m_columns = 0;
00079 }
00080
00081
00082 KisPalette::KisPalette(const KisGradient * gradient, Q_INT32 nColors, const QString & name)
00083 : super(QString("")),
00084 m_name(name)
00085 {
00086 Q_ASSERT(nColors > 0);
00087 Q_ASSERT(gradient != 0);
00088
00089 double dx, cur_x;
00090 QColor c;
00091 Q_INT32 i;
00092 Q_UINT8 opacity;
00093 dx = 1.0 / (nColors - 1);
00094
00095 KisPaletteEntry e;
00096 for (i = 0, cur_x = 0; i < nColors; i++, cur_x += dx) {
00097 gradient->colorAt(cur_x, &e.color, &opacity);
00098 e.name = "Untitled";
00099 add(e);
00100 }
00101
00102 m_columns = 0;
00103 }
00104
00105 KisPalette::KisPalette(const QString& filename)
00106 : super(filename)
00107 {
00108
00109 m_columns = 0;
00110 }
00111
00112 KisPalette::KisPalette()
00113 : super("")
00114 {
00115 m_columns = 0;
00116 }
00117
00119 KisPalette::KisPalette(const KisPalette& rhs)
00120 : super("")
00121 {
00122 setFilename(rhs.filename());
00123 m_ownData = false;
00124 m_img = rhs.m_img;
00125 m_name = rhs.m_name;
00126 m_comment = rhs.m_comment;
00127 m_columns = rhs.m_columns;
00128 m_colors = rhs.m_colors;
00129 setValid(true);
00130 }
00131
00132 KisPalette::~KisPalette()
00133 {
00134 }
00135
00136 bool KisPalette::load()
00137 {
00138 QFile file(filename());
00139 file.open(IO_ReadOnly);
00140 m_data = file.readAll();
00141 file.close();
00142 return init();
00143 }
00144
00145
00146 bool KisPalette::save()
00147 {
00148 QFile file(filename());
00149 if (!file.open(IO_WriteOnly | IO_Truncate)) {
00150 return false;
00151 }
00152
00153 QTextStream stream(&file);
00154
00155
00156 stream << "GIMP Palette\nName: " << name() << "\nColumns: " << m_columns << "\n#\n";
00157
00158 for (uint i = 0; i < m_colors.size(); i++) {
00159 const KisPaletteEntry& entry = m_colors.at(i);
00160 QColor c = entry.color;
00161 stream << c.red() << " " << c.green() << " " << c.blue() << "\t";
00162 if (entry.name.isEmpty())
00163 stream << "Untitled\n";
00164 else
00165 stream << entry.name << "\n";
00166 }
00167
00168 file.close();
00169 return true;
00170 }
00171
00172 QImage KisPalette::img()
00173 {
00174 return m_img;
00175 }
00176
00177 Q_INT32 KisPalette::nColors()
00178 {
00179 return m_colors.count();
00180 }
00181
00182 bool KisPalette::init()
00183 {
00184 enumPaletteType format = FORMAT_UNKNOWN;
00185
00186 QString s = QString::fromUtf8(m_data.data(), m_data.count());
00187
00188 if (s.isEmpty() || s.isNull() || s.length() < 50) {
00189 kdWarning(DBG_AREA_FILE) << "Illegal Gimp palette file: " << filename() << "\n";
00190 return false;
00191 }
00192
00193
00194 if (s.startsWith("RIFF") || s.startsWith("PAL data"))
00195 {
00196 format = FORMAT_PAL;
00197 }
00198 else if (s.startsWith("GIMP Palette"))
00199 {
00200
00201 Q_UINT32 index = 0;
00202
00203 QStringList lines = QStringList::split("\n", s);
00204
00205 if (lines.size() < 3) {
00206 return false;
00207 }
00208
00209 QString entry, channel, columns;
00210 QStringList c;
00211 Q_INT32 r, g, b;
00212 QColor color;
00213 KisPaletteEntry e;
00214
00215 format = FORMAT_GPL;
00216
00217
00218 if (!lines[1].startsWith("Name: ") || !lines[0].startsWith("GIMP") )
00219 {
00220 kdWarning(DBG_AREA_FILE) << "Illegal Gimp palette file: " << filename() << "\n";
00221 return false;
00222 }
00223
00224 setName(i18n(lines[1].mid(strlen("Name: ")).stripWhiteSpace().ascii()));
00225
00226 index = 2;
00227
00228
00229 if (lines[index].startsWith("Columns: ")) {
00230 columns = lines[index].mid(strlen("Columns: ")).stripWhiteSpace();;
00231 m_columns = columns.toInt();
00232 index = 3;
00233 }
00234
00235 for (Q_UINT32 i = index; i < lines.size(); i++) {
00236 if (lines[i].startsWith("#")) {
00237 m_comment += lines[i].mid(1).stripWhiteSpace() + " ";
00238 }
00239 else if (!lines[i].isEmpty())
00240 {
00241 QStringList a = QStringList::split(" ", lines[i].replace(QChar('\t'), " "));
00242
00243 if (a.count() < 3)
00244 {
00245 break;
00246 }
00247
00248 r = a[0].toInt();
00249 a.pop_front();
00250 g = a[0].toInt();
00251 a.pop_front();
00252 b = a[0].toInt();
00253 a.pop_front();
00254
00255 if (r < 0 || r > 255 ||
00256 g < 0 || g > 255 ||
00257 b < 0 || b > 255)
00258 {
00259 break;
00260 }
00261
00262 color = QColor(r, g, b);
00263 e.color = color;
00264
00265 QString name = a.join(" ");
00266 e.name = name.isEmpty() ? i18n("Untitled") : name;
00267
00268 add(e);
00269 }
00270 }
00271 setValid(true);
00272 return true;
00273 }
00274 else if (s.length() == 768) {
00275 kdWarning(DBG_AREA_FILE) << "Photoshop format palette file. Not implemented yet\n";
00276 format = FORMAT_ACT;
00277 }
00278 return false;
00279 }
00280
00281
00282 void KisPalette::add(const KisPaletteEntry & c)
00283 {
00284 m_colors.push_back(c);
00285 }
00286
00287 void KisPalette::remove(const KisPaletteEntry & c)
00288 {
00289 QValueVector<KisPaletteEntry>::iterator it = m_colors.begin();
00290 QValueVector<KisPaletteEntry>::iterator end = m_colors.end();
00291
00292 while (it != end) {
00293 if ((*it) == c) {
00294 m_colors.erase(it);
00295 return;
00296 }
00297 ++it;
00298 }
00299 }
00300
00301 KisPaletteEntry KisPalette::getColor(Q_UINT32 index)
00302 {
00303 return m_colors[index];
00304 }
00305
00306 #include "kis_palette.moc"
|