Plotfield Color Map Manipulation

This page elaborates on the previous example which discussed the use of color maps in connection with Ggi plot fields. That example lacked the possibility of manipulating the color map. In the following example program, colormap manipulation is accomplished by adding a single composite Ggi element which takes care of everything needed. This element is created by calling the function GgiPlotColorEditor(). As illustrated in the figure to the right, it consists of an annotated color wedge, two analog controls for manipulating the mapping function and a number of buttons and menus. One of the menus allows the user to choose from eleven different types of color map: one monochrome and ten pseudo color.

In the example program below, GgiPlotColorEditor() is called twice.
     In the first call the color map manipulation element is created. The first argument, ggiCreate, is a symbolic constant which indicates that an element is to be created. The second and third arguments indicate the start color index and the number of color indices (in the master color map) to be manipulated. The last argument can be used to specify the color index to be used for BLANK pixels. This feature is not used here. If it would, some extra components would be added to the element. These components would enable the user to modify the color representation of BLANK pixels.
     The second call modifies the element created in the first call. In this case data limits are specified for annotation purposes.

In this example both the plot field and the color wedge are placed in the application's main window. An alternative would be detaching the manipulation element from the main window and putting it in a separate window which can be "popped up" whenever the user needs it. How this can be done will be shown in a later example.

An equivalent program written in Python can be found here.

/* example8.c -XT */

#include "stddef.h"
#include "math.h"
#include "gipsyc.h"
#include "cmain.h"
#include "init.h"
#include "finis.h"
#include "userfio.h"
#include "pgplot.h"
#include "ggi.h"

#define ROWS 100                               /* number of image rows    */
#define COLS 100                               /* number of image columns */
#define NCI   50                               /* number of color indices */


/*
 *  QUIT= keyword handler
 */
static void quit(ident id, char *key, int code, void *arg)
{
   bool button=toflog(FALSE);
   (void)userflog(&button, 1, 2, key, " ");
   if (tobool(button)) {
      wkeyf(key);
      finis_c();
   }
}

/*
 *   Main program
 */
MAIN_PROGRAM_ENTRY
{
   ident plotter, wedge, bquit;
   float image[ROWS][COLS];
   int   i, j;
   float tr[6];
   float a1=0.0, a2=1.0;
   fint  idim=COLS, jdim=ROWS, i1=1, i2=COLS, j1=1, j2=ROWS;
   fint  cilo=16, cihi=16+NCI-1;

   init_c();

   GgiAutoLayout(FALSE);                          /* custom layout            */
   GgiPostponeRealize(TRUE);                      /* don't show immediately   */
    
   (void)GgiPlotColors(NULL, 16+NCI);             /* NCI additional colors    */
   plotter = GgiPlotField("PLOT", 325, 325);      /* create plot field        */
   GgiPlotMapColors(plotter, 0, 16, 16, NCI, -1); /* assign colors to plotter */
   wedge   = GgiPlotColorEditor(ggiCreate, 16, NCI, 0);  /* manipulate colors */
   (void)GgiPlotColorEditor(ggiLimits, wedge, 0.0, 1.0); /* set wedge range   */
   bquit   = GgiButton("QUIT=", NULL);
   
   GgiSetPosition(plotter, 0, NULL, 0, NULL);     /* top left                 */
   GgiSetPosition(wedge,   0, NULL, 0, plotter);  /* below plotter, left      */
   GgiSetPosition(bquit,   0, NULL, 0, wedge);    /* below wedge, left        */
   (void)ScheduleKeyevent(quit, "QUIT=", KEYCHANGE, NULL); /* handle QUIT     */

   GgiRealize();                                  /* show what we've made     */

   for (j=0; j<ROWS; j++) {                       /* build "image"            */
      for (i=0; i<COLS; i++) {
         float x = i-COLS/2, y = j-ROWS/2, w = 3*COLS;
         image[j][i] = exp(-(x*x+y*y)/w);
      }
   }

   (void)pgopen_c(tofchar("PLOT"));                  /* open plotter          */

   pgscir_c(&cilo, &cihi);                           /* set color index range */

                                                     /* tranformation matrix  */
   tr[0] = 0.0; tr[1] = 1.0/(float)COLS; tr[2] = 0.0;
   tr[3] = 0.0; tr[4] = 0.0;             tr[5] = 1.0/(float)ROWS;

                                                     /* display image         */
   pgimag_c(&image[0][0], &idim, &jdim, &i1, &i2, &j1, &j2, &a1, &a2, tr);

   MainLoop();
}

Programming GIPSY Maintained by J. P. Terlouw