krita

kis_profile.cc

00001 /*
00002  *  kis_profile.cc - part of Krayon
00003  *
00004  *  Copyright (c) 2000 Matthias Elter <elter@kde.org>
00005  *                2001 John Califf
00006  *                2004 Boudewijn Rempt <boud@valdyas.org>
00007  *
00008  *  This program is free software; you can redistribute it and/or modify
00009  *  it under the terms of the GNU General Public License as published by
00010  *  the Free Software Foundation; either version 2 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  This program is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *  GNU General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU General Public License
00019  *  along with this program; if not, write to the Free Software
00020  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00021  */
00022 #include <cfloat>
00023 #include <cmath>
00024 #include <config.h>
00025 #include LCMS_HEADER
00026 
00027 #include <qimage.h>
00028 #include <qtextstream.h>
00029 #include <qfile.h>
00030 
00031 #include <kdebug.h>
00032 
00033 #include "kis_profile.h"
00034 #include "kis_global.h"
00035 
00036 #include "ksharedptr.h"
00037 
00038 #include <X11/Xlib.h>
00039 #include <X11/Xatom.h>
00040 #include <fixx11h.h>
00041 
00042 KisProfile::KisProfile(QByteArray rawData)
00043     : m_rawData(rawData),
00044       m_filename( QString() ),
00045       m_valid( false ),
00046       m_suitableForOutput(false)
00047 {
00048     m_profile = cmsOpenProfileFromMem(rawData.data(), (DWORD)rawData.size());
00049     init();
00050 }
00051 
00052 KisProfile::KisProfile(const QString& file)
00053     : m_filename(file),
00054       m_valid( false ),
00055       m_suitableForOutput( false )
00056 {
00057 }
00058 
00059 KisProfile::KisProfile(const cmsHPROFILE profile)
00060     : m_profile(profile),
00061       m_filename( QString() ),
00062       m_valid( true )
00063 {
00064     size_t  bytesNeeded=0;
00065 
00066     // Make a raw data image ready for saving
00067     _cmsSaveProfileToMem(m_profile, 0, &bytesNeeded); // calc size
00068     if(m_rawData.resize(bytesNeeded))
00069     {
00070         _cmsSaveProfileToMem(m_profile, m_rawData.data(), &bytesNeeded); // fill buffer
00071         cmsHPROFILE newprofile = cmsOpenProfileFromMem(m_rawData.data(), (DWORD) bytesNeeded);
00072         cmsCloseProfile(m_profile);
00073         m_profile = newprofile;
00074     }
00075     else
00076         m_rawData.resize(0);
00077 
00078     init();
00079 }
00080 
00081 KisProfile::~KisProfile()
00082 {
00083     cmsCloseProfile(m_profile);
00084 }
00085 
00086 
00087 bool KisProfile::load()
00088 {
00089     QFile file(m_filename);
00090     file.open(IO_ReadOnly);
00091     m_rawData = file.readAll();
00092     m_profile = cmsOpenProfileFromMem(m_rawData.data(), (DWORD)m_rawData.size());
00093     file.close();
00094 
00095     if (m_profile == 0) {
00096         kdWarning() << "Failed to load profile from " << m_filename << endl;
00097     }
00098 
00099     return init();
00100 
00101 }
00102 
00103 bool KisProfile::init()
00104 {
00105     if (m_profile) {
00106         m_colorSpaceSignature = cmsGetColorSpace(m_profile);
00107         m_deviceClass = cmsGetDeviceClass(m_profile);
00108         m_productName = cmsTakeProductName(m_profile);
00109         m_productDescription = cmsTakeProductDesc(m_profile);
00110         m_productInfo = cmsTakeProductInfo(m_profile);
00111         m_valid = true;
00112 
00113         // Check if the profile can convert (something->this)
00114 //         LPMATSHAPER OutMatShaper = cmsBuildOutputMatrixShaper(m_profile);
00115 //         if( OutMatShaper )
00116 //         {
00117 //             m_suitableForOutput = true;
00118 //         }
00119         cmsCIEXYZTRIPLE Primaries;
00120 
00121         if (cmsTakeColorants(&Primaries, m_profile))
00122         {
00123             m_suitableForOutput = true;
00124         }
00125 
00126 #if 0
00127     // XXX: It wasn't that easy to save a little memory: thsi gives an lcms error
00128         // Okay, we know enough. Free the memory; we'll load it again if needed.
00129 
00130         cmsCloseProfile(m_profile);
00131         m_profile = 0;
00132 
00133 #endif
00134         return true;
00135     }
00136     return false;
00137 }
00138 
00139 cmsHPROFILE KisProfile::profile()
00140 {
00141 #if 0
00142     if (m_profile = 0) {
00143         QFile file(m_filename);
00144         file.open(IO_ReadOnly);
00145         m_rawData = file.readAll();
00146         m_profile = cmsOpenProfileFromMem(m_rawData.data(), (DWORD)m_rawData.size());
00147         file.close();
00148     }
00149 #endif
00150     return m_profile;
00151 }
00152 
00153 bool KisProfile::save()
00154 {
00155     return false;
00156 }
00157 
00158 KisAnnotationSP KisProfile::annotation() const
00159 {
00160     // XXX we hardcode icc, this is correct for lcms?
00161     // XXX productName(), or just "ICC Profile"?
00162     if (!m_rawData.isEmpty())
00163         return new KisAnnotation("icc", productName(), m_rawData);
00164     else
00165         return 0;
00166 }
00167 
00168 KisProfile *  KisProfile::getScreenProfile (int screen)
00169 {
00170 
00171 #ifdef Q_WS_X11
00172 
00173     Atom type;
00174     int format;
00175     unsigned long nitems;
00176     unsigned long bytes_after;
00177     Q_UINT8 * str;
00178 
00179     static Atom icc_atom = XInternAtom( qt_xdisplay(), "_ICC_PROFILE", False );
00180 
00181     if  ( XGetWindowProperty ( qt_xdisplay(),
00182                     qt_xrootwin( screen ),
00183                     icc_atom,
00184                     0,
00185                     INT_MAX,
00186                     False,
00187                     XA_CARDINAL,
00188                     &type,
00189                     &format,
00190                     &nitems,
00191                     &bytes_after,
00192                     (unsigned char **) &str)
00193                 ) {
00194 
00195         QByteArray bytes (nitems);
00196         bytes.assign((char*)str, (Q_UINT32)nitems);
00197 
00198         return new KisProfile(bytes);
00199     } else {
00200         return NULL;
00201     }
00202 #else
00203     return NULL;
00204 
00205 #endif
00206 }
00207 
00208 
KDE Home | KDE Accessibility Home | Description of Access Keys