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"
17 #include "format_raw.h"
18 #include "ui_fileops.h"
23 /* bytes to read from file per read() */
24 #define IMAGE_LOADER_BUFFER_SIZE 512
26 /* the number of bytes to read per idle call (define x IMAGE_LOADER_BUFFER_SIZE) */
27 #define IMAGE_LOADER_BUFFER_DEFAULT_COUNT 1
29 static const gchar *image_loader_path(ImageLoader *il)
36 static void image_loader_sync_pixbuf(ImageLoader *il)
40 if (!il->loader) return;
42 pb = gdk_pixbuf_loader_get_pixbuf(il->loader);
44 if (pb == il->pixbuf) return;
46 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
48 if (il->pixbuf) gdk_pixbuf_ref(il->pixbuf);
51 static void image_loader_area_cb(GdkPixbufLoader *loader,
52 guint x, guint y, guint w, guint h,
55 ImageLoader *il = data;
57 if (il->func_area_ready)
61 image_loader_sync_pixbuf(il);
64 printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
67 il->func_area_ready(il, x, y, w, h, il->data_area_ready);
71 static void image_loader_size_cb(GdkPixbufLoader *loader,
72 gint width, gint height, gpointer data)
74 ImageLoader *il = data;
75 GdkPixbufFormat *format;
80 if (il->requested_width < 1 || il->requested_height < 1) return;
82 format = gdk_pixbuf_loader_get_format(loader);
85 mime_types = gdk_pixbuf_format_get_mime_types(format);
89 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
92 g_strfreev(mime_types);
96 if (width > il->requested_width || height > il->requested_height)
100 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
102 nw = il->requested_width;
103 nh = (gdouble)nw / width * height;
108 nh = il->requested_height;
109 nw = (gdouble)nh / height * width;
113 gdk_pixbuf_loader_set_size(loader, nw, nh);
118 static void image_loader_stop(ImageLoader *il)
122 if (il->idle_id != -1)
124 g_source_remove(il->idle_id);
130 /* some loaders do not have a pixbuf till close, order is important here */
131 gdk_pixbuf_loader_close(il->loader, NULL);
132 image_loader_sync_pixbuf(il);
133 g_object_unref(G_OBJECT(il->loader));
137 if (il->load_fd != -1)
146 static void image_loader_done(ImageLoader *il)
148 image_loader_stop(il);
150 if (il->func_done) il->func_done(il, il->data_done);
153 static gint image_loader_done_delay_cb(gpointer data)
155 ImageLoader *il = data;
157 il->idle_done_id = -1;
158 image_loader_done(il);
162 static void image_loader_done_delay(ImageLoader *il)
164 if (il->idle_done_id == -1) il->idle_done_id = g_idle_add_full(il->idle_priority,
165 image_loader_done_delay_cb, il, NULL);
168 static void image_loader_error(ImageLoader *il)
170 image_loader_stop(il);
172 if (debug) printf("pixbuf_loader reported load error for: %s\n", image_loader_path(il));
174 if (il->func_error) il->func_error(il, il->data_error);
177 static gint image_loader_idle_cb(gpointer data)
179 ImageLoader *il = data;
180 guchar buf[IMAGE_LOADER_BUFFER_SIZE];
184 if (!il) return FALSE;
186 if (il->idle_id == -1) return FALSE;
188 c = il->buffer_size ? il->buffer_size : 1;
191 b = read(il->load_fd, &buf, sizeof(buf));
195 image_loader_done(il);
199 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, buf, b, NULL)))
201 image_loader_error(il);
210 if (il->func_percent && il->bytes_total > 0)
212 il->func_percent(il, (gdouble)il->bytes_read / il->bytes_total, il->data_percent);
218 static gint image_loader_begin(ImageLoader *il)
220 guchar buf[IMAGE_LOADER_BUFFER_SIZE];
222 unsigned int offset = 0;
224 if (!il->loader || il->pixbuf) return FALSE;
226 b = read(il->load_fd, &buf, sizeof(buf));
229 format_raw_img_exif_offsets_fd(il->load_fd, image_loader_path(il), buf, b, &offset, NULL))
231 if (debug) printf("Raw file %s contains embedded image\n", image_loader_path(il));
233 b = read(il->load_fd, &buf, sizeof(buf));
238 image_loader_stop(il);
242 if (!gdk_pixbuf_loader_write(il->loader, buf, b, NULL))
244 image_loader_stop(il);
248 il->bytes_read += b + offset;
250 /* read until size is known */
251 while (il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0)
253 b = read(il->load_fd, &buf, sizeof(buf));
254 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, buf, b, NULL)))
256 image_loader_stop(il);
261 if (!il->pixbuf) image_loader_sync_pixbuf(il);
263 if (il->bytes_read == il->bytes_total || b < 1)
265 /* done, handle (broken) loaders that do not have pixbuf till close */
266 image_loader_stop(il);
268 if (!il->pixbuf) return FALSE;
270 image_loader_done_delay(il);
276 image_loader_stop(il);
280 /* finally, progressive loading :) */
281 il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
286 static gint image_loader_setup(ImageLoader *il)
291 if (!il || il->load_fd != -1 || il->loader) return FALSE;
293 pathl = path_from_utf8(image_loader_path(il));
294 il->load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
296 if (il->load_fd == -1) return FALSE;
298 if (fstat(il->load_fd, &st) == 0)
300 il->bytes_total = st.st_size;
303 il->loader = gdk_pixbuf_loader_new();
304 g_signal_connect(G_OBJECT(il->loader), "area_updated",
305 G_CALLBACK(image_loader_area_cb), il);
306 g_signal_connect(G_OBJECT(il->loader), "size_prepared",
307 G_CALLBACK(image_loader_size_cb), il);
311 return image_loader_begin(il);
314 static ImageLoader *image_loader_new_real(FileData *fd, const gchar *path)
318 if (!fd && !path) return NULL;
320 il = g_new0(ImageLoader, 1);
321 if (fd) il->fd = file_data_ref(fd);
322 if (path) il->path = g_strdup(path);
325 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
333 il->idle_done_id = -1;
335 il->buffer_size = IMAGE_LOADER_BUFFER_DEFAULT_COUNT;
337 il->requested_width = 0;
338 il->requested_height = 0;
344 ImageLoader *image_loader_new(FileData *fd)
346 return image_loader_new_real(fd, NULL);
349 ImageLoader *image_loader_new_from_path(const gchar *path)
351 return image_loader_new_real(NULL, path);
354 void image_loader_free(ImageLoader *il)
358 image_loader_stop(il);
359 if (il->idle_done_id != -1) g_source_remove(il->idle_done_id);
360 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
361 if (il->fd) file_data_unref(il->fd);
362 if (il->path) g_free(il->path);
366 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
367 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
369 if (!il) return NULL;
374 gchar *image_loader_get_format(ImageLoader *il)
376 GdkPixbufFormat *format;
380 if (!il || !il->loader) return NULL;
382 format = gdk_pixbuf_loader_get_format(il->loader);
383 if (!format) return NULL;
385 mimev = gdk_pixbuf_format_get_mime_types(format);
386 if (!mimev) return NULL;
388 /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
389 mime = g_strdup(mimev[0]);
395 void image_loader_set_area_ready_func(ImageLoader *il,
396 void (*func_area_ready)(ImageLoader *, guint, guint, guint, guint, gpointer),
397 gpointer data_area_ready)
401 il->func_area_ready = func_area_ready;
402 il->data_area_ready = data_area_ready;
405 void image_loader_set_error_func(ImageLoader *il,
406 void (*func_error)(ImageLoader *, gpointer),
411 il->func_error = func_error;
412 il->data_error = data_error;
415 void image_loader_set_percent_func(ImageLoader *il,
416 void (*func_percent)(ImageLoader *, gdouble, gpointer),
417 gpointer data_percent)
421 il->func_percent = func_percent;
422 il->data_percent = data_percent;
425 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
429 il->requested_width = width;
430 il->requested_height = height;
433 void image_loader_set_buffer_size(ImageLoader *il, guint size)
437 il->buffer_size = size ? size : 1;
440 void image_loader_set_priority(ImageLoader *il, gint priority)
444 il->idle_priority = priority;
447 gint image_loader_start(ImageLoader *il, void (*func_done)(ImageLoader *, gpointer), gpointer data_done)
449 if (!il) return FALSE;
451 if (!image_loader_path(il)) return FALSE;
453 il->func_done = func_done;
454 il->data_done = data_done;
456 return image_loader_setup(il);
459 gdouble image_loader_get_percent(ImageLoader *il)
461 if (!il || il->bytes_total == 0) return 0.0;
463 return (gdouble)il->bytes_read / il->bytes_total;
466 gint image_loader_get_is_done(ImageLoader *il)
468 if (!il) return FALSE;
473 gint image_load_dimensions(FileData *fd, gint *width, gint *height)
478 il = image_loader_new(fd);
480 success = image_loader_start(il, NULL, NULL);
482 if (success && il->pixbuf)
484 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
485 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
489 if (width) *width = -1;
490 if (height) *height = -1;
493 image_loader_free(il);