2 * Copyright (C) 2009-2010 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/>.
18 #include <gtk/gtkgl.h>
19 #include <glib/gstdio.h>
26 #define MAX_RESOLUTION 500
27 #define TILE_WIDTH 1024
28 #define TILE_HEIGHT 512
29 #define TILE_SIZE (TILE_WIDTH*TILE_HEIGHT*sizeof(guint16))
32 /* OpenGL has to be first to make gis_opengl_render_tiles happy */
37 static gdouble _height_func(gdouble lat, gdouble lon, gpointer _self)
39 GisPluginElev *self = _self;
42 GisTile *tile = gis_tile_find(self->tiles, lat, lon);
45 struct _TileData *data = tile->data;
48 guint16 *bil = data->bil;
54 gdouble ymin = tile->edge.s;
55 gdouble ymax = tile->edge.n;
56 gdouble xmin = tile->edge.w;
57 gdouble xmax = tile->edge.e;
59 gdouble xdist = xmax - xmin;
60 gdouble ydist = ymax - ymin;
62 gdouble x = (lon-xmin)/xdist * w;
63 gdouble y = (1-(lat-ymin)/ydist) * h;
65 gdouble x_rem = x - (int)x;
66 gdouble y_rem = y - (int)y;
70 //if (lon == 180 || lon == -180)
71 // g_message("lon=%f w=%d min=%f max=%f dist=%f x=%f rem=%f flr=%d",
72 // lon, w, xmin, xmax, xdist, x, x_rem, x_flr);
74 /* TODO: Fix interpolation at edges:
75 * - Pad these at the edges instead of wrapping/truncating
76 * - Figure out which pixels to index (is 0,0 edge, center, etc) */
77 gint16 px00 = bil[MIN((y_flr ),h-1)*w + MIN((x_flr ),w-1)];
78 gint16 px10 = bil[MIN((y_flr ),h-1)*w + MIN((x_flr+1),w-1)];
79 gint16 px01 = bil[MIN((y_flr+1),h-1)*w + MIN((x_flr ),w-1)];
80 gint16 px11 = bil[MIN((y_flr+1),h-1)*w + MIN((x_flr+1),w-1)];
83 px00 * (1-x_rem) * (1-y_rem) +
84 px10 * ( x_rem) * (1-y_rem) +
85 px01 * (1-x_rem) * ( y_rem) +
86 px11 * ( x_rem) * ( y_rem);
90 /**********************
91 * Loader and Freeers *
92 **********************/
94 #define LOAD_OPENGL FALSE
95 struct _LoadTileData {
100 struct _TileData *data;
102 static guint16 *_load_bil(gchar *path)
106 g_file_get_contents(path, &data, &len, NULL);
107 g_debug("GisPluginElev: load_bil %p", data);
108 if (len != TILE_SIZE) {
109 g_warning("GisPluginElev: _load_bil - unexpected tile size %d, != %d",
114 return (guint16*)data;
116 static GdkPixbuf *_load_pixbuf(guint16 *bil)
118 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, TILE_WIDTH, TILE_HEIGHT);
119 guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
120 gint stride = gdk_pixbuf_get_rowstride(pixbuf);
121 gint nchan = gdk_pixbuf_get_n_channels(pixbuf);
123 for (int r = 0; r < TILE_HEIGHT; r++) {
124 for (int c = 0; c < TILE_WIDTH; c++) {
125 gint16 value = bil[r*TILE_WIDTH + c];
126 //guchar color = (float)(MAX(value,0))/8848 * 255;
127 guchar color = (float)value/8848 * 255;
128 pixels[r*stride + c*nchan + 0] = color;
129 pixels[r*stride + c*nchan + 1] = color;
130 pixels[r*stride + c*nchan + 2] = color;
132 pixels[r*stride + c*nchan + 3] = 128;
135 g_debug("GisPluginElev: load_pixbuf %p", pixbuf);
138 static guint _load_opengl(GdkPixbuf *pixbuf)
141 guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
142 gint alpha = gdk_pixbuf_get_has_alpha(pixbuf);
143 gint nchan = 4; // gdk_pixbuf_get_n_channels(pixbuf);
144 gint width = gdk_pixbuf_get_width(pixbuf);
145 gint height = gdk_pixbuf_get_height(pixbuf);
149 glGenTextures(1, &opengl);
150 glBindTexture(GL_TEXTURE_2D, opengl);
152 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
153 glPixelStorei(GL_PACK_ALIGNMENT, 1);
154 glTexImage2D(GL_TEXTURE_2D, 0, nchan, width, height, 0,
155 (alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, pixels);
156 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
157 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
158 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
159 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
161 g_debug("GisPluginElev: load_opengl %d", opengl);
164 static gboolean _load_tile_cb(gpointer _load)
166 struct _LoadTileData *load = _load;
167 g_debug("GisPluginElev: _load_tile_cb: %s", load->path);
168 GisPluginElev *self = load->self;
169 GisTile *tile = load->tile;
170 GdkPixbuf *pixbuf = load->pixbuf;
171 struct _TileData *data = load->data;
176 data->opengl = _load_opengl(pixbuf);
180 /* Do necessasairy processing */
181 /* TODO: Lock this and move to thread, can remove self from _load then */
183 gis_viewer_set_height_func(self->viewer, tile, _height_func, self, TRUE);
185 /* Cleanup unneeded things */
189 g_object_unref(pixbuf);
193 static void _load_tile(GisTile *tile, gpointer _self)
195 GisPluginElev *self = _self;
197 struct _LoadTileData *load = g_new0(struct _LoadTileData, 1);
198 load->path = gis_wms_fetch(self->wms, tile, GIS_ONCE, NULL, NULL);
199 g_debug("GisPluginElev: _load_tile: %s", load->path);
202 load->data = g_new0(struct _TileData, 1);
203 if (LOAD_BIL || LOAD_OPENGL) {
204 load->data->bil = _load_bil(load->path);
205 if (!load->data->bil) {
206 g_remove(load->path);
214 load->pixbuf = _load_pixbuf(load->data->bil);
217 g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, load, NULL);
220 static gboolean _free_tile_cb(gpointer _data)
222 struct _TileData *data = _data;
226 glDeleteTextures(1, &data->opengl);
230 static void _free_tile(GisTile *tile, gpointer _self)
232 GisPluginElev *self = _self;
233 g_debug("GisPluginElev: _free_tile: %p", tile->data);
235 g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
238 static gpointer _update_tiles(gpointer _self)
240 GisPluginElev *self = _self;
241 g_mutex_lock(self->mutex);
242 gdouble lat, lon, elev;
243 gis_viewer_get_location(self->viewer, &lat, &lon, &elev);
244 gis_tile_update(self->tiles,
245 MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
248 gis_tile_gc(self->tiles, time(NULL)-10,
250 g_mutex_unlock(self->mutex);
257 static void _on_location_changed(GisViewer *viewer,
258 gdouble lat, gdouble lon, gdouble elev, GisPluginElev *self)
260 g_thread_create(_update_tiles, self, FALSE, NULL);
266 GisPluginElev *gis_plugin_elev_new(GisViewer *viewer)
268 g_debug("GisPluginElev: new");
269 GisPluginElev *self = g_object_new(GIS_TYPE_PLUGIN_ELEV, NULL);
270 self->viewer = g_object_ref(viewer);
272 /* Load initial tiles */
273 _load_tile(self->tiles, self);
274 g_thread_create(_update_tiles, self, FALSE, NULL);
276 /* Connect signals */
277 self->sigid = g_signal_connect(self->viewer, "location-changed",
278 G_CALLBACK(_on_location_changed), self);
282 gis_viewer_add(viewer, GIS_OBJECT(self->tiles), GIS_LEVEL_WORLD, 0);
292 static void gis_plugin_elev_plugin_init(GisPluginInterface *iface);
293 G_DEFINE_TYPE_WITH_CODE(GisPluginElev, gis_plugin_elev, G_TYPE_OBJECT,
294 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
295 gis_plugin_elev_plugin_init));
296 static void gis_plugin_elev_plugin_init(GisPluginInterface *iface)
298 g_debug("GisPluginElev: plugin_init");
299 /* Add methods to the interface */
301 /* Class/Object init */
302 static void gis_plugin_elev_init(GisPluginElev *self)
304 g_debug("GisPluginElev: init");
306 self->mutex = g_mutex_new();
307 self->tiles = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
308 self->wms = gis_wms_new(
309 "http://www.nasa.network.com/srtm", "srtm30", "application/bil",
310 "srtm/", "bil", TILE_WIDTH, TILE_HEIGHT);
312 static void gis_plugin_elev_dispose(GObject *gobject)
314 g_debug("GisPluginElev: dispose");
315 GisPluginElev *self = GIS_PLUGIN_ELEV(gobject);
316 /* Drop references */
318 gis_viewer_clear_height_func(self->viewer);
320 g_signal_handler_disconnect(self->viewer, self->sigid);
321 g_object_unref(self->viewer);
324 G_OBJECT_CLASS(gis_plugin_elev_parent_class)->dispose(gobject);
326 static void gis_plugin_elev_finalize(GObject *gobject)
328 g_debug("GisPluginElev: finalize");
329 GisPluginElev *self = GIS_PLUGIN_ELEV(gobject);
331 gis_tile_free(self->tiles, _free_tile, self);
332 gis_wms_free(self->wms);
333 g_mutex_free(self->mutex);
334 G_OBJECT_CLASS(gis_plugin_elev_parent_class)->finalize(gobject);
337 static void gis_plugin_elev_class_init(GisPluginElevClass *klass)
339 g_debug("GisPluginElev: class_init");
340 GObjectClass *gobject_class = (GObjectClass*)klass;
341 gobject_class->dispose = gis_plugin_elev_dispose;
342 gobject_class->finalize = gis_plugin_elev_finalize;