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