]> Pileus Git - grits/blob - src/plugins/sat.c
Update copyright notices
[grits] / src / plugins / sat.c
1 /*
2  * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.com>
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 /**
19  * SECTION:sat
20  * @short_description: Satellite plugin
21  *
22  * #GritsPluginSat provides overlays using satellite imagery. This is mostly
23  * provided by NASA's Blue Marble Next Generation.
24  */
25
26 #include <time.h>
27 #include <glib/gstdio.h>
28 #include <GL/gl.h>
29
30 #include <grits.h>
31
32 #include "sat.h"
33
34 #define MAX_RESOLUTION 500
35 #define TILE_WIDTH     1024
36 #define TILE_HEIGHT    512
37
38 struct _LoadTileData {
39         GritsPluginSat *sat;
40         GritsTile      *tile;
41         guint8         *pixels;
42         gboolean        alpha;
43         gint            width;
44         gint            height;
45 };
46 static gboolean _load_tile_cb(gpointer _data)
47 {
48         struct _LoadTileData *data = _data;
49         g_debug("GritsPluginSat: _load_tile_cb start");
50
51         guint *tex = g_new0(guint, 1);
52         glGenTextures(1, tex);
53         glBindTexture(GL_TEXTURE_2D, *tex);
54
55         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
56         glPixelStorei(GL_PACK_ALIGNMENT, 1);
57         glTexImage2D(GL_TEXTURE_2D, 0, 4, data->width, data->height, 0,
58                         (data->alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, data->pixels);
59         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
60         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
61         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
62         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
63         glFlush();
64
65         data->tile->data = tex;
66         gtk_widget_queue_draw(GTK_WIDGET(data->sat->viewer));
67         g_free(data->pixels);
68         g_free(data);
69         return FALSE;
70 }
71
72 static void _load_tile(GritsTile *tile, gpointer _sat)
73 {
74         GritsPluginSat *sat = _sat;
75         g_debug("GritsPluginSat: _load_tile start %p", g_thread_self());
76         if (sat->aborted) {
77                 g_debug("GritsPluginSat: _load_tile - aborted");
78                 return;
79         }
80
81         /* Download tile */
82         gchar *path = grits_wms_fetch(sat->wms, tile, GRITS_ONCE, NULL, NULL);
83         if (!path) return; // Canceled/error
84
85         /* Load pixbuf */
86         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
87         if (!pixbuf) {
88                 g_warning("GritsPluginSat: _load_tile - Error loading pixbuf %s", path);
89                 g_remove(path);
90                 g_free(path);
91                 return;
92         }
93         g_free(path);
94
95         /* Copy pixbuf data for callback */
96         struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
97         data->sat    = sat;
98         data->tile   = tile;
99         data->pixels = gdk_pixbuf_get_pixels(pixbuf);
100         data->alpha  = gdk_pixbuf_get_has_alpha(pixbuf);
101         data->width  = gdk_pixbuf_get_width(pixbuf);
102         data->height = gdk_pixbuf_get_height(pixbuf);
103         data->pixels = g_memdup(data->pixels,
104                         data->width * data->height * (data->alpha ? 4 : 3));
105         g_object_unref(pixbuf);
106
107         /* Draw a border */
108         //gint border = 10;
109         //gint stride = gdk_pixbuf_get_rowstride(pixbuf);
110         //for (int i = 0; i < border; i++) {
111         //      memset(&pixels[(       i)*stride], 0xff, stride);
112         //      memset(&pixels[(height-i)*stride], 0xff, stride);
113         //}
114         //for (int i = 0; i < height; i++) {
115         //      memset(&pixels[(i*stride)], 0xff, border*4);
116         //      memset(&pixels[(i*stride)+((width-border)*4)], 0xff, border*4);
117         //}
118
119         /* Load the GL texture from the main thread */
120         g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
121         g_debug("GritsPluginSat: _load_tile end %p", g_thread_self());
122 }
123
124 static gboolean _free_tile_cb(gpointer data)
125 {
126         glDeleteTextures(1, data);
127         g_free(data);
128         return FALSE;
129 }
130 static void _free_tile(GritsTile *tile, gpointer _sat)
131 {
132         g_debug("GritsPluginSat: _free_tile: %p", tile->data);
133         if (tile->data)
134                 g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
135 }
136
137 static void _update_tiles(gpointer _, gpointer _sat)
138 {
139         g_debug("GritsPluginSat: _update_tiles");
140         GritsPluginSat *sat = _sat;
141         GritsPoint eye;
142         grits_viewer_get_location(sat->viewer, &eye.lat, &eye.lon, &eye.elev);
143         grits_tile_update(sat->tiles, &eye,
144                         MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
145                         _load_tile, sat);
146         grits_tile_gc(sat->tiles, time(NULL)-10,
147                         _free_tile, sat);
148 }
149
150 /*************
151  * Callbacks *
152  *************/
153 static void _on_location_changed(GritsViewer *viewer,
154                 gdouble lat, gdouble lon, gdouble elev, GritsPluginSat *sat)
155 {
156         g_thread_pool_push(sat->threads, NULL+1, NULL);
157 }
158
159 /***********
160  * Methods *
161  ***********/
162 /**
163  * grits_plugin_sat_new:
164  * @viewer: the #GritsViewer to use for drawing
165  *
166  * Create a new instance of the satellite plugin.
167  *
168  * Returns: the new #GritsPluginSat
169  */
170 GritsPluginSat *grits_plugin_sat_new(GritsViewer *viewer)
171 {
172         g_debug("GritsPluginSat: new");
173         GritsPluginSat *sat = g_object_new(GRITS_TYPE_PLUGIN_SAT, NULL);
174         sat->viewer = g_object_ref(viewer);
175
176         /* Load initial tiles */
177         _load_tile(sat->tiles, sat);
178         _update_tiles(NULL, sat);
179
180         /* Connect signals */
181         sat->sigid = g_signal_connect(sat->viewer, "location-changed",
182                         G_CALLBACK(_on_location_changed), sat);
183
184         /* Add renderers */
185         grits_viewer_add(viewer, GRITS_OBJECT(sat->tiles), GRITS_LEVEL_WORLD, FALSE);
186
187         return sat;
188 }
189
190
191 /****************
192  * GObject code *
193  ****************/
194 /* Plugin init */
195 static void grits_plugin_sat_plugin_init(GritsPluginInterface *iface);
196 G_DEFINE_TYPE_WITH_CODE(GritsPluginSat, grits_plugin_sat, G_TYPE_OBJECT,
197                 G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
198                         grits_plugin_sat_plugin_init));
199 static void grits_plugin_sat_plugin_init(GritsPluginInterface *iface)
200 {
201         g_debug("GritsPluginSat: plugin_init");
202         /* Add methods to the interface */
203 }
204 /* Class/Object init */
205 static void grits_plugin_sat_init(GritsPluginSat *sat)
206 {
207         g_debug("GritsPluginSat: init");
208         /* Set defaults */
209         sat->threads = g_thread_pool_new(_update_tiles, sat, 1, FALSE, NULL);
210         sat->tiles = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
211         sat->wms   = grits_wms_new(
212                 "http://www.nasa.network.com/wms", "bmng200406", "image/jpeg",
213                 "bmng/", "jpg", TILE_WIDTH, TILE_HEIGHT);
214         g_object_ref(sat->tiles);
215 }
216 static void grits_plugin_sat_dispose(GObject *gobject)
217 {
218         g_debug("GritsPluginSat: dispose");
219         GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject);
220         sat->aborted = TRUE;
221         /* Drop references */
222         if (sat->viewer) {
223                 g_signal_handler_disconnect(sat->viewer, sat->sigid);
224                 grits_viewer_remove(sat->viewer, GRITS_OBJECT(sat->tiles));
225                 soup_session_abort(sat->wms->http->soup);
226                 g_thread_pool_free(sat->threads, TRUE, TRUE);
227                 while (gtk_events_pending())
228                         gtk_main_iteration();
229                 g_object_unref(sat->viewer);
230                 sat->viewer = NULL;
231         }
232         G_OBJECT_CLASS(grits_plugin_sat_parent_class)->dispose(gobject);
233 }
234 static void grits_plugin_sat_finalize(GObject *gobject)
235 {
236         g_debug("GritsPluginSat: finalize");
237         GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject);
238         /* Free data */
239         grits_wms_free(sat->wms);
240         grits_tile_free(sat->tiles, _free_tile, sat);
241         G_OBJECT_CLASS(grits_plugin_sat_parent_class)->finalize(gobject);
242
243 }
244 static void grits_plugin_sat_class_init(GritsPluginSatClass *klass)
245 {
246         g_debug("GritsPluginSat: class_init");
247         GObjectClass *gobject_class = (GObjectClass*)klass;
248         gobject_class->dispose  = grits_plugin_sat_dispose;
249         gobject_class->finalize = grits_plugin_sat_finalize;
250 }