Mercurial > geeqie.yaz
annotate src/image-load.c @ 1015:2cdcf67e9300
run image loader in separate thread
this feature is disabled by default for now, it must be enabled by
configure --enable-threads
| author | nadvornik |
|---|---|
| date | Sat, 30 Aug 2008 20:15:47 +0000 |
| parents | 4d3c98219246 |
| children | 9865e22d05f3 |
| rev | line source |
|---|---|
| 9 | 1 /* |
| 196 | 2 * Geeqie |
| 9 | 3 * (C) 2004 John Ellis |
| 475 | 4 * Copyright (C) 2008 The Geeqie Team |
| 9 | 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 | |
| 281 | 14 #include "main.h" |
| 9 | 15 #include "image-load.h" |
| 507 | 16 |
| 17 #include "exif.h" | |
| 586 | 18 #include "filedata.h" |
| 9 | 19 #include "ui_fileops.h" |
|
1012
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
20 #include "gq-marshal.h" |
| 9 | 21 |
| 22 #include <fcntl.h> | |
| 1008 | 23 #include <sys/mman.h> |
| 9 | 24 |
| 1014 | 25 |
| 26 /**************************************************************************************/ | |
| 27 /* image looader class */ | |
| 28 | |
| 29 | |
|
1012
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
30 enum { |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
31 SIGNAL_AREA_READY = 0, |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
32 SIGNAL_ERROR, |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
33 SIGNAL_DONE, |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
34 SIGNAL_PERCENT, |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
35 SIGNAL_COUNT |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
36 }; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
37 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
38 static guint signals[SIGNAL_COUNT] = { 0 }; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
39 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
40 static void image_loader_init (GTypeInstance *instance, gpointer g_class); |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
41 static void image_loader_class_init (ImageLoaderClass *class); |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
42 static void image_loader_finalize(GObject *object); |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
43 static void image_loader_stop(ImageLoader *il); |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
44 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
45 GType image_loader_get_type (void) |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
46 { |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
47 static GType type = 0; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
48 if (type == 0) |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
49 { |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
50 static const GTypeInfo info = { |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
51 sizeof (ImageLoaderClass), |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
52 NULL, /* base_init */ |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
53 NULL, /* base_finalize */ |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
54 (GClassInitFunc)image_loader_class_init, /* class_init */ |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
55 NULL, /* class_finalize */ |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
56 NULL, /* class_data */ |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
57 sizeof (ImageLoader), |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
58 0, /* n_preallocs */ |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
59 (GInstanceInitFunc)image_loader_init, /* instance_init */ |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
60 }; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
61 type = g_type_register_static (G_TYPE_OBJECT, "ImageLoaderType", &info, 0); |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
62 } |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
63 return type; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
64 } |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
65 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
66 static void image_loader_init (GTypeInstance *instance, gpointer g_class) |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
67 { |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
68 ImageLoader *il = (ImageLoader *)instance; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
69 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
70 il->pixbuf = NULL; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
71 il->idle_id = -1; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
72 il->idle_priority = G_PRIORITY_DEFAULT_IDLE; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
73 il->done = FALSE; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
74 il->loader = NULL; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
75 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
76 il->bytes_read = 0; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
77 il->bytes_total = 0; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
78 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
79 il->idle_done_id = -1; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
80 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
81 il->idle_read_loop_count = options->image.idle_read_loop_count; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
82 il->read_buffer_size = options->image.read_buffer_size; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
83 il->mapped_file = NULL; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
84 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
85 il->requested_width = 0; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
86 il->requested_height = 0; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
87 il->shrunk = FALSE; |
| 1015 | 88 |
| 89 #ifdef HAVE_GTHREAD | |
| 90 il->data_mutex = g_mutex_new(); | |
| 91 #endif | |
|
1012
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
92 DEBUG_1("new image loader %p, bufsize=%u idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count); |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
93 } |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
94 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
95 static void image_loader_class_init (ImageLoaderClass *class) |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
96 { |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
97 GObjectClass *gobject_class = G_OBJECT_CLASS (class); |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
98 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
99 // gobject_class->set_property = image_loader_set_property; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
100 // gobject_class->get_property = image_loader_get_property; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
101 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
102 gobject_class->finalize = image_loader_finalize; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
103 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
104 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
105 signals[SIGNAL_AREA_READY] = |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
106 g_signal_new("area_ready", |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
107 G_OBJECT_CLASS_TYPE(gobject_class), |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
108 G_SIGNAL_RUN_LAST, |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
109 G_STRUCT_OFFSET(ImageLoaderClass, area_ready), |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
110 NULL, NULL, |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
111 gq_marshal_VOID__INT_INT_INT_INT, |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
112 G_TYPE_NONE, 4, |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
113 G_TYPE_INT, |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
114 G_TYPE_INT, |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
115 G_TYPE_INT, |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
116 G_TYPE_INT); |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
117 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
118 signals[SIGNAL_ERROR] = |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
119 g_signal_new("error", |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
120 G_OBJECT_CLASS_TYPE(gobject_class), |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
121 G_SIGNAL_RUN_LAST, |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
122 G_STRUCT_OFFSET(ImageLoaderClass, error), |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
123 NULL, NULL, |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
124 g_cclosure_marshal_VOID__VOID, |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
125 G_TYPE_NONE, 1, |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
126 GDK_TYPE_EVENT); |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
127 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
128 signals[SIGNAL_DONE] = |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
129 g_signal_new("done", |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
130 G_OBJECT_CLASS_TYPE(gobject_class), |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
131 G_SIGNAL_RUN_LAST, |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
132 G_STRUCT_OFFSET(ImageLoaderClass, done), |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
133 NULL, NULL, |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
134 g_cclosure_marshal_VOID__VOID, |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
135 G_TYPE_NONE, 0); |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
136 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
137 signals[SIGNAL_PERCENT] = |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
138 g_signal_new("percent", |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
139 G_OBJECT_CLASS_TYPE(gobject_class), |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
140 G_SIGNAL_RUN_LAST, |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
141 G_STRUCT_OFFSET(ImageLoaderClass, percent), |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
142 NULL, NULL, |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
143 g_cclosure_marshal_VOID__DOUBLE, |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
144 G_TYPE_NONE, 1, |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
145 G_TYPE_DOUBLE); |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
146 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
147 } |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
148 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
149 static void image_loader_finalize(GObject *object) |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
150 { |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
151 ImageLoader *il = (ImageLoader *)object; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
152 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
153 image_loader_stop(il); |
| 1014 | 154 |
| 155 DEBUG_1("freeing image loader %p bytes_read=%d", il, il->bytes_read); | |
| 156 | |
|
1012
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
157 if (il->idle_done_id != -1) g_source_remove(il->idle_done_id); |
| 1014 | 158 |
| 159 while (g_source_remove_by_user_data(il)) | |
| 160 { | |
| 161 DEBUG_1("pending signals detected"); | |
| 162 } | |
| 163 | |
| 164 while (il->area_param_list) | |
| 165 { | |
| 166 DEBUG_1("pending area_ready signals detected"); | |
| 167 while (g_source_remove_by_user_data(il->area_param_list->data)) {} | |
| 168 g_free(il->area_param_list->data); | |
| 169 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list); | |
| 170 } | |
| 171 | |
|
1012
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
172 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf); |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
173 file_data_unref(il->fd); |
| 1015 | 174 #ifdef HAVE_GTHREAD |
| 175 g_mutex_free(il->data_mutex); | |
| 176 #endif | |
|
1012
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
177 } |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
178 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
179 void image_loader_free(ImageLoader *il) |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
180 { |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
181 if (!il) return; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
182 g_object_unref(G_OBJECT(il)); |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
183 } |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
184 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
185 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
186 ImageLoader *image_loader_new(FileData *fd) |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
187 { |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
188 ImageLoader *il; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
189 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
190 if (!fd) return NULL; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
191 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
192 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL); |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
193 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
194 il->fd = file_data_ref(fd); |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
195 |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
196 return il; |
|
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
197 } |
| 9 | 198 |
| 1014 | 199 /**************************************************************************************/ |
| 200 /* send signals via idle calbacks - the callback are executed in the main thread */ | |
| 201 | |
| 202 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam; | |
| 203 struct _ImageLoaderAreaParam { | |
| 204 ImageLoader *il; | |
| 205 guint x; | |
| 206 guint y; | |
| 207 guint w; | |
| 208 guint h; | |
| 209 }; | |
| 210 | |
| 211 | |
| 212 static gint image_loader_emit_area_ready_cb(gpointer data) | |
| 213 { | |
| 214 ImageLoaderAreaParam *par = data; | |
| 215 ImageLoader *il = par->il; | |
| 216 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h); | |
| 1015 | 217 g_mutex_lock(il->data_mutex); |
| 1014 | 218 il->area_param_list = g_list_remove(il->area_param_list, par); |
| 219 g_free(par); | |
| 1015 | 220 g_mutex_unlock(il->data_mutex); |
| 1014 | 221 |
| 222 return FALSE; | |
| 223 } | |
| 224 | |
| 225 static gint image_loader_emit_done_cb(gpointer data) | |
| 226 { | |
| 227 ImageLoader *il = data; | |
| 228 g_signal_emit(il, signals[SIGNAL_DONE], 0); | |
| 229 return FALSE; | |
| 230 } | |
| 231 | |
| 232 static gint image_loader_emit_error_cb(gpointer data) | |
| 233 { | |
| 234 ImageLoader *il = data; | |
| 235 g_signal_emit(il, signals[SIGNAL_ERROR], 0); | |
| 236 return FALSE; | |
| 237 } | |
| 238 | |
| 239 static gint image_loader_emit_percent_cb(gpointer data) | |
| 240 { | |
| 241 ImageLoader *il = data; | |
| 1015 | 242 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il)); |
| 1014 | 243 return FALSE; |
| 244 } | |
| 245 | |
| 246 /* DONE and ERROR are emited only once, thus they can have normal priority | |
| 247 PERCENT and AREA_READY should be processed ASAP | |
| 248 */ | |
| 249 | |
| 250 static void image_loader_emit_done(ImageLoader *il) | |
| 251 { | |
| 252 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL); | |
| 253 } | |
| 254 | |
| 255 static void image_loader_emit_error(ImageLoader *il) | |
| 256 { | |
| 257 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL); | |
| 258 } | |
| 259 | |
| 260 static void image_loader_emit_percent(ImageLoader *il) | |
| 261 { | |
| 262 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL); | |
| 263 } | |
| 264 | |
| 265 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h) | |
| 266 { | |
| 267 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1); | |
| 268 par->il = il; | |
| 269 par->x = x; | |
| 270 par->y = y; | |
| 271 par->w = w; | |
| 272 par->h = h; | |
| 273 | |
| 1015 | 274 g_mutex_lock(il->data_mutex); |
| 1014 | 275 il->area_param_list = g_list_prepend(il->area_param_list, par); |
| 1015 | 276 g_mutex_unlock(il->data_mutex); |
| 277 | |
| 1014 | 278 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL); |
| 279 } | |
| 280 | |
| 281 /**************************************************************************************/ | |
| 282 /* the following functions may be executed in separate thread */ | |
| 283 | |
| 9 | 284 static void image_loader_sync_pixbuf(ImageLoader *il) |
| 285 { | |
| 286 GdkPixbuf *pb; | |
| 1015 | 287 |
| 288 g_mutex_lock(il->data_mutex); | |
| 289 | |
| 290 if (!il->loader) | |
| 291 { | |
| 292 g_mutex_unlock(il->data_mutex); | |
| 293 return; | |
| 294 } | |
| 9 | 295 |
| 296 pb = gdk_pixbuf_loader_get_pixbuf(il->loader); | |
| 297 | |
| 1015 | 298 if (pb == il->pixbuf) |
| 299 { | |
| 300 g_mutex_unlock(il->data_mutex); | |
| 301 return; | |
| 302 } | |
| 9 | 303 |
| 304 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf); | |
| 305 il->pixbuf = pb; | |
| 306 if (il->pixbuf) gdk_pixbuf_ref(il->pixbuf); | |
| 1015 | 307 g_mutex_unlock(il->data_mutex); |
| 9 | 308 } |
| 309 | |
| 500 | 310 static void image_loader_area_updated_cb(GdkPixbufLoader *loader, |
| 9 | 311 guint x, guint y, guint w, guint h, |
| 312 gpointer data) | |
| 313 { | |
| 314 ImageLoader *il = data; | |
| 315 | |
| 1015 | 316 if (!image_loader_get_pixbuf(il)) |
| 9 | 317 { |
|
1012
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
318 image_loader_sync_pixbuf(il); |
| 1015 | 319 if (!image_loader_get_pixbuf(il)) |
| 9 | 320 { |
|
1012
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
321 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n"); |
| 9 | 322 } |
| 323 } | |
| 1014 | 324 image_loader_emit_area_ready(il, x, y, w, h); |
| 9 | 325 } |
| 326 | |
| 500 | 327 static void image_loader_area_prepared_cb(GdkPixbufLoader *loader, gpointer data) |
| 328 { | |
| 329 GdkPixbuf *pb; | |
| 330 guchar *pix; | |
| 331 size_t h, rs; | |
|
961
3f03282ec40e
a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669
nadvornik
parents:
890
diff
changeset
|
332 |
|
3f03282ec40e
a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669
nadvornik
parents:
890
diff
changeset
|
333 /* a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669 */ |
|
3f03282ec40e
a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669
nadvornik
parents:
890
diff
changeset
|
334 gchar *format = gdk_pixbuf_format_get_name(gdk_pixbuf_loader_get_format(loader)); |
| 995 | 335 if (strcmp(format, "svg") == 0) |
|
961
3f03282ec40e
a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669
nadvornik
parents:
890
diff
changeset
|
336 { |
|
3f03282ec40e
a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669
nadvornik
parents:
890
diff
changeset
|
337 g_free(format); |
|
3f03282ec40e
a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669
nadvornik
parents:
890
diff
changeset
|
338 return; |
|
3f03282ec40e
a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669
nadvornik
parents:
890
diff
changeset
|
339 } |
|
3f03282ec40e
a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669
nadvornik
parents:
890
diff
changeset
|
340 |
|
3f03282ec40e
a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669
nadvornik
parents:
890
diff
changeset
|
341 g_free(format); |
| 500 | 342 |
| 343 pb = gdk_pixbuf_loader_get_pixbuf(loader); | |
| 344 | |
| 345 h = gdk_pixbuf_get_height(pb); | |
| 346 rs = gdk_pixbuf_get_rowstride(pb); | |
| 347 pix = gdk_pixbuf_get_pixels(pb); | |
| 348 | |
| 349 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */ | |
| 350 | |
| 351 } | |
| 352 | |
| 9 | 353 static void image_loader_size_cb(GdkPixbufLoader *loader, |
| 354 gint width, gint height, gpointer data) | |
| 355 { | |
| 356 ImageLoader *il = data; | |
| 357 GdkPixbufFormat *format; | |
| 358 gchar **mime_types; | |
| 359 gint scale = FALSE; | |
| 360 gint n; | |
| 361 | |
| 1015 | 362 g_mutex_lock(il->data_mutex); |
| 363 if (il->requested_width < 1 || il->requested_height < 1) | |
| 364 { | |
| 365 g_mutex_unlock(il->data_mutex); | |
| 366 return; | |
| 367 } | |
| 368 g_mutex_unlock(il->data_mutex); | |
| 9 | 369 |
| 370 format = gdk_pixbuf_loader_get_format(loader); | |
| 371 if (!format) return; | |
| 372 | |
| 373 mime_types = gdk_pixbuf_format_get_mime_types(format); | |
| 374 n = 0; | |
| 375 while (mime_types[n]) | |
| 376 { | |
| 377 if (strstr(mime_types[n], "jpeg")) scale = TRUE; | |
| 378 n++; | |
| 379 } | |
| 380 g_strfreev(mime_types); | |
| 442 | 381 |
| 9 | 382 if (!scale) return; |
| 383 | |
| 1015 | 384 g_mutex_lock(il->data_mutex); |
| 385 | |
| 9 | 386 if (width > il->requested_width || height > il->requested_height) |
| 387 { | |
| 388 gint nw, nh; | |
| 389 | |
| 390 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height)) | |
| 391 { | |
| 392 nw = il->requested_width; | |
| 393 nh = (gdouble)nw / width * height; | |
| 394 if (nh < 1) nh = 1; | |
| 395 } | |
| 396 else | |
| 397 { | |
| 398 nh = il->requested_height; | |
| 399 nw = (gdouble)nh / height * width; | |
| 400 if (nw < 1) nw = 1; | |
| 401 } | |
| 442 | 402 |
| 9 | 403 gdk_pixbuf_loader_set_size(loader, nw, nh); |
|
14
25335c62cd9b
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
404 il->shrunk = TRUE; |
| 9 | 405 } |
| 1015 | 406 g_mutex_unlock(il->data_mutex); |
| 407 | |
| 9 | 408 } |
| 409 | |
| 1014 | 410 static void image_loader_stop_loader(ImageLoader *il) |
| 9 | 411 { |
| 412 if (!il) return; | |
| 413 | |
| 414 if (il->loader) | |
| 415 { | |
| 416 /* some loaders do not have a pixbuf till close, order is important here */ | |
| 417 gdk_pixbuf_loader_close(il->loader, NULL); | |
| 418 image_loader_sync_pixbuf(il); | |
| 419 g_object_unref(G_OBJECT(il->loader)); | |
| 420 il->loader = NULL; | |
| 421 } | |
| 422 | |
| 423 il->done = TRUE; | |
| 424 } | |
| 425 | |
| 1014 | 426 static void image_loader_setup_loader(ImageLoader *il) |
| 427 { | |
| 1015 | 428 g_mutex_lock(il->data_mutex); |
| 1014 | 429 il->loader = gdk_pixbuf_loader_new(); |
| 1015 | 430 g_mutex_unlock(il->data_mutex); |
| 431 | |
| 1014 | 432 g_signal_connect(G_OBJECT(il->loader), "area_updated", |
| 433 G_CALLBACK(image_loader_area_updated_cb), il); | |
| 434 g_signal_connect(G_OBJECT(il->loader), "size_prepared", | |
| 435 G_CALLBACK(image_loader_size_cb), il); | |
| 436 g_signal_connect(G_OBJECT(il->loader), "area_prepared", | |
| 437 G_CALLBACK(image_loader_area_prepared_cb), il); | |
| 438 } | |
| 439 | |
| 440 | |
| 9 | 441 static void image_loader_done(ImageLoader *il) |
| 442 { | |
| 1014 | 443 image_loader_stop_loader(il); |
| 9 | 444 |
| 1014 | 445 image_loader_emit_done(il); |
| 9 | 446 } |
| 447 | |
| 448 static void image_loader_error(ImageLoader *il) | |
| 449 { | |
| 1014 | 450 image_loader_stop_loader(il); |
| 9 | 451 |
|
1009
dd311dae857a
fixed thumbnail loader for the new raw preview interface
nadvornik
parents:
1008
diff
changeset
|
452 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path); |
| 9 | 453 |
| 1014 | 454 image_loader_emit_error(il); |
| 9 | 455 } |
| 456 | |
| 1014 | 457 static gint image_loader_continue(ImageLoader *il) |
| 9 | 458 { |
| 459 gint b; | |
| 460 gint c; | |
| 461 | |
| 462 if (!il) return FALSE; | |
| 463 | |
|
413
9e521adbf312
Add two new options to control image read buffer at runtime.
zas_
parents:
281
diff
changeset
|
464 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1; |
| 9 | 465 while (c > 0) |
| 466 { | |
| 1008 | 467 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read); |
| 9 | 468 |
| 469 if (b == 0) | |
| 470 { | |
| 471 image_loader_done(il); | |
| 472 return FALSE; | |
| 473 } | |
| 474 | |
| 1008 | 475 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL))) |
| 9 | 476 { |
| 477 image_loader_error(il); | |
| 478 return FALSE; | |
| 479 } | |
| 480 | |
| 481 il->bytes_read += b; | |
| 482 | |
| 483 c--; | |
| 484 } | |
| 485 | |
|
1012
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
486 if (il->bytes_total > 0) |
| 9 | 487 { |
| 1014 | 488 image_loader_emit_percent(il); |
| 9 | 489 } |
| 490 | |
| 491 return TRUE; | |
| 492 } | |
| 493 | |
| 494 static gint image_loader_begin(ImageLoader *il) | |
| 495 { | |
|
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
995
diff
changeset
|
496 gint b; |
| 1014 | 497 |
| 498 if (il->pixbuf) return FALSE; | |
| 442 | 499 |
| 1008 | 500 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read); |
| 1014 | 501 if (b < 1) return FALSE; |
|
45
7cfa60beda76
Thu May 26 13:57:19 2005 John Ellis <johne@verizon.net>
gqview
parents:
43
diff
changeset
|
502 |
| 1014 | 503 image_loader_setup_loader(il); |
| 9 | 504 |
| 1008 | 505 if (!gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)) |
| 9 | 506 { |
| 1014 | 507 image_loader_stop_loader(il); |
| 9 | 508 return FALSE; |
| 509 } | |
| 510 | |
| 1008 | 511 il->bytes_read += b; |
|
60
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
512 |
|
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
513 /* read until size is known */ |
|
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
514 while (il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0) |
|
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
515 { |
| 1008 | 516 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read); |
| 517 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL))) | |
|
60
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
518 { |
| 1014 | 519 image_loader_stop_loader(il); |
|
60
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
520 return FALSE; |
|
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
521 } |
|
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
522 il->bytes_read += b; |
|
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
523 } |
|
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
524 if (!il->pixbuf) image_loader_sync_pixbuf(il); |
|
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
525 |
|
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
526 if (il->bytes_read == il->bytes_total || b < 1) |
|
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
527 { |
|
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
528 /* done, handle (broken) loaders that do not have pixbuf till close */ |
| 1014 | 529 image_loader_stop_loader(il); |
|
60
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
530 |
|
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
531 if (!il->pixbuf) return FALSE; |
|
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
532 |
| 1014 | 533 image_loader_done(il); |
|
60
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
534 return TRUE; |
|
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
535 } |
|
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
536 |
|
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
537 if (!il->pixbuf) |
|
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
538 { |
| 1014 | 539 image_loader_stop_loader(il); |
|
60
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
540 return FALSE; |
|
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
541 } |
|
9c0c402b0ef3
Mon Jun 13 17:31:46 2005 John Ellis <johne@verizon.net>
gqview
parents:
54
diff
changeset
|
542 |
| 9 | 543 return TRUE; |
| 544 } | |
| 545 | |
| 1014 | 546 /**************************************************************************************/ |
| 547 /* the following functions are always executed in the main thread */ | |
| 548 | |
| 549 | |
| 550 static gint image_loader_setup_source(ImageLoader *il) | |
| 9 | 551 { |
| 552 struct stat st; | |
| 553 gchar *pathl; | |
| 554 | |
| 1008 | 555 if (!il || il->loader || il->mapped_file) return FALSE; |
| 556 | |
| 557 il->mapped_file = NULL; | |
| 558 | |
| 559 if (il->fd) | |
| 560 { | |
| 561 ExifData *exif = exif_read_fd(il->fd); | |
| 562 | |
| 563 il->mapped_file = exif_get_preview(exif, &il->bytes_total); | |
| 564 | |
| 565 if (il->mapped_file) | |
| 566 { | |
| 567 il->preview = TRUE; | |
|
1009
dd311dae857a
fixed thumbnail loader for the new raw preview interface
nadvornik
parents:
1008
diff
changeset
|
568 DEBUG_1("Raw file %s contains embedded image", il->fd->path); |
| 1008 | 569 } |
| 570 exif_free_fd(il->fd, exif); | |
| 571 } | |
| 9 | 572 |
| 1008 | 573 |
| 574 if (!il->mapped_file) | |
| 575 { | |
| 576 /* normal file */ | |
| 577 gint load_fd; | |
| 578 | |
|
1009
dd311dae857a
fixed thumbnail loader for the new raw preview interface
nadvornik
parents:
1008
diff
changeset
|
579 pathl = path_from_utf8(il->fd->path); |
| 1008 | 580 load_fd = open(pathl, O_RDONLY | O_NONBLOCK); |
| 581 g_free(pathl); | |
| 582 if (load_fd == -1) return FALSE; | |
| 9 | 583 |
| 1008 | 584 if (fstat(load_fd, &st) == 0) |
| 585 { | |
| 586 il->bytes_total = st.st_size; | |
| 587 } | |
| 588 else | |
| 589 { | |
| 590 close(load_fd); | |
| 591 return FALSE; | |
| 592 } | |
| 593 | |
| 594 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0); | |
| 595 close(load_fd); | |
| 596 if (il->mapped_file == MAP_FAILED) | |
| 597 { | |
| 598 il->mapped_file = 0; | |
| 599 return FALSE; | |
| 600 } | |
| 601 il->preview = FALSE; | |
| 9 | 602 } |
| 1014 | 603 |
| 604 return TRUE; | |
| 605 } | |
| 606 | |
| 607 static void image_loader_stop_source(ImageLoader *il) | |
| 608 { | |
| 609 if (!il) return; | |
| 610 | |
| 611 if (il->mapped_file) | |
| 612 { | |
| 613 if (il->preview) | |
| 614 { | |
| 615 exif_free_preview(il->mapped_file); | |
| 616 } | |
| 617 else | |
| 618 { | |
| 619 munmap(il->mapped_file, il->bytes_total); | |
| 620 } | |
| 621 il->mapped_file = NULL; | |
| 622 } | |
| 623 } | |
| 9 | 624 |
| 1008 | 625 |
| 1014 | 626 /* |
| 627 static gint image_loader_setup(ImageLoader *il) | |
| 628 { | |
| 629 if (!image_loader_setup_source(il)) return FALSE; | |
| 630 | |
| 631 return image_loader_begin(il); | |
| 632 } | |
| 633 */ | |
| 634 | |
| 635 static void image_loader_stop(ImageLoader *il) | |
| 636 { | |
| 637 if (!il) return; | |
| 638 | |
| 639 if (il->idle_id != -1) | |
| 640 { | |
| 641 g_source_remove(il->idle_id); | |
| 642 il->idle_id = -1; | |
| 643 } | |
| 644 | |
| 1015 | 645 if (il->thread) |
| 646 { | |
| 647 il->stopping = TRUE; | |
| 648 g_thread_join(il->thread); | |
| 649 } | |
| 1014 | 650 |
| 651 image_loader_stop_loader(il); | |
| 652 image_loader_stop_source(il); | |
| 653 | |
| 654 } | |
| 655 | |
| 656 /**************************************************************************************/ | |
| 657 /* execution via idle calls */ | |
| 9 | 658 |
| 1014 | 659 static gint image_loader_idle_cb(gpointer data) |
| 660 { | |
| 661 gint ret = FALSE; | |
| 662 ImageLoader *il = data; | |
| 663 if (il->idle_id != -1) | |
| 664 { | |
| 665 ret = image_loader_continue(il); | |
| 666 } | |
| 667 if (!ret) | |
| 668 { | |
| 669 image_loader_stop_source(il); | |
| 670 } | |
| 671 return ret; | |
| 672 } | |
| 673 | |
| 674 | |
| 675 gint image_loader_start_idle(ImageLoader *il) | |
| 676 { | |
| 677 gint ret; | |
| 678 if (!il) return FALSE; | |
|
14
25335c62cd9b
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
679 |
| 1014 | 680 if (!il->fd) return FALSE; |
| 681 | |
| 682 if (!image_loader_setup_source(il)) return FALSE; | |
| 683 | |
| 684 ret = image_loader_begin(il); | |
| 685 | |
| 686 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL); | |
| 687 return ret; | |
| 688 } | |
| 689 | |
| 690 /**************************************************************************************/ | |
| 1015 | 691 /* execution via thread */ |
| 692 | |
| 693 gpointer image_loader_thread_run(gpointer data) | |
| 694 { | |
| 695 ImageLoader *il = data; | |
| 696 gint cont = image_loader_begin(il); | |
| 697 | |
| 698 while (cont && !il->done && !il->stopping) | |
| 699 { | |
| 700 cont = image_loader_continue(il); | |
| 701 } | |
| 702 return NULL; | |
| 703 } | |
| 704 | |
| 705 gint image_loader_start_thread(ImageLoader *il) | |
| 706 { | |
| 707 if (!il) return FALSE; | |
| 708 | |
| 709 if (!il->fd) return FALSE; | |
| 710 | |
| 711 if (!image_loader_setup_source(il)) return FALSE; | |
| 712 | |
| 713 il->thread = g_thread_create(image_loader_thread_run, il, TRUE, NULL); | |
| 714 if (!il->thread) return FALSE; | |
| 715 | |
| 716 return TRUE; | |
| 717 } | |
| 718 | |
| 719 | |
| 720 /**************************************************************************************/ | |
| 1014 | 721 /* public interface */ |
| 722 | |
| 723 | |
| 724 gint image_loader_start(ImageLoader *il) | |
| 725 { | |
| 726 if (!il) return FALSE; | |
| 727 | |
| 728 if (!il->fd) return FALSE; | |
| 729 | |
| 1015 | 730 #ifdef HAVE_GTHREAD |
| 731 return image_loader_start_thread(il); | |
| 732 #else | |
| 1014 | 733 return image_loader_start_idle(il); |
| 1015 | 734 #endif |
| 9 | 735 } |
| 736 | |
| 737 | |
| 738 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */ | |
| 739 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il) | |
| 740 { | |
| 1015 | 741 GdkPixbuf *ret; |
| 9 | 742 if (!il) return NULL; |
| 1015 | 743 |
| 744 g_mutex_lock(il->data_mutex); | |
| 745 ret = il->pixbuf; | |
| 746 g_mutex_unlock(il->data_mutex); | |
| 747 return ret; | |
| 9 | 748 } |
| 749 | |
| 750 gchar *image_loader_get_format(ImageLoader *il) | |
| 751 { | |
| 752 GdkPixbufFormat *format; | |
| 753 gchar **mimev; | |
| 754 gchar *mime; | |
| 755 | |
| 756 if (!il || !il->loader) return NULL; | |
| 757 | |
| 758 format = gdk_pixbuf_loader_get_format(il->loader); | |
| 759 if (!format) return NULL; | |
| 760 | |
| 761 mimev = gdk_pixbuf_format_get_mime_types(format); | |
| 762 if (!mimev) return NULL; | |
| 763 | |
| 764 /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */ | |
| 765 mime = g_strdup(mimev[0]); | |
| 766 g_strfreev(mimev); | |
| 767 | |
| 768 return mime; | |
| 769 } | |
| 770 | |
| 771 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height) | |
| 772 { | |
| 773 if (!il) return; | |
| 774 | |
| 1015 | 775 g_mutex_lock(il->data_mutex); |
| 9 | 776 il->requested_width = width; |
| 777 il->requested_height = height; | |
| 1015 | 778 g_mutex_unlock(il->data_mutex); |
| 9 | 779 } |
| 780 | |
|
413
9e521adbf312
Add two new options to control image read buffer at runtime.
zas_
parents:
281
diff
changeset
|
781 void image_loader_set_buffer_size(ImageLoader *il, guint count) |
| 9 | 782 { |
| 783 if (!il) return; | |
| 784 | |
| 1015 | 785 g_mutex_lock(il->data_mutex); |
|
413
9e521adbf312
Add two new options to control image read buffer at runtime.
zas_
parents:
281
diff
changeset
|
786 il->idle_read_loop_count = count ? count : 1; |
| 1015 | 787 g_mutex_unlock(il->data_mutex); |
| 9 | 788 } |
| 789 | |
| 790 void image_loader_set_priority(ImageLoader *il, gint priority) | |
| 791 { | |
| 792 if (!il) return; | |
| 793 | |
| 1015 | 794 g_mutex_lock(il->data_mutex); |
| 9 | 795 il->idle_priority = priority; |
| 1015 | 796 g_mutex_unlock(il->data_mutex); |
| 9 | 797 } |
| 798 | |
| 799 | |
| 800 gdouble image_loader_get_percent(ImageLoader *il) | |
| 801 { | |
| 1015 | 802 gdouble ret; |
| 803 if (!il) return 0.0; | |
| 804 | |
| 805 g_mutex_lock(il->data_mutex); | |
| 806 if (il->bytes_total == 0) | |
| 807 { | |
| 808 ret = 0.0; | |
| 809 } | |
| 810 else | |
| 811 { | |
| 812 ret = (gdouble)il->bytes_read / il->bytes_total; | |
| 813 } | |
| 814 g_mutex_unlock(il->data_mutex); | |
| 815 return ret; | |
| 9 | 816 } |
| 817 | |
| 818 gint image_loader_get_is_done(ImageLoader *il) | |
| 819 { | |
| 1015 | 820 gint ret; |
| 9 | 821 if (!il) return FALSE; |
| 822 | |
| 1015 | 823 g_mutex_lock(il->data_mutex); |
| 824 ret = il->done; | |
| 825 g_mutex_unlock(il->data_mutex); | |
| 826 | |
| 827 return ret; | |
| 9 | 828 } |
| 829 | |
| 1011 | 830 FileData *image_loader_get_fd(ImageLoader *il) |
| 831 { | |
| 1015 | 832 FileData *ret; |
| 1011 | 833 if (!il) return NULL; |
| 834 | |
| 1015 | 835 g_mutex_lock(il->data_mutex); |
| 836 ret = il->fd; | |
| 837 g_mutex_unlock(il->data_mutex); | |
| 838 | |
| 839 return ret; | |
| 1011 | 840 } |
| 841 | |
| 842 gint image_loader_get_shrunk(ImageLoader *il) | |
| 843 { | |
| 1015 | 844 gint ret; |
| 1011 | 845 if (!il) return FALSE; |
| 846 | |
| 1015 | 847 g_mutex_lock(il->data_mutex); |
| 848 ret = il->shrunk; | |
| 849 g_mutex_unlock(il->data_mutex); | |
| 850 return ret; | |
| 1011 | 851 } |
| 852 | |
| 853 | |
| 1014 | 854 /* FIXME - this can be rather slow and blocks until the size is known */ |
| 138 | 855 gint image_load_dimensions(FileData *fd, gint *width, gint *height) |
| 9 | 856 { |
| 857 ImageLoader *il; | |
| 858 gint success; | |
| 859 | |
| 138 | 860 il = image_loader_new(fd); |
| 9 | 861 |
| 1014 | 862 success = image_loader_start_idle(il); |
| 9 | 863 |
| 864 if (success && il->pixbuf) | |
| 865 { | |
| 866 if (width) *width = gdk_pixbuf_get_width(il->pixbuf); | |
| 867 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);; | |
| 868 } | |
| 869 else | |
| 870 { | |
| 871 if (width) *width = -1; | |
| 872 if (height) *height = -1; | |
| 873 } | |
| 874 | |
| 875 image_loader_free(il); | |
| 876 | |
| 877 return success; | |
| 878 } |
