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