~ubuntu-branches/ubuntu/breezy/ufraw/breezy-backports

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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 * UFRaw - Unidentified Flying Raw converter for digital camera images
 * by Udi Fuchs,
 *
 * ufraw-gimp.c - The GIMP plug-in.
 *
 * based on the gimp plug-in by Pawel T. Jochym jochym at ifj edu pl,
 *
 * based on the gimp plug-in by Dave Coffin
 * http://www.cybercom.net/~dcoffin/
 *
 * UFRaw is licensed under the GNU General Public License.
 * It uses DCRaw code to do the actual raw decoding.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
#include <locale.h>
#include "ufraw.h"
#include "ufraw_icon.h"

void query();
void run(const gchar *name,
	gint nparams,
	const GimpParam *param,
	gint *nreturn_vals,
	GimpParam **return_vals);

long ufraw_save_gimp_image(GtkWidget *widget, ufraw_data *uf);

GimpPlugInInfo PLUG_IN_INFO = {
    NULL,  /* init_procedure */
    NULL,  /* quit_procedure */
    query, /* query_procedure */
    run,   /* run_procedure */
};

MAIN()

void query()
{
    static GimpParamDef load_args[] = {
	{ GIMP_PDB_INT32,  "run_mode", "Interactive, non-interactive" },
	{ GIMP_PDB_STRING, "filename", "The name of the file to load" },
	{ GIMP_PDB_STRING, "raw_filename", "The name of the file to load" },
    };
    static GimpParamDef load_return_vals[] = {
	{ GIMP_PDB_IMAGE, "image", "Output image" },
    };
#if GIMP_CHECK_VERSION(2,2,0)
    static GimpParamDef thumb_args[] = {
	{ GIMP_PDB_STRING, "filename",     "The name of the file to load" },
	{ GIMP_PDB_INT32,  "thumb_size",   "Preferred thumbnail size" }
    };
    static GimpParamDef thumb_return_vals[] = {
	{ GIMP_PDB_IMAGE,  "image",        "Thumbnail image" },
	{ GIMP_PDB_INT32,  "image_width",  "Width of full-sized image" },
	{ GIMP_PDB_INT32,  "image_height", "Height of full-sized image" }
    };
#endif
    gimp_install_procedure("file_ufraw_load",
	    "Loads raw digital camera files",
	    "This plug-in loads raw digital camera files.",
	    "Udi Fuchs",
	    "Copyright 2003 by Dave Coffin\n"
	    "Copyright 2004 by Pawel Jochym\n"
	    "Copyright 2004 by Udi Fuchs",
	    "ufraw-" VERSION,
#if GIMP_CHECK_VERSION(2,2,0)
	    "raw image",
#else
	    "<Load>/UFRaw",
#endif
	    NULL,
	    GIMP_PLUGIN,
	    G_N_ELEMENTS(load_args),
	    G_N_ELEMENTS(load_return_vals),
	    load_args,
	    load_return_vals);

    gimp_register_load_handler("file_ufraw_load", raw_ext, "");
#if GIMP_CHECK_VERSION(2,2,0)
    gimp_install_procedure ("file_ufraw_load_thumb",
	    "Loads a small preview from a raw image",
	    "",
	    "",
	    "Copyright 2004 by Udi Fuchs",
	    "ufraw-" VERSION,
	    NULL,
	    NULL,
	    GIMP_PLUGIN,
	    G_N_ELEMENTS(thumb_args),
	    G_N_ELEMENTS(thumb_return_vals),
	    thumb_args, thumb_return_vals);
    gimp_register_thumbnail_loader("file_ufraw_load",
	    "file_ufraw_load_thumb");
#endif
}

char *ufraw_binary;

