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"
20 #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 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 pb = gdk_pixbuf_loader_get_pixbuf(loader);
75 h = gdk_pixbuf_get_height(pb);
76 rs = gdk_pixbuf_get_rowstride(pb);
77 pix = gdk_pixbuf_get_pixels(pb);
79 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
83 static void image_loader_size_cb(GdkPixbufLoader *loader,
84 gint width, gint height, gpointer data)
86 ImageLoader *il = data;
87 GdkPixbufFormat *format;
92 if (il->requested_width < 1 || il->requested_height < 1) return;
94 format = gdk_pixbuf_loader_get_format(loader);
97 mime_types = gdk_pixbuf_format_get_mime_types(format);
101 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
104 g_strfreev(mime_types);
108 if (width > il->requested_width || height > il->requested_height)
112 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
114 nw = il->requested_width;
115 nh = (gdouble)nw / width * height;
120 nh = il->requested_height;
121 nw = (gdouble)nh / height * width;
125 gdk_pixbuf_loader_set_size(loader, nw, nh);
130 static void image_loader_stop(ImageLoader *il)
134 if (il->idle_id != -1)
136 g_source_remove(il->idle_id);
142 /* some loaders do not have a pixbuf till close, order is important here */
143 gdk_pixbuf_loader_close(il->loader, NULL);
144 image_loader_sync_pixbuf(il);
145 g_object_unref(G_OBJECT(il->loader));
149 if (il->load_fd != -1)
158 static void image_loader_done(ImageLoader *il)
160 image_loader_stop(il);
162 if (il->func_done) il->func_done(il, il->data_done);
165 static gint image_loader_done_delay_cb(gpointer data)
167 ImageLoader *il = data;
169 il->idle_done_id = -1;
170 image_loader_done(il);
174 static void image_loader_done_delay(ImageLoader *il)
176 if (il->idle_done_id == -1) il->idle_done_id = g_idle_add_full(il->idle_priority,
177 image_loader_done_delay_cb, il, NULL);
180 static void image_loader_error(ImageLoader *il)
182 image_loader_stop(il);
184 DEBUG_1("pixbuf_loader reported load error for: %s", image_loader_path(il));
186 if (il->func_error) il->func_error(il, il->data_error);
189 static gint image_loader_idle_cb(gpointer data)
191 ImageLoader *il = data;
195 if (!il) return FALSE;
197 if (il->idle_id == -1) return FALSE;
199 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
202 b = read(il->load_fd, il->read_buffer, il->read_buffer_size);
206 image_loader_done(il);
210 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->read_buffer, b, NULL)))
212 image_loader_error(il);
221 if (il->func_percent && il->bytes_total > 0)
223 il->func_percent(il, (gdouble)il->bytes_read / il->bytes_total, il->data_percent);
229 static gint image_loader_begin(ImageLoader *il)
232 unsigned int offset = 0;
234 if (!il->loader || il->pixbuf) return FALSE;
236 b = read(il->load_fd, il->read_buffer, il->read_buffer_size);
239 format_raw_img_exif_offsets_fd(il->load_fd, image_loader_path(il), il->read_buffer, b, &offset, NULL))
241 DEBUG_1("Raw file %s contains embedded image", image_loader_path(il));
243 b = read(il->load_fd, il->read_buffer, il->read_buffer_size);
248 image_loader_stop(il);
252 if (!gdk_pixbuf_loader_write(il->loader, il->read_buffer, b, NULL))
254 image_loader_stop(il);
258 il->bytes_read += b + offset;
260 /* read until size is known */
261 while (il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0)
263 b = read(il->load_fd, il->read_buffer, il->read_buffer_size);
264 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->read_buffer, b, NULL)))
266 image_loader_stop(il);
271 if (!il->pixbuf) image_loader_sync_pixbuf(il);
273 if (il->bytes_read == il->bytes_total || b < 1)
275 /* done, handle (broken) loaders that do not have pixbuf till close */
276 image_loader_stop(il);
278 if (!il->pixbuf) return FALSE;
280 image_loader_done_delay(il);
286 image_loader_stop(il);
290 /* finally, progressive loading :) */
291 il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
296 static gint image_loader_setup(ImageLoader *il)
301 if (!il || il->load_fd != -1 || il->loader) return FALSE;
303 pathl = path_from_utf8(image_loader_path(il));
304 il->load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
306 if (il->load_fd == -1) return FALSE;
308 if (fstat(il->load_fd, &st) == 0)
310 il->bytes_total = st.st_size;
313 il->loader = gdk_pixbuf_loader_new();
314 g_signal_connect(G_OBJECT(il->loader), "area_updated",
315 G_CALLBACK(image_loader_area_updated_cb), il);
316 g_signal_connect(G_OBJECT(il->loader), "size_prepared",
317 G_CALLBACK(image_loader_size_cb), il);
318 g_signal_connect(G_OBJECT(il->loader), "area_prepared",
319 G_CALLBACK(image_loader_area_prepared_cb), il);
323 return image_loader_begin(il);
326 static ImageLoader *image_loader_new_real(FileData *fd, const gchar *path)
330 if (!fd && !path) return NULL;
332 il = g_new0(ImageLoader, 1);
333 if (fd) il->fd = file_data_ref(fd);
334 if (path) il->path = g_strdup(path);
337 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
345 il->idle_done_id = -1;
347 il->idle_read_loop_count = options->image.idle_read_loop_count;
348 il->read_buffer_size = options->image.read_buffer_size;
349 il->read_buffer = g_new(guchar, il->read_buffer_size);
351 il->requested_width = 0;
352 il->requested_height = 0;
354 DEBUG_1("new image loader %p, bufsize=%u idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
358 ImageLoader *image_loader_new(FileData *fd)
360 return image_loader_new_real(fd, NULL);
363 ImageLoader *image_loader_new_from_path(const gchar *path)
365 return image_loader_new_real(NULL, path);
368 void image_loader_free(ImageLoader *il)
372 image_loader_stop(il);
373 if (il->idle_done_id != -1) g_source_remove(il->idle_done_id);
374 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
375 if (il->fd) file_data_unref(il->fd);
376 if (il->path) g_free(il->path);
377 if (il->read_buffer) g_free(il->read_buffer);
378 DEBUG_1("freeing image loader %p bytes_read=%d", il, il->bytes_read);
382 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
383 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
385 if (!il) return NULL;
390 gchar *image_loader_get_format(ImageLoader *il)
392 GdkPixbufFormat *format;
396 if (!il || !il->loader) return NULL;
398 format = gdk_pixbuf_loader_get_format(il->loader);
399 if (!format) return NULL;
401 mimev = gdk_pixbuf_format_get_mime_types(format);
402 if (!mimev) return NULL;
404 /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
405 mime = g_strdup(mimev[0]);
411 void image_loader_set_area_ready_func(ImageLoader *il,
412 void (*func_area_ready)(ImageLoader *, guint, guint, guint, guint, gpointer),
413 gpointer data_area_ready)
417 il->func_area_ready = func_area_ready;
418 il->data_area_ready = data_area_ready;
421 void image_loader_set_error_func(ImageLoader *il,
422 void (*func_error)(ImageLoader *, gpointer),
427 il->func_error = func_error;
428 il->data_error = data_error;
431 void image_loader_set_percent_func(ImageLoader *il,
432 void (*func_percent)(ImageLoader *, gdouble, gpointer),
433 gpointer data_percent)
437 il->func_percent = func_percent;
438 il->data_percent = data_percent;
441 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
445 il->requested_width = width;
446 il->requested_height = height;
449 void image_loader_set_buffer_size(ImageLoader *il, guint count)
453 il->idle_read_loop_count = count ? count : 1;
456 void image_loader_set_priority(ImageLoader *il, gint priority)
460 il->idle_priority = priority;
463 gint image_loader_start(ImageLoader *il, void (*func_done)(ImageLoader *, gpointer), gpointer data_done)
465 if (!il) return FALSE;
467 if (!image_loader_path(il)) return FALSE;
469 il->func_done = func_done;
470 il->data_done = data_done;
472 return image_loader_setup(il);
475 gdouble image_loader_get_percent(ImageLoader *il)
477 if (!il || il->bytes_total == 0) return 0.0;
479 return (gdouble)il->bytes_read / il->bytes_total;
482 gint image_loader_get_is_done(ImageLoader *il)
484 if (!il) return FALSE;
489 gint image_load_dimensions(FileData *fd, gint *width, gint *height)
494 il = image_loader_new(fd);
496 success = image_loader_start(il, NULL, NULL);
498 if (success && il->pixbuf)
500 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
501 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
505 if (width) *width = -1;
506 if (height) *height = -1;
509 image_loader_free(il);