chalk

kis_profile.cpp
1/*
2 * kis_profile.cpp - part of Krayon
3 *
4 * Copyright (c) 2000 Matthias Elter <elter@kde.org>
5 * 2001 John Califf
6 * 2004 Boudewijn Rempt <boud@valdyas.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22#include <cfloat>
23#include <cmath>
24#include <config.h>
25#include LCMS_HEADER
26
27#include <tqimage.h>
28#include <tqtextstream.h>
29#include <tqfile.h>
30
31#include <kdebug.h>
32
33#include "kis_profile.h"
34#include "kis_global.h"
35
36#include "ksharedptr.h"
37
38#include <X11/Xlib.h>
39#include <X11/Xatom.h>
40#include <fixx11h.h>
41
42KisProfile::KisProfile(TQByteArray rawData)
43 : m_rawData(rawData),
44 m_filename( TQString() ),
45 m_valid( false ),
46 m_suitableForOutput(false)
47{
48 m_profile = cmsOpenProfileFromMem(rawData.data(), (DWORD)rawData.size());
49 init();
50}
51
52KisProfile::KisProfile(const TQString& file)
53 : m_filename(file),
54 m_valid( false ),
55 m_suitableForOutput( false )
56{
57}
58
59KisProfile::KisProfile(const cmsHPROFILE profile)
60 : m_profile(profile),
61 m_filename( TQString() ),
62 m_valid( true )
63{
64 size_t bytesNeeded=0;
65
66 // Make a raw data image ready for saving
67 _cmsSaveProfileToMem(m_profile, 0, &bytesNeeded); // calc size
68 if(m_rawData.resize(bytesNeeded))
69 {
70 _cmsSaveProfileToMem(m_profile, m_rawData.data(), &bytesNeeded); // fill buffer
71 cmsHPROFILE newprofile = cmsOpenProfileFromMem(m_rawData.data(), (DWORD) bytesNeeded);
72 cmsCloseProfile(m_profile);
73 m_profile = newprofile;
74 }
75 else
76 m_rawData.resize(0);
77
78 init();
79}
80
81KisProfile::~KisProfile()
82{
83 cmsCloseProfile(m_profile);
84}
85
86
87bool KisProfile::load()
88{
89 TQFile file(m_filename);
90 file.open(IO_ReadOnly);
91 m_rawData = file.readAll();
92 m_profile = cmsOpenProfileFromMem(m_rawData.data(), (DWORD)m_rawData.size());
93 file.close();
94
95 if (m_profile == 0) {
96 kdWarning() << "Failed to load profile from " << m_filename << endl;
97 }
98
99 return init();
100
101}
102
103bool KisProfile::init()
104{
105 if (m_profile) {
106 m_colorSpaceSignature = cmsGetColorSpace(m_profile);
107 m_deviceClass = cmsGetDeviceClass(m_profile);
108 m_productName = cmsTakeProductName(m_profile);
109 m_productDescription = cmsTakeProductDesc(m_profile);
110 m_productInfo = cmsTakeProductInfo(m_profile);
111 m_valid = true;
112
113 // Check if the profile can convert (something->this)
114// LPMATSHAPER OutMatShaper = cmsBuildOutputMatrixShaper(m_profile);
115// if( OutMatShaper )
116// {
117// m_suitableForOutput = true;
118// }
119 cmsCIEXYZTRIPLE Primaries;
120
121 if (cmsTakeColorants(&Primaries, m_profile))
122 {
123 m_suitableForOutput = true;
124 }
125
126#if 0
127 // XXX: It wasn't that easy to save a little memory: thsi gives an lcms error
128 // Okay, we know enough. Free the memory; we'll load it again if needed.
129
130 cmsCloseProfile(m_profile);
131 m_profile = 0;
132
133#endif
134 return true;
135 }
136 return false;
137}
138
139cmsHPROFILE KisProfile::profile()
140{
141#if 0
142 if (m_profile = 0) {
143 TQFile file(m_filename);
144 file.open(IO_ReadOnly);
145 m_rawData = file.readAll();
146 m_profile = cmsOpenProfileFromMem(m_rawData.data(), (DWORD)m_rawData.size());
147 file.close();
148 }
149#endif
150 return m_profile;
151}
152
153bool KisProfile::save()
154{
155 return false;
156}
157
158KisAnnotationSP KisProfile::annotation() const
159{
160 // XXX we hardcode icc, this is correct for lcms?
161 // XXX productName(), or just "ICC Profile"?
162 if (!m_rawData.isEmpty())
163 return new KisAnnotation("icc", productName(), m_rawData);
164 else
165 return 0;
166}
167
168KisProfile * KisProfile::getScreenProfile (int screen)
169{
170
171#ifdef TQ_WS_X11
172
173 Atom type;
174 int format;
175 unsigned long nitems;
176 unsigned long bytes_after;
177 TQ_UINT8 * str;
178
179 static Atom icc_atom = XInternAtom( tqt_xdisplay(), "_ICC_PROFILE", False );
180
181 if ( XGetWindowProperty ( tqt_xdisplay(),
182 tqt_xrootwin( screen ),
183 icc_atom,
184 0,
185 INT_MAX,
186 False,
187 XA_CARDINAL,
188 &type,
189 &format,
190 &nitems,
191 &bytes_after,
192 (unsigned char **) &str)
193 ) {
194
195 TQByteArray bytes (nitems);
196 bytes.assign((char*)str, (TQ_UINT32)nitems);
197
198 return new KisProfile(bytes);
199 } else {
200 return NULL;
201 }
202#else
203 return NULL;
204
205#endif
206}
207
208