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"
24 static const gchar *image_loader_path(ImageLoader *il)
31 static void image_loader_sync_pixbuf(ImageLoader *il)
35 if (!il->loader) return;
37 pb = gdk_pixbuf_loader_get_pixbuf(il->loader);
39 if (pb == il->pixbuf) return;
41 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
43 if (il->pixbuf) gdk_pixbuf_ref(il->pixbuf);
46 static void image_loader_area_updated_cb(GdkPixbufLoader *loader,
47 guint x, guint y, guint w, guint h,
50 ImageLoader *il = data;
52 if (il->func_area_ready)
56 image_loader_sync_pixbuf(il);
59 printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
62 il->func_area_ready(il, x, y, w, h, il->data_area_ready);
66 static void image_loader_area_prepared_cb(GdkPixbufLoader *loader, gpointer data)
72 pb = gdk_pixbuf_loader_get_pixbuf(loader);
74 h = gdk_pixbuf_get_height(pb);
75 rs = gdk_pixbuf_get_rowstride(pb);
76 pix = gdk_pixbuf_get_pixels(pb);
78 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
82 static void image_loader_size_cb(GdkPixbufLoader *loader,
83 gint width, gint height, gpointer data)
85 ImageLoader *il = data;
86 GdkPixbufFormat *format;
91 if (il->requested_width < 1 || il->requested_height < 1) return;
93 format = gdk_pixbuf_loader_get_format(loader);
96 mime_types = gdk_pixbuf_format_get_mime_types(format);
100 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
103 g_strfreev(mime_types);
107 if (width > il->requested_width || height > il->requested_height)
111 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
113 nw = il->requested_width;
114 nh = (gdouble)nw / width * height;
119 nh = il->requested_height;
120 nw = (gdouble)nh / height * width;
124 gdk_pixbuf_loader_set_size(loader, nw, nh);
129 static void image_loader_stop(ImageLoader *il)
133 if (il->idle_id != -1)
135 g_source_remove(il->idle_id);
141 /* some loaders do not have a pixbuf till close, order is important here */
142 gdk_pixbuf_loader_close(il->loader, NULL);
143 image_loader_sync_pixbuf(il);
144 g_object_unref(G_OBJECT(il->loader));
148 if (il->load_fd != -1)
157 static void image_loader_done(ImageLoader *il)
159 image_loader_stop(il);
161 if (il->func_done) il->func_done(il, il->data_done);
164 static gint image_loader_done_delay_cb(gpointer data)
166 ImageLoader *il = data;
168 il->idle_done_id = -1;
169 image_loader_done(il);
173 static void image_loader_done_delay(ImageLoader *il)
175 if (il->idle_done_id == -1) il->idle_done_id = g_idle_add_full(il->idle_priority,
176 image_loader_done_delay_cb, il, NULL);
179 static void image_loader_error(ImageLoader *il)
181 image_loader_stop(il);
183 DEBUG_1("pixbuf_loader reported load error for: %s", image_loader_path(il));
185 if (il->func_error) il->func_error(il, il->data_error);
188 static gint image_loader_idle_cb(gpointer data)
190 ImageLoader *il = data;
194 if (!il) return FALSE;
196 if (il->idle_id == -1) return FALSE;
198 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
201 b = read(il->load_fd, il->read_buffer, il->read_buffer_size);
205 image_loader_done(il);
209 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->read_buffer, b, NULL)))
211 image_loader_error(il);
220 if (il->func_percent && il->bytes_total > 0)
222 il->func_percent(il, (gdouble)il->bytes_read / il->bytes_total, il->data_percent);
228 static gint image_loader_begin(ImageLoader *il)
231 unsigned int offset = 0;
233 if (!il->loader || il->pixbuf) return FALSE;
235 b = read(il->load_fd, il->read_buffer, il->read_buffer_size);
238 format_raw_img_exif_offsets_fd(il->load_fd, image_loader_path(il), il->read_buffer, b, &offset, NULL))
240 DEBUG_1("Raw file %s contains embedded image", image_loader_path(il));
242 b = read(il->load_fd, il->read_buffer, il->read_buffer_size);
247 image_loader_stop(il);
251 if (!gdk_pixbuf_loader_write(il->loader, il->read_buffer, b, NULL))
253 image_loader_stop(il);
257 il->bytes_read += b + offset;
259 /* read until size is known */
260 while (il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0)
262 b = read(il->load_fd, il->read_buffer, il->read_buffer_size);
263 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->read_buffer, b, NULL)))
265 image_loader_stop(il);
270 if (!il->pixbuf) image_loader_sync_pixbuf(il);
272 if (il->bytes_read == il->bytes_total || b < 1)
274 /* done, handle (broken) loaders that do not have pixbuf till close */
275 image_loader_stop(il);
277 if (!il->pixbuf) return FALSE;
279 image_loader_done_delay(il);
285 image_loader_stop(il);
289 /* finally, progressive loading :) */
290 il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
295 static gint image_loader_setup(ImageLoader *il)
300 if (!il || il->load_fd != -1 || il->loader) return FALSE;
302 pathl = path_from_utf8(image_loader_path(il));
303 il->load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
305 if (il->load_fd == -1) return FALSE;
307 if (fstat(il->load_fd, &st) == 0)
309 il->bytes_total = st.st_size;
312 il->loader = gdk_pixbuf_loader_new();
313 g_signal_connect(G_OBJECT(il->loader), "area_updated",
314 G_CALLBACK(image_loader_area_updated_cb), il);
315 g_signal_connect(G_OBJECT(il->loader), "size_prepared",
316 G_CALLBACK(image_loader_size_cb), il);
317 g_signal_connect(G_OBJECT(il->loader), "area_prepared",
318 G_CALLBACK(image_loader_area_prepared_cb), il);
322 return image_loader_begin(il);
325 static ImageLoader *image_loader_new_real(FileData *fd, const gchar *path)
329 if (!fd && !path) return NULL;
331 il = g_new0(ImageLoader, 1);
332 if (fd) il->fd = file_data_ref(fd);
333 if (path) il->path = g_strdup(path);
336 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
344 il->idle_done_id = -1;
346 il->idle_read_loop_count = options->image.idle_read_loop_count;
347 il->read_buffer_size = options->image.read_buffer_size;
348 il->read_buffer = g_new(guchar, il->read_buffer_size);
350 il->requested_width = 0;
351 il->requested_height = 0;
353 DEBUG_1("new image loader %p, bufsize=%u idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
357 ImageLoader *image_loader_new(FileData *fd)
359 return image_loader_new_real(fd, NULL);
362 ImageLoader *image_loader_new_from_path(const gchar *path)
364 return image_loader_new_real(NULL, path);
367 void image_loader_free(ImageLoader *il)
371 image_loader_stop(il);
372 if (il->idle_done_id != -1) g_source_remove(il->idle_done_id);
373 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
374 if (il->fd) file_data_unref(il->fd);
375 if (il->path) g_free(il->path);
376 if (il->read_buffer) g_free(il->read_buffer);
377 DEBUG_1("freeing image loader %p bytes_read=%d", il, il->bytes_read);
381 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
382 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
384 if (!il) return NULL;
389 gchar *image_loader_get_format(ImageLoader *il)
391 GdkPixbufFormat *format;
395 if (!il || !il->loader) return NULL;
397 format = gdk_pixbuf_loader_get_format(il->loader);
398 if (!format) return NULL;
400 mimev = gdk_pixbuf_format_get_mime_types(format);
401 if (!mimev) return NULL;
403 /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
404 mime = g_strdup(mimev[0]);
410 void image_loader_set_area_ready_func(ImageLoader *il,
411 void (*func_area_ready)(ImageLoader *, guint, guint, guint, guint, gpointer),
412 gpointer data_area_ready)
416 il->func_area_ready = func_area_ready;
417 il->data_area_ready = data_area_ready;
420 void image_loader_set_error_func(ImageLoader *il,
421 void (*func_error)(ImageLoader *, gpointer),
426 il->func_error = func_error;
427 il->data_error = data_error;
430 void image_loader_set_percent_func(ImageLoader *il,
431 void (*func_percent)(ImageLoader *, gdouble, gpointer),
432 gpointer data_percent)
436 il->func_percent = func_percent;
437 il->data_percent = data_percent;
440 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
444 il->requested_width = width;
445 il->requested_height = height;
448 void image_loader_set_buffer_size(ImageLoader *il, guint count)
452 il->idle_read_loop_count = count ? count : 1;
455 void image_loader_set_priority(ImageLoader *il, gint priority)
459 il->idle_priority = priority;
462 gint image_loader_start(ImageLoader *il, void (*func_done)(ImageLoader *, gpointer), gpointer data_done)
464 if (!il) return FALSE;
466 if (!image_loader_path(il)) return FALSE;
468 il->func_done = func_done;
469 il->data_done = data_done;
471 return image_loader_setup(il);
474 gdouble image_loader_get_percent(ImageLoader *il)
476 if (!il || il->bytes_total == 0) return 0.0;
478 return (gdouble)il->bytes_read / il->bytes_total;
481 gint image_loader_get_is_done(ImageLoader *il)
483 if (!il) return FALSE;
488 gint image_load_dimensions(FileData *fd, gint *width, gint *height)
493 il = image_loader_new(fd);
495 success = image_loader_start(il, NULL, NULL);
497 if (success && il->pixbuf)
499 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
500 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
504 if (width) *width = -1;
505 if (height) *height = -1;
508 image_loader_free(il);