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