2 * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
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/>.
18 #include <gtk/gtkgl.h>
25 #define MAX_RESOLUTION 500
26 #define TILE_WIDTH 1024
27 #define TILE_HEIGHT 512
30 /* OpenGL has to be first to make gis_opengl_render_tiles happy */
35 static gdouble _height_func(gdouble lat, gdouble lon, gpointer _self)
37 GisPluginSrtm *self = _self;
40 GisTile *tile = gis_tile_find(self->tiles, lat, lon);
43 struct _TileData *data = tile->data;
46 guint16 *bil = data->bil;
52 gdouble ymin = tile->edge.s;
53 gdouble ymax = tile->edge.n;
54 gdouble xmin = tile->edge.w;
55 gdouble xmax = tile->edge.e;
57 gdouble xdist = xmax - xmin;
58 gdouble ydist = ymax - ymin;
60 gdouble x = (lon-xmin)/xdist * w;
61 gdouble y = (1-(lat-ymin)/ydist) * h;
63 gdouble x_rem = x - (int)x;
64 gdouble y_rem = y - (int)y;
68 /* TODO: Fix interpolation at edges:
69 * - Pad these at the edges instead of wrapping/truncating
70 * - Figure out which pixels to index (is 0,0 edge, center, etc) */
71 gint16 px00 = bil[MIN((y_flr ),h-1)*w + MIN((x_flr ),w-1)];
72 gint16 px10 = bil[MIN((y_flr ),h-1)*w + MIN((x_flr+1),w-1)];
73 gint16 px01 = bil[MIN((y_flr+1),h-1)*w + MIN((x_flr ),w-1)];
74 gint16 px11 = bil[MIN((y_flr+1),h-1)*w + MIN((x_flr+1),w-1)];
77 px00 * (1-x_rem) * (1-y_rem) +
78 px10 * ( x_rem) * (1-y_rem) +
79 px01 * (1-x_rem) * ( y_rem) +
80 px11 * ( x_rem) * ( y_rem);
84 /**********************
85 * Loader and Freeers *
86 **********************/
88 #define LOAD_OPENGL FALSE
89 struct _LoadTileData {
94 struct _TileData *data;
96 static guint16 *_load_bil(gchar *path)
99 g_file_get_contents(path, &data, NULL, NULL);
100 g_debug("GisPluginSrtm: load_bil %p", data);
101 return (guint16*)data;
103 static GdkPixbuf *_load_pixbuf(guint16 *bil)
105 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, TILE_WIDTH, TILE_HEIGHT);
106 guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
107 gint stride = gdk_pixbuf_get_rowstride(pixbuf);
108 gint nchan = gdk_pixbuf_get_n_channels(pixbuf);
110 for (int r = 0; r < TILE_HEIGHT; r++) {
111 for (int c = 0; c < TILE_WIDTH; c++) {
112 gint16 value = bil[r*TILE_WIDTH + c];
113 //guchar color = (float)(MAX(value,0))/8848 * 255;
114 guchar color = (float)value/8848 * 255;
115 pixels[r*stride + c*nchan + 0] = color;
116 pixels[r*stride + c*nchan + 1] = color;
117 pixels[r*stride + c*nchan + 2] = color;
119 pixels[r*stride + c*nchan + 3] = 128;
122 g_debug("GisPluginSrtm: load_pixbuf %p", pixbuf);
125 static guint _load_opengl(GdkPixbuf *pixbuf)
128 guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
129 gint alpha = gdk_pixbuf_get_has_alpha(pixbuf);
130 gint nchan = 4; // gdk_pixbuf_get_n_channels(pixbuf);
131 gint width = gdk_pixbuf_get_width(pixbuf);
132 gint height = gdk_pixbuf_get_height(pixbuf);
136 glGenTextures(1, &opengl);
137 glBindTexture(GL_TEXTURE_2D, opengl);
139 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
140 glPixelStorei(GL_PACK_ALIGNMENT, 1);
141 glTexImage2D(GL_TEXTURE_2D, 0, nchan, width, height, 0,
142 (alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, pixels);
143 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
144 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
145 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
146 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
148 g_debug("GisPluginSrtm: load_opengl %d", opengl);
151 static gboolean _load_tile_cb(gpointer _load)
153 struct _LoadTileData *load = _load;
154 g_debug("GisPluginSrtm: _load_tile_cb: %s", load->path);
155 GisPluginSrtm *self = load->self;
156 GisTile *tile = load->tile;
157 GdkPixbuf *pixbuf = load->pixbuf;
158 struct _TileData *data = load->data;
163 data->opengl = _load_opengl(pixbuf);
167 /* Do necessasairy processing */
168 /* TODO: Lock this and move to thread, can remove self from _load then */
170 gis_viewer_set_height_func(self->viewer, tile, _height_func, self, TRUE);
172 /* Cleanup unneeded things */
176 g_object_unref(pixbuf);
180 static void _load_tile(GisTile *tile, gpointer _self)
182 GisPluginSrtm *self = _self;
184 struct _LoadTileData *load = g_new0(struct _LoadTileData, 1);
185 load->path = gis_wms_make_local(self->wms, tile);
186 g_debug("GisPluginSrtm: _load_tile: %s", load->path);
189 load->data = g_new0(struct _TileData, 1);
190 if (LOAD_BIL || LOAD_OPENGL)
191 load->data->bil = _load_bil(load->path);
193 load->pixbuf = _load_pixbuf(load->data->bil);
195 g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, load, NULL);
198 static gboolean _free_tile_cb(gpointer _data)
200 struct _TileData *data = _data;
204 glDeleteTextures(1, &data->opengl);
208 static void _free_tile(GisTile *tile, gpointer _self)
210 GisPluginSrtm *self = _self;
211 g_debug("GisPluginSrtm: _free_tile: %p=%d", tile->data, *(guint*)tile->data);
212 g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
215 static gpointer _update_tiles(gpointer _self)
217 GisPluginSrtm *self = _self;
218 g_mutex_lock(self->mutex);
219 gdouble lat, lon, elev;
220 gis_viewer_get_location(self->viewer, &lat, &lon, &elev);
221 gis_tile_update(self->tiles,
222 MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
225 gis_tile_gc(self->tiles, time(NULL)-10,
227 g_mutex_unlock(self->mutex);
234 static void _on_location_changed(GisViewer *viewer,
235 gdouble lat, gdouble lon, gdouble elev, GisPluginSrtm *self)
237 g_thread_create(_update_tiles, self, FALSE, NULL);
240 static gpointer _expose(GisCallback *callback, gpointer _self)
242 GisPluginSrtm *self = GIS_PLUGIN_SRTM(_self);
243 g_debug("GisPluginSrtm: expose tiles=%p data=%p",
244 self->tiles, self->tiles->data);
246 gis_viewer_render_tiles(self->viewer, self->tiles);
253 GisPluginSrtm *gis_plugin_srtm_new(GisViewer *viewer)
255 g_debug("GisPluginSrtm: new");
256 GisPluginSrtm *self = g_object_new(GIS_TYPE_PLUGIN_SRTM, NULL);
257 self->viewer = viewer;
259 /* Load initial tiles */
260 _load_tile(self->tiles, self);
261 g_thread_create(_update_tiles, self, FALSE, NULL);
263 /* Connect signals */
264 self->sigid = g_signal_connect(self->viewer, "location-changed",
265 G_CALLBACK(_on_location_changed), self);
268 GisCallback *callback = gis_callback_new(_expose, self);
269 gis_viewer_add(viewer, GIS_OBJECT(callback), GIS_LEVEL_WORLD, 0);
279 static void gis_plugin_srtm_plugin_init(GisPluginInterface *iface);
280 G_DEFINE_TYPE_WITH_CODE(GisPluginSrtm, gis_plugin_srtm, G_TYPE_OBJECT,
281 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
282 gis_plugin_srtm_plugin_init));
283 static void gis_plugin_srtm_plugin_init(GisPluginInterface *iface)
285 g_debug("GisPluginSrtm: plugin_init");
286 /* Add methods to the interface */
288 /* Class/Object init */
289 static void gis_plugin_srtm_init(GisPluginSrtm *self)
291 g_debug("GisPluginSrtm: init");
293 self->mutex = g_mutex_new();
294 self->tiles = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
295 self->wms = gis_wms_new(
296 "http://www.nasa.network.com/elev", "srtm30", "application/bil",
297 "srtm/", "bil", TILE_WIDTH, TILE_HEIGHT);
299 static void gis_plugin_srtm_dispose(GObject *gobject)
301 g_debug("GisPluginSrtm: dispose");
302 GisPluginSrtm *self = GIS_PLUGIN_SRTM(gobject);
303 /* Drop references */
304 g_signal_handler_disconnect(self->viewer, self->sigid);
306 gis_viewer_clear_height_func(self->viewer);
307 G_OBJECT_CLASS(gis_plugin_srtm_parent_class)->dispose(gobject);
309 static void gis_plugin_srtm_finalize(GObject *gobject)
311 g_debug("GisPluginSrtm: finalize");
312 GisPluginSrtm *self = GIS_PLUGIN_SRTM(gobject);
314 gis_tile_free(self->tiles, _free_tile, self);
315 gis_wms_free(self->wms);
316 g_mutex_free(self->mutex);
317 G_OBJECT_CLASS(gis_plugin_srtm_parent_class)->finalize(gobject);
320 static void gis_plugin_srtm_class_init(GisPluginSrtmClass *klass)
322 g_debug("GisPluginSrtm: class_init");
323 GObjectClass *gobject_class = (GObjectClass*)klass;
324 gobject_class->dispose = gis_plugin_srtm_dispose;
325 gobject_class->finalize = gis_plugin_srtm_finalize;