Code

Initial support of color-profile on <image>
[inkscape.git] / src / color-profile.cpp
3 #include "xml/repr.h"
4 #include "color-profile.h"
5 #include "color-profile-fns.h"
6 #include "attributes.h"
7 #include "document.h"
9 using Inkscape::ColorProfile;
10 using Inkscape::ColorProfileClass;
12 static void colorprofile_class_init( ColorProfileClass *klass );
13 static void colorprofile_init( ColorProfile *cprof );
15 static void colorprofile_release( SPObject *object );
16 static void colorprofile_build( SPObject *object, SPDocument *document, Inkscape::XML::Node *repr );
17 static void colorprofile_set( SPObject *object, unsigned key, gchar const *value );
18 static Inkscape::XML::Node *colorprofile_write( SPObject *object, Inkscape::XML::Node *repr, guint flags );
20 static SPObject *cprof_parent_class;
22 /**
23  * Register ColorProfile class and return its type.
24  */
25 GType Inkscape::colorprofile_get_type()
26 {
27     static GType type = 0;
28     if (!type) {
29         GTypeInfo info = {
30             sizeof(ColorProfileClass),
31             NULL, NULL,
32             (GClassInitFunc) colorprofile_class_init,
33             NULL, NULL,
34             sizeof(ColorProfile),
35             16,
36             (GInstanceInitFunc) colorprofile_init,
37             NULL,   /* value_table */
38         };
39         type = g_type_register_static( SP_TYPE_OBJECT, "ColorProfile", &info, static_cast<GTypeFlags>(0) );
40     }
41     return type;
42 }
44 /**
45  * ColorProfile vtable initialization.
46  */
47 static void colorprofile_class_init( ColorProfileClass *klass )
48 {
49     SPObjectClass *sp_object_class = reinterpret_cast<SPObjectClass *>(klass);
51     cprof_parent_class = static_cast<SPObject*>(g_type_class_ref(SP_TYPE_OBJECT));
53     sp_object_class->release = colorprofile_release;
54     sp_object_class->build = colorprofile_build;
55     sp_object_class->set = colorprofile_set;
56     sp_object_class->write = colorprofile_write;
57 }
59 /**
60  * Callback for ColorProfile object initialization.
61  */
62 static void colorprofile_init( ColorProfile *cprof )
63 {
64     cprof->href = 0;
65     cprof->local = 0;
66     cprof->name = 0;
67     cprof->rendering_intent = 0;
68 #if ENABLE_LCMS
69     cprof->profHandle = 0;
70 #endif // ENABLE_LCMS
71 }
73 /**
74  * Callback: free object
75  */
76 static void colorprofile_release( SPObject *object )
77 {
78     ColorProfile *cprof = COLORPROFILE(object);
79     if ( cprof->href ) {
80         g_free( cprof->href );
81         cprof->href = 0;
82     }
84     if ( cprof->local ) {
85         g_free( cprof->local );
86         cprof->local = 0;
87     }
89     if ( cprof->name ) {
90         g_free( cprof->name );
91         cprof->name = 0;
92     }
94 #if ENABLE_LCMS
95     if ( cprof->profHandle ) {
96         cmsCloseProfile( cprof->profHandle );
97         cprof->profHandle = 0;
98     }
99 #endif // ENABLE_LCMS
102 /**
103  * Callback: set attributes from associated repr.
104  */
105 static void colorprofile_build( SPObject *object, SPDocument *document, Inkscape::XML::Node *repr )
107     ColorProfile *cprof = COLORPROFILE(object);
108     g_assert(cprof->href == 0);
109     g_assert(cprof->local == 0);
110     g_assert(cprof->name == 0);
112     if (((SPObjectClass *) cprof_parent_class)->build) {
113         (* ((SPObjectClass *) cprof_parent_class)->build)(object, document, repr);
114     }
115     sp_object_read_attr( object, "xlink:href" );
116     sp_object_read_attr( object, "local" );
117     sp_object_read_attr( object, "name" );
118     sp_object_read_attr( object, "rendering-intent" );
121 /**
122  * Callback: set attribute.
123  */
124 static void colorprofile_set( SPObject *object, unsigned key, gchar const *value )
126     ColorProfile *cprof = COLORPROFILE(object);
128     switch (key) {
129         case SP_ATTR_XLINK_HREF:
130             if ( value ) {
131                 cprof->href = g_strdup( value );
132                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
133                 if ( *cprof->href ) {
134 #if ENABLE_LCMS
135                     cmsErrorAction( LCMS_ERROR_SHOW );
137                     // TODO open filename and URIs properly
138                     //FILE* fp = fopen_utf8name( filename, "r" );
139                     //LCMSAPI cmsHPROFILE   LCMSEXPORT cmsOpenProfileFromMem(LPVOID MemPtr, DWORD dwSize);
140                     cprof->profHandle = cmsOpenProfileFromFile( value, "r" );
141 #endif // ENABLE_LCMS
142                 }
143             }
144             break;
146         case SP_ATTR_LOCAL:
147             if ( value ) {
148                 cprof->local = g_strdup( value );
149                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
150             }
151             break;
153         case SP_ATTR_NAME:
154             if ( value ) {
155                 cprof->name = g_strdup( value );
156                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
157             }
158             break;
160         case SP_ATTR_RENDERING_INTENT:
161             if ( value ) {
162 // auto | perceptual | relative-colorimetric | saturation | absolute-colorimetric
163                 //cprof->name = g_strdup( value );
164                 //object->requestModified(SP_OBJECT_MODIFIED_FLAG);
165             }
166             break;
168         default:
169             if (((SPObjectClass *) cprof_parent_class)->set) {
170                 (* ((SPObjectClass *) cprof_parent_class)->set)(object, key, value);
171             }
172             break;
173     }
176 /**
177  * Callback: write attributes to associated repr.
178  */
179 static Inkscape::XML::Node* colorprofile_write( SPObject *object, Inkscape::XML::Node *repr, guint flags )
181     ColorProfile *cprof = COLORPROFILE(object);
183     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
184         repr = sp_repr_new("svg:color-profile");
185     }
187     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
188         repr->setAttribute( "xlink:href", cprof->name );
189     }
191     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
192         repr->setAttribute( "local", cprof->name );
193     }
195     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
196         repr->setAttribute( "name", cprof->name );
197     }
199     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
200 //        repr->setAttribute( "rendering-intent", cprof->name );
201     }
203     if (((SPObjectClass *) cprof_parent_class)->write) {
204         (* ((SPObjectClass *) cprof_parent_class)->write)(object, repr, flags);
205     }
207     return repr;
211 #if ENABLE_LCMS
214 static SPObject* bruteFind( SPObject* curr, gchar* const name )
216     SPObject* result = 0;
218     if ( curr ) {
219         if ( IS_COLORPROFILE(curr) ) {
220             ColorProfile* prof = COLORPROFILE(curr);
221             if ( prof ) {
222                 if ( prof->name && (strcmp(prof->name, name) == 0) ) {
223                     result = curr;
224                 }
225             }
226         } else {
227             if ( curr->hasChildren() ) {
228                 SPObject* child = curr->firstChild();
229                 while ( child && !result ) {
230                     result = bruteFind( child, name );
231                     if ( !result ) {
232                         child = child->next;
233                     }
234                 };
235             }
236         }
237     }
239     return result;
242 cmsHPROFILE Inkscape::colorprofile_get_handle( SPDocument* document, gchar* const name )
244     cmsHPROFILE prof = 0;
246     SPObject* root = SP_DOCUMENT_ROOT(document);
247     SPObject* thing = bruteFind( root, name );
248     if ( thing ) {
249         prof = COLORPROFILE(thing)->profHandle;
250     }
252     return prof;
254 #endif // ENABLE_LCMS
256 /*
257   Local Variables:
258   mode:c++
259   c-file-style:"stroustrup"
260   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
261   indent-tabs-mode:nil
262   fill-column:99
263   End:
264 */
265 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :