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