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