All,

Sorry for another long post... Here's a "better" version of the image plotter 
demo...

--------------
/***********************************************************/
// compile as:
// fltk-config --compile laser-plot.cxx
//
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#include <FL/Fl.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Button.H>
#include <Fl/fl_draw.H>

static Fl_Double_Window *main_win;

// defines for the image window sizes
#define WW 512 // width
#define HH 512 // height
#define DD 3   // depth

#define RR 0 // Red cell index
#define GG 1 // Green cell index
#define BB 2 // Blue cell index

/***********************************************************/
class vw_box : public Fl_Box {
protected:
  void draw(void);
  int iw, ih, id;
  uchar *data;
public:
  vw_box(int X, int Y, int W, int H) : Fl_Box(X,Y,W,H)
    {data = NULL;}

  void set_data(uchar *newdat, int W, int H, int D);
};
/***********************************************************/
void vw_box::set_data(uchar *newdat, int W, int H, int D) {
  if(!newdat) return; // no new data, so return
  // set image dimensions
  iw = W; ih = H; id = D;
  // If seting new image data array, discard the old array
  if (data) delete [] data;
  data = newdat;
}

/***********************************************************/
void vw_box::draw(void) {
  if (!data) return;
  /* Redraw the whole image */
  fl_draw_image(data, x(), y(), iw, ih, id);
  fl_pop_clip();
}

/***********************************************************/
static vw_box *box_2; // image box
static uchar *da;     // image array
static uchar sa[WW][HH];   // "sensor ranges" array
static uchar clut[200][3]; // colour look-up table

/***********************************************************/
void quit_cb(Fl_Button *, void *) {
  main_win->hide();
}

/***********************************************************/
void simulate_sensor(void *) {
  // make up fake random data - do a few rows on each pass
  static int yo = 0;

  for(int iter = 0; iter < 4; iter++) { // iter rows
    for(int xo = 0; xo < WW; xo++){
      float rf = 200.0 * rand() / (float)RAND_MAX;
      int range = (int)rf;
      sa[xo][yo] = range;
    } // xo
    yo ++;
    if (yo >= HH) yo = 0;
  } // iter

  Fl::repeat_timeout(0.01, simulate_sensor); // 100Hz
}

/***********************************************************/
void display_refresh(void *) {
  int xo, yo, offs;
  for(yo = 0; yo < HH; yo++){
    offs = yo * WW;
    for(xo = 0; xo < WW; xo++){
      int range = (int)sa[xo][yo];
      int cell = (offs + xo) * DD;
      da[cell+RR] = clut[range][RR]; // R
      da[cell+GG] = clut[range][GG]; // G
      da[cell+BB] = clut[range][BB]; // B
    } // xo
  } // yo
  box_2->redraw(); // redisplay the data
  Fl::repeat_timeout(0.1, display_refresh); // 10Hz
}

/***********************************************************/
int main(int argc, char **argv) {
  int idx, val;

  main_win = new Fl_Double_Window(WW + 100, HH + 20);
  main_win->begin();

  box_2 = new vw_box(10, 10, WW, HH);

  Fl_Button *quit = new Fl_Button(WW + 20, 10, 60, 30);
  quit->label("Quit");
  quit->callback((Fl_Callback *)quit_cb);

  main_win->end();

  // make the range/colour mapping table
  // I think this is what Sam described in the post, although
  // I don't think it's the colour mapping I would opt for...
  memset(clut, 0, sizeof(clut)); // set to black
  for(idx = 0; idx < 200; idx++){
    if(idx < 100){ // blue cells
      val = (idx * 258) / 100;
      clut[idx][BB] = val;
    } else {
      clut[idx][BB] = 0;
    }

    if (idx < 50) { // green cells
      clut[idx][GG] = 0;
    } else if(idx < 150) {
      val = ((idx - 50) * 258) / 100;
      clut[idx][GG] = val;
    } else {
      clut[idx][GG] = 0;
    }

    if (idx < 100) { // red cells
      clut[idx][RR] = 0;
    } else {
      val = ((idx - 100) * 258) / 100;
      clut[idx][RR] = val;
    }
  }
  // Fill the "sensor" array with something to start with
  int xo, yo;
  for(yo = 0; yo < HH; yo++) {
    for(xo = 0; xo < WW; xo++) {
      val = (xo + yo) % 200;
      sa[xo][yo] = val;
    }
  }

  // make the image data array
  da = new uchar[WW * HH * DD];
  memset(da, 128, (WW * HH * DD));
  // set the data array as the box image
  box_2->set_data(da, WW, HH, DD);

  // initialise the random sequence generator for simulation
  srand(time(0));

  // add some animation - simulate input data
  Fl::add_timeout(0.01, simulate_sensor);
  // set a display refresh
  Fl::add_timeout(0.1, display_refresh);

  // run the main loop
  main_win->show(argc, argv);
  return Fl::run();
}

/* End of File */


--------------
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to