]> Pileus Git - grits/blob - src/plugins/elev.c
938d77acc1b3ae0e528b2d0788a8324b86c07b10
[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 _self)
38 {
39         GisPluginElev *self = _self;
40         if (!self) return 0;
41
42         GisTile *tile = gis_tile_find(self->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         gdouble elev =
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);
87         return elev;
88 }
89
90 /**********************
91  * Loader and Freeers *
92  **********************/
93 #define LOAD_BIL    TRUE
94 #define LOAD_OPENGL FALSE
95 struct _LoadTileData {
96         GisPluginElev    *self;
97         gchar            *path;
98         GisTile          *tile;
99         GdkPixbuf        *pixbuf;
100         struct _TileData *data;
101 };
102 static guint16 *_load_bil(gchar *path)
103 {
104         gsize len;
105         gchar *data = NULL;
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",
110                                 len, TILE_SIZE);
111                 g_free(data);
112                 return NULL;
113         }
114         return (guint16*)data;
115 }
116 static GdkPixbuf *_load_pixbuf(guint16 *bil)
117 {
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);
122
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;
131                         if (nchan == 4)
132                                 pixels[r*stride + c*nchan + 3] = 128;
133                 }
134         }
135         g_debug("GisPluginElev: load_pixbuf %p", pixbuf);
136         return pixbuf;
137 }
138 static guint _load_opengl(GdkPixbuf *pixbuf)
139 {
140         /* Load image */
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);
146
147         /* Create Texture */
148         guint opengl;
149         glGenTextures(1, &opengl);
150         glBindTexture(GL_TEXTURE_2D, opengl);
151
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);
160
161         g_debug("GisPluginElev: load_opengl %d", opengl);
162         return opengl;
163 }
164 static gboolean _load_tile_cb(gpointer _load)
165 {
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;
172         g_free(load->path);
173         g_free(load);
174
175         if (LOAD_OPENGL)
176                 data->opengl = _load_opengl(pixbuf);
177
178         tile->data = data;
179
180         /* Do necessasairy processing */
181         /* TODO: Lock this and move to thread, can remove self from _load then */
182         if (LOAD_BIL)
183                 gis_viewer_set_height_func(self->viewer, tile, _height_func, self, TRUE);
184
185         /* Cleanup unneeded things */
186         if (!LOAD_BIL)
187                 g_free(data->bil);
188         if (LOAD_OPENGL)
189                 g_object_unref(pixbuf);
190
191         return FALSE;
192 }
193 static void _load_tile(GisTile *tile, gpointer _self)
194 {
195         GisPluginElev *self = _self;
196
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);
200         load->self = self;
201         load->tile = tile;
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);
207                         g_free(load->data);
208                         g_free(load->path);
209                         g_free(load);
210                         return;
211                 }
212         }
213         if (LOAD_OPENGL) {
214                 load->pixbuf = _load_pixbuf(load->data->bil);
215         }
216
217         g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, load, NULL);
218 }
219
220 static gboolean _free_tile_cb(gpointer _data)
221 {
222         struct _TileData *data = _data;
223         if (LOAD_BIL)
224                 g_free(data->bil);
225         if (LOAD_OPENGL)
226                 glDeleteTextures(1, &data->opengl);
227         g_free(data);
228         return FALSE;
229 }
230 static void _free_tile(GisTile *tile, gpointer _self)
231 {
232         GisPluginElev *self = _self;
233         g_debug("GisPluginElev: _free_tile: %p", tile->data);
234         if (tile->data)
235                 g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
236 }
237
238 static gpointer _update_tiles(gpointer _self)
239 {
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,
246                         lat, lon, elev,
247                         _load_tile, self);
248         gis_tile_gc(self->tiles, time(NULL)-10,
249                         _free_tile, self);
250         g_mutex_unlock(self->mutex);
251         return NULL;
252 }
253
254 /*************
255  * Callbacks *
256  *************/
257 static void _on_location_changed(GisViewer *viewer,
258                 gdouble lat, gdouble lon, gdouble elev, GisPluginElev *self)
259 {
260         g_thread_create(_update_tiles, self, FALSE, NULL);
261 }
262
263 /***********
264  * Methods *
265  ***********/
266 GisPluginElev *gis_plugin_elev_new(GisViewer *viewer)
267 {
268         g_debug("GisPluginElev: new");
269         GisPluginElev *self = g_object_new(GIS_TYPE_PLUGIN_ELEV, NULL);
270         self->viewer = g_object_ref(viewer);
271
272         /* Load initial tiles */
273         _load_tile(self->tiles, self);
274         g_thread_create(_update_tiles, self, FALSE, NULL);
275
276         /* Connect signals */
277         self->sigid = g_signal_connect(self->viewer, "location-changed",
278                         G_CALLBACK(_on_location_changed), self);
279
280         /* Add renderers */
281         if (LOAD_OPENGL)
282                 gis_viewer_add(viewer, GIS_OBJECT(self->tiles), GIS_LEVEL_WORLD, 0);
283
284         return self;
285 }
286
287
288 /****************
289  * GObject code *
290  ****************/
291 /* Plugin init */
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)
297 {
298         g_debug("GisPluginElev: plugin_init");
299         /* Add methods to the interface */
300 }
301 /* Class/Object init */
302 static void gis_plugin_elev_init(GisPluginElev *self)
303 {
304         g_debug("GisPluginElev: init");
305         /* Set defaults */
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/elev", "srtm30", "application/bil",
310                 "srtm/", "bil", TILE_WIDTH, TILE_HEIGHT);
311 }
312 static void gis_plugin_elev_dispose(GObject *gobject)
313 {
314         g_debug("GisPluginElev: dispose");
315         GisPluginElev *self = GIS_PLUGIN_ELEV(gobject);
316         /* Drop references */
317         if (LOAD_BIL)
318                 gis_viewer_clear_height_func(self->viewer);
319         if (self->viewer) {
320                 g_signal_handler_disconnect(self->viewer, self->sigid);
321                 g_object_unref(self->viewer);
322                 self->viewer = NULL;
323         }
324         G_OBJECT_CLASS(gis_plugin_elev_parent_class)->dispose(gobject);
325 }
326 static void gis_plugin_elev_finalize(GObject *gobject)
327 {
328         g_debug("GisPluginElev: finalize");
329         GisPluginElev *self = GIS_PLUGIN_ELEV(gobject);
330         /* Free data */
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);
335
336 }
337 static void gis_plugin_elev_class_init(GisPluginElevClass *klass)
338 {
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;
343 }