]> Pileus Git - grits/blob - src/plugins/elev.c
Convert self to real names
[grits] / src / plugins / elev.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 #include <gtk/gtkgl.h>
19 #include <glib/gstdio.h>
20 #include <GL/gl.h>
21
22 #include <gis.h>
23
24 #include "elev.h"
25
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))
30
31 struct _TileData {
32         /* OpenGL has to be first to make gis_opengl_render_tiles happy */
33         guint      opengl;
34         guint16   *bil;
35 };
36
37 static gdouble _height_func(gdouble lat, gdouble lon, gpointer _elev)
38 {
39         GisPluginElev *elev = _elev;
40         if (!elev) return 0;
41
42         GisTile *tile = gis_tile_find(elev->tiles, lat, lon);
43         if (!tile) return 0;
44
45         struct _TileData *data = tile->data;
46         if (!data) return 0;
47
48         guint16 *bil  = data->bil;
49         if (!bil)  return 0;
50
51         gint w = TILE_WIDTH;
52         gint h = TILE_HEIGHT;
53
54         gdouble ymin  = tile->edge.s;
55         gdouble ymax  = tile->edge.n;
56         gdouble xmin  = tile->edge.w;
57         gdouble xmax  = tile->edge.e;
58
59         gdouble xdist = xmax - xmin;
60         gdouble ydist = ymax - ymin;
61
62         gdouble x =    (lon-xmin)/xdist  * w;
63         gdouble y = (1-(lat-ymin)/ydist) * h;
64
65         gdouble x_rem = x - (int)x;
66         gdouble y_rem = y - (int)y;
67         guint x_flr = (int)x;
68         guint y_flr = (int)y;
69
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);
73
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)];
81
82         return px00 * (1-x_rem) * (1-y_rem) +
83                px10 * (  x_rem) * (1-y_rem) +
84                px01 * (1-x_rem) * (  y_rem) +
85                px11 * (  x_rem) * (  y_rem);
86 }
87
88 /**********************
89  * Loader and Freeers *
90  **********************/
91 #define LOAD_BIL    TRUE
92 #define LOAD_OPENGL FALSE
93 struct _LoadTileData {
94         GisPluginElev    *elev;
95         gchar            *path;
96         GisTile          *tile;
97         GdkPixbuf        *pixbuf;
98         struct _TileData *data;
99 };
100 static guint16 *_load_bil(gchar *path)
101 {
102         gsize len;
103         gchar *data = NULL;
104         g_file_get_contents(path, &data, &len, NULL);
105         g_debug("GisPluginElev: load_bil %p", data);
106         if (len != TILE_SIZE) {
107                 g_warning("GisPluginElev: _load_bil - unexpected tile size %d, != %d",
108                                 len, TILE_SIZE);
109                 g_free(data);
110                 return NULL;
111         }
112         return (guint16*)data;
113 }
114 static GdkPixbuf *_load_pixbuf(guint16 *bil)
115 {
116         GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, TILE_WIDTH, TILE_HEIGHT);
117         guchar    *pixels = gdk_pixbuf_get_pixels(pixbuf);
118         gint       stride = gdk_pixbuf_get_rowstride(pixbuf);
119         gint       nchan  = gdk_pixbuf_get_n_channels(pixbuf);
120
121         for (int r = 0; r < TILE_HEIGHT; r++) {
122                 for (int c = 0; c < TILE_WIDTH; c++) {
123                         gint16 value = bil[r*TILE_WIDTH + c];
124                         //guchar color = (float)(MAX(value,0))/8848 * 255;
125                         guchar color = (float)value/8848 * 255;
126                         pixels[r*stride + c*nchan + 0] = color;
127                         pixels[r*stride + c*nchan + 1] = color;
128                         pixels[r*stride + c*nchan + 2] = color;
129                         if (nchan == 4)
130                                 pixels[r*stride + c*nchan + 3] = 128;
131                 }
132         }
133         g_debug("GisPluginElev: load_pixbuf %p", pixbuf);
134         return pixbuf;
135 }
136 static guint _load_opengl(GdkPixbuf *pixbuf)
137 {
138         /* Load image */
139         guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
140         gint    alpha  = gdk_pixbuf_get_has_alpha(pixbuf);
141         gint    nchan  = 4; // gdk_pixbuf_get_n_channels(pixbuf);
142         gint    width  = gdk_pixbuf_get_width(pixbuf);
143         gint    height = gdk_pixbuf_get_height(pixbuf);
144
145         /* Create Texture */
146         guint opengl;
147         glGenTextures(1, &opengl);
148         glBindTexture(GL_TEXTURE_2D, opengl);
149
150         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
151         glPixelStorei(GL_PACK_ALIGNMENT, 1);
152         glTexImage2D(GL_TEXTURE_2D, 0, nchan, width, height, 0,
153                         (alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, pixels);
154         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
155         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
156         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
157         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
158
159         g_debug("GisPluginElev: load_opengl %d", opengl);
160         return opengl;
161 }
162 static gboolean _load_tile_cb(gpointer _load)
163 {
164         struct _LoadTileData *load = _load;
165         g_debug("GisPluginElev: _load_tile_cb: %s", load->path);
166         GisPluginElev    *elev   = load->elev;
167         GisTile          *tile   = load->tile;
168         GdkPixbuf        *pixbuf = load->pixbuf;
169         struct _TileData *data   = load->data;
170         g_free(load->path);
171         g_free(load);
172
173         if (LOAD_OPENGL)
174                 data->opengl = _load_opengl(pixbuf);
175
176         tile->data = data;
177
178         /* Do necessasairy processing */
179         /* TODO: Lock this and move to thread, can remove elev from _load then */
180         if (LOAD_BIL)
181                 gis_viewer_set_height_func(elev->viewer, tile, _height_func, elev, TRUE);
182
183         /* Cleanup unneeded things */
184         if (!LOAD_BIL)
185                 g_free(data->bil);
186         if (LOAD_OPENGL)
187                 g_object_unref(pixbuf);
188
189         return FALSE;
190 }
191 static void _load_tile(GisTile *tile, gpointer _elev)
192 {
193         GisPluginElev *elev = _elev;
194
195         struct _LoadTileData *load = g_new0(struct _LoadTileData, 1);
196         load->path = gis_wms_fetch(elev->wms, tile, GIS_ONCE, NULL, NULL);
197         g_debug("GisPluginElev: _load_tile: %s", load->path);
198         load->elev = elev;
199         load->tile = tile;
200         load->data = g_new0(struct _TileData, 1);
201         if (LOAD_BIL || LOAD_OPENGL) {
202                 load->data->bil = _load_bil(load->path);
203                 if (!load->data->bil) {
204                         g_remove(load->path);
205                         g_free(load->data);
206                         g_free(load->path);
207                         g_free(load);
208                         return;
209                 }
210         }
211         if (LOAD_OPENGL) {
212                 load->pixbuf = _load_pixbuf(load->data->bil);
213         }
214
215         g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, load, NULL);
216 }
217
218 static gboolean _free_tile_cb(gpointer _data)
219 {
220         struct _TileData *data = _data;
221         if (LOAD_BIL)
222                 g_free(data->bil);
223         if (LOAD_OPENGL)
224                 glDeleteTextures(1, &data->opengl);
225         g_free(data);
226         return FALSE;
227 }
228 static void _free_tile(GisTile *tile, gpointer _elev)
229 {
230         GisPluginElev *elev = _elev;
231         g_debug("GisPluginElev: _free_tile: %p", tile->data);
232         if (tile->data)
233                 g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
234 }
235
236 static gpointer _update_tiles(gpointer _elev)
237 {
238         GisPluginElev *elev = _elev;
239         g_mutex_lock(elev->mutex);
240         gdouble lat, lon, elevation;
241         gis_viewer_get_location(elev->viewer, &lat, &lon, &elevation);
242         gis_tile_update(elev->tiles,
243                         MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
244                         lat, lon, elevation,
245                         _load_tile, elev);
246         gis_tile_gc(elev->tiles, time(NULL)-10,
247                         _free_tile, elev);
248         g_mutex_unlock(elev->mutex);
249         return NULL;
250 }
251
252 /*************
253  * Callbacks *
254  *************/
255 static void _on_location_changed(GisViewer *viewer,
256                 gdouble lat, gdouble lon, gdouble elevation, GisPluginElev *elev)
257 {
258         g_thread_create(_update_tiles, elev, FALSE, NULL);
259 }
260
261 /***********
262  * Methods *
263  ***********/
264 GisPluginElev *gis_plugin_elev_new(GisViewer *viewer)
265 {
266         g_debug("GisPluginElev: new");
267         GisPluginElev *elev = g_object_new(GIS_TYPE_PLUGIN_ELEV, NULL);
268         elev->viewer = g_object_ref(viewer);
269
270         /* Load initial tiles */
271         _load_tile(elev->tiles, elev);
272         g_thread_create(_update_tiles, elev, FALSE, NULL);
273
274         /* Connect signals */
275         elev->sigid = g_signal_connect(elev->viewer, "location-changed",
276                         G_CALLBACK(_on_location_changed), elev);
277
278         /* Add renderers */
279         if (LOAD_OPENGL)
280                 gis_viewer_add(viewer, GIS_OBJECT(elev->tiles), GIS_LEVEL_WORLD, 0);
281
282         return elev;
283 }
284
285
286 /****************
287  * GObject code *
288  ****************/
289 /* Plugin init */
290 static void gis_plugin_elev_plugin_init(GisPluginInterface *iface);
291 G_DEFINE_TYPE_WITH_CODE(GisPluginElev, gis_plugin_elev, G_TYPE_OBJECT,
292                 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
293                         gis_plugin_elev_plugin_init));
294 static void gis_plugin_elev_plugin_init(GisPluginInterface *iface)
295 {
296         g_debug("GisPluginElev: plugin_init");
297         /* Add methods to the interface */
298 }
299 /* Class/Object init */
300 static void gis_plugin_elev_init(GisPluginElev *elev)
301 {
302         g_debug("GisPluginElev: init");
303         /* Set defaults */
304         elev->mutex = g_mutex_new();
305         elev->tiles = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
306         elev->wms   = gis_wms_new(
307                 "http://www.nasa.network.com/elev", "srtm30", "application/bil",
308                 "srtm/", "bil", TILE_WIDTH, TILE_HEIGHT);
309 }
310 static void gis_plugin_elev_dispose(GObject *gobject)
311 {
312         g_debug("GisPluginElev: dispose");
313         GisPluginElev *elev = GIS_PLUGIN_ELEV(gobject);
314         /* Drop references */
315         if (LOAD_BIL)
316                 gis_viewer_clear_height_func(elev->viewer);
317         if (elev->viewer) {
318                 g_signal_handler_disconnect(elev->viewer, elev->sigid);
319                 g_object_unref(elev->viewer);
320                 elev->viewer = NULL;
321         }
322         G_OBJECT_CLASS(gis_plugin_elev_parent_class)->dispose(gobject);
323 }
324 static void gis_plugin_elev_finalize(GObject *gobject)
325 {
326         g_debug("GisPluginElev: finalize");
327         GisPluginElev *elev = GIS_PLUGIN_ELEV(gobject);
328         /* Free data */
329         gis_tile_free(elev->tiles, _free_tile, elev);
330         gis_wms_free(elev->wms);
331         g_mutex_free(elev->mutex);
332         G_OBJECT_CLASS(gis_plugin_elev_parent_class)->finalize(gobject);
333
334 }
335 static void gis_plugin_elev_class_init(GisPluginElevClass *klass)
336 {
337         g_debug("GisPluginElev: class_init");
338         GObjectClass *gobject_class = (GObjectClass*)klass;
339         gobject_class->dispose  = gis_plugin_elev_dispose;
340         gobject_class->finalize = gis_plugin_elev_finalize;
341 }