summaryrefslogtreecommitdiff
path: root/gnome/pixbuf-loader.c
blob: 152e7a88ca8d658f912586e208cd02be6380378c (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
/*
 * libopenraw - pixbuf-loader.c
 *
 * Copyright (C) 2008 Hubert Figuiere
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */


/** @brief gdkpixbuf loader for RAW files */

#include <stdlib.h>

#include <libopenraw/libopenraw.h>

#define GDK_PIXBUF_ENABLE_BACKEND
#include <gdk-pixbuf/gdk-pixbuf-io.h>
#include <gdk-pixbuf/gdk-pixbuf.h>


G_MODULE_EXPORT void fill_vtable (GdkPixbufModule *module);
G_MODULE_EXPORT void fill_info (GdkPixbufFormat *info);

static void pixbuf_free(guchar * data, gpointer u)
{
    ORBitmapDataRef b = (ORBitmapDataRef)u;
    (void)data;
    or_bitmapdata_release(b);
}

#if 0
static GdkPixbuf * 
gdk_pixbuf__or_image_load(FILE *f, GError **error)
{
    (void)f;
    (void)error;
    return NULL;
}
#endif


typedef struct {
    GdkPixbufModuleSizeFunc     size_func;
    GdkPixbufModulePreparedFunc prepared_func;
    GdkPixbufModuleUpdatedFunc  updated_func;
    gpointer                    user_data;
    GByteArray                 *data;
} OrContext;

static gpointer
gdk_pixbuf__or_image_begin_load (GdkPixbufModuleSizeFunc size_func,
                                 GdkPixbufModulePreparedFunc prepared_func,
                                 GdkPixbufModuleUpdatedFunc  updated_func,
                                 gpointer user_data,
                                 GError **error)
{
    OrContext *context = (OrContext*)calloc(1, sizeof(OrContext));

    (void)error;

    context->size_func = size_func;
    context->prepared_func = prepared_func;
    context->updated_func = updated_func;
    context->user_data = user_data;
    context->data = g_byte_array_new();

    return (gpointer)context;
}

static gboolean
gdk_pixbuf__or_image_load_increment (gpointer data,
                                     const guchar *buf, guint size,
                                     GError **error)
{
    OrContext *context = (OrContext*)data;
    (void)error;
    g_byte_array_append (context->data, buf, size);
    return TRUE;
}

static gboolean
gdk_pixbuf__or_image_stop_load (gpointer data, GError **error)
{
    OrContext *context = (OrContext*)data;
    gboolean result = FALSE;
	
    GdkPixbuf *pixbuf = NULL;
    ORRawFileRef raw_file = NULL;
    (void)error;

    raw_file = or_rawfile_new_from_memory(context->data->data, context->data->len,
                                          OR_DATA_TYPE_NONE);
	
    if(raw_file) {
        or_error err;
        ORBitmapDataRef bitmapdata = or_bitmapdata_new();
        err = or_rawfile_get_rendered_image(raw_file, bitmapdata, 0);
        if(err == OR_ERROR_NONE) {
            uint32_t x,y;
            x = y = 0;
            or_bitmapdata_dimensions(bitmapdata, &x, &y);
            pixbuf = gdk_pixbuf_new_from_data(or_bitmapdata_data(bitmapdata), 
                                              GDK_COLORSPACE_RGB,
                                              FALSE, 8, x, y, 
                                              (x - 2) * 3, 
                                              pixbuf_free, bitmapdata);
        }
        or_rawfile_release(raw_file);

        if (context->prepared_func != NULL) {
            (*context->prepared_func) (pixbuf, NULL, context->user_data);
        }
        if (context->updated_func != NULL) {
            (*context->updated_func) (pixbuf, 0, 0,
                                      gdk_pixbuf_get_width(pixbuf),
                                      gdk_pixbuf_get_height(pixbuf),
                                      context->user_data);
        }
        result = TRUE;
    }


    g_byte_array_free(context->data, TRUE);
    free(context);
    return result;
}

void
fill_vtable (GdkPixbufModule *module)
{
    module->begin_load     = gdk_pixbuf__or_image_begin_load;
    module->stop_load      = gdk_pixbuf__or_image_stop_load;
    module->load_increment = gdk_pixbuf__or_image_load_increment;

    module->load           = NULL; /*gdk_pixbuf__or_image_load;*/
}


void
fill_info (GdkPixbufFormat *info)
{
    static GdkPixbufModulePattern signature[] = {
        { "MM \x2a", "  z ", 80 }, /* TIFF */
        { "II\x2a \x10   CR\x02 ", "   z zzz   z", 100 }, /* CR2 */
        { "II\x2a ", "   z", 80 }, /* TIFF */
        { "IIRO", "    ", 100 },   /* ORF */
        { " MRM", "z   ", 100 },   /* MRW */
        { "II\x1a   HEAPCCDR", "   zzz        ", 100 }, /* CRW */
        { NULL, NULL, 0 }
    };
	
    static gchar *mime_types[] = {
        "image/x-adobe-dng",
        "image/x-canon-cr2",
        "image/x-canon-crw",
        "image/x-nikon-nef",
        "image/x-olympus-orf",
        "image/x-pentax-pef",
        "image/x-sony-arw",
        "image/x-epson-erf",
        "image/x-minolta-mrw",
        NULL
    };
	
    static gchar *extensions[] = {
        "dng",
        "cr2",
        "crw",
        "nef",
        "orf",
        "pef",
        "arw",
        "erf",
        "mrw",
        NULL
    };
	
    info->name        = "Digital camera RAW";
    info->signature   = signature;
    info->description = "Digital camera RAW images loader.";
    info->mime_types  = mime_types;
    info->extensions  = extensions;
    info->flags       = 0;
    info->license     = "LGPL";
}


/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0))
  indent-tabs-mode:nil
  fill-column:80
  End:
*/