home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / plotting / imagetoo / imagetl1.lha / Imagetool / src+obj / palette.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-11  |  42.8 KB  |  1,618 lines

  1. /* cat > headers/palette.h << "EOF" */
  2. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  3. /* palette.h: header for palette.c file            */
  4. /*          handles any color related operations,    */
  5. /*          color maps initializations, palette    */
  6. /*          loading, viewing, editing (contour and    */
  7. /*          fiddle).                    */
  8. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  9. /* SCCS information: %W%    %G% - NCSA */
  10.  
  11. #define palette_h    1
  12.  
  13. #include "all.h"
  14. #include "newext.h"
  15.  
  16.     static int fiddled;
  17.  
  18.     static char map[256];
  19.  
  20.     static unsigned char red1[LUTSIZE], green1[LUTSIZE], blue1[LUTSIZE];
  21.  
  22.     static Notify_value rotate_cmap();
  23.  
  24. /* EOF */
  25. /* cat > src+obj/palette/check_pal.c << "EOF" */
  26. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  27. /* check_pal: check palette for correct features. The    */
  28. /*          file must be fully resolved and exist.    */
  29. /*          if the palette type is PAL_HDF then the    */
  30. /*          first palette is used in the file.    */
  31. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  32. /* SCCS information: %W%    %G% - NCSA */
  33.  
  34. /* #include "palette.h" */
  35.  
  36. int
  37. check_pal (palfile, pptype, type)
  38.                 /* returns: error - 0 = no error
  39.                            -1 = error in checking palette */
  40.     char           *palfile;
  41.                 /* input: string of size MAXNAMELEN - 1 */
  42.     enum pal_type  *pptype;
  43.                 /* returns: palette type if no error */
  44.     int             type;
  45.                 /* input: REGULAR  = regular
  46.                       DEFAULT  = default */
  47. {
  48.     int             err;
  49.  
  50.     if ((err = check_palette (palfile, pptype)))
  51.     {
  52.         switch (err)
  53.         {
  54.             case 1:
  55.                 sprintf (wkstr, "Error: %s file not found.", pal_msg2);
  56.                 msg_write (wkstr);
  57.                 break;
  58.             case 2:
  59.                 sprintf (wkstr, "Error: %s size != 768.", pal_msg2);
  60.                 msg_write (wkstr);
  61.                 break;
  62.             case 3:
  63.                 sprintf (wkstr, "Error: No %s - HDF file has no palettes.", pal_msg1);
  64.                 msg_write (wkstr);
  65.                 break;
  66.             default:
  67.                 sprintf (wkstr, "HDF file error: (DFerror = %d) while checking for %s.", err, pal_msg1);
  68.                 msg_write (wkstr);
  69.                 break;
  70.         }
  71.         return (-1);
  72.     }
  73.     return (0);
  74. }
  75. /* EOF */
  76. /* cat > src+obj/palette/check_palette.c << "EOF" */
  77. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  78. /* check_palette: check for palette            */
  79. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  80. /* SCCS information: %W%    %G% - NCSA */
  81.  
  82. /* #include "palette.h" */
  83.  
  84. int
  85. check_palette (s, pptype)
  86.                 /* returns: 0 - success
  87.                         1 - file not openable
  88.                         2 - raw file - size is wrong
  89.                         3 - not enough palettes in HDF file
  90.                         < 0 - DFerror - other HDF error */
  91.     char           *s;
  92.                 /* input: filename */
  93.     enum pal_type  *pptype;
  94.                 /* returns: palette type if success */
  95. {
  96.     int             npals, size;
  97.     struct stat     my_stat;
  98.  
  99.     if ((npals = DFPnpals (s)) == -1)
  100.     {
  101.         DFPrestart ();
  102.         if (DFerror == DFE_FNF)        /* file not found error */
  103.             return (1);
  104.         else if (DFerror == DFE_NOTDFFILE)
  105.         {        /* regular file */
  106.             (void) stat (s, &my_stat);
  107.             size = PALETTE_SIZE;
  108.             if (my_stat.st_size != size)
  109.                 return (2);
  110.             *pptype = PAL_RAW;
  111.             return (0);
  112.         }
  113.         else
  114.             return (DFerror);    /* other HDF error */
  115.     }
  116.     else if (npals < 1)
  117.     {
  118.         DFPrestart ();
  119.         return (3);
  120.     }
  121.     else
  122.     {
  123.         DFPrestart ();
  124.         *pptype = PAL_HDF;
  125.         return (0);
  126.     }
  127. }
  128. /* EOF */
  129. /* cat > src+obj/palette/check_palette_RIS8.c << "EOF" */
  130. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  131. /* check_palette_RIS8: checks and retrieves palette    */
  132. /*               from first RIS8 in an HDF file.    */
  133. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  134. /* SCCS information: %W%    %G% - NCSA */
  135.  
  136. /* #include "palette.h" */
  137.  
  138. int
  139. check_palette_RIS8 (fn, pptype, palette)
  140.                     /* returns: 0 - success
  141.                             1 - filename not found
  142.                             2 - raw file - size is wrong
  143.                             4 - no RIS8 with palette
  144.                             < 0 - DFerror - other HDF error */
  145.     char           *fn;
  146.                     /* input: filename to get palette */
  147.     enum pal_type  *pptype;
  148.                     /* returns: palette type if success */
  149.     char           *palette;
  150.                     /* returns: palette from first RIS8 which has one */
  151. {
  152.     DF             *file;
  153.     int             dummy;
  154.     struct stat     file_stat;
  155.     uint16          tag, ref;
  156.     DFdesc          dd;
  157.     DFGRrig         rig;
  158.  
  159.     if ((file = DFopen (fn, DFACC_READ, dummy)) == NULL)
  160.     {        /* error */
  161.         switch (DFerror)
  162.         {
  163.             case DFE_FNF:    /* file not found */
  164.                 return (1);
  165.             case DFE_NOTDFFILE:    /* raw file */
  166.                 stat (fn, &file_stat);
  167.                 if (file_stat.st_size != PALETTE_SIZE)
  168.                     return (2);
  169.                 *pptype = PAL_RAW;
  170.                 return (0);
  171.             default:
  172.                 return (DFerror);
  173.         }
  174.     }
  175.  
  176.     tag = DFTAG_RIG;
  177.     ref = DFTAG_WILDCARD;
  178.     if (DFsetfind (file, tag, ref))
  179.     {
  180.         DFclose (file);
  181.         return (DFerror);
  182.     }
  183.     while (TRUE)
  184.     {
  185.         if (DFfind (file, &dd))
  186.         {
  187.             DFclose (file);
  188.             return (4);
  189.         }
  190.         if (DFGRgetrig (file, dd.ref, &rig))
  191.         {
  192.             DFclose (file);
  193.             return (DFerror);
  194.         }
  195.         if (rig.datadesc[IMAGE].ncomponents != 1)
  196.             break;
  197.         if (rig.data[LUT].tag)
  198.         {
  199.             DFclose (file);
  200.             if (DFPreadref (fn, rig.data[LUT].ref))
  201.                 return (DFerror);
  202.             if (DFPgetpal (fn, palette))
  203.                 return (DFerror);
  204.             DFPrestart ();
  205.             *pptype = PAL_RIS8;
  206.             return (0);
  207.         }
  208.     }
  209. }
  210. /* EOF */
  211. /* cat > src+obj/palette/check_palfn.c << "EOF" */
  212. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  213. /* check_palfn: resolve palette filename. In 1.1 no     */
  214. /*        appending of .pal is done under any     */
  215. /*        conditions.                */
  216. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  217. /* SCCS information: %W%    %G% - NCSA */
  218.  
  219. /* #include "palette.h" */
  220.  
  221. int
  222. check_palfn (palfile, type)
  223.                 /* returns error - 0 = no error
  224.                           -1 = error in resolving palette filename */
  225.     char           *palfile;
  226.                  /* input: string of size MAXNAMELEN - 1.
  227.                    returns: (no error) resolved palette filename
  228.                         (error) nothing of significance */
  229.     int             type;
  230.                 /* input: REGULAR  = regular
  231.                       DEFAULT  = default */
  232. {
  233.     int             err;
  234.         /* try panel for name first */
  235.     strcpy (palfile, (char *) panel_get_value (pal_board));
  236.     (void) strip_wspace (palfile);
  237.     if (strlen (palfile) == 0)
  238.     {
  239.         if (strlen (default_pal.fn) != 0)
  240.         {
  241.             if (type == REGULAR)
  242.             {        /* default_pal is fully resolved */
  243.                 strcpy (palfile, default_pal.fn);
  244.                 return (0);
  245.             }
  246.             else if (type == DEFAULT)
  247.             {        /* must resolve it */
  248.                 strcpy (palfile, default_pal.fn);
  249.                 panel_set_value (pal_board, palfile);
  250.             }
  251.         }
  252.         else
  253.             switch (type)
  254.             {
  255.                 case REGULAR:
  256.                     msg_write ("Warning: No palette filename given and no default.");
  257.                     return (-1);
  258.                 case DEFAULT:
  259.                     msg_write ("Warning: No (default) palette filename given.");
  260.                     return (-1);
  261.             }
  262.     }
  263.  
  264.     if ((err = get_abspathname (palfile, MAXNAMELEN, TYPE_FILE)))
  265.     {
  266.         switch (err)
  267.         {
  268.             case 1:
  269.                 sprintf (wkstr, "Error: %s file not found.", pal_msg2);
  270.                 msg_write (wkstr);
  271.                 break;
  272.             case 2:
  273.                 sprintf (wkstr, "Error: %s not a file.", pal_msg2);
  274.                 msg_write (wkstr);
  275.                 break;
  276.             case 3:
  277.                 sprintf (wkstr, "Error: No %s filename.", pal_msg1);
  278.                 msg_write (wkstr);
  279.                 break;
  280.             case 4:
  281.                 sprintf (wkstr, "Error: More than one %s file given.", pal_msg1);
  282.                 msg_write (wkstr);
  283.                 break;
  284.             case 5:
  285.                 sprintf (wkstr, "Error: Wildcard matches more than one %s file.", pal_msg1);
  286.                 msg_write (wkstr);
  287.                 break;
  288.             case 6:
  289.                 sprintf (wkstr, "Error: Wildcard syntax in %s filename.", pal_msg1);
  290.                 msg_write (wkstr);
  291.                 break;
  292.             default:
  293.                 sprintf (wkstr, "Internal error (%d): While processing %s filename.", err, pal_msg1);
  294.                 msg_write (wkstr);
  295.                 break;
  296.         }
  297.         return (-1);
  298.     }
  299.     return (0);
  300. }
  301. /* EOF */
  302. /* cat > src+obj/palette/check_prev_pal.c << "EOF" */
  303. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  304. /* check_prev_pal: check previous palette for correct    */
  305. /*           features. The type is passed on     */
  306. /*           input and a check is done of this    */
  307. /*           type. An error occurs if that type    */
  308. /*           fails. This doesn't exclude somebody */
  309. /*           from changing the palette from under    */
  310. /*           you but at least it must be replaced    */
  311. /*           with something of the same type.    */
  312. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  313. /* SCCS information: %W%    %G% - NCSA */
  314.  
  315. /* #include "palette.h" */
  316.  
  317. int 
  318. check_prev_pal (palfile, ptype, palette)
  319.                 /* returns: - 0 = no error
  320.                          -1 = error in checking palette */
  321.     char           *palfile;
  322.                 /* input: palette filename */
  323.     enum pal_type   ptype;
  324.                 /* input: expected palette type */
  325.     char            palette[PALETTE_SIZE];
  326.                  /* input: string for palette
  327.                    returns: palette if no error and expected palette type is RIS8 */
  328. {
  329.     int             err;
  330.     enum pal_type   actual_ptype;
  331.  
  332.         /* PAL_UNKNOWN should not occur */
  333.     switch (ptype)
  334.     {
  335.         case PAL_RAW:
  336.             if ((err = check_palette (palfile, &actual_ptype)))
  337.                 break;
  338.             if (actual_ptype != ptype)
  339.                 sprintf (wkstr, "Error: Previous palette now not RAW. Something has changed! (Filename = %s)", palfile);
  340.             break;
  341.         case PAL_HDF:
  342.             if ((err = check_palette (palfile, &actual_ptype)))
  343.                 break;
  344.             if (actual_ptype != ptype)
  345.                 sprintf (wkstr, "Error: Previous palette now not HDF. Something has changed! (Filename = %s)", palfile);
  346.             break;
  347.         case PAL_RIS8:
  348.             if ((err = check_palette_RIS8 (palfile, &actual_ptype, palette)))
  349.                 break;
  350.             if (actual_ptype != ptype)
  351.                 sprintf (wkstr, "Error: Previous palette now not from a 8-Bit Raster Image set. Something has changed! (Filename = %s)", palfile);
  352.             break;
  353.     }
  354.     switch (err)
  355.     {
  356.         case 0:
  357.             return (0);
  358.         case 1:
  359.             sprintf (wkstr, "Error: Previous palette file now not found. Something has changed! (Filename = %s)", palfile);
  360.             msg_write (wkstr);
  361.             return (-1);
  362.         case 2:
  363.             sprintf (wkstr, "Error: Previous RAW palette file size now != 768. Something has changed! (Filename = %s)", palfile);
  364.             msg_write (wkstr);
  365.             return (-1);
  366.         case 3:
  367.             sprintf (wkstr, "Error: Previous palette HDF file now has no palettes. Something has changed! (Filename = %s)", palfile);
  368.             msg_write (wkstr);
  369.             return (-1);
  370.         case 4:
  371.             sprintf (wkstr, "Error: Previous palette HDF file now has no 8-Bit Raster Image set with a palette. Something has changed! (Filename = %s)", palfile);
  372.             msg_write (wkstr);
  373.             return (-1);
  374.         default:
  375.             sprintf (wkstr, "Error: (DFerror = %d) while checking previous palette. Something has changed! (Filename = %s)", palfile);
  376.             msg_write (wkstr);
  377.             return (-1);
  378.     }
  379. }
  380. /* EOF */
  381. /* cat > src+obj/palette/check_prev_palfn.c << "EOF" */
  382. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  383. /* check_prev_palfn: resolve previous palette filename. */
  384. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  385. /* SCCS information: %W%    %G% - NCSA */
  386.  
  387. /* #include "palette.h" */
  388.  
  389. int 
  390. check_prev_palfn (palfile)
  391.                 /* returns: - 0 = no error
  392.                          -1 = error in resolving palette filename */
  393.     char           *palfile;
  394.                 /* input: palette filename  */
  395. {
  396.     int             err;
  397.     char            save_palfile[MAXNAMELEN];
  398.  
  399.     strcpy (save_palfile, palfile);
  400.     if ((err = get_abspathname (save_palfile, MAXNAMELEN, TYPE_FILE)))
  401.     {
  402.         switch (err)
  403.         {
  404.             case 1:
  405.                 sprintf (wkstr, "Error: Previous palette filename not found (Filename = %s)", save_palfile);
  406.                 msg_write (wkstr);
  407.                 break;
  408.             case 2:
  409.                 sprintf (wkstr, "Error: Previous palette filename not a file (Filename = %s).", save_palfile);
  410.                 msg_write (wkstr);
  411.                 break;
  412.             default:
  413.                 sprintf (wkstr, "Internal error (%d): While processing previous palette filename (Filename = %s).", err, palfile);
  414.                 msg_write (wkstr);
  415.                 break;
  416.         }
  417.         return (-1);
  418.     }
  419.     return (0);
  420. }
  421. /* EOF */
  422. /* cat > src+obj/palette/color_def_win.c << "EOF" */
  423. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  424. /* color_def_win: set colors for defaults window    */
  425. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  426. /* SCCS information: %W%    %G% - NCSA */
  427.  
  428. /* #include "palette.h" */
  429.  
  430. int 
  431. color_def_win ()
  432. {
  433.         /* set color map same with imagetool */
  434.     if (def_box != NULL)
  435.         init_same_colormap ((Pixwin *) window_get (def_box, WIN_PIXWIN));
  436.     if (def_panel != NULL)
  437.         init_same_colormap ((Pixwin *) window_get (def_panel, WIN_PIXWIN));
  438.     if (imagesave_box != NULL)
  439.         init_same_colormap ((Pixwin *) window_get (imagesave_box, WIN_PIXWIN));
  440.     if (imagesave_panel != NULL)
  441.         init_same_colormap ((Pixwin *) window_get (imagesave_panel, WIN_PIXWIN));
  442.     if (palsave_box != NULL)
  443.         init_same_colormap ((Pixwin *) window_get (palsave_box, WIN_PIXWIN));
  444.     if (palsave_panel != NULL)
  445.         init_same_colormap ((Pixwin *) window_get (palsave_panel, WIN_PIXWIN));
  446.     return (0);
  447. }
  448. /* EOF */
  449. /* cat > src+obj/palette/color_dialog_win.c << "EOF" */
  450. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  451. /* color_dialog_win: set colors for dialog window    */
  452. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  453. /* SCCS information: %W%    %G% - NCSA */
  454.  
  455. /* #include "palette.h" */
  456.  
  457. int 
  458. color_dialog_win ()
  459. {
  460.         /* blocking pop up - none required */
  461.  
  462.     return (0);
  463. }
  464. /* EOF */
  465. /* cat > src+obj/palette/color_entry_win.c << "EOF" */
  466. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  467. /* color_entry_win: set colors for entry window        */
  468. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  469. /* SCCS information: %W%    %G% - NCSA */
  470.  
  471. /* #include "palette.h" */
  472.  
  473. int 
  474. color_entry_win ()
  475. {
  476.         /* set color map same with imagetool */
  477.     init_same_colormap ((Pixwin *) window_get (entry_frame, WIN_PIXWIN));
  478.     init_same_colormap ((Pixwin *) window_get (entry_panel1, WIN_PIXWIN));
  479.     init_same_colormap ((Pixwin *) window_get (entry_panel2, WIN_PIXWIN));
  480.  
  481.     return (0);
  482. }
  483. /* EOF */
  484. /* cat > src+obj/palette/color_help_win.c << "EOF" */
  485. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  486. /* color_help_win: set colors for help window(s)    */
  487. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  488. /* SCCS information: %W%    %G% - NCSA */
  489.  
  490. /* #include "palette.h" */
  491.  
  492. int 
  493. color_help_win ()
  494. {
  495.         /* set color map same with imagetool */
  496.     init_same_colormap ((Pixwin *) window_get (help_window, WIN_PIXWIN));
  497.     init_same_colormap ((Pixwin *) window_get (textsw, WIN_PIXWIN));
  498.  
  499.     return (0);
  500. }
  501. /* EOF */
  502. /* cat > src+obj/palette/color_path_win.c << "EOF" */
  503. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  504. /* color_path_win: set colors for pathname window    */
  505. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  506. /* SCCS information: %W%    %G% - NCSA */
  507.  
  508. /* #include "palette.h" */
  509.  
  510. int 
  511. color_path_win ()
  512. {
  513.         /* set color map same with imagetool */
  514.     init_same_colormap ((Pixwin *) window_get (path_frame, WIN_PIXWIN));
  515.     init_same_colormap ((Pixwin *) window_get (path_panel, WIN_PIXWIN));
  516.  
  517.     return (0);
  518. }
  519. /* EOF */
  520. /* cat > src+obj/palette/color_win.c << "EOF" */
  521. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  522. /* color_win: load the same colormap to the major    */
  523. /*          ImageTool windows                */
  524. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  525. /* SCCS information: %W%    %G% - NCSA */
  526.  
  527. /* #include "palette.h" */
  528.  
  529. void
  530. color_win (rmap, gmap, bmap)
  531.     unsigned char  *rmap, *gmap, *bmap;
  532.                 /* input: red, green, and blue colormaps of 256 values */
  533. {
  534.         /* load the same colormap to four imagetool windows */
  535.     pw_putcolormap ((Pixwin *) window_get (base, WIN_PIXWIN),
  536.             1, color_index - 4, rmap + 1, gmap + 1, bmap + 1);
  537.     pw_putcolormap ((Pixwin *) window_get (panel, WIN_PIXWIN),
  538.             1, color_index - 4, rmap + 1, gmap + 1, bmap + 1);
  539.     pw_putcolormap ((Pixwin *) window_get (menu_panel, WIN_PIXWIN),
  540.             1, color_index - 4, rmap + 1, gmap + 1, bmap + 1);
  541.     pw_putcolormap ((Pixwin *) canvas_pixwin (canvas),
  542.             1, color_index - 4, rmap + 1, gmap + 1, bmap + 1);
  543.     pw_putcolormap ((Pixwin *) window_get (msgsw, WIN_PIXWIN),
  544.             1, color_index - 4, rmap + 1, gmap + 1, bmap + 1);
  545.     pw_putcolormap ((Pixwin *) window_get (ttysw, WIN_PIXWIN),
  546.             1, color_index - 4, rmap + 1, gmap + 1, bmap + 1);
  547.     if (color_edit_mode == CEDIT_FIDDLE)
  548.         pw_getcolormap (pw, 0, color_index, red1, green1, blue1);
  549. }
  550. /* EOF */
  551. /* cat > src+obj/palette/create_pal_menu.c << "EOF" */
  552. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  553. /* create_pal_menu: create menu for 'PALETTE'        */
  554. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  555. /* SCCS information: %W%    %G% - NCSA */
  556.  
  557. /* #include "palette.h" */
  558.  
  559. int
  560. create_pal_menu ()
  561. {
  562.     pal_menu = menu_create (MENU_FONT, font_menu,
  563.                 MENU_STRINGS,
  564.                     "Load",
  565.                     "Restore",
  566.                     "Show",
  567.                     "Contour",
  568.                     "Fiddle",
  569.                     "Transpose",
  570.                     "Save",
  571.                     0,
  572.                 MENU_NOTIFY_PROC, pal_proc,
  573.                 0);
  574. }
  575. /* EOF */
  576. /* cat > src+obj/palette/create_palsave_box.c << "EOF" */
  577. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  578. /* create_palsave_box: create save palette frame    */
  579. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  580. /* SCCS information: %W%    %G% - NCSA */
  581.  
  582. /* #include "option.h" */
  583.  
  584. create_palsave_box ()
  585. {
  586.     int             h, w;
  587.  
  588.     palsave_box = window_create (0, FRAME,
  589.                      FRAME_SHOW_LABEL, FALSE,
  590.                      WIN_SHOW, FALSE,
  591.                      0);
  592.     palsave_panel = window_create (palsave_box, PANEL,
  593.                        PANEL_FONT, font_panel,
  594.                        PANEL_LABEL_BOLD, TRUE,
  595.                        0);
  596. /*
  597.     panel_create_item (palsave_panel, PANEL_MESSAGE,
  598.                PANEL_LABEL_STRING, "Please enter below:",
  599.                0);
  600. */
  601.     palsave_item = panel_create_item (palsave_panel, PANEL_TEXT,
  602.                     PANEL_VALUE_DISPLAY_LENGTH, 64,
  603.                     PANEL_VALUE_STORED_LENGTH, MAXNAMELEN - 1,
  604.                     PANEL_LABEL_STRING, "Palette filename: ",
  605.                     /*
  606.                     PANEL_MENU_CHOICE_STRINGS,
  607.                         "^N - Get directory name",
  608.                         0,
  609.                     PANEL_MENU_CHOICE_FONTS,
  610.                         font_menu,
  611.                         0,
  612.                     PANEL_MENU_CHOICE_VALUES,
  613.                         CTRL_N,
  614.                         0,
  615.                     PANEL_SHOW_MENU, TRUE,
  616.                     PANEL_NOTIFY_LEVEL, PANEL_ALL,
  617.                     PANEL_NOTIFY_PROC, defaults_proc,
  618.                     PANEL_CLIENT_DATA, 1,
  619.                     */
  620.                     0);
  621.     palsave_toggle = panel_create_item (palsave_panel, PANEL_CYCLE,
  622.                         PANEL_LABEL_STRING, "Format:",
  623.                         PANEL_CHOICE_STRINGS,
  624.                         "HDF                        ",
  625.                          "Raw                        ",
  626.                          0,
  627.                      0);
  628.     panel_create_item (palsave_panel, PANEL_BUTTON,
  629.                PANEL_LABEL_IMAGE, panel_button_image (palsave_panel, "Ok", 3, font_panel_button),
  630.                PANEL_CLIENT_DATA, 1,
  631.                PANEL_NOTIFY_PROC, palsave_ok_cancel,
  632.                0);
  633.     panel_create_item (palsave_panel, PANEL_BUTTON,
  634.                PANEL_LABEL_IMAGE, panel_button_image (palsave_panel, "Cancel", 6, font_panel_button),
  635.                PANEL_CLIENT_DATA, 0,
  636.                PANEL_NOTIFY_PROC, palsave_ok_cancel,
  637.                0);
  638.     window_fit (palsave_panel);
  639.     window_fit (palsave_box);
  640.  
  641.     w = (int) window_get (palsave_box, WIN_WIDTH);
  642.     h = (int) window_get (palsave_box, WIN_HEIGHT);
  643.     window_set (palsave_box, WIN_X, 1150 / 2 - w / 2, WIN_Y, 900 / 2 - h / 2, 0);
  644.  
  645.         /* set color map same with imagetool */
  646.     (void) color_def_win ();
  647.  
  648. }
  649. /* EOF */
  650. /* cat > src+obj/palette/fiddle_proc.c << "EOF" */
  651. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  652. /* fiddle_proc: A linear transformation on the original    */
  653. /*        palette. The original palette is to be    */
  654. /*        squeezed, or expanded, or shifted to     */
  655. /*        the left or right horizontally. A    */
  656. /*        linear equation is used to perform the    */
  657. /*        transformation:             */
  658. /*            y = a*(x - b1) + b        */
  659. /*        where x models the original palette and    */
  660. /*        y the transformed. Both x and y take    */
  661. /*        discrete values 0, 1, 2, ... 255 as the */
  662. /*        indices to the color table. In the    */
  663. /*        above equation, a is the SLOPE of the    */
  664. /*        line function, b1 is  the translation    */
  665. /*        in x direction and b the y intercept    */
  666. /*        of the linear function. These        */
  667. /*        parameters bound the cursor movement to    */
  668. /*        the linear function, that is, a relates    */
  669. /*        the vertical movement of the cursor to    */
  670. /*        the slope of the line function, and b1    */
  671. /*        and b relate the horizontal movement    */
  672. /*        (left or right) to the x and y         */
  673. /*        translation of the function.         */
  674. /*                            */
  675. /*         The formula for a, b1 and b are:    */
  676. /*                            */
  677. /*        a = tan((3.1415926/(2*window_height))*    */
  678. /*            (window_height - y_cursor))    */
  679. /*        b1 = (256/window_width)*(window_width    */
  680. /*            - x_cursor)            */
  681. /*        b = (256/window_width)*x_cursor        */
  682. /*                            */
  683. /*        A mapping vector is recorded for    */
  684. /*        saving the altered image.        */
  685. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  686. /* SCCS information: %W%    %G% - NCSA */
  687.  
  688. /* #include "palette.h" */
  689.  
  690. fiddle_proc (eid, event)
  691.     int             eid;
  692.     Event          *event;
  693. {
  694.     unsigned char   red[256], green[256], blue[256];
  695.     double          up, down, a, b, unit_theta;
  696.     int             x, y, i, j, k, prej;
  697.     int             cwin_width = (int) window_get (canvas, WIN_WIDTH, 0);
  698.     int             cwin_height = (int) window_get (canvas, WIN_HEIGHT, 0);
  699.     static int      middle_down;
  700.  
  701.         /* init new palette */
  702.     fiddled = 1;
  703.     for (i = 0; i < color_index; i++)
  704.     {
  705.         red[i] = red1[color_index - 4];
  706.         green[i] = green1[color_index - 4];
  707.         blue[i] = blue1[color_index - 4];
  708.     }
  709.  
  710.         /* init parameters */
  711.     unit_theta = PI / (2.0 * cwin_height);
  712.  
  713.     switch (eid)
  714.     {
  715.         case LOC_DRAG:
  716.         case MS_LEFT:
  717.             if (middle_down)
  718.                 return;
  719.             x = event_x (event);
  720.             y = event_y (event);
  721.             if (y < 10)
  722.                 y = 10;
  723.  
  724.             a = tan ((double) (unit_theta * (cwin_height - y)));
  725.  
  726.             up = 1 + (127 * a);
  727.             down = 256 - (128 * a);
  728.             if (up > down)
  729.                 b = (((1.0 * x) / (1.0 * cwin_width)) * (up - down)) + down;
  730.             else
  731.                 b = (((1.0 * x) / (1.0 * cwin_width)) * (down - up)) + up;
  732.  
  733.                 /* linear transformation */
  734.             prej = 0;
  735.             for (i = 1; i < color_index - 3; i++)
  736.             {
  737.                 j = (int) (a * (i - (color_index / 2)) + b);
  738.                 if (j > 0 && j < color_index - 3)
  739.                 {
  740.                     if (j - prej > 1)
  741.                         for (k = prej + 1; k < j; k++)
  742.                         {
  743.                             red[k] = red1[i];
  744.                             green[k] = green1[i];
  745.                             blue[k] = blue1[i];
  746.                             map[k] = i;
  747.                         }
  748.                     red[j] = red1[i];
  749.                     green[j] = green1[i];
  750.                     blue[j] = blue1[i];
  751.                     map[j] = i;
  752.                 }
  753.                 else
  754.                 {
  755.                     if (j >= color_index - 3)
  756.                     {
  757.                         for (k = prej + 1; k < color_index - 3; k++)
  758.                         {
  759.                             red[k] = red[prej];
  760.                             green[k] = green[prej];
  761.                             blue[k] = blue[prej];
  762.                             map[k] = map[prej];
  763.                         }
  764.                         break;
  765.                     }
  766.                     else
  767.                         continue;
  768.                 }
  769.                 prej = j;
  770.             }
  771.             pw_putcolormap (pw, 1, color_index - 4, red + 1, green + 1, blue + 1);
  772.             pw_putcolormap ((Pixwin *) window_get (base, WIN_PIXWIN),
  773.                     1, color_index - 4, red + 1, green + 1, blue + 1);
  774.             break;
  775.         case MS_MIDDLE:
  776.             if (event_is_down (event))
  777.             {
  778.                 (void) notify_set_itimer_func (panel, rotate_cmap,
  779.                                    ITIMER_REAL, &NOTIFY_POLLING_ITIMER, NULL);
  780.                 middle_down = 1;
  781.             }
  782.             else
  783.             {        /* release button to stop rotating */
  784.                 (void) notify_set_itimer_func (panel, rotate_cmap,
  785.                                    ITIMER_REAL, NULL, NULL);
  786.                 middle_down = 0;
  787.             }
  788.             break;
  789.         case MS_RIGHT:
  790.             color_edit_mode = CEDIT_NONE;
  791.             msg_write ("Note: Exit from fiddle mode.");
  792.             break;
  793.         default:
  794.             break;
  795.     }
  796. }
  797. /* EOF */
  798. /* cat > src+obj/palette/init_cmsname.c << "EOF" */
  799. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  800. /* init_cmsname:  init color segments for all        */
  801. /*          subwindows and frames            */
  802. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  803. /* SCCS information: %W%    %G% - NCSA */
  804.  
  805. /* #include "palette.h" */
  806.  
  807. int
  808. init_cmsname ()
  809. {
  810.     Pixwin         *pixw;
  811.     char            red[LUTSIZE], green[LUTSIZE], blue[LUTSIZE];
  812.     int             i;
  813.  
  814.         /* set background and forground color */
  815.     if (dark_bg)
  816.     {
  817.         red[0] = green[0] = blue[0] = 0;
  818.         i = color_index - 1;
  819.         red[i] = green[i] = 255;
  820.         blue[i] = 0;
  821.         i = color_index - 2;
  822.         red[i] = green[i] = blue[i] = 0;
  823.         i = color_index - 3;
  824.         red[i] = green[i] = 255;
  825.         blue[i] = 0;
  826.     }
  827.     else
  828.     {
  829.         red[0] = green[0] = blue[0] = 255;
  830.         i = color_index - 1;
  831.         red[i] = green[i] = blue[i] = 0;
  832.         i = color_index - 2;
  833.         red[i] = green[i] = blue[i] = 255;
  834.         i = color_index - 3;
  835.         red[i] = green[i] = blue[i] = 0;
  836.     }
  837.  
  838.         /* create color segments with each subwindow.
  839.            Trick here for b/w environment. */
  840.  
  841.     sprintf (cmsname, "imgtl%D", getpid ());
  842.  
  843.     pixw = (Pixwin *) window_get (base, WIN_PIXWIN);
  844.     pw_setcmsname (pixw, cmsname);
  845.     pw_putcolormap (pixw, 0, color_index - 2, red, green, blue);
  846.  
  847.     strcat (cmsname, "cs");
  848.  
  849.     pixw = (Pixwin *) window_get (ttysw, WIN_PIXWIN);
  850.     pw_setcmsname (pixw, cmsname);
  851.     pw_putcolormap (pixw, 0, color_index, red, green, blue);
  852.  
  853.     pixw = (Pixwin *) window_get (panel, WIN_PIXWIN);
  854.     pw_setcmsname (pixw, cmsname);
  855.     pw_putcolormap (pixw, 0, color_index, red, green, blue);
  856.  
  857.     pixw = (Pixwin *) window_get (msgsw, WIN_PIXWIN);
  858.     pw_setcmsname (pixw, cmsname);
  859.     pw_putcolormap (pixw, 0, color_index, red, green, blue);
  860.  
  861.     pixw = (Pixwin *) window_get (menu_panel, WIN_PIXWIN);
  862.     pw_setcmsname (pixw, cmsname);
  863.     pw_putcolormap (pixw, 0, color_index, red, green, blue);
  864.  
  865.     pw_setcmsname (pw, cmsname);
  866.     pw_putcolormap (pw, 0, color_index, red, green, blue);
  867. }
  868. /* EOF */
  869. /* cat > src+obj/palette/init_same_colormap.c << "EOF" */
  870. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  871. /* init_same_colormap: set pwin's color map same as    */
  872. /*               ImageTool base            */
  873. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  874. /* SCCS information: %W%    %G% - NCSA */
  875.  
  876. /* #include "palette.h" */
  877.  
  878. int
  879. init_same_colormap (pwin)
  880.     Pixwin         *pwin;
  881. {
  882.     unsigned char   red[LUTSIZE], green[LUTSIZE], blue[LUTSIZE];
  883.  
  884.         /* first get imagetool canvas colormap */
  885.     pw_getcolormap (pw, 0, color_index, red, green, blue);
  886.  
  887.         /* set pwin to the same colormap */
  888.     pw_setcmsname (pwin, cmsname);
  889.     pw_putcolormap (pwin, 0, color_index, red, green, blue);
  890. }
  891. /* EOF */
  892. /* cat > src+obj/palette/load_pal.c << "EOF" */
  893. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  894. /* load_pal: Three steps:                */
  895. /*         1. Get the resolved name using        */
  896. /*        check_palfn ()                */
  897. /*         2. Check the file using check_pal ()    */
  898. /*         3. Get the palette using either         */
  899. /*        set_palRAW () or set_palHDF ()        */
  900. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  901. /* SCCS information: %W%    %G% - NCSA */
  902.  
  903. /* #include "palette.h" */
  904.  
  905. int
  906. load_pal (type)
  907.                 /* returns error - 0 = no error
  908.                           -1 = palette not loaded */
  909.     int             type;
  910.                 /* input: REGULAR  = regular
  911.                       DEFAULT  = default */
  912. {
  913.     char            palfile[MAXNAMELEN];
  914.     enum pal_type   ptype;
  915.  
  916.     clear_request = 0;    /* What is this for? */
  917.  
  918.     if (check_palfn (palfile, type))
  919.         return (-1);
  920.     if (check_pal (palfile, &ptype, type))
  921.         return (-1);
  922.     switch (ptype)
  923.     {
  924.         case PAL_RAW:
  925.             (void) set_palRAW (palfile, type);
  926.             break;
  927.         case PAL_HDF:
  928.             (void) set_palHDF (palfile, PAL_HDF, NULL, type);
  929.             break;
  930.     }
  931.     return (0);
  932. }
  933. /* EOF */
  934. /* cat > src+obj/palette/pal_board_proc.c << "EOF" */
  935. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  936. /* pal_board_proc: handler for palette text input    */
  937. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  938. /* SCCS information: %W%    %G% - NCSA */
  939.  
  940. /* #include "palette.h" */
  941.  
  942. Panel_setting
  943. pal_board_proc (item, event)
  944.     Panel_item      item;
  945.     Event          *event;
  946. {
  947.     switch (event_id (event))
  948.     {
  949.         case CTRL_N:
  950.             (void) display_pathnamesw ("Get palette filename", update_pal_board);
  951.             return PANEL_NONE;
  952.         case CTRL_L:
  953.             load_pal (REGULAR);
  954.             return PANEL_NONE;
  955.         case CTRL_R:
  956.             undo_pal ();
  957.             return PANEL_NONE;
  958.         case CTRL_S:
  959.             view_pal ();
  960.             return PANEL_NONE;
  961.         default:
  962.             return panel_text_notify (item, event);
  963.     }
  964. }
  965. /* EOF */
  966. /* cat > src+obj/palette/pal_handler.c << "EOF" */
  967. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  968. /* pal_handler: palette menu bar handler        */
  969. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  970. /* SCCS information: %W%    %G% - NCSA */
  971.  
  972. /* #include "palette.h" */
  973.  
  974. int
  975. pal_handler (item, event)
  976.     Panel_item      item;
  977.     Event          *event;
  978. {
  979.         /* display image menu */
  980.     if (display_panel_menu (item, event, menu_panel, pal_menu))
  981.         pal_proc (pal_menu, menu_get (pal_menu, MENU_NTH_ITEM, 1));
  982. }
  983. /* EOF */
  984. /* cat > src+obj/palette/pal_proc.c << "EOF" */
  985. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  986. /* pal_proc: handler for palette menu items         */
  987. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  988. /* SCCS information: %W%    %G% - NCSA */
  989.  
  990. /* #include "palette.h" */
  991.  
  992. void
  993. pal_proc (m, mi)
  994.     Menu            m;
  995.     Menu_item       mi;
  996. {
  997.     char            str[MAXNAMELEN];
  998.     int             i;
  999.  
  1000.     strcpy (str, (char *) menu_get (mi, MENU_STRING));
  1001.     if (!strcmp ("Load", str))
  1002.         load_pal (REGULAR);
  1003.     else if (!strcmp ("Restore", str))
  1004.         undo_pal ();
  1005.     else if (!strcmp ("Show", str))
  1006.         view_pal ();
  1007.     else if (!strcmp ("Save", str))
  1008.         save_palette ();
  1009.     else if (!strcmp ("Contour", str))
  1010.     {
  1011.         color_edit_mode = CEDIT_CONT;
  1012.         msg_write ("Note: In editing mode. Press right button to exit.");
  1013.         pw_getcolormap (pw, 0, color_index, red1, green1, blue1);
  1014.         first_middle_down = 0;
  1015.     }
  1016.     else if (!strcmp ("Fiddle", str))
  1017.     {
  1018.         color_edit_mode = CEDIT_FIDDLE;
  1019.         msg_write ("Note: In fiddle mode. Press right button to exit.");
  1020.         pw_getcolormap (pw, 0, color_index, red1, green1, blue1);
  1021.         for (i = 0; i < color_index; i++)
  1022.             map[i] = color_index - 4;
  1023.         first_middle_down = 0;
  1024.     }
  1025.     else if (!strcmp ("Transpose", str))
  1026.         transpose ();
  1027. }
  1028. /* EOF */
  1029. /* cat > src+obj/palette/palsave_ok_cancel.c << "EOF" */
  1030. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1031. /* palsave_ok_cancel: ok/cancel notify proc for     */
  1032. /*              saving palette            */
  1033. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1034. /* SCCS information: %W%    %G% - NCSA */
  1035.  
  1036. /* #include "palette.h" */
  1037.  
  1038. palsave_ok_cancel (item, event)
  1039.     Panel_item      item;
  1040.     Event          *event;
  1041. {
  1042.     int             which_item, nw;
  1043.     int             value, f, i;
  1044.     unsigned char    *p_palette;
  1045.     char            pal[MAXNAMELEN];
  1046.     unsigned char     red[LUTSIZE], green[LUTSIZE], blue[LUTSIZE];
  1047.     char        str[20];
  1048.  
  1049.     if ((which_item = (int) panel_get (item, PANEL_CLIENT_DATA)))
  1050.     {
  1051.         strcpy (pal, (char *) panel_get_value (palsave_item));
  1052.  
  1053.             /* check value  - report first error and wait for the
  1054.                       user to respond again */
  1055.         if ((nw = word_count (pal)) == 0)
  1056.         {        /* null entry */
  1057.             msg_write ("Error: No filename. Please provide one or cancel.");
  1058.             return;
  1059.         }
  1060.         else if (nw != 1)
  1061.         {
  1062.             msg_write ("Error: More than one filename. Please provide only one or cancel.");
  1063.             return;
  1064.         }
  1065.         (void) strip_wspace (pal);
  1066.  
  1067.             /* append .pal if file will be saved as RAW */
  1068.         value = (int) panel_get_value (palsave_toggle);
  1069.         if (value == 1)
  1070.             strcat(pal, ".pal");
  1071.         if ((f = creat (pal, 0644)) == -1)
  1072.         {
  1073.             msg_write ("Error: File creation failed. Bad path or access problem.");
  1074.             return; 
  1075.         }
  1076.  
  1077.         pw_getcolormap (pw, 0, color_index, red, green, blue);
  1078.         if (value == 0)
  1079.         {        /* HDF */
  1080.             close (f);
  1081.             p_palette = palette;
  1082.             for (i = 0; i < 256; i++)
  1083.             {
  1084.                 *p_palette++ = red[i];
  1085.                 *p_palette++ = green[i];
  1086.                 *p_palette++ = blue[i];
  1087.             }
  1088.                 /* write as a fresh palette and create new file */
  1089.             if (DFPputpal (pal, palette, 0, "w"))
  1090.             {
  1091.                 sprintf (wkstr, "Error: HDF putpal call failed (DFerror = %d).", DFerror);
  1092.                 msg_write (wkstr);
  1093.                 return;
  1094.             }
  1095.         }
  1096.         else
  1097.         {        /* Raw */
  1098.             p_palette = palette;
  1099.             for (i = 0; i < 256; i++)
  1100.                 *p_palette++ = red[i];
  1101.             for (i = 0; i < 256; i++)
  1102.                 *p_palette++ = blue[i];
  1103.             for (i = 0; i < 256; i++)
  1104.                 *p_palette++ = green[i];
  1105.             if (PALETTE_SIZE != (nw = write (f, palette, PALETTE_SIZE)))
  1106.             {
  1107.                 if (nw == -1)
  1108.                 {
  1109.                     msg_write ("Error: Raw palette write to file failed.");
  1110.                     close (f);
  1111.                     return (-1);
  1112.                 }
  1113.                 else
  1114.                 {
  1115.                     sprintf (wkstr, "Error: Only %d of %d bytes written to raw raster file.", nw, PALETTE_SIZE);
  1116.                     msg_write (wkstr);
  1117.                     close (f);
  1118.                     return (-1);
  1119.                 }
  1120.             }
  1121.             close (f);
  1122.         }
  1123.         window_set (palsave_box, WIN_SHOW, FALSE, 0);
  1124.         msg_write ("Note: Palette saved.");
  1125.     }
  1126.     else
  1127.         window_set (palsave_box, WIN_SHOW, FALSE, 0);
  1128.  
  1129.     return;
  1130. }
  1131. /* EOF */
  1132. /* cat > src+obj/palette/reset_pal.c << "EOF" */
  1133. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1134. /* reset_pal: restore the current palette         */
  1135. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1136. /* SCCS information: %W%    %G% - NCSA */
  1137.  
  1138. /* #include "palette.h" */
  1139.  
  1140. int
  1141. reset_pal ()
  1142. {
  1143.     char palfile[MAXNAMELEN];
  1144.  
  1145.     if (curr_pal.fn[0] == NULL)     /* no current palette filename */
  1146.     {
  1147.         msg_write ("Warning: No current or default palette to restore.");
  1148.         panel_set_value (pal_board, "");
  1149.         return (-1);
  1150.     }
  1151.  
  1152.     panel_set_value (pal_board, curr_pal.fn);
  1153.     if (check_prev_palfn (curr_pal.fn))
  1154.     {
  1155.         panel_set_value (pal_board, "");
  1156.         return;
  1157.     }
  1158.     if (check_prev_pal (curr_pal.fn, curr_pal.type, palette))
  1159.     {
  1160.         panel_set_value (pal_board, "");
  1161.         return;
  1162.     }
  1163.     switch (last_pal.type)
  1164.     {
  1165.         case PAL_RAW:
  1166.             (void) set_palRAW (curr_pal.fn, REGULAR);
  1167.             break;
  1168.         case PAL_HDF:    
  1169.             (void) set_palHDF (curr_pal.fn, PAL_HDF, NULL, REGULAR);
  1170.             break;
  1171.         case PAL_RIS8:    
  1172.             (void) set_palHDF (curr_pal.fn, PAL_RIS8, NULL, REGULAR);
  1173.             break;
  1174.     }
  1175.     return (0);
  1176. }
  1177. /* EOF */
  1178.  
  1179. /* cat > src+obj/palette/rotate_cmap.c << "EOF" */
  1180. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1181. /* rotate_cmap: (static) rotate color mape        */
  1182. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1183. /* SCCS information: %W%    %G% - NCSA */
  1184.  
  1185. /* #include "palette.h" */
  1186.  
  1187. static          Notify_value
  1188. rotate_cmap (client, itimer_type)
  1189.     Notify_client   client;
  1190.     int             itimer_type;
  1191. {
  1192.     char            red1[256], green1[256], blue1[256];
  1193.     char            red2[256], green2[256], blue2[256];
  1194.     int             i, j;
  1195.  
  1196.         /* rotate color map by one entry each time */
  1197.     pw_getcolormap (pw, 0, color_index, red1, green1, blue1);
  1198.     for (i = 1; i < color_index - 4; i++)
  1199.     {
  1200.         red2[i + 1] = red1[i];
  1201.         green2[i + 1] = green1[i];
  1202.         blue2[i + 1] = blue1[i];
  1203.     }
  1204.     red2[1] = red1[color_index - 4];
  1205.     green2[1] = green1[color_index - 4];
  1206.     blue2[1] = blue1[color_index - 4];
  1207.     pw_putcolormap (pw, 1, color_index - 4, red2 + 1, green2 + 1, blue2 + 1);
  1208.     pw_putcolormap ((Pixwin *) window_get (base, WIN_PIXWIN),
  1209.             1, color_index - 4, red2 + 1, green2 + 1, blue2 + 1);
  1210.  
  1211.     return (NOTIFY_DONE);
  1212. }
  1213. /* EOF */
  1214. /* cat > src+obj/palette/save_palette.c << "EOF" */
  1215. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1216. /* save_palette: bring up palette save box        */
  1217. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1218. /* SCCS information: %W%    %G% - NCSA */
  1219.  
  1220. /* #include "palette.h" */
  1221.  
  1222. save_palette()
  1223. {
  1224.     window_set (palsave_box, WIN_SHOW, TRUE, 0);
  1225. }
  1226. /* EOF */
  1227. /* cat > src+obj/palette/set_palHDF.c << "EOF" */
  1228. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1229. /* set_palHDF: load and set HDF palette. The file    */
  1230. /*           should be fully resolved and checked    */
  1231. /*           before calling. The HDF palette must    */
  1232. /*           be provided as an argument if associated */
  1233. /*           with a 8-Bit raster image set.        */
  1234. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1235. /* SCCS information: %W%    %G% - NCSA */
  1236.  
  1237. /* #include "palette.h" */
  1238.  
  1239. int
  1240. set_palHDF (palfile, ptype, RIS8pal, type)
  1241.                 /* returns: - 0 = no error
  1242.                          -1 = error - should not occur. Not checked for. */
  1243.     char           *palfile;
  1244.                 /* input: string of size MAXNAMELEN - 1 */
  1245.     enum pal_type   ptype;
  1246.                 /* input: palette type */
  1247.     unsigned char   RIS8pal[LUTSIZE];
  1248.                 /* input: RIS8 palette if ptype = PAL_RIS8.
  1249.                       NULL otherwise */
  1250.     int             type;
  1251.                 /* input: REGULAR  = regular
  1252.                       DEFAULT  = default */
  1253. {
  1254.     int        i;
  1255.     unsigned char  *p_palette;
  1256.     unsigned char   rmap[LUTSIZE], bmap[LUTSIZE], gmap[LUTSIZE];
  1257.     unsigned char   newpal[LUTSIZE*3];
  1258.  
  1259.     if (RIS8pal == NULL)
  1260.     {
  1261.         DFPrestart ();
  1262.         if (DFPgetpal (palfile, newpal))    /* should not fail */
  1263.             msg_write ("Error: there was not palette in the file.");
  1264.         p_palette = newpal;
  1265.     }
  1266.     else
  1267.         p_palette = RIS8pal;
  1268.  
  1269.     for (i = 0; i < 256; i++)
  1270.     {        /* put in red, green, blue arrays */
  1271.         rmap[i] = *p_palette++;
  1272.         gmap[i] = *p_palette++;
  1273.         bmap[i] = *p_palette++;
  1274.     }
  1275.  
  1276.     color_win (rmap, gmap, bmap);
  1277.     panel_set_value (pal_board, palfile);
  1278.  
  1279.     if (type == REGULAR)
  1280.     {
  1281.         strcpy (last_pal.fn, curr_pal.fn);
  1282.         last_pal.type = curr_pal.type;
  1283.         last_pal.num = curr_pal.num;
  1284.  
  1285.         strcpy (curr_pal.fn, palfile);
  1286.         curr_pal.type = PAL_HDF;
  1287.         curr_pal.num = 0;
  1288.     }
  1289.     else if (type == DEFAULT)
  1290.     {
  1291.         strcpy (curr_pal.fn, palfile);
  1292.         curr_pal.type = PAL_HDF;
  1293.         curr_pal.num = 0;
  1294.  
  1295.         strcpy (default_pal.fn, palfile);
  1296.         default_pal.type = PAL_HDF;
  1297.         default_pal.num = 0;
  1298.     }
  1299.  
  1300.     return (0);
  1301. }
  1302. /* EOF */
  1303. /* cat > src+obj/palette/set_palRAW.c << "EOF" */
  1304. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1305. /* set_palRAW: load and set raw palette. The file    */
  1306. /*           should be fully resolved and checked    */
  1307. /*           before calling.                 */
  1308. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1309. /* SCCS information: %W%    %G% - NCSA */
  1310.  
  1311. /* #include "palette.h" */
  1312.  
  1313. int
  1314. set_palRAW (palfile, type)
  1315.                 /* returns: - 0 = no error
  1316.                          -1 = error - should not occur. Not checked for. */
  1317.     char           *palfile;
  1318.                 /* input: string of size MAXNAMELEN - 1 */
  1319.     int             type;
  1320.                 /* input: REGULAR  = regular
  1321.                       DEFAULT  = default */
  1322. {
  1323.     int             f;
  1324.     unsigned char   rmap[LUTSIZE], bmap[LUTSIZE], gmap[LUTSIZE];
  1325.  
  1326.     f = open (palfile, O_RDONLY);    /* should not fail */
  1327.     read (f, rmap, color_index);    /* color_index = LUTSIZE */
  1328.     read (f, gmap, color_index);
  1329.     read (f, bmap, color_index);
  1330.     close (f);
  1331.  
  1332.     color_win (rmap, gmap, bmap);
  1333.  
  1334.     panel_set_value (pal_board, palfile);
  1335.  
  1336.     if (type == REGULAR)
  1337.     {
  1338.         strcpy (last_pal.fn, curr_pal.fn);
  1339.         last_pal.type = curr_pal.type;
  1340.         last_pal.num = curr_pal.num;
  1341.  
  1342.         strcpy (curr_pal.fn, palfile);
  1343.         curr_pal.type = PAL_RAW;
  1344.         curr_pal.num = 0;
  1345.     }
  1346.     else if (type == DEFAULT)
  1347.     {
  1348.         strcpy (curr_pal.fn, palfile);
  1349.         curr_pal.type = PAL_RAW;
  1350.         curr_pal.num = 0;
  1351.  
  1352.         strcpy (default_pal.fn, palfile);
  1353.         default_pal.type = PAL_RAW;
  1354.         default_pal.num = 0;
  1355.     }
  1356.  
  1357.     return (0);
  1358. }
  1359. /* EOF */
  1360. /* cat > src+obj/palette/transpose.c << "EOF" */
  1361. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1362. /* transpose: inverse the color map            */
  1363. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1364. /* SCCS information: %W%    %G% - NCSA */
  1365.  
  1366. /* #include "palette.h" */
  1367.  
  1368. transpose ()
  1369. {
  1370.     unsigned char   red[256], green[256], blue[256];
  1371.     int             i, k, tmp, n = color_index - 4;
  1372.  
  1373.     pw_getcolormap (pw, 0, color_index, red, green, blue);
  1374.     for (i = 1; i <= n / 2; i++)
  1375.     {
  1376.         k = n - i + 1;
  1377.         tmp = red[i];
  1378.         red[i] = red[k];
  1379.         red[k] = tmp;
  1380.         tmp = green[i];
  1381.         green[i] = green[k];
  1382.         green[k] = tmp;
  1383.         tmp = blue[i];
  1384.         blue[i] = blue[k];
  1385.         blue[k] = tmp;
  1386.         map[i] = k;
  1387.         map[k] = i;
  1388.     }
  1389.     map[0] = 0;
  1390.     map[n + 1] = n + 1;
  1391.     map[n + 2] = n + 2;
  1392.     map[n + 3] = n + 3;
  1393.     fiddled = 1;
  1394.     pw_putcolormap (pw, 1, color_index - 4, red + 1, green + 1, blue + 1);
  1395.     pw_putcolormap ((Pixwin *) window_get (base, WIN_PIXWIN),
  1396.             1, color_index - 4, red + 1, green + 1, blue + 1);
  1397. }
  1398. /* EOF */
  1399. /* cat > src+obj/palette/undo_pal.c << "EOF" */
  1400. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1401. /* undo_pal: restore a previous palette            */
  1402. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1403. /* SCCS information: %W%    %G% - NCSA */
  1404.  
  1405. /* #include "palette.h" */
  1406.  
  1407. int
  1408. undo_pal ()
  1409. {
  1410.     char            palfile[MAXNAMELEN];
  1411.  
  1412.     if (last_pal.fn[0] == NULL)
  1413.     {        /* no palette filename */
  1414.         msg_write ("Warning: No previous palette. Nothing loaded.");
  1415.         return;
  1416.     }
  1417.  
  1418.     panel_set_value (pal_board, last_pal.fn);
  1419.     if (check_prev_palfn (last_pal.fn))
  1420.     {
  1421.         panel_set_value (pal_board, curr_pal.fn);
  1422.         return;
  1423.     }
  1424.     if (check_prev_pal (last_pal.fn, last_pal.type, palette))
  1425.     {
  1426.         panel_set_value (pal_board, curr_pal.fn);
  1427.         return;
  1428.     }
  1429.  
  1430.     /* make a copy of the file name. The routines we are about to call
  1431.     will change last_pal.fn, so we can't pass it in. */
  1432.  
  1433.     strcpy (palfile, last_pal.fn);
  1434.     switch (last_pal.type)
  1435.     {
  1436.         case PAL_RAW:
  1437.             (void) set_palRAW(palfile, REGULAR);
  1438.             break;
  1439.         case PAL_HDF:
  1440.             (void) set_palHDF(palfile, PAL_HDF, NULL, REGULAR);
  1441.             break;
  1442.         case PAL_RIS8:
  1443.             (void) set_palHDF(palfile, PAL_RIS8, NULL, REGULAR);
  1444.             break;
  1445.     }
  1446.     return (0);
  1447. }
  1448. /* EOF */
  1449. /* cat > src+obj/palette/update_dir_board.c << "EOF" */
  1450. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1451. /* update_pal_board: updates the pal_board with     */
  1452. /*             full palette pathname        */
  1453. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1454. /* SCCS information: %W%    %G% - NCSA */
  1455.  
  1456. /* #include "palette.h" */
  1457.  
  1458. int
  1459. update_pal_board (dir, file)
  1460.     char           *dir;
  1461.     char           *file;
  1462. {
  1463.     sprintf (wkstr, "%s/%s", dir, file);
  1464.     panel_set_value (pal_board, wkstr);
  1465. }
  1466. /* EOF */
  1467. /* cat > src+obj/palette/view_pal.c << "EOF" */
  1468. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1469. /* view_pal: display a palette as a color bar 512x50    */
  1470. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1471. /* SCCS information: %W%    %G% - NCSA */
  1472.  
  1473. /* #include "palette.h" */
  1474.  
  1475. view_pal ()
  1476. {
  1477.     register        i, j;
  1478.     struct pixrect *pr;
  1479.     struct mpr_data *mpr;
  1480.     char           *s;
  1481.     int             x, y;
  1482.  
  1483.     first_middle_down = 0;
  1484.  
  1485.     if ((pr = mem_create (512 - 4, 50, 8)) == NULL)
  1486.     {
  1487.         msg_write ("Error: Not enough memory to create color bar.");
  1488.         return (-1);
  1489.     }
  1490.     mpr = mpr_d (pr);
  1491.     s = (char *) mpr->md_image;
  1492.     for (i = 0; i < 50; i++)
  1493.     {
  1494.         for (j = 0; j < color_index - 3; j++)
  1495.         {
  1496.             *s = j;
  1497.             s++;
  1498.             *s = j;
  1499.             s++;
  1500.         }
  1501.         *s = 0;
  1502.         s++;
  1503.         *s = 0;
  1504.         s++;
  1505.     }
  1506.     if (pos_picked)
  1507.     {
  1508.         x = startx;
  1509.         y = starty;
  1510.         pos_picked = 0;
  1511.     }
  1512.     else
  1513.     {
  1514.         x = curr_image.startx;
  1515.         y = curr_image.starty + curr_image.ydim + 5;
  1516.     }
  1517.     pw_write (pw, x, y, 512 - 4, 50, PIX_SRC, pr, 0, 0);
  1518.     pr_close (pr);
  1519.     return (0);
  1520. }
  1521. /* EOF */
  1522. /* cat > src+obj/palette/whiteout.c << "EOF" */
  1523. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1524. /* whiteout: contouring a palette user picked color    */
  1525. /*         will be whited out                */
  1526. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1527. /* SCCS information: %W%    %G% - NCSA */
  1528.  
  1529. /* #include "palette.h" */
  1530.  
  1531. whiteout (eid, event)
  1532.     int             eid;
  1533.     Event          *event;
  1534. {
  1535.     int             x, y, index, j;
  1536.     static unsigned char r_old, g_old, b_old;
  1537.     static unsigned char r_new, g_new, b_new;
  1538.     static int      left_down, middle_down, index0;
  1539.  
  1540.     x = event_x (event);
  1541.     y = event_y (event);
  1542.     index = pw_get (pw, x, y);
  1543.     switch (eid)
  1544.     {
  1545.         case MS_LEFT:
  1546.             if (event_is_down (event))
  1547.             {
  1548.                 left_down = 1;
  1549.                 if (event_ctrl_is_down (event))
  1550.                     pw_getcolormap (pw, index, 1, &r_new, &g_new, &b_new);
  1551.                 else
  1552.                     r_new = g_new = b_new = red1[0];
  1553.                 index0 = index;
  1554.                 pw_getcolormap (pw, index0, 1, &r_old, &g_old, &b_old);
  1555.                 if (index > 0 && index < color_index - 3)
  1556.                 {
  1557.                     pw_putcolormap (pw, index, 1, &r_new, &g_new, &b_new);
  1558.                     pw_putcolormap ((Pixwin *) window_get (base, WIN_PIXWIN),
  1559.                             index, 1, &r_new, &g_new, &b_new);
  1560.                 }
  1561.             }
  1562.             else
  1563.             {
  1564.                 left_down = 0;
  1565.             }
  1566.             break;
  1567.         case MS_MIDDLE:
  1568.             if (event_is_down (event))
  1569.             {
  1570.                 middle_down = 1;
  1571.                 if (event_ctrl_is_down (event))
  1572.                     pw_getcolormap (pw, index, 1, &r_new, &g_new, &b_new);
  1573.                 else
  1574.                     r_new = g_new = b_new = red1[0];
  1575.                 if (index > 0 && index < color_index - 3)
  1576.                 {
  1577.                     pw_putcolormap (pw, index, 1, &r_new, &g_new, &b_new);
  1578.                     pw_putcolormap ((Pixwin *) window_get (base, WIN_PIXWIN),
  1579.                             index, 1, &r_new, &g_new, &b_new);
  1580.                 }
  1581.             }
  1582.             else
  1583.             {        /* button released */
  1584.                 middle_down = 0;
  1585.             }
  1586.             break;
  1587.         case LOC_DRAG:
  1588.             if (middle_down && index > 0 && index < color_index - 3)
  1589.             {        /* white out colors along the way */
  1590.                 pw_putcolormap (pw, index, 1, &r_new, &g_new, &b_new);
  1591.                 pw_putcolormap ((Pixwin *) window_get (base, WIN_PIXWIN),
  1592.                         index, 1, &r_new, &g_new, &b_new);
  1593.             }
  1594.             else if (left_down && index != index0)
  1595.             {        /* restore previous index and white out only one entry */
  1596.                 pw_putcolormap (pw, index0, 1, &r_old, &g_old, &b_old);
  1597.                 pw_putcolormap ((Pixwin *) window_get (base, WIN_PIXWIN),
  1598.                         index0, 1, &r_old, &g_old, &b_old);
  1599.                 if (index > 0 && index < color_index - 3)
  1600.                 {
  1601.                     index0 = index;
  1602.                     pw_getcolormap (pw, index0, 1, &r_old, &g_old, &b_old);
  1603.                     pw_putcolormap (pw, index0, 1, &r_new, &g_new, &b_new);
  1604.                     pw_putcolormap ((Pixwin *) window_get (base, WIN_PIXWIN),
  1605.                             index0, 1, &r_new, &g_new, &b_new);
  1606.                 }
  1607.             }
  1608.             break;
  1609.         case MS_RIGHT:
  1610.             color_edit_mode = CEDIT_NONE;
  1611.             msg_write ("Note: Exit from editing mode.");
  1612.             break;
  1613.         default:
  1614.             break;
  1615.     }
  1616. }
  1617. /* EOF */
  1618.