]> Pileus Git - grits/blob - src/plugin-radar.c
Removing remaining GVFS dependencies.
[grits] / src / plugin-radar.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 <config.h>
19 #include <gtk/gtk.h>
20 #include <gtk/gtkgl.h>
21 #include <gio/gio.h>
22 #include <GL/gl.h>
23 #include <math.h>
24 #include <rsl.h>
25
26 #include "aweather-gui.h"
27 #include "plugin-radar.h"
28 #include "data.h"
29
30 static char *nexrad_base = "http://mesonet.agron.iastate.edu/data/";
31
32 /****************
33  * GObject code *
34  ****************/
35 /* Plugin init */
36 static void aweather_radar_plugin_init(AWeatherPluginInterface *iface);
37 static void _aweather_radar_expose(AWeatherPlugin *_radar);
38 G_DEFINE_TYPE_WITH_CODE(AWeatherRadar, aweather_radar, G_TYPE_OBJECT,
39                 G_IMPLEMENT_INTERFACE(AWEATHER_TYPE_PLUGIN,
40                         aweather_radar_plugin_init));
41 static void aweather_radar_plugin_init(AWeatherPluginInterface *iface)
42 {
43         g_debug("AWeatherRadar: plugin_init");
44         /* Add methods to the interface */
45         iface->expose = _aweather_radar_expose;
46 }
47 /* Class/Object init */
48 static void aweather_radar_init(AWeatherRadar *radar)
49 {
50         g_debug("AWeatherRadar: class_init");
51         /* Set defaults */
52         radar->gui  = NULL;
53         radar->soup = NULL;
54 }
55 static void aweather_radar_dispose(GObject *gobject)
56 {
57         g_debug("AWeatherRadar: dispose");
58         AWeatherRadar *self = AWEATHER_RADAR(gobject);
59         /* Drop references */
60         G_OBJECT_CLASS(aweather_radar_parent_class)->dispose(gobject);
61 }
62 static void aweather_radar_finalize(GObject *gobject)
63 {
64         g_debug("AWeatherRadar: finalize");
65         AWeatherRadar *self = AWEATHER_RADAR(gobject);
66         /* Free data */
67         G_OBJECT_CLASS(aweather_radar_parent_class)->finalize(gobject);
68
69 }
70 static void aweather_radar_class_init(AWeatherRadarClass *klass)
71 {
72         g_debug("AWeatherRadar: class_init");
73         GObjectClass *gobject_class = (GObjectClass*)klass;
74         gobject_class->dispose  = aweather_radar_dispose;
75         gobject_class->finalize = aweather_radar_finalize;
76 }
77
78 /**************************
79  * Data loading functions *
80  **************************/
81 /* Convert a sweep to an 2d array of data points */
82 static void bscan_sweep(AWeatherRadar *self, Sweep *sweep, colormap_t *colormap,
83                 guint8 **data, int *width, int *height)
84 {
85         /* Calculate max number of bins */
86         int i, max_bins = 0;
87         for (i = 0; i < sweep->h.nrays; i++)
88                 max_bins = MAX(max_bins, sweep->ray[i]->h.nbins);
89
90         /* Allocate buffer using max number of bins for each ray */
91         guint8 *buf = g_malloc0(sweep->h.nrays * max_bins * 4);
92
93         /* Fill the data */
94         int ri, bi;
95         for (ri = 0; ri < sweep->h.nrays; ri++) {
96                 Ray *ray  = sweep->ray[ri];
97                 for (bi = 0; bi < ray->h.nbins; bi++) {
98                         /* copy RGBA into buffer */
99                         //guint val   = dz_f(ray->range[bi]);
100                         guint8 val   = (guint8)ray->h.f(ray->range[bi]);
101                         guint  buf_i = (ri*max_bins+bi)*4;
102                         buf[buf_i+0] = colormap->data[val][0];
103                         buf[buf_i+1] = colormap->data[val][1];
104                         buf[buf_i+2] = colormap->data[val][2];
105                         buf[buf_i+3] = colormap->data[val][3];
106                         if (val == BADVAL     || val == RFVAL      || val == APFLAG ||
107                             val == NOTFOUND_H || val == NOTFOUND_V || val == NOECHO) {
108                                 buf[buf_i+3] = 0x00; // transparent
109                         }
110                 }
111         }
112
113         /* set output */
114         *width  = max_bins;
115         *height = sweep->h.nrays;
116         *data   = buf;
117 }
118
119 /* Load a sweep as the active texture */
120 static void load_sweep(AWeatherRadar *self, Sweep *sweep)
121 {
122         aweather_gui_gl_begin(self->gui);
123         self->cur_sweep = sweep;
124         int height, width;
125         guint8 *data;
126         bscan_sweep(self, sweep, self->cur_colormap, &data, &width, &height);
127         glDeleteTextures(1, &self->cur_sweep_tex);
128         glGenTextures(1, &self->cur_sweep_tex);
129         glBindTexture(GL_TEXTURE_2D, self->cur_sweep_tex);
130         glPixelStorei(GL_PACK_ALIGNMENT, 1);
131         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
132         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
133         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
134         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0,
135                         GL_RGBA, GL_UNSIGNED_BYTE, data);
136         g_free(data);
137         aweather_gui_gl_redraw(self->gui);
138         aweather_gui_gl_end(self->gui);
139 }
140
141 static void load_colormap(AWeatherRadar *self, gchar *table)
142 {
143         /* Set colormap so we can draw it on expose */
144         for (int i = 0; colormaps[i].name; i++)
145                 if (g_str_equal(colormaps[i].name, table))
146                         self->cur_colormap = &colormaps[i];
147 }
148
149 /* Add selectors to the config area for the sweeps */
150 static void on_sweep_clicked(GtkRadioButton *button, gpointer _self);
151 static void load_radar_gui(AWeatherRadar *self, Radar *radar)
152 {
153         /* Clear existing items */
154         GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body));
155         if (child)
156                 gtk_widget_destroy(child);
157
158         gdouble elev;
159         guint rows = 1, cols = 1, cur_cols;
160         gchar row_label_str[64], col_label_str[64], button_str[64];
161         GtkWidget *row_label, *col_label, *button = NULL, *elev_box = NULL;
162         GtkWidget *table = gtk_table_new(rows, cols, FALSE);
163
164         for (guint vi = 0; vi < radar->h.nvolumes; vi++) {
165                 Volume *vol = radar->v[vi];
166                 if (vol == NULL) continue;
167                 rows++; cols = 1; elev = 0;
168
169                 /* Row label */
170                 g_snprintf(row_label_str, 64, "<b>%s:</b>", vol->h.type_str);
171                 row_label = gtk_label_new(row_label_str);
172                 gtk_label_set_use_markup(GTK_LABEL(row_label), TRUE);
173                 gtk_misc_set_alignment(GTK_MISC(row_label), 1, 0.5);
174                 gtk_table_attach(GTK_TABLE(table), row_label,
175                                 0,1, rows-1,rows, GTK_FILL,GTK_FILL, 5,0);
176
177                 for (guint si = 0; si < vol->h.nsweeps; si++) {
178                         Sweep *sweep = vol->sweep[si];
179                         if (sweep == NULL || sweep->h.elev == 0) continue;
180                         if (sweep->h.elev != elev) {
181                                 cols++;
182                                 elev = sweep->h.elev;
183
184                                 /* Column label */
185                                 g_object_get(table, "n-columns", &cur_cols, NULL);
186                                 if (cols >  cur_cols) {
187                                         g_snprintf(col_label_str, 64, "<b>%.2f°</b>", elev);
188                                         col_label = gtk_label_new(col_label_str);
189                                         gtk_label_set_use_markup(GTK_LABEL(col_label), TRUE);
190                                         gtk_widget_set_size_request(col_label, 40, -1);
191                                         gtk_table_attach(GTK_TABLE(table), col_label,
192                                                         cols-1,cols, 0,1, GTK_FILL,GTK_FILL, 0,0);
193                                 }
194
195                                 elev_box = gtk_hbox_new(TRUE, 0);
196                                 gtk_table_attach(GTK_TABLE(table), elev_box,
197                                                 cols-1,cols, rows-1,rows, GTK_FILL,GTK_FILL, 0,0);
198                         }
199
200
201                         /* Button */
202                         g_snprintf(button_str, 64, "%3.2f", elev);
203                         button = gtk_radio_button_new_with_label_from_widget(
204                                         GTK_RADIO_BUTTON(button), button_str);
205                         gtk_widget_set_size_request(button, -1, 26);
206                         //button = gtk_radio_button_new_from_widget(GTK_RADIO_BUTTON(button));
207                         //gtk_widget_set_size_request(button, -1, 22);
208                         g_object_set(button, "draw-indicator", FALSE, NULL);
209                         gtk_box_pack_end(GTK_BOX(elev_box), button, TRUE, TRUE, 0);
210
211                         g_object_set_data(G_OBJECT(button), "type",  vol->h.type_str);
212                         g_object_set_data(G_OBJECT(button), "sweep", sweep);
213                         g_signal_connect(button, "clicked", G_CALLBACK(on_sweep_clicked), self);
214                 }
215         }
216         gtk_container_add(GTK_CONTAINER(self->config_body), table);
217         gtk_widget_show_all(table);
218 }
219
220 /* Load a radar from a decompressed file */
221 static void load_radar(AWeatherRadar *self, gchar *radar_file)
222 {
223         char *dir  = g_path_get_dirname(radar_file);
224         char *site = g_path_get_basename(dir);
225         g_free(dir);
226         g_debug("AWeatherRadar: load_radar - Loading new radar");
227         RSL_read_these_sweeps("all", NULL);
228         Radar *radar = self->cur_radar = RSL_wsr88d_to_radar(radar_file, site);
229         if (radar == NULL) {
230                 g_warning("fail to load radar: path=%s, site=%s", radar_file, site);
231                 g_free(site);
232                 return;
233         }
234         g_free(site);
235
236         /* Load the first sweep by default */
237         if (radar->h.nvolumes < 1 || radar->v[0]->h.nsweeps < 1) {
238                 g_warning("No sweeps found\n");
239         } else {
240                 /* load first available sweep */
241                 for (int vi = 0; vi < radar->h.nvolumes; vi++) {
242                         if (radar->v[vi]== NULL) continue;
243                         for (int si = 0; si < radar->v[vi]->h.nsweeps; si++) {
244                                 if (radar->v[vi]->sweep[si]== NULL) continue;
245                                 load_colormap(self, radar->v[vi]->h.type_str);
246                                 load_sweep(self, radar->v[vi]->sweep[si]);
247                                 break;
248                         }
249                         break;
250                 }
251         }
252
253         load_radar_gui(self, radar);
254 }
255
256 /* TODO: These update times functions are getting ugly... */
257 static void update_times_gtk(AWeatherRadar *self, GList *times)
258 {
259         gchar *last_time = NULL;
260         GRegex *regex = g_regex_new("^[A-Z]{4}_([0-9]{8}_[0-9]{4})$", 0, 0, NULL); // KLSX_20090622_2113
261         GMatchInfo *info;
262
263         GtkTreeView  *tview  = GTK_TREE_VIEW(aweather_gui_get_widget(self->gui, "time"));
264         GtkListStore *lstore = GTK_LIST_STORE(gtk_tree_view_get_model(tview));
265         gtk_list_store_clear(lstore);
266         GtkTreeIter iter;
267         times = g_list_reverse(times);
268         for (GList *cur = times; cur; cur = cur->next) {
269                 g_message("trying time %s", (gchar*)cur->data);
270                 if (g_regex_match(regex, cur->data, 0, &info)) {
271                         gchar *time = g_match_info_fetch(info, 1);
272                         g_message("adding time %s", (gchar*)cur->data);
273                         gtk_list_store_insert(lstore, &iter, 0);
274                         gtk_list_store_set(lstore, &iter, 0, time, -1);
275                         last_time = time;
276                 }
277         }
278
279         AWeatherView *view = aweather_gui_get_view(self->gui);
280         aweather_view_set_time(view, last_time);
281
282         g_regex_unref(regex);
283         g_list_foreach(times, (GFunc)g_free, NULL);
284         g_list_free(times);
285 }
286 static void update_times_online_cb(char *path, gboolean updated, gpointer _self)
287 {
288         GList *times = NULL;
289         gchar *data;
290         gsize length;
291         g_file_get_contents(path, &data, &length, NULL);
292         gchar **lines = g_strsplit(data, "\n", -1);
293         for (int i = 0; lines[i] && lines[i][0]; i++) {
294                 char **parts = g_strsplit(lines[i], " ", 2);
295                 times = g_list_prepend(times, g_strdup(parts[1]));
296                 g_strfreev(parts);
297         }
298         g_strfreev(lines);
299         g_free(data);
300
301         update_times_gtk(_self, times);
302 }
303 static void update_times(AWeatherRadar *self, AWeatherView *view, char *site)
304 {
305         if (aweather_view_get_offline(view)) {
306                 GList *times = NULL;
307                 gchar *path = g_build_filename(g_get_user_cache_dir(), PACKAGE, "nexrd2", "raw", site, NULL);
308                 GDir *dir = g_dir_open(path, 0, NULL);
309                 if (dir) {
310                         const gchar *name;
311                         while ((name = g_dir_read_name(dir))) {
312                                 times = g_list_prepend(times, g_strdup(name));
313                         }
314                         g_dir_close(dir);
315                 }
316                 g_free(path);
317                 update_times_gtk(self, times);
318         } else {
319                 gchar *path = g_strdup_printf("nexrd2/raw/%s/dir.list", site);
320                 cache_file(nexrad_base, path, AWEATHER_REFRESH, NULL, update_times_online_cb, self);
321                 /* update_times_gtk from update_times_online_cb */
322         }
323 }
324
325 /*****************
326  * ASync helpers *
327  *****************/
328 typedef struct {
329         AWeatherRadar *self;
330         gchar *radar_file;
331 } decompressed_t;
332
333 static void decompressed_cb(GPid pid, gint status, gpointer _udata)
334 {
335         decompressed_t *udata = _udata;
336         if (status != 0) {
337                 g_warning("wsr88ddec exited with status %d", status);
338                 return;
339         }
340         load_radar(udata->self, udata->radar_file);
341         g_spawn_close_pid(pid);
342         g_free(udata->radar_file);
343         g_free(udata);
344 }
345
346 static void cache_chunk_cb(char *path, goffset cur, goffset total, gpointer _self)
347 {
348         AWeatherRadar *self = AWEATHER_RADAR(_self);
349         double percent = (double)cur/total;
350
351         g_message("AWeatherRadar: cache_chunk_cb - %lld/%lld = %.2f%%",
352                         cur, total, percent*100);
353
354         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(self->progress_bar), MIN(percent, 1.0));
355
356         gchar *msg = g_strdup_printf("Loading radar... %5.1f%% (%.2f/%.2f MB)",
357                         percent*100, (double)cur/1000000, (double)total/1000000);
358         gtk_label_set_text(GTK_LABEL(self->progress_label), msg);
359         g_free(msg);
360 }
361
362 static void cache_done_cb(char *path, gboolean updated, gpointer _self)
363 {
364         AWeatherRadar *self = AWEATHER_RADAR(_self);
365         char *decompressed = g_strconcat(path, ".raw", NULL);
366         if (!updated) {
367                 load_radar(self, decompressed);
368                 return;
369         }
370
371         decompressed_t *udata = g_malloc(sizeof(decompressed_t));
372         udata->self       = self;
373         udata->radar_file = decompressed;
374         g_debug("AWeatherRadar: cache_done_cb - File updated, decompressing..");
375         char *argv[] = {"wsr88ddec", path, decompressed, NULL};
376         GPid pid;
377         GError *error = NULL;
378         g_spawn_async(
379                 NULL,    // const gchar *working_directory,
380                 argv,    // gchar **argv,
381                 NULL,    // gchar **envp,
382                 G_SPAWN_SEARCH_PATH|
383                 G_SPAWN_DO_NOT_REAP_CHILD, 
384                          // GSpawnFlags flags,
385                 NULL,    // GSpawnChildSetupFunc child_setup,
386                 NULL,    // gpointer user_data,
387                 &pid,    // GPid *child_pid,
388                 &error); // GError **error
389         if (error) {
390                 g_warning("failed to decompress WSR88D data: %s",
391                                 error->message);
392                 g_error_free(error);
393         }
394         g_child_watch_add(pid, decompressed_cb, udata);
395         self->soup = NULL;
396 }
397
398 /*************
399  * Callbacks *
400  *************/
401 static void on_sweep_clicked(GtkRadioButton *button, gpointer _self)
402 {
403         AWeatherRadar *self = AWEATHER_RADAR(_self);
404         load_colormap(self, g_object_get_data(G_OBJECT(button), "type" ));
405         load_sweep   (self, g_object_get_data(G_OBJECT(button), "sweep"));
406 }
407
408 static void on_time_changed(AWeatherView *view, const char *time, gpointer _self)
409 {
410         AWeatherRadar *self = AWEATHER_RADAR(_self);
411         g_debug("AWeatherRadar: on_time_changed - setting time=%s", time);
412         // format: http://mesonet.agron.iastate.edu/data/nexrd2/raw/KABR/KABR_20090510_0323
413         char *site = aweather_view_get_site(view);
414         char *path = g_strdup_printf("nexrd2/raw/%s/%s_%s", site, site, time);
415
416         /* Set up progress bar */
417         GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body));
418         if (child) gtk_widget_destroy(child);
419
420         GtkWidget *vbox = gtk_vbox_new(FALSE, 10);
421         gtk_container_set_border_width(GTK_CONTAINER(vbox), 10);
422         self->progress_bar   = gtk_progress_bar_new();
423         self->progress_label = gtk_label_new("Loading radar...");
424         gtk_box_pack_start(GTK_BOX(vbox), self->progress_bar,   FALSE, FALSE, 0);
425         gtk_box_pack_start(GTK_BOX(vbox), self->progress_label, FALSE, FALSE, 0);
426         gtk_container_add(GTK_CONTAINER(self->config_body), vbox);
427         gtk_widget_show_all(self->config_body);
428
429         /* Clear radar */
430         if (self->cur_radar)
431                 RSL_free_radar(self->cur_radar);
432         self->cur_radar = NULL;
433         self->cur_sweep = NULL;
434         aweather_gui_gl_redraw(self->gui);
435
436         /* Start loading the new radar */
437         if (self->soup) {
438                 soup_session_abort(self->soup);
439                 self->soup = NULL;
440         }
441         if (aweather_view_get_offline(view)) 
442                 self->soup = cache_file(nexrad_base, path, AWEATHER_ONCE,
443                                 cache_chunk_cb, cache_done_cb, self);
444         else 
445                 self->soup = cache_file(nexrad_base, path, AWEATHER_UPDATE,
446                                 cache_chunk_cb, cache_done_cb, self);
447         g_free(path);
448 }
449
450 static void on_site_changed(AWeatherView *view, char *site, gpointer _self)
451 {
452         AWeatherRadar *self = AWEATHER_RADAR(_self);
453         g_debug("AWeatherRadar: on_site_changed - Loading wsr88d list for %s", site);
454         update_times(self, view, site), &time;
455 }
456
457 static void on_refresh(AWeatherView *view, gpointer _self)
458 {
459         AWeatherRadar *self = AWEATHER_RADAR(_self);
460         char *site = aweather_view_get_site(view);
461         update_times(self, view, site);
462 }
463
464 /***********
465  * Methods *
466  ***********/
467 AWeatherRadar *aweather_radar_new(AWeatherGui *gui)
468 {
469         g_debug("AWeatherRadar: new");
470         AWeatherRadar *self = g_object_new(AWEATHER_TYPE_RADAR, NULL);
471         self->gui = gui;
472
473         GtkWidget    *config  = aweather_gui_get_widget(gui, "tabs");
474         AWeatherView *view    = aweather_gui_get_view(gui);
475
476         /* Add configuration tab */
477         self->config_body = gtk_alignment_new(0, 0, 1, 1);
478         gtk_container_set_border_width(GTK_CONTAINER(self->config_body), 5);
479         gtk_container_add(GTK_CONTAINER(self->config_body), gtk_label_new("No radar loaded"));
480         gtk_notebook_prepend_page(GTK_NOTEBOOK(config), self->config_body, gtk_label_new("Radar"));
481
482         /* Set up OpenGL Stuff */
483         g_signal_connect(view,    "site-changed", G_CALLBACK(on_site_changed), self);
484         g_signal_connect(view,    "time-changed", G_CALLBACK(on_time_changed), self);
485         g_signal_connect(view,    "refresh",      G_CALLBACK(on_refresh),      self);
486
487         return self;
488 }
489
490 static void _aweather_radar_expose(AWeatherPlugin *_self)
491 {
492         AWeatherRadar *self = AWEATHER_RADAR(_self);
493         g_debug("AWeatherRadar: expose");
494         if (self->cur_sweep == NULL)
495                 return;
496         Sweep *sweep = self->cur_sweep;
497
498         /* Draw the rays */
499
500         glMatrixMode(GL_MODELVIEW);
501         glPushMatrix();
502         glBindTexture(GL_TEXTURE_2D, self->cur_sweep_tex);
503         glEnable(GL_TEXTURE_2D);
504         glDisable(GL_ALPHA_TEST);
505         glColor4f(1,1,1,1);
506         glBegin(GL_QUAD_STRIP);
507         for (int ri = 0; ri <= sweep->h.nrays; ri++) {
508                 Ray  *ray = NULL;
509                 double angle = 0;
510                 if (ri < sweep->h.nrays) {
511                         ray = sweep->ray[ri];
512                         angle = ((ray->h.azimuth - ((double)ray->h.beam_width/2.))*M_PI)/180.0; 
513                 } else {
514                         /* Do the right side of the last sweep */
515                         ray = sweep->ray[ri-1];
516                         angle = ((ray->h.azimuth + ((double)ray->h.beam_width/2.))*M_PI)/180.0; 
517                 }
518
519                 double lx = sin(angle);
520                 double ly = cos(angle);
521
522                 double near_dist = ray->h.range_bin1;
523                 double far_dist  = ray->h.nbins*ray->h.gate_size + ray->h.range_bin1;
524
525                 /* (find middle of bin) / scale for opengl */
526                 // near left
527                 glTexCoord2f(0.0, (double)ri/sweep->h.nrays-0.01);
528                 glVertex3f(lx*near_dist, ly*near_dist, 2.0);
529
530                 // far  left
531                 glTexCoord2f(1.0, (double)ri/sweep->h.nrays-0.01);
532                 glVertex3f(lx*far_dist,  ly*far_dist,  2.0);
533         }
534         //g_print("ri=%d, nr=%d, bw=%f\n", _ri, sweep->h.nrays, sweep->h.beam_width);
535         glEnd();
536         glPopMatrix();
537
538         /* Texture debug */
539         //glBegin(GL_QUADS);
540         //glTexCoord2d( 0.,  0.); glVertex3f(-500.,   0., 0.); // bot left
541         //glTexCoord2d( 0.,  1.); glVertex3f(-500., 500., 0.); // top left
542         //glTexCoord2d( 1.,  1.); glVertex3f( 0.,   500., 3.); // top right
543         //glTexCoord2d( 1.,  0.); glVertex3f( 0.,     0., 3.); // bot right
544         //glEnd();
545
546         /* Print the color table */
547         glDisable(GL_TEXTURE_2D);
548         glDisable(GL_DEPTH_TEST);
549         glMatrixMode(GL_MODELVIEW ); glPushMatrix(); glLoadIdentity();
550         glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity();
551         glBegin(GL_QUADS);
552         int i;
553         for (i = 0; i < 256; i++) {
554                 glColor4ub(self->cur_colormap->data[i][0],
555                            self->cur_colormap->data[i][1],
556                            self->cur_colormap->data[i][2],
557                            self->cur_colormap->data[i][3]);
558                 glVertex3f(-1.0, (float)((i  ) - 256/2)/(256/2), 0.0); // bot left
559                 glVertex3f(-1.0, (float)((i+1) - 256/2)/(256/2), 0.0); // top left
560                 glVertex3f(-0.9, (float)((i+1) - 256/2)/(256/2), 0.0); // top right
561                 glVertex3f(-0.9, (float)((i  ) - 256/2)/(256/2), 0.0); // bot right
562         }
563         glEnd();
564         glEnable(GL_DEPTH_TEST);
565         glEnable(GL_ALPHA_TEST);
566         glMatrixMode(GL_PROJECTION); glPopMatrix(); 
567         glMatrixMode(GL_MODELVIEW ); glPopMatrix();
568 }