]> Pileus Git - grits/blob - src/plugins/sat.c
Fix compiler warnings
[grits] / src / plugins / sat.c
1 /*
2  * Copyright (C) 2009-2010 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  * #GisPluginSat 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 <string.h>
28 #include <glib/gstdio.h>
29 #include <GL/gl.h>
30
31 #include <gis.h>
32
33 #include "sat.h"
34
35 #define MAX_RESOLUTION 500
36 #define TILE_WIDTH     1024
37 #define TILE_HEIGHT    512
38
39 struct _LoadTileData {
40         GisPluginSat *sat;
41         GisTile      *tile;
42         gchar        *path;
43 };
44 static gboolean _load_tile_cb(gpointer _data)
45 {
46         struct _LoadTileData *data = _data;
47         GisPluginSat *sat  = data->sat;
48         GisTile      *tile = data->tile;
49         gchar        *path = data->path;
50         g_free(data);
51
52         /* Load pixbuf */
53         g_debug("GisPluginSat: _load_tile_cb start");
54         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
55         if (!pixbuf) {
56                 g_warning("GisPluginSat: _load_tile - Error loading pixbuf %s", path);
57                 g_remove(path);
58                 g_free(path);
59                 return FALSE;
60         }
61         g_free(path);
62
63         /* Create Texture */
64         guchar   *pixels = gdk_pixbuf_get_pixels(pixbuf);
65         gboolean  alpha  = gdk_pixbuf_get_has_alpha(pixbuf);
66         gint      width  = gdk_pixbuf_get_width(pixbuf);
67         gint      height = gdk_pixbuf_get_height(pixbuf);
68
69         /* Draw a border */
70         //gint border = 10;
71         //gint stride = gdk_pixbuf_get_rowstride(pixbuf);
72         //for (int i = 0; i < border; i++) {
73         //      memset(&pixels[(       i)*stride], 0xff, stride);
74         //      memset(&pixels[(height-i)*stride], 0xff, stride);
75         //}
76         //for (int i = 0; i < height; i++) {
77         //      memset(&pixels[(i*stride)], 0xff, border*4);
78         //      memset(&pixels[(i*stride)+((width-border)*4)], 0xff, border*4);
79         //}
80
81         guint *tex = g_new0(guint, 1);
82         glGenTextures(1, tex);
83         glBindTexture(GL_TEXTURE_2D, *tex);
84
85         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
86         glPixelStorei(GL_PACK_ALIGNMENT, 1);
87         glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0,
88                         (alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, pixels);
89         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
90         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
91         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
92         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
93         glFlush();
94
95         tile->data = tex;
96         gtk_widget_queue_draw(GTK_WIDGET(sat->viewer));
97         g_object_unref(pixbuf);
98         return FALSE;
99 }
100
101 static void _load_tile(GisTile *tile, gpointer _sat)
102 {
103         GisPluginSat *sat = _sat;
104         g_debug("GisPluginSat: _load_tile start %p", g_thread_self());
105         struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
106         data->sat  = sat;
107         data->tile = tile;
108         data->path = gis_wms_fetch(sat->wms, tile, GIS_ONCE, NULL, NULL);
109         g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
110         g_debug("GisPluginSat: _load_tile end %p", g_thread_self());
111 }
112
113 static gboolean _free_tile_cb(gpointer data)
114 {
115         glDeleteTextures(1, data);
116         g_free(data);
117         return FALSE;
118 }
119 static void _free_tile(GisTile *tile, gpointer _sat)
120 {
121         g_debug("GisPluginSat: _free_tile: %p", tile->data);
122         if (tile->data)
123                 g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
124 }
125
126 static gpointer _update_tiles(gpointer _sat)
127 {
128         g_debug("GisPluginSat: _update_tiles");
129         GisPluginSat *sat = _sat;
130         if (!g_mutex_trylock(sat->mutex))
131                 return NULL;
132         GisPoint eye;
133         gis_viewer_get_location(sat->viewer, &eye.lat, &eye.lon, &eye.elev);
134         gis_tile_update(sat->tiles, &eye,
135                         MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
136                         _load_tile, sat);
137         gis_tile_gc(sat->tiles, time(NULL)-10,
138                         _free_tile, sat);
139         g_mutex_unlock(sat->mutex);
140         return NULL;
141 }
142
143 /*************
144  * Callbacks *
145  *************/
146 static void _on_location_changed(GisViewer *viewer,
147                 gdouble lat, gdouble lon, gdouble elev, GisPluginSat *sat)
148 {
149         g_thread_create(_update_tiles, sat, FALSE, NULL);
150 }
151
152 static gpointer _threaded_init(GisPluginSat *sat)
153 {
154         _load_tile(sat->tiles, sat);
155         _update_tiles(sat);
156         return NULL;
157 }
158
159 /***********
160  * Methods *
161  ***********/
162 /**
163  * gis_plugin_sat_new:
164  * @viewer: the #GisViewer to use for drawing
165  *
166  * Create a new instance of the satellite plugin.
167  *
168  * Returns: the new #GisPluginSat
169  */
170 GisPluginSat *gis_plugin_sat_new(GisViewer *viewer)
171 {
172         g_debug("GisPluginSat: new");
173         GisPluginSat *sat = g_object_new(GIS_TYPE_PLUGIN_SAT, NULL);
174         sat->viewer = g_object_ref(viewer);
175
176         /* Load initial tiles */
177         g_thread_create((GThreadFunc)_threaded_init, sat, FALSE, NULL);
178
179         /* Connect signals */
180         sat->sigid = g_signal_connect(sat->viewer, "location-changed",
181                         G_CALLBACK(_on_location_changed), sat);
182
183         /* Add renderers */
184         gis_viewer_add(viewer, GIS_OBJECT(sat->tiles), GIS_LEVEL_WORLD, FALSE);
185
186         return sat;
187 }
188
189
190 /****************
191  * GObject code *
192  ****************/
193 /* Plugin init */
194 static void gis_plugin_sat_plugin_init(GisPluginInterface *iface);
195 G_DEFINE_TYPE_WITH_CODE(GisPluginSat, gis_plugin_sat, G_TYPE_OBJECT,
196                 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
197                         gis_plugin_sat_plugin_init));
198 static void gis_plugin_sat_plugin_init(GisPluginInterface *iface)
199 {
200         g_debug("GisPluginSat: plugin_init");
201         /* Add methods to the interface */
202 }
203 /* Class/Object init */
204 static void gis_plugin_sat_init(GisPluginSat *sat)
205 {
206         g_debug("GisPluginSat: init");
207         /* Set defaults */
208         sat->mutex = g_mutex_new();
209         sat->tiles = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
210         sat->wms   = gis_wms_new(
211                 "http://www.nasa.network.com/wms", "bmng200406", "image/jpeg",
212                 "bmng/", "jpg", TILE_WIDTH, TILE_HEIGHT);
213 }
214 static void gis_plugin_sat_dispose(GObject *gobject)
215 {
216         g_debug("GisPluginSat: dispose");
217         GisPluginSat *sat = GIS_PLUGIN_SAT(gobject);
218         /* Drop references */
219         if (sat->viewer) {
220                 g_signal_handler_disconnect(sat->viewer, sat->sigid);
221                 g_object_unref(sat->viewer);
222                 sat->viewer = NULL;
223         }
224         G_OBJECT_CLASS(gis_plugin_sat_parent_class)->dispose(gobject);
225 }
226 static void gis_plugin_sat_finalize(GObject *gobject)
227 {
228         g_debug("GisPluginSat: finalize");
229         GisPluginSat *sat = GIS_PLUGIN_SAT(gobject);
230         /* Free data */
231         gis_tile_free(sat->tiles, _free_tile, sat);
232         gis_wms_free(sat->wms);
233         g_mutex_free(sat->mutex);
234         G_OBJECT_CLASS(gis_plugin_sat_parent_class)->finalize(gobject);
235
236 }
237 static void gis_plugin_sat_class_init(GisPluginSatClass *klass)
238 {
239         g_debug("GisPluginSat: class_init");
240         GObjectClass *gobject_class = (GObjectClass*)klass;
241         gobject_class->dispose  = gis_plugin_sat_dispose;
242         gobject_class->finalize = gis_plugin_sat_finalize;
243 }