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 const gchar *image_loader_path(ImageLoader *il)
32 static void image_loader_sync_pixbuf(ImageLoader *il)
36 if (!il->loader) return;
38 pb = gdk_pixbuf_loader_get_pixbuf(il->loader);
40 if (pb == il->pixbuf) return;
42 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
44 if (il->pixbuf) gdk_pixbuf_ref(il->pixbuf);
47 static void image_loader_area_updated_cb(GdkPixbufLoader *loader,
48 guint x, guint y, guint w, guint h,
51 ImageLoader *il = data;
53 if (il->func_area_ready)
57 image_loader_sync_pixbuf(il);
60 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
63 il->func_area_ready(il, x, y, w, h, il->data_area_ready);
67 static void image_loader_area_prepared_cb(GdkPixbufLoader *loader, gpointer data)
73 /* a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669 */
74 gchar *format = gdk_pixbuf_format_get_name(gdk_pixbuf_loader_get_format(loader));
75 if (strcmp(format, "svg") == 0)
83 pb = gdk_pixbuf_loader_get_pixbuf(loader);
85 h = gdk_pixbuf_get_height(pb);
86 rs = gdk_pixbuf_get_rowstride(pb);
87 pix = gdk_pixbuf_get_pixels(pb);
89 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
93 static void image_loader_size_cb(GdkPixbufLoader *loader,
94 gint width, gint height, gpointer data)
96 ImageLoader *il = data;
97 GdkPixbufFormat *format;
102 if (il->requested_width < 1 || il->requested_height < 1) return;
104 format = gdk_pixbuf_loader_get_format(loader);
107 mime_types = gdk_pixbuf_format_get_mime_types(format);
109 while (mime_types[n])
111 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
114 g_strfreev(mime_types);
118 if (width > il->requested_width || height > il->requested_height)
122 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
124 nw = il->requested_width;
125 nh = (gdouble)nw / width * height;
130 nh = il->requested_height;
131 nw = (gdouble)nh / height * width;
135 gdk_pixbuf_loader_set_size(loader, nw, nh);
140 static void image_loader_stop(ImageLoader *il)
144 if (il->idle_id != -1)
146 g_source_remove(il->idle_id);
152 /* some loaders do not have a pixbuf till close, order is important here */
153 gdk_pixbuf_loader_close(il->loader, NULL);
154 image_loader_sync_pixbuf(il);
155 g_object_unref(G_OBJECT(il->loader));
163 exif_free_preview(il->mapped_file);
167 munmap(il->mapped_file, il->bytes_total);
169 il->mapped_file = NULL;
175 static void image_loader_done(ImageLoader *il)
177 image_loader_stop(il);
179 if (il->func_done) il->func_done(il, il->data_done);
182 static gint image_loader_done_delay_cb(gpointer data)
184 ImageLoader *il = data;
186 il->idle_done_id = -1;
187 image_loader_done(il);
191 static void image_loader_done_delay(ImageLoader *il)
193 if (il->idle_done_id == -1) il->idle_done_id = g_idle_add_full(il->idle_priority,
194 image_loader_done_delay_cb, il, NULL);
197 static void image_loader_error(ImageLoader *il)
199 image_loader_stop(il);
201 DEBUG_1("pixbuf_loader reported load error for: %s", image_loader_path(il));
203 if (il->func_error) il->func_error(il, il->data_error);
206 static gint image_loader_idle_cb(gpointer data)
208 ImageLoader *il = data;
212 if (!il) return FALSE;
214 if (il->idle_id == -1) return FALSE;
216 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
219 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
223 image_loader_done(il);
227 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)))
229 image_loader_error(il);
238 if (il->func_percent && il->bytes_total > 0)
240 il->func_percent(il, (gdouble)il->bytes_read / il->bytes_total, il->data_percent);
246 static gint image_loader_begin(ImageLoader *il)
250 if (!il->loader || il->pixbuf) return FALSE;
252 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
256 image_loader_stop(il);
260 if (!gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL))
262 image_loader_stop(il);
268 /* read until size is known */
269 while (il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0)
271 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
272 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)))
274 image_loader_stop(il);
279 if (!il->pixbuf) image_loader_sync_pixbuf(il);
281 if (il->bytes_read == il->bytes_total || b < 1)
283 /* done, handle (broken) loaders that do not have pixbuf till close */
284 image_loader_stop(il);
286 if (!il->pixbuf) return FALSE;
288 image_loader_done_delay(il);
294 image_loader_stop(il);
298 /* finally, progressive loading :) */
299 il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
304 static gint image_loader_setup(ImageLoader *il)
309 if (!il || il->loader || il->mapped_file) return FALSE;
311 il->mapped_file = NULL;
315 ExifData *exif = exif_read_fd(il->fd);
317 il->mapped_file = exif_get_preview(exif, &il->bytes_total);
322 DEBUG_1("Raw file %s contains embedded image", image_loader_path(il));
324 exif_free_fd(il->fd, exif);
328 if (!il->mapped_file)
333 pathl = path_from_utf8(image_loader_path(il));
334 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
336 if (load_fd == -1) return FALSE;
338 if (fstat(load_fd, &st) == 0)
340 il->bytes_total = st.st_size;
348 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
350 if (il->mapped_file == MAP_FAILED)
359 il->loader = gdk_pixbuf_loader_new();
360 g_signal_connect(G_OBJECT(il->loader), "area_updated",
361 G_CALLBACK(image_loader_area_updated_cb), il);
362 g_signal_connect(G_OBJECT(il->loader), "size_prepared",
363 G_CALLBACK(image_loader_size_cb), il);
364 g_signal_connect(G_OBJECT(il->loader), "area_prepared",
365 G_CALLBACK(image_loader_area_prepared_cb), il);
369 return image_loader_begin(il);
372 static ImageLoader *image_loader_new_real(FileData *fd, const gchar *path)
376 if (!fd && !path) return NULL;
378 il = g_new0(ImageLoader, 1);
379 if (fd) il->fd = file_data_ref(fd);
380 if (path) il->path = g_strdup(path);
383 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
390 il->idle_done_id = -1;
392 il->idle_read_loop_count = options->image.idle_read_loop_count;
393 il->read_buffer_size = options->image.read_buffer_size;
394 il->mapped_file = NULL;
396 il->requested_width = 0;
397 il->requested_height = 0;
399 DEBUG_1("new image loader %p, bufsize=%u idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
403 ImageLoader *image_loader_new(FileData *fd)
405 return image_loader_new_real(fd, NULL);
408 ImageLoader *image_loader_new_from_path(const gchar *path)
410 return image_loader_new_real(NULL, path);
413 void image_loader_free(ImageLoader *il)
417 image_loader_stop(il);
418 if (il->idle_done_id != -1) g_source_remove(il->idle_done_id);
419 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
420 if (il->fd) file_data_unref(il->fd);
421 if (il->path) g_free(il->path);
422 DEBUG_1("freeing image loader %p bytes_read=%d", il, il->bytes_read);
426 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
427 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
429 if (!il) return NULL;
434 gchar *image_loader_get_format(ImageLoader *il)
436 GdkPixbufFormat *format;
440 if (!il || !il->loader) return NULL;
442 format = gdk_pixbuf_loader_get_format(il->loader);
443 if (!format) return NULL;
445 mimev = gdk_pixbuf_format_get_mime_types(format);
446 if (!mimev) return NULL;
448 /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
449 mime = g_strdup(mimev[0]);
455 void image_loader_set_area_ready_func(ImageLoader *il,
456 void (*func_area_ready)(ImageLoader *, guint, guint, guint, guint, gpointer),
457 gpointer data_area_ready)
461 il->func_area_ready = func_area_ready;
462 il->data_area_ready = data_area_ready;
465 void image_loader_set_error_func(ImageLoader *il,
466 void (*func_error)(ImageLoader *, gpointer),
471 il->func_error = func_error;
472 il->data_error = data_error;
475 void image_loader_set_done_func(ImageLoader *il,
476 void (*func_done)(ImageLoader *, gpointer),
481 il->func_done = func_done;
482 il->data_done = data_done;
485 void image_loader_set_percent_func(ImageLoader *il,
486 void (*func_percent)(ImageLoader *, gdouble, gpointer),
487 gpointer data_percent)
491 il->func_percent = func_percent;
492 il->data_percent = data_percent;
495 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
499 il->requested_width = width;
500 il->requested_height = height;
503 void image_loader_set_buffer_size(ImageLoader *il, guint count)
507 il->idle_read_loop_count = count ? count : 1;
510 void image_loader_set_priority(ImageLoader *il, gint priority)
514 il->idle_priority = priority;
517 gint image_loader_start(ImageLoader *il, void (*func_done)(ImageLoader *, gpointer), gpointer data_done)
519 if (!il) return FALSE;
521 if (!image_loader_path(il)) return FALSE;
523 image_loader_set_done_func(il, func_done, data_done);
525 return image_loader_setup(il);
528 gdouble image_loader_get_percent(ImageLoader *il)
530 if (!il || il->bytes_total == 0) return 0.0;
532 return (gdouble)il->bytes_read / il->bytes_total;
535 gint image_loader_get_is_done(ImageLoader *il)
537 if (!il) return FALSE;
542 gint image_load_dimensions(FileData *fd, gint *width, gint *height)
547 il = image_loader_new(fd);
549 success = image_loader_start(il, NULL, NULL);
551 if (success && il->pixbuf)
553 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
554 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
558 if (width) *width = -1;
559 if (height) *height = -1;
562 image_loader_free(il);