7 * This software is released under the GNU General Public License (GNU GPL).
8 * Please read the included file COPYING for more information.
9 * This software comes with no warranty of any kind, use at your own risk!
14 #include "image-load.h"
16 #include "format_raw.h"
17 #include "ui_fileops.h"
22 /* bytes to read from file per read() */
23 #define IMAGE_LOADER_BUFFER_SIZE 512
25 /* the number of bytes to read per idle call (define x IMAGE_LOADER_BUFFER_SIZE) */
26 #define IMAGE_LOADER_BUFFER_DEFAULT_COUNT 1
28 static void image_loader_sync_pixbuf(ImageLoader *il)
32 if (!il->loader) return;
34 pb = gdk_pixbuf_loader_get_pixbuf(il->loader);
36 if (pb == il->pixbuf) return;
38 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
40 if (il->pixbuf) gdk_pixbuf_ref(il->pixbuf);
43 static void image_loader_area_cb(GdkPixbufLoader *loader,
44 guint x, guint y, guint w, guint h,
47 ImageLoader *il = data;
49 if (il->func_area_ready)
53 image_loader_sync_pixbuf(il);
56 printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
59 il->func_area_ready(il, x, y, w, h, il->data_area_ready);
63 static void image_loader_size_cb(GdkPixbufLoader *loader,
64 gint width, gint height, gpointer data)
66 ImageLoader *il = data;
67 GdkPixbufFormat *format;
72 if (il->requested_width < 1 || il->requested_height < 1) return;
74 format = gdk_pixbuf_loader_get_format(loader);
77 mime_types = gdk_pixbuf_format_get_mime_types(format);
81 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
84 g_strfreev(mime_types);
88 if (width > il->requested_width || height > il->requested_height)
92 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
94 nw = il->requested_width;
95 nh = (gdouble)nw / width * height;
100 nh = il->requested_height;
101 nw = (gdouble)nh / height * width;
105 gdk_pixbuf_loader_set_size(loader, nw, nh);
110 static void image_loader_stop(ImageLoader *il)
114 if (il->idle_id != -1)
116 g_source_remove(il->idle_id);
122 /* some loaders do not have a pixbuf till close, order is important here */
123 gdk_pixbuf_loader_close(il->loader, NULL);
124 image_loader_sync_pixbuf(il);
125 g_object_unref(G_OBJECT(il->loader));
129 if (il->load_fd != -1)
138 static void image_loader_done(ImageLoader *il)
140 image_loader_stop(il);
142 if (il->func_done) il->func_done(il, il->data_done);
145 static gint image_loader_done_delay_cb(gpointer data)
147 ImageLoader *il = data;
149 il->idle_done_id = -1;
150 image_loader_done(il);
154 static void image_loader_done_delay(ImageLoader *il)
156 if (il->idle_done_id == -1) il->idle_done_id = g_idle_add_full(il->idle_priority,
157 image_loader_done_delay_cb, il, NULL);
160 static void image_loader_error(ImageLoader *il)
162 image_loader_stop(il);
164 if (debug) printf("pixbuf_loader reported load error for: %s\n", il->path);
166 if (il->func_error) il->func_error(il, il->data_error);
169 static gint image_loader_idle_cb(gpointer data)
171 ImageLoader *il = data;
172 guchar buf[IMAGE_LOADER_BUFFER_SIZE];
176 if (!il) return FALSE;
178 if (il->idle_id == -1) return FALSE;
180 c = il->buffer_size ? il->buffer_size : 1;
183 b = read(il->load_fd, &buf, sizeof(buf));
187 image_loader_done(il);
191 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, buf, b, NULL)))
193 image_loader_error(il);
202 if (il->func_percent && il->bytes_total > 0)
204 il->func_percent(il, (gdouble)il->bytes_read / il->bytes_total, il->data_percent);
210 static gint image_loader_begin(ImageLoader *il)
212 guchar buf[IMAGE_LOADER_BUFFER_SIZE];
214 unsigned int offset = 0;
216 if (!il->loader || il->pixbuf) return FALSE;
218 b = read(il->load_fd, &buf, sizeof(buf));
221 format_raw_img_exif_offsets_fd(il->load_fd, il->path, buf, b, &offset, NULL))
223 if (debug) printf("Raw file %s contains embedded image\n", il->path);
225 b = read(il->load_fd, &buf, sizeof(buf));
230 image_loader_stop(il);
234 if (!gdk_pixbuf_loader_write(il->loader, buf, b, NULL))
236 image_loader_stop(il);
240 il->bytes_read += b + offset;
242 /* read until size is known */
243 while (il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0)
245 b = read(il->load_fd, &buf, sizeof(buf));
246 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, buf, b, NULL)))
248 image_loader_stop(il);
253 if (!il->pixbuf) image_loader_sync_pixbuf(il);
255 if (il->bytes_read == il->bytes_total || b < 1)
257 /* done, handle (broken) loaders that do not have pixbuf till close */
258 image_loader_stop(il);
260 if (!il->pixbuf) return FALSE;
262 image_loader_done_delay(il);
268 image_loader_stop(il);
272 /* finally, progressive loading :) */
273 il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
278 static gint image_loader_setup(ImageLoader *il)
283 if (!il || il->load_fd != -1 || il->loader) return FALSE;
285 pathl = path_from_utf8(il->path);
286 il->load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
288 if (il->load_fd == -1) return FALSE;
290 if (fstat(il->load_fd, &st) == 0)
292 il->bytes_total = st.st_size;
295 il->loader = gdk_pixbuf_loader_new();
296 g_signal_connect(G_OBJECT(il->loader), "area_updated",
297 G_CALLBACK(image_loader_area_cb), il);
298 g_signal_connect(G_OBJECT(il->loader), "size_prepared",
299 G_CALLBACK(image_loader_size_cb), il);
303 return image_loader_begin(il);
306 ImageLoader *image_loader_new(const gchar *path)
310 if (!path) return NULL;
312 il = g_new0(ImageLoader, 1);
313 if (path) il->path = g_strdup(path);
316 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
324 il->idle_done_id = -1;
326 il->buffer_size = IMAGE_LOADER_BUFFER_DEFAULT_COUNT;
328 il->requested_width = 0;
329 il->requested_height = 0;
335 void image_loader_free(ImageLoader *il)
339 image_loader_stop(il);
340 if (il->idle_done_id != -1) g_source_remove(il->idle_done_id);
341 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
346 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
347 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
349 if (!il) return NULL;
354 gchar *image_loader_get_format(ImageLoader *il)
356 GdkPixbufFormat *format;
360 if (!il || !il->loader) return NULL;
362 format = gdk_pixbuf_loader_get_format(il->loader);
363 if (!format) return NULL;
365 mimev = gdk_pixbuf_format_get_mime_types(format);
366 if (!mimev) return NULL;
368 /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
369 mime = g_strdup(mimev[0]);
375 void image_loader_set_area_ready_func(ImageLoader *il,
376 void (*func_area_ready)(ImageLoader *, guint, guint, guint, guint, gpointer),
377 gpointer data_area_ready)
381 il->func_area_ready = func_area_ready;
382 il->data_area_ready = data_area_ready;
385 void image_loader_set_error_func(ImageLoader *il,
386 void (*func_error)(ImageLoader *, gpointer),
391 il->func_error = func_error;
392 il->data_error = data_error;
395 void image_loader_set_percent_func(ImageLoader *il,
396 void (*func_percent)(ImageLoader *, gdouble, gpointer),
397 gpointer data_percent)
401 il->func_percent = func_percent;
402 il->data_percent = data_percent;
405 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
409 il->requested_width = width;
410 il->requested_height = height;
413 void image_loader_set_buffer_size(ImageLoader *il, guint size)
417 il->buffer_size = size ? size : 1;
420 void image_loader_set_priority(ImageLoader *il, gint priority)
424 il->idle_priority = priority;
427 gint image_loader_start(ImageLoader *il, void (*func_done)(ImageLoader *, gpointer), gpointer data_done)
429 if (!il) return FALSE;
431 if (!il->path) return FALSE;
433 il->func_done = func_done;
434 il->data_done = data_done;
436 return image_loader_setup(il);
439 gdouble image_loader_get_percent(ImageLoader *il)
441 if (!il || il->bytes_total == 0) return 0.0;
443 return (gdouble)il->bytes_read / il->bytes_total;
446 gint image_loader_get_is_done(ImageLoader *il)
448 if (!il) return FALSE;
453 gint image_load_dimensions(const gchar *path, gint *width, gint *height)
458 il = image_loader_new(path);
460 success = image_loader_start(il, NULL, NULL);
462 if (success && il->pixbuf)
464 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
465 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
469 if (width) *width = -1;
470 if (height) *height = -1;
473 image_loader_free(il);