##### Note: GQview CVS on sourceforge is not always up to date, please use #####
[geeqie.git] / src / image-load.c
1 /*
2  * GQview
3  * (C) 2004 John Ellis
4  *
5  * Author: John Ellis
6  *
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!
10  */
11
12
13 #include "gqview.h"
14 #include "image-load.h"
15
16 #include "ui_fileops.h"
17
18 #include <fcntl.h>
19
20
21 /* bytes to read from file per read() */
22 #define IMAGE_LOADER_BUFFER_SIZE 512
23
24 /* the number of bytes to read per idle call (define x IMAGE_LOADER_BUFFER_SIZE) */
25 #define IMAGE_LOADER_BUFFER_DEFAULT_COUNT 1
26
27 static void image_loader_sync_pixbuf(ImageLoader *il)
28 {
29         GdkPixbuf *pb;
30
31         if (!il->loader) return;
32
33         pb = gdk_pixbuf_loader_get_pixbuf(il->loader);
34
35         if (pb == il->pixbuf) return;
36
37         if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
38         il->pixbuf = pb;
39         if (il->pixbuf) gdk_pixbuf_ref(il->pixbuf);
40 }
41
42 static void image_loader_area_cb(GdkPixbufLoader *loader,
43                                  guint x, guint y, guint w, guint h,
44                                  gpointer data)
45 {
46         ImageLoader *il = data;
47
48         if (il->func_area_ready)
49                 {
50                 if (!il->pixbuf)
51                         {
52                         image_loader_sync_pixbuf(il);
53                         if (!il->pixbuf)
54                                 {
55                                 printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
56                                 }
57                         }
58                 il->func_area_ready(il, x, y, w, h, il->data_area_ready);
59                 }
60 }
61
62 static void image_loader_size_cb(GdkPixbufLoader *loader,
63                                  gint width, gint height, gpointer data)
64 {
65         ImageLoader *il = data;
66         GdkPixbufFormat *format;
67         gchar **mime_types;
68         gint scale = FALSE;
69         gint n;
70
71         if (il->requested_width < 1 || il->requested_height < 1) return;
72
73         format = gdk_pixbuf_loader_get_format(loader);
74         if (!format) return;
75
76         mime_types = gdk_pixbuf_format_get_mime_types(format);
77         n = 0;
78         while (mime_types[n])
79                 {
80                 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
81                 n++;
82                 }
83         g_strfreev(mime_types);
84         
85         if (!scale) return;
86
87         if (width > il->requested_width || height > il->requested_height)
88                 {
89                 gint nw, nh;
90
91                 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
92                         {
93                         nw = il->requested_width;
94                         nh = (gdouble)nw / width * height;
95                         if (nh < 1) nh = 1;
96                         }
97                 else
98                         {
99                         nh = il->requested_height;
100                         nw = (gdouble)nh / height * width;
101                         if (nw < 1) nw = 1;
102                         }
103                 
104                 gdk_pixbuf_loader_set_size(loader, nw, nh);
105                 il->shrunk = TRUE;
106                 }
107 }
108
109 static void image_loader_stop(ImageLoader *il)
110 {
111         if (!il) return;
112
113         if (il->idle_id != -1)
114                 {
115                 g_source_remove(il->idle_id);
116                 il->idle_id = -1;
117                 }
118
119         if (il->loader)
120                 {
121                 /* some loaders do not have a pixbuf till close, order is important here */
122                 gdk_pixbuf_loader_close(il->loader, NULL);
123                 image_loader_sync_pixbuf(il);
124                 g_object_unref(G_OBJECT(il->loader));
125                 il->loader = NULL;
126                 }
127
128         if (il->load_fd != -1)
129                 {
130                 close(il->load_fd);
131                 il->load_fd = -1;
132                 }
133
134         il->done = TRUE;
135 }
136
137 static void image_loader_done(ImageLoader *il)
138 {
139         image_loader_stop(il);
140
141         if (il->func_done) il->func_done(il, il->data_done);
142 }
143
144 static gint image_loader_done_delay_cb(gpointer data)
145 {
146         ImageLoader *il = data;
147
148         il->idle_done_id = -1;
149         image_loader_done(il);
150         return FALSE;
151 }
152
153 static void image_loader_done_delay(ImageLoader *il)
154 {
155         if (il->idle_done_id == -1) il->idle_done_id = g_idle_add_full(il->idle_priority,
156                                                                        image_loader_done_delay_cb, il, NULL);
157 }
158
159 static void image_loader_error(ImageLoader *il)
160 {
161         image_loader_stop(il);
162
163         if (debug) printf("pixbuf_loader reported load error for: %s\n", il->path);
164
165         if (il->func_error) il->func_error(il, il->data_error);
166 }
167
168 static gint image_loader_idle_cb(gpointer data)
169 {
170         ImageLoader *il = data;
171         guchar buf[IMAGE_LOADER_BUFFER_SIZE];
172         gint b;
173         gint c;
174
175         if (!il) return FALSE;
176
177         if (il->idle_id == -1) return FALSE;
178
179         c = il->buffer_size ? il->buffer_size : 1;
180         while (c > 0)
181                 {
182                 b = read(il->load_fd, &buf, sizeof(buf));
183
184                 if (b == 0)
185                         {
186                         image_loader_done(il);
187                         return FALSE;
188                         }
189
190                 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, buf, b, NULL)))
191                         {
192                         image_loader_error(il);
193                         return FALSE;
194                         }
195
196                 il->bytes_read += b;
197
198                 c--;
199                 }
200
201         if (il->func_percent && il->bytes_total > 0)
202                 {
203                 il->func_percent(il, (gdouble)il->bytes_read / il->bytes_total, il->data_percent);
204                 }
205
206         return TRUE;
207 }
208
209 static gint image_loader_begin(ImageLoader *il)
210 {
211         guchar buf[IMAGE_LOADER_BUFFER_SIZE];
212         int b;
213
214         if (!il->loader || il->pixbuf) return FALSE;
215
216         b = read(il->load_fd, &buf, sizeof(buf));
217
218         if (b < 1)
219                 {
220                 image_loader_stop(il);
221                 return FALSE;
222                 }
223
224         if (gdk_pixbuf_loader_write(il->loader, buf, b, NULL))
225                 {
226                 il->bytes_read += b;
227
228                 if (b < sizeof(buf))
229                         {
230                         /* end of file already */
231
232                         image_loader_stop(il);
233
234                         if (!il->pixbuf) return FALSE;
235
236                         image_loader_done_delay(il);
237                         return TRUE;
238                         }
239                 else
240                         {
241                         /* larger file */
242
243                         /* read until size is known */
244                         while(il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0)
245                                 {
246                                 b = read(il->load_fd, &buf, sizeof(buf));
247                                 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, buf, b, NULL)))
248                                         {
249                                         image_loader_stop(il);
250                                         return FALSE;
251                                         }
252                                 il->bytes_read += b;
253                                 }
254                         if (!il->pixbuf) image_loader_sync_pixbuf(il);
255
256                         if (il->bytes_read == il->bytes_total || b < sizeof(buf))
257                                 {
258                                 /* done, handle (broken) loaders that do not have pixbuf till close */
259                                 image_loader_stop(il);
260
261                                 if (!il->pixbuf) return FALSE;
262
263                                 image_loader_done_delay(il);
264                                 return TRUE;
265                                 }
266
267                         if (!il->pixbuf)
268                                 {
269                                 image_loader_stop(il);
270                                 return FALSE;
271                                 }
272
273                         /* finally, progressive loading :) */
274                         il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
275                         return TRUE;
276                         }
277                 }
278         else
279                 {
280                 image_loader_stop(il);
281                 return FALSE;
282                 }
283
284         return TRUE;
285 }
286
287 static gint image_loader_setup(ImageLoader *il)
288 {
289         struct stat st;
290         gchar *pathl;
291
292         if (!il || il->load_fd != -1 || il->loader) return FALSE;
293
294         pathl = path_from_utf8(il->path);
295         il->load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
296         g_free(pathl);
297         if (il->load_fd == -1) return FALSE;
298
299         if (fstat(il->load_fd, &st) == 0)
300                 {
301                 il->bytes_total = st.st_size;
302                 }
303
304         il->loader = gdk_pixbuf_loader_new();
305         g_signal_connect(G_OBJECT(il->loader), "area_updated",
306                          G_CALLBACK(image_loader_area_cb), il);
307         g_signal_connect(G_OBJECT(il->loader), "size_prepared",
308                          G_CALLBACK(image_loader_size_cb), il);
309
310         il->shrunk = FALSE;
311
312         return image_loader_begin(il);
313 }
314
315 ImageLoader *image_loader_new(const gchar *path)
316 {
317         ImageLoader *il;
318
319         if (!path) return NULL;
320
321         il = g_new0(ImageLoader, 1);
322         if (path) il->path = g_strdup(path);
323         il->pixbuf = NULL;
324         il->idle_id = -1;
325         il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
326         il->done = FALSE;
327         il->loader = NULL;
328         il->load_fd = -1;
329
330         il->bytes_read = 0;
331         il->bytes_total = 0;
332
333         il->idle_done_id = -1;
334
335         il->buffer_size = IMAGE_LOADER_BUFFER_DEFAULT_COUNT;
336
337         il->requested_width = 0;
338         il->requested_height = 0;
339         il->shrunk = FALSE;
340
341         return il;
342 }
343
344 void image_loader_free(ImageLoader *il)
345 {
346         if (!il) return;
347
348         image_loader_stop(il);
349         if (il->idle_done_id != -1) g_source_remove(il->idle_done_id);
350         if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
351         g_free(il->path);
352         g_free(il);
353 }
354
355 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
356 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
357 {
358         if (!il) return NULL;
359
360         return il->pixbuf;
361 }
362
363 gchar *image_loader_get_format(ImageLoader *il)
364 {
365         GdkPixbufFormat *format;
366         gchar **mimev;
367         gchar *mime;
368
369         if (!il || !il->loader) return NULL;
370
371         format = gdk_pixbuf_loader_get_format(il->loader);
372         if (!format) return NULL;
373
374         mimev = gdk_pixbuf_format_get_mime_types(format);
375         if (!mimev) return NULL;
376
377         /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
378         mime = g_strdup(mimev[0]);
379         g_strfreev(mimev);
380
381         return mime;
382 }
383
384 void image_loader_set_area_ready_func(ImageLoader *il,
385                                       void (*func_area_ready)(ImageLoader *, guint, guint, guint, guint, gpointer),
386                                       gpointer data_area_ready)
387 {
388         if (!il) return;
389
390         il->func_area_ready = func_area_ready;
391         il->data_area_ready = data_area_ready;
392 }
393
394 void image_loader_set_error_func(ImageLoader *il,
395                                  void (*func_error)(ImageLoader *, gpointer),
396                                  gpointer data_error)
397 {
398         if (!il) return;
399
400         il->func_error = func_error;
401         il->data_error = data_error;
402 }
403
404 void image_loader_set_percent_func(ImageLoader *il,
405                                    void (*func_percent)(ImageLoader *, gdouble, gpointer),
406                                    gpointer data_percent)
407 {
408         if (!il) return;
409
410         il->func_percent = func_percent;
411         il->data_percent = data_percent;
412 }
413
414 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
415 {
416         if (!il) return;
417
418         il->requested_width = width;
419         il->requested_height = height;
420 }
421
422 void image_loader_set_buffer_size(ImageLoader *il, guint size)
423 {
424         if (!il) return;
425
426         il->buffer_size = size ? size : 1;
427 }
428
429 void image_loader_set_priority(ImageLoader *il, gint priority)
430 {
431         if (!il) return;
432
433         il->idle_priority = priority;
434 }
435
436 gint image_loader_start(ImageLoader *il, void (*func_done)(ImageLoader *, gpointer), gpointer data_done)
437 {
438         if (!il) return FALSE;
439
440         if (!il->path) return FALSE;
441
442         il->func_done = func_done;
443         il->data_done = data_done;
444
445         return image_loader_setup(il);
446 }
447
448 gdouble image_loader_get_percent(ImageLoader *il)
449 {
450         if (!il || il->bytes_total == 0) return 0.0;
451
452         return (gdouble)il->bytes_read / il->bytes_total;
453 }
454
455 gint image_loader_get_is_done(ImageLoader *il)
456 {
457         if (!il) return FALSE;
458
459         return il->done;
460 }
461
462 gint image_load_dimensions(const gchar *path, gint *width, gint *height)
463 {
464         ImageLoader *il;
465         gint success;
466
467         il = image_loader_new(path);
468
469         success = image_loader_start(il, NULL, NULL);
470
471         if (success && il->pixbuf)
472                 {
473                 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
474                 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
475                 }
476         else
477                 {
478                 if (width) *width = -1;
479                 if (height) *height = -1;
480                 }
481
482         image_loader_free(il);
483
484         return success;
485 }
486