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"
16 #include "ui_fileops.h"
21 /* bytes to read from file per read() */
22 #define IMAGE_LOADER_BUFFER_SIZE 512
24 /* the number of bytes to read per idle call (define x IMAGE_LOADER_BUFFER_SIZE) */
25 #define IMAGE_LOADER_BUFFER_DEFAULT_COUNT 1
27 static void image_loader_sync_pixbuf(ImageLoader *il)
31 if (!il->loader) return;
33 pb = gdk_pixbuf_loader_get_pixbuf(il->loader);
35 if (pb == il->pixbuf) return;
37 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
39 if (il->pixbuf) gdk_pixbuf_ref(il->pixbuf);
42 static void image_loader_area_cb(GdkPixbufLoader *loader,
43 guint x, guint y, guint w, guint h,
46 ImageLoader *il = data;
48 if (il->func_area_ready)
52 image_loader_sync_pixbuf(il);
55 printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
58 il->func_area_ready(il, x, y, w, h, il->data_area_ready);
62 static void image_loader_size_cb(GdkPixbufLoader *loader,
63 gint width, gint height, gpointer data)
65 ImageLoader *il = data;
66 GdkPixbufFormat *format;
71 if (il->requested_width < 1 || il->requested_height < 1) return;
73 format = gdk_pixbuf_loader_get_format(loader);
76 mime_types = gdk_pixbuf_format_get_mime_types(format);
80 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
83 g_strfreev(mime_types);
87 if (width > il->requested_width || height > il->requested_height)
91 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
93 nw = il->requested_width;
94 nh = (gdouble)nw / width * height;
99 nh = il->requested_height;
100 nw = (gdouble)nh / height * width;
104 gdk_pixbuf_loader_set_size(loader, nw, nh);
108 static void image_loader_stop(ImageLoader *il)
112 if (il->idle_id != -1)
114 g_source_remove(il->idle_id);
120 /* some loaders do not have a pixbuf till close, order is important here */
121 gdk_pixbuf_loader_close(il->loader, NULL);
122 image_loader_sync_pixbuf(il);
123 g_object_unref(G_OBJECT(il->loader));
127 if (il->load_fd != -1)
136 static void image_loader_done(ImageLoader *il)
138 image_loader_stop(il);
140 if (il->func_done) il->func_done(il, il->data_done);
143 static gint image_loader_done_delay_cb(gpointer data)
145 ImageLoader *il = data;
147 il->idle_done_id = -1;
148 image_loader_done(il);
152 static void image_loader_done_delay(ImageLoader *il)
154 if (il->idle_done_id == -1) il->idle_done_id = g_idle_add_full(il->idle_priority,
155 image_loader_done_delay_cb, il, NULL);
158 static void image_loader_error(ImageLoader *il)
160 image_loader_stop(il);
162 if (debug) printf("pixbuf_loader reported load error for: %s\n", il->path);
164 if (il->func_error) il->func_error(il, il->data_error);
167 static gint image_loader_idle_cb(gpointer data)
169 ImageLoader *il = data;
170 guchar buf[IMAGE_LOADER_BUFFER_SIZE];
174 if (!il) return FALSE;
176 if (il->idle_id == -1) return FALSE;
178 c = il->buffer_size ? il->buffer_size : 1;
181 b = read(il->load_fd, &buf, sizeof(buf));
185 image_loader_done(il);
189 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, buf, b, NULL)))
191 image_loader_error(il);
200 if (il->func_percent && il->bytes_total > 0)
202 il->func_percent(il, (gdouble)il->bytes_read / il->bytes_total, il->data_percent);
208 static gint image_loader_begin(ImageLoader *il)
210 guchar buf[IMAGE_LOADER_BUFFER_SIZE];
213 if (!il->loader || il->pixbuf) return FALSE;
215 b = read(il->load_fd, &buf, sizeof(buf));
219 image_loader_stop(il);
223 if (gdk_pixbuf_loader_write(il->loader, buf, b, NULL))
229 /* end of file already */
231 image_loader_stop(il);
233 if (!il->pixbuf) return FALSE;
235 image_loader_done_delay(il);
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, &buf, sizeof(buf));
246 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, buf, 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 < sizeof(buf))
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);
279 image_loader_stop(il);
286 static gint image_loader_setup(ImageLoader *il)
291 if (!il || il->load_fd != -1 || il->loader) return FALSE;
293 pathl = path_from_utf8(il->path);
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);
309 return image_loader_begin(il);
312 ImageLoader *image_loader_new(const gchar *path)
316 if (!path) return NULL;
318 il = g_new0(ImageLoader, 1);
319 if (path) il->path = g_strdup(path);
322 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
330 il->idle_done_id = -1;
332 il->buffer_size = IMAGE_LOADER_BUFFER_DEFAULT_COUNT;
334 il->requested_width = 0;
335 il->requested_height = 0;
340 void image_loader_free(ImageLoader *il)
344 image_loader_stop(il);
345 if (il->idle_done_id != -1) g_source_remove(il->idle_done_id);
346 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
351 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
352 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
354 if (!il) return NULL;
359 gchar *image_loader_get_format(ImageLoader *il)
361 GdkPixbufFormat *format;
365 if (!il || !il->loader) return NULL;
367 format = gdk_pixbuf_loader_get_format(il->loader);
368 if (!format) return NULL;
370 mimev = gdk_pixbuf_format_get_mime_types(format);
371 if (!mimev) return NULL;
373 /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
374 mime = g_strdup(mimev[0]);
380 void image_loader_set_area_ready_func(ImageLoader *il,
381 void (*func_area_ready)(ImageLoader *, guint, guint, guint, guint, gpointer),
382 gpointer data_area_ready)
386 il->func_area_ready = func_area_ready;
387 il->data_area_ready = data_area_ready;
390 void image_loader_set_error_func(ImageLoader *il,
391 void (*func_error)(ImageLoader *, gpointer),
396 il->func_error = func_error;
397 il->data_error = data_error;
400 void image_loader_set_percent_func(ImageLoader *il,
401 void (*func_percent)(ImageLoader *, gdouble, gpointer),
402 gpointer data_percent)
406 il->func_percent = func_percent;
407 il->data_percent = data_percent;
410 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
414 il->requested_width = width;
415 il->requested_height = height;
418 void image_loader_set_buffer_size(ImageLoader *il, guint size)
422 il->buffer_size = size ? size : 1;
425 void image_loader_set_priority(ImageLoader *il, gint priority)
429 il->idle_priority = priority;
432 gint image_loader_start(ImageLoader *il, void (*func_done)(ImageLoader *, gpointer), gpointer data_done)
434 if (!il) return FALSE;
436 if (!il->path) return FALSE;
438 il->func_done = func_done;
439 il->data_done = data_done;
441 return image_loader_setup(il);
444 gdouble image_loader_get_percent(ImageLoader *il)
446 if (!il || il->bytes_total == 0) return 0.0;
448 return (gdouble)il->bytes_read / il->bytes_total;
451 gint image_loader_get_is_done(ImageLoader *il)
453 if (!il) return FALSE;
458 gint image_load_dimensions(const gchar *path, gint *width, gint *height)
463 il = image_loader_new(path);
465 success = image_loader_start(il, NULL, NULL);
467 if (success && il->pixbuf)
469 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
470 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
474 if (width) *width = -1;
475 if (height) *height = -1;
478 image_loader_free(il);