]> Pileus Git - grits/blob - src/plugins/map.c
Add color mapping to map plugin
[grits] / src / plugins / map.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 <time.h>
19 #include <glib/gstdio.h>
20 #include <GL/gl.h>
21
22 #include <gis.h>
23
24 #include "map.h"
25
26 #define MAX_RESOLUTION 500
27 #define TILE_WIDTH     1024
28 #define TILE_HEIGHT    512
29
30 const guchar colormap[][2][4] = {
31         {{0x73, 0x91, 0xad}, {0x73, 0x91, 0xad, 0x20}}, // Oceans
32         {{0xf6, 0xee, 0xee}, {0xf6, 0xee, 0xee, 0x00}}, // Ground
33         {{0xff, 0xff, 0xff}, {0xff, 0xff, 0xff, 0xff}}, // Borders
34         {{0x73, 0x93, 0xad}, {0x73, 0x93, 0xad, 0x40}}, // Lakes
35 };
36
37 struct _LoadTileData {
38         GisPluginMap *self;
39         GisTile      *tile;
40         GdkPixbuf    *pixbuf;
41 };
42 #include <stdlib.h>
43 static gboolean _load_tile_cb(gpointer _data)
44 {
45         struct _LoadTileData *data = _data;
46         GisPluginMap *self   = data->self;
47         GisTile      *tile   = data->tile;
48         GdkPixbuf    *pixbuf = data->pixbuf;
49         g_free(data);
50
51         /* Create Texture */
52         g_debug("GisPluginMap: _load_tile_cb start");
53         guchar   *pixels = gdk_pixbuf_get_pixels(pixbuf);
54         gboolean  alpha  = gdk_pixbuf_get_has_alpha(pixbuf);
55         gint      width  = gdk_pixbuf_get_width(pixbuf);
56         gint      height = gdk_pixbuf_get_height(pixbuf);
57
58         for (int i = 0; i < width*height; i++) {
59                 for (int j = 0; j < G_N_ELEMENTS(colormap); j++) {
60                         if (pixels[i*4+0] == colormap[j][0][0] &&
61                             pixels[i*4+1] == colormap[j][0][1] &&
62                             pixels[i*4+2] == colormap[j][0][2]) {
63                                 pixels[i*4+0] = colormap[j][1][0];
64                                 pixels[i*4+1] = colormap[j][1][1];
65                                 pixels[i*4+2] = colormap[j][1][2];
66                                 pixels[i*4+3] = colormap[j][1][3];
67                                 break;
68                         }
69                 }
70         }
71
72         guint *tex = g_new0(guint, 1);
73         gis_viewer_begin(self->viewer);
74         glGenTextures(1, tex);
75         glBindTexture(GL_TEXTURE_2D, *tex);
76
77         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
78         glPixelStorei(GL_PACK_ALIGNMENT, 1);
79         glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0,
80                         (alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, pixels);
81         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
82         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
83         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
84         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
85         glFlush();
86         gis_viewer_end(self->viewer);
87
88         tile->data = tex;
89         gtk_widget_queue_draw(GTK_WIDGET(self->viewer));
90         g_object_unref(pixbuf);
91         return FALSE;
92 }
93
94 static void _load_tile(GisTile *tile, gpointer _self)
95 {
96         GisPluginMap *self = _self;
97         g_debug("GisPluginMap: _load_tile start %p", g_thread_self());
98         char *path = gis_wms_make_local(self->wms, tile);
99         struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
100         data->self   = self;
101         data->tile   = tile;
102         data->pixbuf = gdk_pixbuf_new_from_file(path, NULL);
103         if (data->pixbuf) {
104                 g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
105         } else {
106                 g_warning("GisPluginMap: _load_tile - Error loading pixbuf %s", path);
107                 g_remove(path);
108         }
109         g_free(path);
110         g_debug("GisPluginMap: _load_tile end %p", g_thread_self());
111 }
112
113 static gboolean _free_tile_cb(gpointer data)
114 {
115         glDeleteTextures(1, data);
116         g_free(data);
117         return FALSE;
118 }
119 static void _free_tile(GisTile *tile, gpointer _self)
120 {
121         GisPluginMap *self = _self;
122         g_debug("GisPluginMap: _free_tile: %p=%d", tile->data, *(guint*)tile->data);
123         g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
124 }
125
126 static gpointer _update_tiles(gpointer _self)
127 {
128         g_debug("GisPluginMap: _update_tiles");
129         GisPluginMap *self = _self;
130         g_mutex_lock(self->mutex);
131         gdouble lat, lon, elev;
132         gis_viewer_get_location(self->viewer, &lat, &lon, &elev);
133         gis_tile_update(self->tiles,
134                         MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
135                         lat, lon, elev,
136                         _load_tile, self);
137         gis_tile_gc(self->tiles, time(NULL)-10,
138                         _free_tile, self);
139         g_mutex_unlock(self->mutex);
140         return NULL;
141 }
142
143 /*************
144  * Callbacks *
145  *************/
146 static void _on_location_changed(GisViewer *viewer,
147                 gdouble lat, gdouble lon, gdouble elev, GisPluginMap *self)
148 {
149         g_thread_create(_update_tiles, self, FALSE, NULL);
150 }
151
152 static gpointer _expose(GisCallback *callback, gpointer _self)
153 {
154         GisPluginMap *self = GIS_PLUGIN_MAP(_self);
155         g_debug("GisPluginMap: expose viewer=%p tiles=%p,%p",
156                         self->viewer, self->tiles, self->tiles->data);
157         gis_viewer_render_tiles(self->viewer, self->tiles);
158         return NULL;
159 }
160
161 /***********
162  * Methods *
163  ***********/
164 GisPluginMap *gis_plugin_map_new(GisViewer *viewer)
165 {
166         g_debug("GisPluginMap: new");
167         GisPluginMap *self = g_object_new(GIS_TYPE_PLUGIN_MAP, NULL);
168         self->viewer = g_object_ref(viewer);
169
170         /* Load initial tiles */
171         _load_tile(self->tiles, self);
172         g_thread_create(_update_tiles, self, FALSE, NULL);
173
174         /* Connect signals */
175         self->sigid = g_signal_connect(self->viewer, "location-changed",
176                         G_CALLBACK(_on_location_changed), self);
177
178         /* Add renderers */
179         GisCallback *callback = gis_callback_new(_expose, self);
180         gis_viewer_add(viewer, GIS_OBJECT(callback), GIS_LEVEL_OVERLAY, 0);
181
182         return self;
183 }
184
185
186 /****************
187  * GObject code *
188  ****************/
189 /* Plugin init */
190 static void gis_plugin_map_plugin_init(GisPluginInterface *iface);
191 G_DEFINE_TYPE_WITH_CODE(GisPluginMap, gis_plugin_map, G_TYPE_OBJECT,
192                 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
193                         gis_plugin_map_plugin_init));
194 static void gis_plugin_map_plugin_init(GisPluginInterface *iface)
195 {
196         g_debug("GisPluginMap: plugin_init");
197         /* Add methods to the interface */
198 }
199 /* Class/Object init */
200 static void gis_plugin_map_init(GisPluginMap *self)
201 {
202         g_debug("GisPluginMap: init");
203         /* Set defaults */
204         self->mutex  = g_mutex_new();
205         self->tiles  = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
206         self->wms    = gis_wms_new(
207                 "http://labs.metacarta.com/wms/vmap0", "basic", "image/png",
208                 "osm/", "png", TILE_WIDTH, TILE_HEIGHT);
209 }
210 static void gis_plugin_map_dispose(GObject *gobject)
211 {
212         g_debug("GisPluginMap: dispose");
213         GisPluginMap *self = GIS_PLUGIN_MAP(gobject);
214         /* Drop references */
215         if (self->viewer) {
216                 g_signal_handler_disconnect(self->viewer, self->sigid);
217                 g_object_unref(self->viewer);
218                 self->viewer = NULL;
219         }
220         G_OBJECT_CLASS(gis_plugin_map_parent_class)->dispose(gobject);
221 }
222 static void gis_plugin_map_finalize(GObject *gobject)
223 {
224         g_debug("GisPluginMap: finalize");
225         GisPluginMap *self = GIS_PLUGIN_MAP(gobject);
226         /* Free data */
227         gis_tile_free(self->tiles, _free_tile, self);
228         gis_wms_free(self->wms);
229         g_mutex_free(self->mutex);
230         G_OBJECT_CLASS(gis_plugin_map_parent_class)->finalize(gobject);
231
232 }
233 static void gis_plugin_map_class_init(GisPluginMapClass *klass)
234 {
235         g_debug("GisPluginMap: class_init");
236         GObjectClass *gobject_class = (GObjectClass*)klass;
237         gobject_class->dispose  = gis_plugin_map_dispose;
238         gobject_class->finalize = gis_plugin_map_finalize;
239 }