F:/KPlato/koffice/libs/pigment/KoColorProfile.cpp

Aller à la documentation de ce fichier.
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 library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Library General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2 of the License, or (at your option) any later version.
00012  *
00013  * This library 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 GNU
00016  * Library General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Library General Public License
00019  * along with this library; see the file COPYING.LIB.  If not, write to
00020  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00021  * Boston, MA 02110-1301, USA.
00022 */
00023 #include <cfloat>
00024 #include <cmath>
00025 #include <config.h>
00026 #include <lcms.h>
00027 #include <limits.h>
00028 
00029 #include <QImage>
00030 #include <QTextStream>
00031 #include <QFile>
00032 
00033 #include <kdebug.h>
00034 
00035 #include "KoColorProfile.h"
00036 
00037 
00038 #ifdef Q_WS_X11
00039 #include <X11/Xlib.h>
00040 #include <X11/Xatom.h>
00041 #include <fixx11h.h>
00042 #include <QX11Info>
00043 #endif
00044 
00045 
00046 KoColorProfile::KoColorProfile(QByteArray rawData)
00047     : m_rawData(rawData),
00048       m_filename( QString() ),
00049       m_valid( false ),
00050       m_suitableForOutput(false)
00051 {
00052     m_profile = cmsOpenProfileFromMem(rawData.data(), (DWORD)rawData.size());
00053     init();
00054 }
00055 
00056 KoColorProfile::KoColorProfile(const QString& file)
00057     : m_filename(file),
00058       m_valid( false ),
00059       m_suitableForOutput( false )
00060 {
00061 }
00062 
00063 KoColorProfile::KoColorProfile(const cmsHPROFILE profile)
00064     : m_profile(profile),
00065       m_filename( QString() ),
00066       m_valid( true )
00067 {
00068     size_t  bytesNeeded=0;
00069 
00070     // Make a raw data image ready for saving
00071     _cmsSaveProfileToMem(m_profile, 0, &bytesNeeded); // calc size
00072     m_rawData.resize(bytesNeeded);
00073     if(m_rawData.size() >= (int)bytesNeeded)
00074     {
00075         _cmsSaveProfileToMem(m_profile, m_rawData.data(), &bytesNeeded); // fill buffer
00076         cmsHPROFILE newprofile = cmsOpenProfileFromMem(m_rawData.data(), (DWORD) bytesNeeded);
00077         cmsCloseProfile(m_profile);
00078         m_profile = newprofile;
00079     }
00080     else
00081         m_rawData.resize(0);
00082 
00083     init();
00084 }
00085 
00086 KoColorProfile::~KoColorProfile()
00087 {
00088     cmsCloseProfile(m_profile);
00089 }
00090 
00091 
00092 bool KoColorProfile::load()
00093 {
00094     QFile file(m_filename);
00095     file.open(QIODevice::ReadOnly);
00096     m_rawData = file.readAll();
00097     m_profile = cmsOpenProfileFromMem(m_rawData.data(), (DWORD)m_rawData.size());
00098     file.close();
00099 
00100     if (m_profile == 0) {
00101         kWarning() << "Failed to load profile from " << m_filename << endl;
00102     }
00103 
00104     return init();
00105 
00106 }
00107 
00108 bool KoColorProfile::init()
00109 {
00110     if (m_profile) {
00111         m_colorSpaceSignature = cmsGetColorSpace(m_profile);
00112         m_deviceClass = cmsGetDeviceClass(m_profile);
00113         m_productName = cmsTakeProductName(m_profile);
00114         m_productDescription = cmsTakeProductDesc(m_profile);
00115         m_productInfo = cmsTakeProductInfo(m_profile);
00116         m_valid = true;
00117 
00118         // Check if the profile can convert (something->this)
00119 //         LPMATSHAPER OutMatShaper = cmsBuildOutputMatrixShaper(m_profile);
00120 //         if( OutMatShaper )
00121 //         {
00122 //             m_suitableForOutput = true;
00123 //         }
00124         cmsCIEXYZTRIPLE Primaries;
00125 
00126         if (cmsTakeColorants(&Primaries, m_profile))
00127         {
00128             m_suitableForOutput = true;
00129         }
00130 
00131 #if 0
00132     // XXX: It wasn't that easy to save a little memory: thsi gives an lcms error
00133         // Okay, we know enough. Free the memory; we'll load it again if needed.
00134 
00135         cmsCloseProfile(m_profile);
00136         m_profile = 0;
00137 
00138 #endif
00139         return true;
00140     }
00141     return false;
00142 }
00143 
00144 cmsHPROFILE KoColorProfile::profile()
00145 {
00146 #if 0
00147         if (m_profile = 0) {
00148             QFile file(m_filename);
00149             file.open(QIODevice::ReadOnly);
00150             m_rawData = file.readAll();
00151             m_profile = cmsOpenProfileFromMem(m_rawData.data(), (DWORD)m_rawData.size());
00152         file.close();
00153         }
00154 #endif
00155         return m_profile;
00156 }
00157 
00158 bool KoColorProfile::save()
00159 {
00160     return false;
00161 }
00162 
00163 
00164 KoColorProfile *  KoColorProfile::getScreenProfile (int screen)
00165 {
00166 
00167 #ifdef Q_WS_X11
00168 
00169     Atom type;
00170     int format;
00171     unsigned long nitems;
00172     unsigned long bytes_after;
00173     quint8 * str;
00174 
00175     static Atom icc_atom = XInternAtom( QX11Info::display(), "_ICC_PROFILE", False );
00176 
00177     if  ( XGetWindowProperty ( QX11Info::display(),
00178                     QX11Info::appRootWindow( screen ),
00179                     icc_atom,
00180                     0,
00181                     INT_MAX,
00182                     False,
00183                     XA_CARDINAL,
00184                     &type,
00185                     &format,
00186                     &nitems,
00187                     &bytes_after,
00188                     (unsigned char **) &str)
00189                 ) {
00190 
00191         QByteArray bytes (nitems, '\0');
00192         bytes = QByteArray::fromRawData((char*)str, (quint32)nitems);
00193 
00194         return new KoColorProfile(bytes);
00195     } else {
00196         return NULL;
00197     }
00198 #else
00199     return NULL;
00200 
00201 #endif
00202 }
00203 
00204 

Généré le Wed Nov 22 23:41:13 2006 pour KPlato par  doxygen 1.5.1-p1