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