summaryrefslogtreecommitdiffstats
path: root/src/color-profile.cpp
blob: 5f4e8324e16189cdb5906bbc9bb69a6cc23ac98a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "xml/repr.h"
#include "color-profile.h"
#include "color-profile-fns.h"
#include "attributes.h"

using Inkscape::ColorProfile;
using Inkscape::ColorProfileClass;

static void colorprofile_class_init( ColorProfileClass *klass );
static void colorprofile_init( ColorProfile *cprof );

static void colorprofile_release( SPObject *object );
static void colorprofile_build( SPObject *object, SPDocument *document, Inkscape::XML::Node *repr );
static void colorprofile_set( SPObject *object, unsigned key, gchar const *value );
static Inkscape::XML::Node *colorprofile_write( SPObject *object, Inkscape::XML::Node *repr, guint flags );

static SPObject *cprof_parent_class;

/**
 * Register ColorProfile class and return its type.
 */
GType Inkscape::colorprofile_get_type()
{
    static GType type = 0;
    if (!type) {
        GTypeInfo info = {
            sizeof(ColorProfileClass),
            NULL, NULL,
            (GClassInitFunc) colorprofile_class_init,
            NULL, NULL,
            sizeof(ColorProfile),
            16,
            (GInstanceInitFunc) colorprofile_init,
            NULL,   /* value_table */
        };
        type = g_type_register_static( SP_TYPE_OBJECT, "ColorProfile", &info, static_cast<GTypeFlags>(0) );
    }
    return type;
}

/**
 * ColorProfile vtable initialization.
 */
static void colorprofile_class_init( ColorProfileClass *klass )
{
    SPObjectClass *sp_object_class = reinterpret_cast<SPObjectClass *>(klass);

    cprof_parent_class = static_cast<SPObject*>(g_type_class_ref(SP_TYPE_OBJECT));

    sp_object_class->release = colorprofile_release;
    sp_object_class->build = colorprofile_build;
    sp_object_class->set = colorprofile_set;
    sp_object_class->write = colorprofile_write;
}

/**
 * Callback for ColorProfile object initialization.
 */
static void colorprofile_init( ColorProfile *cprof )
{
    cprof->href = 0;
    cprof->local = 0;
    cprof->name = 0;
    cprof->rendering_intent = 0;
#if ENABLE_LCMS
    cprof->profHandle = 0;
#endif // ENABLE_LCMS
}

/**
 * Callback: free object
 */
static void colorprofile_release( SPObject *object )
{
    ColorProfile *cprof = COLORPROFILE(object);
    if ( cprof->href ) {
        g_free( cprof->href );
        cprof->href = 0;
    }

    if ( cprof->local ) {
        g_free( cprof->local );
        cprof->local = 0;
    }

    if ( cprof->name ) {
        g_free( cprof->name );
        cprof->name = 0;
    }

#if ENABLE_LCMS
    if ( cprof->profHandle ) {
        cmsCloseProfile( cprof->profHandle );
        cprof->profHandle = 0;
    }
#endif // ENABLE_LCMS
}

/**
 * Callback: set attributes from associated repr.
 */
static void colorprofile_build( SPObject *object, SPDocument *document, Inkscape::XML::Node *repr )
{
    ColorProfile *cprof = COLORPROFILE(object);
    g_assert(cprof->href == 0);
    g_assert(cprof->local == 0);
    g_assert(cprof->name == 0);

    if (((SPObjectClass *) cprof_parent_class)->build) {
        (* ((SPObjectClass *) cprof_parent_class)->build)(object, document, repr);
    }
    sp_object_read_attr( object, "xlink:href" );
    sp_object_read_attr( object, "local" );
    sp_object_read_attr( object, "name" );
    sp_object_read_attr( object, "rendering-intent" );
}

/**
 * Callback: set attribute.
 */
static void colorprofile_set( SPObject *object, unsigned key, gchar const *value )
{
    ColorProfile *cprof = COLORPROFILE(object);

    switch (key) {
        case SP_ATTR_XLINK_HREF:
            if ( value ) {
                cprof->href = g_strdup( value );
                object->requestModified(SP_OBJECT_MODIFIED_FLAG);
                if ( *cprof->href ) {
#if ENABLE_LCMS
                    cmsErrorAction( LCMS_ERROR_SHOW );

                    // TODO open filename and URIs properly
                    //FILE* fp = fopen_utf8name( filename, "r" );
                    //LCMSAPI cmsHPROFILE   LCMSEXPORT cmsOpenProfileFromMem(LPVOID MemPtr, DWORD dwSize);
                    cprof->profHandle = cmsOpenProfileFromFile( value, "r" );
#endif // ENABLE_LCMS
                }
            }
            break;

        case SP_ATTR_LOCAL:
            if ( value ) {
                cprof->local = g_strdup( value );
                object->requestModified(SP_OBJECT_MODIFIED_FLAG);
            }
            break;

        case SP_ATTR_NAME:
            if ( value ) {
                cprof->name = g_strdup( value );
                object->requestModified(SP_OBJECT_MODIFIED_FLAG);
            }
            break;

        case SP_ATTR_RENDERING_INTENT:
            if ( value ) {
// auto | perceptual | relative-colorimetric | saturation | absolute-colorimetric
                //cprof->name = g_strdup( value );
                //object->requestModified(SP_OBJECT_MODIFIED_FLAG);
            }
            break;

        default:
            if (((SPObjectClass *) cprof_parent_class)->set) {
                (* ((SPObjectClass *) cprof_parent_class)->set)(object, key, value);
            }
            break;
    }
}

/**
 * Callback: write attributes to associated repr.
 */
static Inkscape::XML::Node* colorprofile_write( SPObject *object, Inkscape::XML::Node *repr, guint flags )
{
    ColorProfile *cprof = COLORPROFILE(object);

    if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
        repr = sp_repr_new("svg:color-profile");
    }

    if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
        repr->setAttribute( "xlink:href", cprof->name );
    }

    if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
        repr->setAttribute( "local", cprof->name );
    }

    if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
        repr->setAttribute( "name", cprof->name );
    }

    if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
//        repr->setAttribute( "rendering-intent", cprof->name );
    }

    if (((SPObjectClass *) cprof_parent_class)->write) {
        (* ((SPObjectClass *) cprof_parent_class)->write)(object, repr, flags);
    }

    return repr;
}


/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :