home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / saoimage / sao1_07.tar / clrmenu.c < prev    next >
C/C++ Source or Header  |  1990-04-28  |  6KB  |  232 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    clrmenu.c (Color Menu)
  6.  * Purpose:    Read and Write file and internally stored color tables
  7.  * Subroutine:    fetch_colortable()        returns: int
  8.  * Xlib calls:    none
  9.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  10.  *        You may do anything you like with this file except remove
  11.  *        this copyright.  The Smithsonian Astrophysical Observatory
  12.  *        makes no representations about the suitability of this
  13.  *        software for any purpose.  It is provided "as is" without
  14.  *        express or implied warranty.
  15.  * Modified:    {0} Michael VanHilst    initial version          16 May 1989
  16.  *        {n} <who> -- <does what> -- <when>
  17.  */
  18.  
  19. #include <stdio.h>            /* stderr, NULL, etc. */
  20. #include <X11/Xlib.h>            /* X window stuff */
  21. #include <X11/Xutil.h>            /* X window manager stuff */
  22. #include "hfiles/constant.h"        /* code values */
  23. #include "hfiles/color.h"        /* color structs */
  24. #include "hfiles/colormap.h"        /* color table storage structs */
  25. #include "hfiles/define.h"        /* define SZ_FNAME, etc. */
  26. #include "hfiles/edit.h"        
  27. /* EditStruct */
  28. #include "defs/colormap.def"        /* define color tables */
  29.  
  30. EditStruct *color_edit;    /* key to popup editor for file name input */
  31.  
  32.  
  33. /*
  34.  * Subroutine:    fetch_colortable
  35.  * Purpose:    Get a new pre-defined color table and load it for use
  36.  * Returns:    1 if succeeded, else 0
  37.  */
  38. int fetch_colortable ( color, table_code, imagefile )
  39.      struct colorRec *color;
  40.      int table_code;
  41.      char *imagefile;        /* i: imagefile name to print in output file */
  42. {
  43.   ColorTable *newtable;
  44.   static void load_newtable(), write_color_file();
  45.   static ColorTable *read_color_file();
  46.  
  47.   switch( table_code ) {
  48.   case MOP_Init_A:
  49.     newtable = &gray_map;
  50.     break;
  51.   case MOP_Init_B:
  52.     newtable = &bbs_map;
  53.     break;
  54.   case MOP_Init_C:
  55.     newtable = &hip_map;
  56.     break;
  57.   case MOP_Init_D:
  58.     newtable = &imp8_map;
  59.     break;
  60.   case MOP_Init_E:
  61.     newtable = &a_map;
  62.     break;
  63.   case MOP_Init_F:
  64.     newtable = &b_map;
  65.     break;
  66.   case MOP_Read:
  67.     if( (newtable = read_color_file()) == NULL )
  68.       return( 0 );
  69.     break;
  70.   case MOP_Write:
  71.     write_color_file(imagefile, color);
  72.     return( 0 );
  73.   default:
  74.     (void)fprintf(stderr, "WARNING: unknown color map code!\n");
  75.     return( 0 );
  76.   }
  77.   load_newtable(&color->ctable, newtable);
  78.   return( 1 );
  79. }
  80.  
  81. static ColorTable ctable;
  82.  
  83. /*
  84.  * Subroutine:    read_color_file
  85.  * Purpose:    Open and read a color table file, and load it into the
  86.  *        current color table.
  87.  */
  88. static ColorTable *read_color_file ( )
  89. {
  90.   FILE *fp;
  91.   int open_input_file(), parse_color_file();
  92.   EditStruct *init_edit_popup();
  93.  
  94.   if( color_edit == NULL )
  95.     color_edit = init_edit_popup((char *)NULL, SZ_FNAME);
  96.   /* open coordinate output file for writing */
  97.   if( open_input_file(&fp, color_edit, 0,
  98.                "Enter name of pseudocolor file:") <= 0 )
  99.     return( NULL );
  100.   bzero((char *)&ctable, sizeof(ColorTable));
  101.   if( parse_color_file(fp, &ctable, CTBL_MAX) ) {
  102.     (void)fclose(fp);
  103.     return( &ctable );
  104.   } else {
  105.     (void)fclose(fp);
  106.     return( NULL );
  107.   }
  108. }
  109.  
  110. /*
  111.  * Subroutine: write_color_file
  112.  * Purpose:    Open and write a color table file
  113.  */
  114. static void write_color_file ( imagefile, color )
  115.      char *imagefile;
  116.      struct colorRec *color;
  117. {
  118.   FILE *fp;
  119.   int error;
  120.   static void print_one_color();
  121.   EditStruct *init_edit_popup();
  122.   int open_output_file();
  123.   void timestamp();
  124.  
  125.   error = 0;
  126.   if( color->ctable.red.vertex_cnt > CTBL_MAX ) {
  127.     (void)fprintf(stderr,
  128.           "WARNING: too many red entries for portable table\n");
  129.     error = 1;
  130.   }
  131.   if( color->ctable.green.vertex_cnt > CTBL_MAX ) {
  132.     (void)fprintf(stderr,
  133.           "WARNING: too many green entries for portable table\n");
  134.     error = 1;
  135.   }
  136.   if( color->ctable.blue.vertex_cnt > CTBL_MAX ) {
  137.     (void)fprintf(stderr,
  138.           "WARNING: too many blue entries for portable table\n");
  139.     error = 1;
  140.   }
  141.   if( color_edit == NULL )
  142.     color_edit = init_edit_popup((char *)NULL, SZ_FNAME);
  143.   /* open coordinate output file for writing */
  144.   if( open_output_file(&fp, color_edit, 0,
  145.               "Enter name of pseudocolor file:") <= 0 )
  146.     error = 1;
  147.   if( error ) {
  148.     (void)fprintf(stderr,"No color file written\n");
  149.     XBell(color->display, 80);
  150.     return;
  151.   }
  152.   /* timestamp the first entry in the output file. */
  153.   (void)fprintf(fp, "# SAOimage color table\n");
  154.   timestamp(fp, imagefile);
  155.   (void)fprintf(fp, "PSEUDOCOLOR\n");
  156.   (void)fprintf(fp, "RED:");
  157.   print_one_color(fp, &color->ctable.red);
  158.   (void)fprintf(fp, "GREEN:");
  159.   print_one_color(fp, &color->ctable.green);
  160.   (void)fprintf(fp, "BLUE:");
  161.   print_one_color(fp, &color->ctable.blue);
  162.   fclose(fp);
  163. }
  164.  
  165. /*
  166.  * Subroutine:
  167.  * Purpose:
  168.  */
  169. static void print_one_color ( fp, table )
  170.      FILE *fp;
  171.      struct subtableRec *table;
  172. {
  173.   int i, j;
  174.   if( table->do_gamma )
  175.     (void)fprintf(fp, " gamma %.3f\n", table->gamma);
  176.   else
  177.     (void)fprintf(fp, "\n");
  178.   for( i=0, j=0; i<table->vertex_cnt; i++ ) {
  179.     (void)fprintf(fp,"(%.3f,%.3f)",table->cell_level[i],table->intensity[i]);
  180.     if( ++j >= 5 ) {
  181.       j=0;
  182.       (void)fprintf(fp, "\n");
  183.     }
  184.   }
  185.   (void)fprintf(fp, "\n");
  186. }
  187.  
  188. /*
  189.  * Subroutine:    load_newtable
  190.  * Purpose:    Load an internally stored color table for use
  191.  */
  192. static void load_newtable ( ctable, new )
  193.      struct colorTable *ctable;
  194.      ColorTable *new;
  195. {
  196.   static void load_subtable();
  197.  
  198.   load_subtable(&ctable->red, &new->red);
  199.   load_subtable(&ctable->green, &new->green);
  200.   load_subtable(&ctable->blue, &new->blue);
  201. }
  202.  
  203. /*
  204.  * Subroutine:    load_subtable
  205.  * Purpose:    Load one color of the internal color table
  206.  */
  207. static void load_subtable ( subtable, new )
  208.      struct subtableRec *subtable;
  209.      struct SubTable *new;
  210. {
  211.   int i;
  212.  
  213.   subtable->fixed_cells = 0;
  214.   if( new->do_gamma ) {
  215.     subtable->do_gamma = 1;
  216.     subtable->gamma = new->gamma;
  217.   } else {
  218.     subtable->do_gamma = 0;
  219.     subtable->gamma = 1.0;
  220.   }
  221.   subtable->invert_order = 0;
  222.   subtable->vertex_cnt = new->vertex_cnt;
  223.   for( i=0; i<new->vertex_cnt; i++ ) {
  224.     subtable->cell_level[i] = new->cell_level[i];
  225.     /* base levels used for dynamic manipulation */
  226.     subtable->base_level[i] = new->cell_level[i] - 0.5;
  227.     subtable->intensity[i] = new->intensity[i];
  228.   }
  229.   subtable->bias = 0.5;
  230.   subtable->contrast = 1.0;
  231. }
  232.