krita

kis_palette.cc

00001 /*
00002  *  Copyright (c) 2005 Boudewijn Rempt <boud@valdyas.org>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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, // Gimp palette
00050         FORMAT_PAL, // RIFF palette
00051         FORMAT_ACT // Photoshop binary color palette
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     // XXX: Implement
00065 
00066     m_columns = 0; // Set the default value that the GIMP uses...
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     // XXX: Implement
00078     m_columns = 0; // Set the default value that the GIMP uses...
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; // Set the default value that the GIMP uses...
00103 }
00104 
00105 KisPalette::KisPalette(const QString& filename)
00106     : super(filename)
00107 {
00108     // Implemented in super class
00109     m_columns = 0; // Set the default value that the GIMP uses...
00110 }
00111 
00112 KisPalette::KisPalette()
00113     : super("")
00114 {
00115     m_columns = 0; // Set the default value that the GIMP uses...
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     // Header: Magic\nName: <name>\nColumns: <no idea what this means, but default = 0>
00155     // In any case, we don't use Columns...
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         // XXX: No checks for wrong input yet!
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         // Read name
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         // Read columns
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         // Loop over the rest of the lines
00236         for (Q_UINT32 i = index; i < lines.size(); i++) {
00237             if (lines[i].startsWith("#")) {
00238                 m_comment += lines[i].mid(1).stripWhiteSpace() + " ";
00239             }
00240             else {
00241                 if (lines[i].contains("\t") > 0) {
00242                     QStringList a = QStringList::split("\t", lines[i]);
00243                     e.name = a[1];
00244 
00245                     QStringList c = QStringList::split(" ", a[0]);
00246                     channel = c[0].stripWhiteSpace();
00247                     r = channel.toInt();
00248                     channel = c[1].stripWhiteSpace();
00249                     g = channel.toInt();
00250                     channel = c[2].stripWhiteSpace();
00251                     b = channel.toInt();
00252                     color = QColor(r, g, b);
00253                     e.color = color;
00254 
00255                     add(e);
00256                 }
00257             }
00258         }
00259 
00260         setValid(true);
00261         return true;
00262     }
00263     else if (s.length() == 768) {
00264         kdWarning(DBG_AREA_FILE) << "Photoshop format palette file. Not implemented yet\n";
00265         format = FORMAT_ACT;
00266     }
00267     return false;
00268 }
00269 
00270 
00271 void KisPalette::add(const KisPaletteEntry & c)
00272 {
00273     m_colors.push_back(c);
00274 }
00275 
00276 void KisPalette::remove(const KisPaletteEntry & c)
00277 {
00278     QValueVector<KisPaletteEntry>::iterator it = m_colors.begin();
00279     QValueVector<KisPaletteEntry>::iterator end = m_colors.end();
00280 
00281     while (it != end) {
00282         if ((*it) == c) {
00283             m_colors.erase(it);
00284             return;
00285         }
00286         ++it;
00287     }
00288 }
00289 
00290 KisPaletteEntry KisPalette::getColor(Q_UINT32 index)
00291 {
00292     return m_colors[index];
00293 }
00294 
00295 #include "kis_palette.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys