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