00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00071 _cmsSaveProfileToMem(m_profile, 0, &bytesNeeded);
00072 m_rawData.resize(bytesNeeded);
00073 if(m_rawData.size() >= (int)bytesNeeded)
00074 {
00075 _cmsSaveProfileToMem(m_profile, m_rawData.data(), &bytesNeeded);
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
00119
00120
00121
00122
00123
00124 cmsCIEXYZTRIPLE Primaries;
00125
00126 if (cmsTakeColorants(&Primaries, m_profile))
00127 {
00128 m_suitableForOutput = true;
00129 }
00130
00131 #if 0
00132
00133
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