Introduce macros to display debug messages.
[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 #include "filelist.h"
17
18 #include "exif.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_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                                 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_size_cb(GdkPixbufLoader *loader,
67                                  gint width, gint height, gpointer data)
68 {
69         ImageLoader *il = data;
70         GdkPixbufFormat *format;
71         gchar **mime_types;
72         gint scale = FALSE;
73         gint n;
74
75         if (il->requested_width < 1 || il->requested_height < 1) return;
76
77         format = gdk_pixbuf_loader_get_format(loader);
78         if (!format) return;
79
80         mime_types = gdk_pixbuf_format_get_mime_types(format);
81         n = 0;
82         while (mime_types[n])
83                 {
84                 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
85                 n++;
86                 }
87         g_strfreev(mime_types);
88
89         if (!scale) return;
90
91         if (width > il->requested_width || height > il->requested_height)
92                 {
93                 gint nw, nh;
94
95                 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
96                         {
97                         nw = il->requested_width;
98                         nh = (gdouble)nw / width * height;
99                         if (nh < 1) nh = 1;
100                         }
101                 else
102                         {
103                         nh = il->requested_height;
104                         nw = (gdouble)nh / height * width;
105                         if (nw < 1) nw = 1;
106                         }
107
108                 gdk_pixbuf_loader_set_size(loader, nw, nh);
109                 il->shrunk = TRUE;
110                 }
111 }
112
113 static void image_loader_stop(ImageLoader *il)
114 {
115         if (!il) return;
116
117         if (il->idle_id != -1)
118                 {
119                 g_source_remove(il->idle_id);
120                 il->idle_id = -1;
121                 }
122
123         if (il->loader)
124                 {
125                 /* some loaders do not have a pixbuf till close, order is important here */
126                 gdk_pixbuf_loader_close(il->loader, NULL);
127                 image_loader_sync_pixbuf(il);
128                 g_object_unref(G_OBJECT(il->loader));
129                 il->loader = NULL;
130                 }
131
132         if (il->load_fd != -1)
133                 {
134                 close(il->load_fd);
135                 il->load_fd = -1;
136                 }
137
138         il->done = TRUE;
139 }
140
141 static void image_loader_done(ImageLoader *il)
142 {
143         image_loader_stop(il);
144
145         if (il->func_done) il->func_done(il, il->data_done);
146 }
147
148 static gint image_loader_done_delay_cb(gpointer data)
149 {
150         ImageLoader *il = data;
151
152         il->idle_done_id = -1;
153         image_loader_done(il);
154         return FALSE;
155 }
156
157 static void image_loader_done_delay(ImageLoader *il)
158 {
159         if (il->idle_done_id == -1) il->idle_done_id = g_idle_add_full(il->idle_priority,
160                                                                        image_loader_done_delay_cb, il, NULL);
161 }
162
163 static void image_loader_error(ImageLoader *il)
164 {
165         image_loader_stop(il);
166
167         DEBUG_1("pixbuf_loader reported load error for: %s\n", image_loader_path(il));
168
169         if (il->func_error) il->func_error(il, il->data_error);
170 }
171
172 static gint image_loader_idle_cb(gpointer data)
173 {
174         ImageLoader *il = data;
175         gint b;
176         gint c;
177
178         if (!il) return FALSE;
179
180         if (il->idle_id == -1) return FALSE;
181
182         c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
183         while (c > 0)
184                 {
185                 b = read(il->load_fd, il->read_buffer, il->read_buffer_size);
186
187                 if (b == 0)
188                         {
189                         image_loader_done(il);
190                         return FALSE;
191                         }
192
193                 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->read_buffer, b, NULL)))
194                         {
195                         image_loader_error(il);
196                         return FALSE;
197                         }
198
199                 il->bytes_read += b;
200
201                 c--;
202                 }
203
204         if (il->func_percent && il->bytes_total > 0)
205                 {
206                 il->func_percent(il, (gdouble)il->bytes_read / il->bytes_total, il->data_percent);
207                 }
208
209         return TRUE;
210 }
211
212 static gint image_loader_begin(ImageLoader *il)
213 {
214         int b;
215         unsigned int offset = 0;
216
217         if (!il->loader || il->pixbuf) return FALSE;
218
219         b = read(il->load_fd, il->read_buffer, il->read_buffer_size);
220
221         if (b > 0 &&
222             format_raw_img_exif_offsets_fd(il->load_fd, image_loader_path(il), il->read_buffer, b, &offset, NULL))
223                 {
224                 DEBUG_1("Raw file %s contains embedded image\n", image_loader_path(il));
225
226                 b = read(il->load_fd, il->read_buffer, il->read_buffer_size);
227                 }
228
229         if (b < 1)
230                 {
231                 image_loader_stop(il);
232                 return FALSE;
233                 }
234
235         if (!gdk_pixbuf_loader_write(il->loader, il->read_buffer, b, NULL))
236                 {
237                 image_loader_stop(il);
238                 return FALSE;
239                 }
240
241         il->bytes_read += b + offset;
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, il->read_buffer, il->read_buffer_size);
247                 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->read_buffer, 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 < 1)
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
276         return TRUE;
277 }
278
279 static gint image_loader_setup(ImageLoader *il)
280 {
281         struct stat st;
282         gchar *pathl;
283
284         if (!il || il->load_fd != -1 || il->loader) return FALSE;
285
286         pathl = path_from_utf8(image_loader_path(il));
287         il->load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
288         g_free(pathl);
289         if (il->load_fd == -1) return FALSE;
290
291         if (fstat(il->load_fd, &st) == 0)
292                 {
293                 il->bytes_total = st.st_size;
294                 }
295
296         il->loader = gdk_pixbuf_loader_new();
297         g_signal_connect(G_OBJECT(il->loader), "area_updated",
298                          G_CALLBACK(image_loader_area_cb), il);
299         g_signal_connect(G_OBJECT(il->loader), "size_prepared",
300                          G_CALLBACK(image_loader_size_cb), il);
301
302         il->shrunk = FALSE;
303
304         return image_loader_begin(il);
305 }
306
307 static ImageLoader *image_loader_new_real(FileData *fd, const gchar *path)
308 {
309         ImageLoader *il;
310
311         if (!fd && !path) return NULL;
312
313         il = g_new0(ImageLoader, 1);
314         if (fd) il->fd = file_data_ref(fd);
315         if (path) il->path = g_strdup(path);
316         il->pixbuf = NULL;
317         il->idle_id = -1;
318         il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
319         il->done = FALSE;
320         il->loader = NULL;
321         il->load_fd = -1;
322
323         il->bytes_read = 0;
324         il->bytes_total = 0;
325
326         il->idle_done_id = -1;
327
328         il->idle_read_loop_count = options->image.idle_read_loop_count;
329         il->read_buffer_size = options->image.read_buffer_size;
330         il->read_buffer = g_new(guchar, il->read_buffer_size);
331
332         il->requested_width = 0;
333         il->requested_height = 0;
334         il->shrunk = FALSE;
335         DEBUG_1("new image loader %p, bufsize=%u idle_loop=%u\n", il, il->read_buffer_size, il->idle_read_loop_count);
336         return il;
337 }
338
339 ImageLoader *image_loader_new(FileData *fd)
340 {
341         return image_loader_new_real(fd, NULL);
342 }
343
344 ImageLoader *image_loader_new_from_path(const gchar *path)
345 {
346         return image_loader_new_real(NULL, path);
347 }
348
349 void image_loader_free(ImageLoader *il)
350 {
351         if (!il) return;
352
353         image_loader_stop(il);
354         if (il->idle_done_id != -1) g_source_remove(il->idle_done_id);
355         if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
356         if (il->fd) file_data_unref(il->fd);
357         if (il->path) g_free(il->path);
358         if (il->read_buffer) g_free(il->read_buffer);
359         DEBUG_1("freeing image loader %p bytes_read=%d\n", il, il->bytes_read);
360         g_free(il);
361 }
362
363 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
364 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
365 {
366         if (!il) return NULL;
367
368         return il->pixbuf;
369 }
370
371 gchar *image_loader_get_format(ImageLoader *il)
372 {
373         GdkPixbufFormat *format;
374         gchar **mimev;
375         gchar *mime;
376
377         if (!il || !il->loader) return NULL;
378
379         format = gdk_pixbuf_loader_get_format(il->loader);
380         if (!format) return NULL;
381
382         mimev = gdk_pixbuf_format_get_mime_types(format);
383         if (!mimev) return NULL;
384
385         /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
386         mime = g_strdup(mimev[0]);
387         g_strfreev(mimev);
388
389         return mime;
390 }
391
392 void image_loader_set_area_ready_func(ImageLoader *il,
393                                       void (*func_area_ready)(ImageLoader *, guint, guint, guint, guint, gpointer),
394                                       gpointer data_area_ready)
395 {
396         if (!il) return;
397
398         il->func_area_ready = func_area_ready;
399         il->data_area_ready = data_area_ready;
400 }
401
402 void image_loader_set_error_func(ImageLoader *il,
403                                  void (*func_error)(ImageLoader *, gpointer),
404                                  gpointer data_error)
405 {
406         if (!il) return;
407
408         il->func_error = func_error;
409         il->data_error = data_error;
410 }
411
412 void image_loader_set_percent_func(ImageLoader *il,
413                                    void (*func_percent)(ImageLoader *, gdouble, gpointer),
414                                    gpointer data_percent)
415 {
416         if (!il) return;
417
418         il->func_percent = func_percent;
419         il->data_percent = data_percent;
420 }
421
422 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
423 {
424         if (!il) return;
425
426         il->requested_width = width;
427         il->requested_height = height;
428 }
429
430 void image_loader_set_buffer_size(ImageLoader *il, guint count)
431 {
432         if (!il) return;
433
434         il->idle_read_loop_count = count ? count : 1;
435 }
436
437 void image_loader_set_priority(ImageLoader *il, gint priority)
438 {
439         if (!il) return;
440
441         il->idle_priority = priority;
442 }
443
444 gint image_loader_start(ImageLoader *il, void (*func_done)(ImageLoader *, gpointer), gpointer data_done)
445 {
446         if (!il) return FALSE;
447
448         if (!image_loader_path(il)) return FALSE;
449
450         il->func_done = func_done;
451         il->data_done = data_done;
452
453         return image_loader_setup(il);
454 }
455
456 gdouble image_loader_get_percent(ImageLoader *il)
457 {
458         if (!il || il->bytes_total == 0) return 0.0;
459
460         return (gdouble)il->bytes_read / il->bytes_total;
461 }
462
463 gint image_loader_get_is_done(ImageLoader *il)
464 {
465         if (!il) return FALSE;
466
467         return il->done;
468 }
469
470 gint image_load_dimensions(FileData *fd, gint *width, gint *height)
471 {
472         ImageLoader *il;
473         gint success;
474
475         il = image_loader_new(fd);
476
477         success = image_loader_start(il, NULL, NULL);
478
479         if (success && il->pixbuf)
480                 {
481                 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
482                 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
483                 }
484         else
485                 {
486                 if (width) *width = -1;
487                 if (height) *height = -1;
488                 }
489
490         image_loader_free(il);
491
492         return success;
493 }