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"
18 #include "ui_fileops.h"
23 static const gchar *image_loader_path(ImageLoader *il)
30 static void image_loader_sync_pixbuf(ImageLoader *il)
34 if (!il->loader) return;
36 pb = gdk_pixbuf_loader_get_pixbuf(il->loader);
38 if (pb == il->pixbuf) return;
40 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
42 if (il->pixbuf) gdk_pixbuf_ref(il->pixbuf);
45 static void image_loader_area_cb(GdkPixbufLoader *loader,
46 guint x, guint y, guint w, guint h,
49 ImageLoader *il = data;
51 if (il->func_area_ready)
55 image_loader_sync_pixbuf(il);
58 printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
61 il->func_area_ready(il, x, y, w, h, il->data_area_ready);
65 static void image_loader_size_cb(GdkPixbufLoader *loader,
66 gint width, gint height, gpointer data)
68 ImageLoader *il = data;
69 GdkPixbufFormat *format;
74 if (il->requested_width < 1 || il->requested_height < 1) return;
76 format = gdk_pixbuf_loader_get_format(loader);
79 mime_types = gdk_pixbuf_format_get_mime_types(format);
83 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
86 g_strfreev(mime_types);
90 if (width > il->requested_width || height > il->requested_height)
94 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
96 nw = il->requested_width;
97 nh = (gdouble)nw / width * height;
102 nh = il->requested_height;
103 nw = (gdouble)nh / height * width;
107 gdk_pixbuf_loader_set_size(loader, nw, nh);
112 static void image_loader_stop(ImageLoader *il)
116 if (il->idle_id != -1)
118 g_source_remove(il->idle_id);
124 /* some loaders do not have a pixbuf till close, order is important here */
125 gdk_pixbuf_loader_close(il->loader, NULL);
126 image_loader_sync_pixbuf(il);
127 g_object_unref(G_OBJECT(il->loader));
131 if (il->load_fd != -1)
140 static void image_loader_done(ImageLoader *il)
142 image_loader_stop(il);
144 if (il->func_done) il->func_done(il, il->data_done);
147 static gint image_loader_done_delay_cb(gpointer data)
149 ImageLoader *il = data;
151 il->idle_done_id = -1;
152 image_loader_done(il);
156 static void image_loader_done_delay(ImageLoader *il)
158 if (il->idle_done_id == -1) il->idle_done_id = g_idle_add_full(il->idle_priority,
159 image_loader_done_delay_cb, il, NULL);
162 static void image_loader_error(ImageLoader *il)
164 image_loader_stop(il);
166 if (debug) printf("pixbuf_loader reported load error for: %s\n", image_loader_path(il));
168 if (il->func_error) il->func_error(il, il->data_error);
171 static gint image_loader_idle_cb(gpointer data)
173 ImageLoader *il = data;
177 if (!il) return FALSE;
179 if (il->idle_id == -1) return FALSE;
181 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
184 b = read(il->load_fd, il->read_buffer, il->read_buffer_size);
188 image_loader_done(il);
192 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->read_buffer, b, NULL)))
194 image_loader_error(il);
203 if (il->func_percent && il->bytes_total > 0)
205 il->func_percent(il, (gdouble)il->bytes_read / il->bytes_total, il->data_percent);
211 static gint image_loader_begin(ImageLoader *il)
214 unsigned int offset = 0;
216 if (!il->loader || il->pixbuf) return FALSE;
218 b = read(il->load_fd, il->read_buffer, il->read_buffer_size);
221 format_raw_img_exif_offsets_fd(il->load_fd, image_loader_path(il), il->read_buffer, b, &offset, NULL))
223 if (debug) printf("Raw file %s contains embedded image\n", image_loader_path(il));
225 b = read(il->load_fd, il->read_buffer, il->read_buffer_size);
230 image_loader_stop(il);
234 if (!gdk_pixbuf_loader_write(il->loader, il->read_buffer, 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, il->read_buffer, il->read_buffer_size);
246 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->read_buffer, 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(image_loader_path(il));
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 static ImageLoader *image_loader_new_real(FileData *fd, const gchar *path)
310 if (!fd && !path) return NULL;
312 il = g_new0(ImageLoader, 1);
313 if (fd) il->fd = file_data_ref(fd);
314 if (path) il->path = g_strdup(path);
317 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
325 il->idle_done_id = -1;
327 il->idle_read_loop_count = options->image.idle_read_loop_count;
328 il->read_buffer_size = options->image.read_buffer_size;
329 il->read_buffer = g_new(guchar, il->read_buffer_size);
331 il->requested_width = 0;
332 il->requested_height = 0;
334 if (debug) printf("new image loader %p, bufsize=%u idle_loop=%u\n", il, il->read_buffer_size, il->idle_read_loop_count);
338 ImageLoader *image_loader_new(FileData *fd)
340 return image_loader_new_real(fd, NULL);
343 ImageLoader *image_loader_new_from_path(const gchar *path)
345 return image_loader_new_real(NULL, path);
348 void image_loader_free(ImageLoader *il)
352 image_loader_stop(il);
353 if (il->idle_done_id != -1) g_source_remove(il->idle_done_id);
354 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
355 if (il->fd) file_data_unref(il->fd);
356 if (il->path) g_free(il->path);
357 if (il->read_buffer) g_free(il->read_buffer);
358 if (debug) printf("freeing image loader %p bytes_read=%d\n", il, il->bytes_read);
362 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
363 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
365 if (!il) return NULL;
370 gchar *image_loader_get_format(ImageLoader *il)
372 GdkPixbufFormat *format;
376 if (!il || !il->loader) return NULL;
378 format = gdk_pixbuf_loader_get_format(il->loader);
379 if (!format) return NULL;
381 mimev = gdk_pixbuf_format_get_mime_types(format);
382 if (!mimev) return NULL;
384 /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
385 mime = g_strdup(mimev[0]);
391 void image_loader_set_area_ready_func(ImageLoader *il,
392 void (*func_area_ready)(ImageLoader *, guint, guint, guint, guint, gpointer),
393 gpointer data_area_ready)
397 il->func_area_ready = func_area_ready;
398 il->data_area_ready = data_area_ready;
401 void image_loader_set_error_func(ImageLoader *il,
402 void (*func_error)(ImageLoader *, gpointer),
407 il->func_error = func_error;
408 il->data_error = data_error;
411 void image_loader_set_percent_func(ImageLoader *il,
412 void (*func_percent)(ImageLoader *, gdouble, gpointer),
413 gpointer data_percent)
417 il->func_percent = func_percent;
418 il->data_percent = data_percent;
421 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
425 il->requested_width = width;
426 il->requested_height = height;
429 void image_loader_set_buffer_size(ImageLoader *il, guint count)
433 il->idle_read_loop_count = count ? count : 1;
436 void image_loader_set_priority(ImageLoader *il, gint priority)
440 il->idle_priority = priority;
443 gint image_loader_start(ImageLoader *il, void (*func_done)(ImageLoader *, gpointer), gpointer data_done)
445 if (!il) return FALSE;
447 if (!image_loader_path(il)) return FALSE;
449 il->func_done = func_done;
450 il->data_done = data_done;
452 return image_loader_setup(il);
455 gdouble image_loader_get_percent(ImageLoader *il)
457 if (!il || il->bytes_total == 0) return 0.0;
459 return (gdouble)il->bytes_read / il->bytes_total;
462 gint image_loader_get_is_done(ImageLoader *il)
464 if (!il) return FALSE;
469 gint image_load_dimensions(FileData *fd, gint *width, gint *height)
474 il = image_loader_new(fd);
476 success = image_loader_start(il, NULL, NULL);
478 if (success && il->pixbuf)
480 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
481 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
485 if (width) *width = -1;
486 if (height) *height = -1;
489 image_loader_free(il);