void run(const gchar *name,
	gint nparams,
	const GimpParam *param,
	gint *nreturn_vals,
	GimpParam **return_vals)
{
    static GimpParam values[4];
    GimpRunMode run_mode;
    char *filename;
    int size;
    ufraw_data *uf;
    conf_data conf;
    int status;
    const char *locale;

    ufraw_binary = "ufraw-gimp";

    *nreturn_vals = 1;
    *return_vals = values;

    if (!strcmp(name, "file_ufraw_load_thumb")) {
	run_mode = 0;
	filename = param[0].data.d_string;
	size = param[1].data.d_int32;
    } else if (!strcmp(name, "file_ufraw_load")) {
	run_mode = (GimpRunMode)param[0].data.d_int32;
	filename = param[1].data.d_string;
	size = 0;
    } else {
	values[0].type = GIMP_PDB_STATUS;
	values[0].data.d_status = GIMP_PDB_CALLING_ERROR;
	return;
    }
    locale = setlocale(LC_ALL, "");
    if ( locale!=NULL &&
	(!strncmp(locale, "he", 2) || !strncmp(locale, "iw", 2) ||
	!strncmp(locale, "ar", 2) ||
	!strncmp(locale, "Hebrew", 6) || !strncmp(locale, "Arabic", 6) ) ) {
	/* I'm not sure why the following doesn't work (on Windows at least) */
/*	setlocale(LC_ALL, "en_US");
	gtk_disable_setlocale(); */
	/* so I'm using setenv */
	g_setenv("LC_ALL", "en_US", TRUE);
    }
    gimp_ui_init("ufraw-gimp", TRUE);

    uf = ufraw_open(filename);
    /* if UFRaw fails on jpg or tif then open with Gimp */
    if (uf==NULL) {
	if (!strcasecmp(filename + strlen(filename) - 4, ".jpg")) {
	    if (size==0)
		*return_vals = gimp_run_procedure2 ("file_jpeg_load",
			nreturn_vals, nparams, param);
	    else
		*return_vals = gimp_run_procedure2 ("file_jpeg_load_thumb",
			nreturn_vals, nparams, param);
	    return;
	} else if (!strcasecmp(filename+strlen(filename)-4, ".tif")) {
	    if (size==0)
		*return_vals = gimp_run_procedure2 ("file_tiff_load",
			nreturn_vals, nparams, param);
	    else {
		/* There is no "file_tiff_load_thumb".
		 * The call to "file_ufraw_load" will handle the thumbnail */
		/* Following is another solution for tiff thumbnails
		GimpParam tiffParam[3];
		tiffParam[0].type = GIMP_PDB_INT32;
		tiffParam[0].data.d_int32 = GIMP_RUN_NONINTERACTIVE;
		tiffParam[1].type = GIMP_PDB_STRING;
		tiffParam[1].data.d_string = filename;
		tiffParam[2].type = GIMP_PDB_STRING;
		tiffParam[2].data.d_string = filename;
		*return_vals = gimp_run_procedure2 ("file_tiff_load",
		    	nreturn_vals, 3, tiffParam);
		*/
	    }
	    return;
	} else {
	    /* Don't issue a message on thumbnail failure, since ufraw-gimp
	     * will be called again with "file_ufraw_load" */
	    if (size>0) return;

	    GtkWidget *dummyWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	    gtk_window_set_icon(GTK_WINDOW(dummyWindow),
		    gdk_pixbuf_new_from_inline(-1, ufraw_icon, FALSE, NULL));
	    ufraw_message(UFRAW_SET_PARENT, (char *)dummyWindow);

	    ufraw_message(UFRAW_REPORT, NULL);
	    values[0].type = GIMP_PDB_STATUS;
	    /* With GIMP_PDB_CANCEL, Gimp won't issue a warning */
	    values[0].data.d_status = GIMP_PDB_CANCEL;

	    gtk_widget_destroy(dummyWindow);
	    return;
	}
    }
    /* Load $HOME/.ufrawrc */
    conf_load(&conf, NULL);

    ufraw_config(uf, &conf, NULL, NULL);
    conf_copy_save(uf->conf, &conf_default);
#if GIMP_CHECK_VERSION(2,2,0)
    if (size>0) uf->conf->size = size;
#else
    if (run_mode==GIMP_RUN_NONINTERACTIVE) uf->conf->shrink = 8;
#endif
    /* UFRaw already issues warnings.
     * With GIMP_PDB_CANCEL, Gimp won't issue another one. */
    values[0].type = GIMP_PDB_STATUS;
    values[0].data.d_status = GIMP_PDB_CANCEL;
    /* BUG - what should be done with GIMP_RUN_WITH_LAST_VALS */
    if (size==0 && run_mode==GIMP_RUN_INTERACTIVE) {
	status = ufraw_preview(uf, TRUE, ufraw_save_gimp_image);
	gimp_set_data("plug_in_ufraw", &conf, sizeof(conf));
    } else {
	status=ufraw_load_raw(uf);
	ufraw_save_gimp_image(NULL, uf);
	ufraw_close(uf);
    }
    if (status != UFRAW_SUCCESS) return;
    if (uf->gimpImage==-1) {
	values[0].type = GIMP_PDB_STATUS;
	values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
	return;
    }
    *nreturn_vals = 2;
    values[0].type = GIMP_PDB_STATUS;
    values[0].data.d_status = GIMP_PDB_SUCCESS;
    values[1].type = GIMP_PDB_IMAGE;
    values[1].data.d_image = uf->gimpImage;
    if (size>0) {
	*nreturn_vals = 4;
	values[2].type = GIMP_PDB_INT32;
	values[2].data.d_int32 = uf->predictateWidth;
	values[3].type = GIMP_PDB_INT32;
	values[3].data.d_int32 = uf->predictateHeight;
    }
}

