4 * Copyright (C) 2008 The Geeqie Team
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
15 #include "image-load.h"
19 #include "ui_fileops.h"
25 static void image_loader_sync_pixbuf(ImageLoader *il)
29 if (!il->loader) return;
31 pb = gdk_pixbuf_loader_get_pixbuf(il->loader);
33 if (pb == il->pixbuf) return;
35 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
37 if (il->pixbuf) gdk_pixbuf_ref(il->pixbuf);
40 static void image_loader_area_updated_cb(GdkPixbufLoader *loader,
41 guint x, guint y, guint w, guint h,
44 ImageLoader *il = data;
46 if (il->func_area_ready)
50 image_loader_sync_pixbuf(il);
53 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
56 il->func_area_ready(il, x, y, w, h, il->data_area_ready);
60 static void image_loader_area_prepared_cb(GdkPixbufLoader *loader, gpointer data)
66 /* a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669 */
67 gchar *format = gdk_pixbuf_format_get_name(gdk_pixbuf_loader_get_format(loader));
68 if (strcmp(format, "svg") == 0)
76 pb = gdk_pixbuf_loader_get_pixbuf(loader);
78 h = gdk_pixbuf_get_height(pb);
79 rs = gdk_pixbuf_get_rowstride(pb);
80 pix = gdk_pixbuf_get_pixels(pb);
82 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
86 static void image_loader_size_cb(GdkPixbufLoader *loader,
87 gint width, gint height, gpointer data)
89 ImageLoader *il = data;
90 GdkPixbufFormat *format;
95 if (il->requested_width < 1 || il->requested_height < 1) return;
97 format = gdk_pixbuf_loader_get_format(loader);
100 mime_types = gdk_pixbuf_format_get_mime_types(format);
102 while (mime_types[n])
104 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
107 g_strfreev(mime_types);
111 if (width > il->requested_width || height > il->requested_height)
115 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
117 nw = il->requested_width;
118 nh = (gdouble)nw / width * height;
123 nh = il->requested_height;
124 nw = (gdouble)nh / height * width;
128 gdk_pixbuf_loader_set_size(loader, nw, nh);
133 static void image_loader_stop(ImageLoader *il)
137 if (il->idle_id != -1)
139 g_source_remove(il->idle_id);
145 /* some loaders do not have a pixbuf till close, order is important here */
146 gdk_pixbuf_loader_close(il->loader, NULL);
147 image_loader_sync_pixbuf(il);
148 g_object_unref(G_OBJECT(il->loader));
156 exif_free_preview(il->mapped_file);
160 munmap(il->mapped_file, il->bytes_total);
162 il->mapped_file = NULL;
168 static void image_loader_done(ImageLoader *il)
170 image_loader_stop(il);
172 if (il->func_done) il->func_done(il, il->data_done);
175 static gint image_loader_done_delay_cb(gpointer data)
177 ImageLoader *il = data;
179 il->idle_done_id = -1;
180 image_loader_done(il);
184 static void image_loader_done_delay(ImageLoader *il)
186 if (il->idle_done_id == -1) il->idle_done_id = g_idle_add_full(il->idle_priority,
187 image_loader_done_delay_cb, il, NULL);
190 static void image_loader_error(ImageLoader *il)
192 image_loader_stop(il);
194 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
196 if (il->func_error) il->func_error(il, il->data_error);
199 static gint image_loader_idle_cb(gpointer data)
201 ImageLoader *il = data;
205 if (!il) return FALSE;
207 if (il->idle_id == -1) return FALSE;
209 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
212 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
216 image_loader_done(il);
220 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)))
222 image_loader_error(il);
231 if (il->func_percent && il->bytes_total > 0)
233 il->func_percent(il, (gdouble)il->bytes_read / il->bytes_total, il->data_percent);
239 static gint image_loader_begin(ImageLoader *il)
243 if (!il->loader || il->pixbuf) return FALSE;
245 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
249 image_loader_stop(il);
253 if (!gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL))
255 image_loader_stop(il);
261 /* read until size is known */
262 while (il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0)
264 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
265 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)))
267 image_loader_stop(il);
272 if (!il->pixbuf) image_loader_sync_pixbuf(il);
274 if (il->bytes_read == il->bytes_total || b < 1)
276 /* done, handle (broken) loaders that do not have pixbuf till close */
277 image_loader_stop(il);
279 if (!il->pixbuf) return FALSE;
281 image_loader_done_delay(il);
287 image_loader_stop(il);
291 /* finally, progressive loading :) */
292 il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
297 static gint image_loader_setup(ImageLoader *il)
302 if (!il || il->loader || il->mapped_file) return FALSE;
304 il->mapped_file = NULL;
308 ExifData *exif = exif_read_fd(il->fd);
310 il->mapped_file = exif_get_preview(exif, &il->bytes_total);
315 DEBUG_1("Raw file %s contains embedded image", il->fd->path);
317 exif_free_fd(il->fd, exif);
321 if (!il->mapped_file)
326 pathl = path_from_utf8(il->fd->path);
327 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
329 if (load_fd == -1) return FALSE;
331 if (fstat(load_fd, &st) == 0)
333 il->bytes_total = st.st_size;
341 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
343 if (il->mapped_file == MAP_FAILED)
352 il->loader = gdk_pixbuf_loader_new();
353 g_signal_connect(G_OBJECT(il->loader), "area_updated",
354 G_CALLBACK(image_loader_area_updated_cb), il);
355 g_signal_connect(G_OBJECT(il->loader), "size_prepared",
356 G_CALLBACK(image_loader_size_cb), il);
357 g_signal_connect(G_OBJECT(il->loader), "area_prepared",
358 G_CALLBACK(image_loader_area_prepared_cb), il);
362 return image_loader_begin(il);
365 ImageLoader *image_loader_new(FileData *fd)
369 if (!fd) return NULL;
371 il = g_new0(ImageLoader, 1);
372 il->fd = file_data_ref(fd);
375 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
382 il->idle_done_id = -1;
384 il->idle_read_loop_count = options->image.idle_read_loop_count;
385 il->read_buffer_size = options->image.read_buffer_size;
386 il->mapped_file = NULL;
388 il->requested_width = 0;
389 il->requested_height = 0;
391 DEBUG_1("new image loader %p, bufsize=%u idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
395 void image_loader_free(ImageLoader *il)
399 image_loader_stop(il);
400 if (il->idle_done_id != -1) g_source_remove(il->idle_done_id);
401 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
402 file_data_unref(il->fd);
403 DEBUG_1("freeing image loader %p bytes_read=%d", il, il->bytes_read);
407 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
408 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
410 if (!il) return NULL;
415 gchar *image_loader_get_format(ImageLoader *il)
417 GdkPixbufFormat *format;
421 if (!il || !il->loader) return NULL;
423 format = gdk_pixbuf_loader_get_format(il->loader);
424 if (!format) return NULL;
426 mimev = gdk_pixbuf_format_get_mime_types(format);
427 if (!mimev) return NULL;
429 /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
430 mime = g_strdup(mimev[0]);
436 void image_loader_set_area_ready_func(ImageLoader *il,
437 void (*func_area_ready)(ImageLoader *, guint, guint, guint, guint, gpointer),
438 gpointer data_area_ready)
442 il->func_area_ready = func_area_ready;
443 il->data_area_ready = data_area_ready;
446 void image_loader_set_error_func(ImageLoader *il,
447 void (*func_error)(ImageLoader *, gpointer),
452 il->func_error = func_error;
453 il->data_error = data_error;
456 void image_loader_set_done_func(ImageLoader *il,
457 void (*func_done)(ImageLoader *, gpointer),
462 il->func_done = func_done;
463 il->data_done = data_done;
466 void image_loader_set_percent_func(ImageLoader *il,
467 void (*func_percent)(ImageLoader *, gdouble, gpointer),
468 gpointer data_percent)
472 il->func_percent = func_percent;
473 il->data_percent = data_percent;
476 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
480 il->requested_width = width;
481 il->requested_height = height;
484 void image_loader_set_buffer_size(ImageLoader *il, guint count)
488 il->idle_read_loop_count = count ? count : 1;
491 void image_loader_set_priority(ImageLoader *il, gint priority)
495 il->idle_priority = priority;
498 gint image_loader_start(ImageLoader *il, void (*func_done)(ImageLoader *, gpointer), gpointer data_done)
500 if (!il) return FALSE;
502 if (!il->fd) return FALSE;
504 image_loader_set_done_func(il, func_done, data_done);
506 return image_loader_setup(il);
509 gdouble image_loader_get_percent(ImageLoader *il)
511 if (!il || il->bytes_total == 0) return 0.0;
513 return (gdouble)il->bytes_read / il->bytes_total;
516 gint image_loader_get_is_done(ImageLoader *il)
518 if (!il) return FALSE;
523 gint image_load_dimensions(FileData *fd, gint *width, gint *height)
528 il = image_loader_new(fd);
530 success = image_loader_start(il, NULL, NULL);
532 if (success && il->pixbuf)
534 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
535 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
539 if (width) *width = -1;
540 if (height) *height = -1;
543 image_loader_free(il);