]> Pileus Git - grits/blob - src/plugins/map.c
Convert self to real names
[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         {{0xff, 0xe1, 0x80}, {0xff, 0xe1, 0x80, 0x60}}, // Cities
36 };
37
38 struct _LoadTileData {
39         GisPluginMap *map;
40         GisTile      *tile;
41         GdkPixbuf    *pixbuf;
42 };
43 #include <stdlib.h>
44 static gboolean _load_tile_cb(gpointer _data)
45 {
46         struct _LoadTileData *data = _data;
47         GisPluginMap *map    = data->map;
48         GisTile      *tile   = data->tile;
49         GdkPixbuf    *pixbuf = data->pixbuf;
50         g_free(data);
51
52         /* Create Texture */
53         g_debug("GisPluginMap: _load_tile_cb start");
54         guchar   *pixels = gdk_pixbuf_get_pixels(pixbuf);
55         gboolean  alpha  = gdk_pixbuf_get_has_alpha(pixbuf);
56         gint      width  = gdk_pixbuf_get_width(pixbuf);
57         gint      height = gdk_pixbuf_get_height(pixbuf);
58
59         for (int i = 0; i < width*height; i++) {
60                 for (int j = 0; j < G_N_ELEMENTS(colormap); j++) {
61                         if (pixels[i*4+0] == colormap[j][0][0] &&
62                             pixels[i*4+1] == colormap[j][0][1] &&
63                             pixels[i*4+2] == colormap[j][0][2]) {
64                                 pixels[i*4+0] = colormap[j][1][0];
65                                 pixels[i*4+1] = colormap[j][1][1];
66                                 pixels[i*4+2] = colormap[j][1][2];
67                                 pixels[i*4+3] = colormap[j][1][3];
68                                 break;
69                         }
70                 }
71         }
72
73         guint *tex = g_new0(guint, 1);
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_LINEAR);
82         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
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
87         tile->data = tex;
88         gtk_widget_queue_draw(GTK_WIDGET(map->viewer));
89         g_object_unref(pixbuf);
90         return FALSE;
91 }
92
93 static void _load_tile(GisTile *tile, gpointer _map)
94 {
95         GisPluginMap *map = _map;
96         g_debug("GisPluginMap: _load_tile start %p", g_thread_self());
97         char *path = gis_wms_fetch(map->wms, tile, GIS_ONCE, NULL, NULL);
98         struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
99         data->map    = map;
100         data->tile   = tile;
101         data->pixbuf = gdk_pixbuf_new_from_file(path, NULL);
102         if (data->pixbuf) {
103                 g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
104         } else {
105                 g_warning("GisPluginMap: _load_tile - Error loading pixbuf %s", path);
106                 g_remove(path);
107         }
108         g_free(path);
109         g_debug("GisPluginMap: _load_tile end %p", g_thread_self());
110 }
111
112 static gboolean _free_tile_cb(gpointer data)
113 {
114         glDeleteTextures(1, data);
115         g_free(data);
116         return FALSE;
117 }
118 static void _free_tile(GisTile *tile, gpointer _map)
119 {
120         GisPluginMap *map = _map;
121         g_debug("GisPluginMap: _free_tile: %p", tile->data);
122         g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
123 }
124
125 static gpointer _update_tiles(gpointer _map)
126 {
127         g_debug("GisPluginMap: _update_tiles");
128         GisPluginMap *map = _map;
129         g_mutex_lock(map->mutex);
130         gdouble lat, lon, elev;
131         gis_viewer_get_location(map->viewer, &lat, &lon, &elev);
132         gis_tile_update(map->tiles,
133                         MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
134                         lat, lon, elev,
135                         _load_tile, map);
136         gis_tile_gc(map->tiles, time(NULL)-10,
137                         _free_tile, map);
138         g_mutex_unlock(map->mutex);
139         return NULL;
140 }
141
142 /*************
143  * Callbacks *
144  *************/
145 static void _on_location_changed(GisViewer *viewer,
146                 gdouble lat, gdouble lon, gdouble elev, GisPluginMap *map)
147 {
148         g_thread_create(_update_tiles, map, FALSE, NULL);
149 }
150
151 /***********
152  * Methods *
153  ***********/
154 GisPluginMap *gis_plugin_map_new(GisViewer *viewer)
155 {
156         g_debug("GisPluginMap: new");
157         GisPluginMap *map = g_object_new(GIS_TYPE_PLUGIN_MAP, NULL);
158         map->viewer = g_object_ref(viewer);
159
160         /* Load initial tiles */
161         _load_tile(map->tiles, map);
162         g_thread_create(_update_tiles, map, FALSE, NULL);
163
164         /* Connect signals */
165         map->sigid = g_signal_connect(map->viewer, "location-changed",
166                         G_CALLBACK(_on_location_changed), map);
167
168         /* Add renderers */
169         gis_viewer_add(viewer, GIS_OBJECT(map->tiles), GIS_LEVEL_OVERLAY, 0);
170
171         return map;
172 }
173
174
175 /****************
176  * GObject code *
177  ****************/
178 /* Plugin init */
179 static void gis_plugin_map_plugin_init(GisPluginInterface *iface);
180 G_DEFINE_TYPE_WITH_CODE(GisPluginMap, gis_plugin_map, G_TYPE_OBJECT,
181                 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
182                         gis_plugin_map_plugin_init));
183 static void gis_plugin_map_plugin_init(GisPluginInterface *iface)
184 {
185         g_debug("GisPluginMap: plugin_init");
186         /* Add methods to the interface */
187 }
188 /* Class/Object init */
189 static void gis_plugin_map_init(GisPluginMap *map)
190 {
191         g_debug("GisPluginMap: init");
192         /* Set defaults */
193         map->mutex  = g_mutex_new();
194         map->tiles  = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
195         map->wms    = gis_wms_new(
196                 "http://labs.metacarta.com/wms/vmap0", "basic", "image/png",
197                 "osm/", "png", TILE_WIDTH, TILE_HEIGHT);
198 }
199 static void gis_plugin_map_dispose(GObject *gobject)
200 {
201         g_debug("GisPluginMap: dispose");
202         GisPluginMap *map = GIS_PLUGIN_MAP(gobject);
203         /* Drop references */
204         if (map->viewer) {
205                 g_signal_handler_disconnect(map->viewer, map->sigid);
206                 g_object_unref(map->viewer);
207                 map->viewer = NULL;
208         }
209         G_OBJECT_CLASS(gis_plugin_map_parent_class)->dispose(gobject);
210 }
211 static void gis_plugin_map_finalize(GObject *gobject)
212 {
213         g_debug("GisPluginMap: finalize");
214         GisPluginMap *map = GIS_PLUGIN_MAP(gobject);
215         /* Free data */
216         gis_tile_free(map->tiles, _free_tile, map);
217         gis_wms_free(map->wms);
218         g_mutex_free(map->mutex);
219         G_OBJECT_CLASS(gis_plugin_map_parent_class)->finalize(gobject);
220
221 }
222 static void gis_plugin_map_class_init(GisPluginMapClass *klass)
223 {
224         g_debug("GisPluginMap: class_init");
225         GObjectClass *gobject_class = (GObjectClass*)klass;
226         gobject_class->dispose  = gis_plugin_map_dispose;
227         gobject_class->finalize = gis_plugin_map_finalize;
228 }