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