long ufraw_save_gimp_image(GtkWidget *widget, ufraw_data *uf)
{
    GtkWindow *window;
    GimpDrawable *drawable;
    GimpPixelRgn pixel_region;
    gint32 layer;
    guint8 *pixbuf;
    guint16 *pixtmp;
    int tile_height, row, nrows, rowStride, y;
    image_type *rawImage;

    if (ufraw_convert_image(uf)!=UFRAW_SUCCESS) {
	uf->gimpImage = -1;
	return UFRAW_ERROR;
    }
    uf->gimpImage = gimp_image_new(uf->image.width, uf->image.height,GIMP_RGB);
    if (uf->gimpImage== -1) {
	ufraw_message(UFRAW_ERROR, "Can't allocate new image.");
	return UFRAW_ERROR;
    }
    gimp_image_set_filename(uf->gimpImage, uf->filename);

    /* Create the "background" layer to hold the image... */
    layer = gimp_layer_new(uf->gimpImage, "Background", uf->image.width,
	    uf->image.height, GIMP_RGB_IMAGE, 100, GIMP_NORMAL_MODE);
    gimp_image_add_layer(uf->gimpImage, layer, 0);

    /* Get the drawable and set the pixel region for our load... */
    drawable = gimp_drawable_get(layer);
    gimp_pixel_rgn_init(&pixel_region, drawable, 0, 0, drawable->width,
	    drawable->height, TRUE, FALSE);
    tile_height = gimp_tile_height();
    pixbuf = g_new(guint8, tile_height * uf->image.width * 3);
    pixtmp = g_new(guint16, tile_height * uf->image.width * 3);

    rowStride = uf->image.width;
    rawImage = uf->image.image;
    for (row = 0; row < uf->image.height; row += tile_height) {
	preview_progress(widget, "Loading image",
		0.5 + 0.5*row/uf->image.height);
	nrows = MIN(uf->image.height-row, tile_height);
	for (y=0 ; y<nrows; y++)
	    develope(&pixbuf[3*y*uf->image.width], rawImage[(row+y)*rowStride],
		    uf->developer, 8, pixtmp, uf->image.width);
	gimp_pixel_rgn_set_rect(&pixel_region, pixbuf, 0, row,
		uf->image.width, nrows);
    }
    g_free(pixbuf);
    g_free(pixtmp);
    gimp_drawable_flush(drawable);
    gimp_drawable_detach(drawable);
    if (uf->exifBuf!=NULL) {
	GimpParasite *exif_parasite;
	exif_parasite = gimp_parasite_new ("exif-data",
		GIMP_PARASITE_PERSISTENT, uf->exifBufLen, uf->exifBuf);
	gimp_image_parasite_attach (uf->gimpImage, exif_parasite);
	gimp_parasite_free (exif_parasite);
	g_free (uf->exifBuf);
	uf->exifBuf = NULL;
    }
    /* Create "icc-profile" parasite from output profile
     * if it is not the internal sRGB.*/
    if (strcmp(uf->developer->profileFile[out_profile], "")) {
	char *buf;
	gsize len;
	if (g_file_get_contents(uf->developer->profileFile[out_profile],
		&buf, &len, NULL))
	{
	    GimpParasite *icc_parasite;
	    icc_parasite = gimp_parasite_new ("icc-profile",
		GIMP_PARASITE_PERSISTENT, len, buf);
	    gimp_image_parasite_attach (uf->gimpImage, icc_parasite);
	    gimp_parasite_free (icc_parasite);
	    g_free(buf);
	} else {
	    ufraw_message(UFRAW_WARNING,
		    "Failed to embed output profile '%s' in image.",
		    uf->developer->profileFile[out_profile]);
	}
    }
    if (widget!=NULL) {
	window = GTK_WINDOW(gtk_widget_get_toplevel(widget));
	g_object_set_data(G_OBJECT(window), "WindowResponse",
		(gpointer)GTK_RESPONSE_OK);
	gtk_main_quit();
    }
    return UFRAW_SUCCESS;
}