2 * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.com>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @short_description: Satellite plugin
22 * #GritsPluginSat provides overlays using satellite imagery. This is mostly
23 * provided by NASA's Blue Marble Next Generation.
27 #include <glib/gstdio.h>
33 #define MAX_RESOLUTION 500
34 #define TILE_WIDTH 1024
35 #define TILE_HEIGHT 512
37 struct _LoadTileData {
45 static gboolean _load_tile_cb(gpointer _data)
47 struct _LoadTileData *data = _data;
48 g_debug("GritsPluginSat: _load_tile_cb start");
49 if (data->sat->aborted) {
50 g_debug("GritsPluginSat: _load_tile - aborted");
54 guint *tex = g_new0(guint, 1);
55 glGenTextures(1, tex);
56 glBindTexture(GL_TEXTURE_2D, *tex);
58 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
59 glPixelStorei(GL_PACK_ALIGNMENT, 1);
60 glTexImage2D(GL_TEXTURE_2D, 0, 4, data->width, data->height, 0,
61 (data->alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, data->pixels);
62 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
63 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
64 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
65 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
68 data->tile->data = tex;
69 gtk_widget_queue_draw(GTK_WIDGET(data->sat->viewer));
75 static void _load_tile(GritsTile *tile, gpointer _sat)
77 GritsPluginSat *sat = _sat;
78 g_debug("GritsPluginSat: _load_tile start %p", g_thread_self());
80 g_debug("GritsPluginSat: _load_tile - aborted");
85 gchar *path = grits_wms_fetch(sat->wms, tile, GRITS_ONCE, NULL, NULL);
86 if (!path) return; // Canceled/error
89 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
91 g_warning("GritsPluginSat: _load_tile - Error loading pixbuf %s", path);
98 /* Copy pixbuf data for callback */
99 struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
102 data->pixels = gdk_pixbuf_get_pixels(pixbuf);
103 data->alpha = gdk_pixbuf_get_has_alpha(pixbuf);
104 data->width = gdk_pixbuf_get_width(pixbuf);
105 data->height = gdk_pixbuf_get_height(pixbuf);
106 data->pixels = g_memdup(data->pixels,
107 data->width * data->height * (data->alpha ? 4 : 3));
108 g_object_unref(pixbuf);
112 //gint stride = gdk_pixbuf_get_rowstride(pixbuf);
113 //for (int i = 0; i < border; i++) {
114 // memset(&pixels[( i)*stride], 0xff, stride);
115 // memset(&pixels[(height-i)*stride], 0xff, stride);
117 //for (int i = 0; i < height; i++) {
118 // memset(&pixels[(i*stride)], 0xff, border*4);
119 // memset(&pixels[(i*stride)+((width-border)*4)], 0xff, border*4);
122 /* Load the GL texture from the main thread */
123 g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
124 g_debug("GritsPluginSat: _load_tile end %p", g_thread_self());
127 static gboolean _free_tile_cb(gpointer data)
129 glDeleteTextures(1, data);
133 static void _free_tile(GritsTile *tile, gpointer _sat)
135 g_debug("GritsPluginSat: _free_tile: %p", tile->data);
137 g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
140 static void _update_tiles(gpointer _, gpointer _sat)
142 g_debug("GritsPluginSat: _update_tiles");
143 GritsPluginSat *sat = _sat;
145 grits_viewer_get_location(sat->viewer, &eye.lat, &eye.lon, &eye.elev);
146 grits_tile_update(sat->tiles, &eye,
147 MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
149 grits_tile_gc(sat->tiles, time(NULL)-10,
156 static void _on_location_changed(GritsViewer *viewer,
157 gdouble lat, gdouble lon, gdouble elev, GritsPluginSat *sat)
159 g_thread_pool_push(sat->threads, NULL+1, NULL);
166 * grits_plugin_sat_new:
167 * @viewer: the #GritsViewer to use for drawing
169 * Create a new instance of the satellite plugin.
171 * Returns: the new #GritsPluginSat
173 GritsPluginSat *grits_plugin_sat_new(GritsViewer *viewer)
175 g_debug("GritsPluginSat: new");
176 GritsPluginSat *sat = g_object_new(GRITS_TYPE_PLUGIN_SAT, NULL);
177 sat->viewer = g_object_ref(viewer);
179 /* Load initial tiles */
180 _update_tiles(NULL, sat);
182 /* Connect signals */
183 sat->sigid = g_signal_connect(sat->viewer, "location-changed",
184 G_CALLBACK(_on_location_changed), sat);
187 grits_viewer_add(viewer, GRITS_OBJECT(sat->tiles), GRITS_LEVEL_WORLD, FALSE);
197 static void grits_plugin_sat_plugin_init(GritsPluginInterface *iface);
198 G_DEFINE_TYPE_WITH_CODE(GritsPluginSat, grits_plugin_sat, G_TYPE_OBJECT,
199 G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
200 grits_plugin_sat_plugin_init));
201 static void grits_plugin_sat_plugin_init(GritsPluginInterface *iface)
203 g_debug("GritsPluginSat: plugin_init");
204 /* Add methods to the interface */
206 /* Class/Object init */
207 static void grits_plugin_sat_init(GritsPluginSat *sat)
209 g_debug("GritsPluginSat: init");
211 sat->threads = g_thread_pool_new(_update_tiles, sat, 1, FALSE, NULL);
212 sat->tiles = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
213 sat->wms = grits_wms_new(
214 "http://www.nasa.network.com/wms", "bmng200406", "image/jpeg",
215 "bmng/", "jpg", TILE_WIDTH, TILE_HEIGHT);
216 g_object_ref(sat->tiles);
218 static void grits_plugin_sat_dispose(GObject *gobject)
220 g_debug("GritsPluginSat: dispose");
221 GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject);
223 /* Drop references */
225 GritsViewer *viewer = sat->viewer;
227 g_signal_handler_disconnect(viewer, sat->sigid);
228 grits_viewer_remove(viewer, GRITS_OBJECT(sat->tiles));
229 g_object_unref(sat->tiles);
230 soup_session_abort(sat->wms->http->soup);
231 g_thread_pool_free(sat->threads, TRUE, TRUE);
232 while (gtk_events_pending())
233 gtk_main_iteration();
234 g_object_unref(viewer);
236 G_OBJECT_CLASS(grits_plugin_sat_parent_class)->dispose(gobject);
238 static void grits_plugin_sat_finalize(GObject *gobject)
240 g_debug("GritsPluginSat: finalize");
241 GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject);
243 grits_wms_free(sat->wms);
244 grits_tile_free(sat->tiles, _free_tile, sat);
245 G_OBJECT_CLASS(grits_plugin_sat_parent_class)->finalize(gobject);
248 static void grits_plugin_sat_class_init(GritsPluginSatClass *klass)
250 g_debug("GritsPluginSat: class_init");
251 GObjectClass *gobject_class = (GObjectClass*)klass;
252 gobject_class->dispose = grits_plugin_sat_dispose;
253 gobject_class->finalize = grits_plugin_sat_finalize;