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