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

  1. /* cat > headers/image2.h << "EOF" */
  2. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  3. /* image2.h: header for image2.c file.            */
  4. /*          contains floating point scaling routines    */
  5. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  6. /* SCCS information: %W%    %G% - NCSA */
  7.  
  8. #define    image2_h    1
  9.  
  10. #include "all.h"
  11. #include "newext.h"
  12.  
  13.     static Panel_item fmax_item, fmin_item;
  14.  
  15.     static Frame init_parabox();
  16.     static int ok_cancel();
  17.     static void set_scale_type(); 
  18.  
  19. /* EOF */
  20. /* cat > src+obj/image2/find_max.c << "EOF" */
  21. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  22. /* find_max: find maximum and minimum of floating point    */
  23. /*         array.                    */
  24. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  25. /* SCCS information: %W%    %G% - NCSA */
  26.  
  27. /* #include "image2.h" */
  28.  
  29. find_max (p, n, pmax, pmin)
  30.     float          *p, *pmax, *pmin;
  31.     int             n;
  32. {
  33.     float          *f, vmax, vmin;
  34.     int             i;
  35.  
  36.     f = p;
  37.     vmax = vmin = *f++;
  38.     for (i = 1; i < n; i++, f++)
  39.     {
  40.         if (*f > vmax)
  41.             vmax = *f;
  42.         else if (*f < vmin)
  43.             vmin = *f;
  44.     }
  45.     *pmax = vmax;
  46.     *pmin = vmin;
  47. }
  48. /* EOF */
  49. /* cat > src+obj/image2/image_scale.c << "EOF" */
  50. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  51. /* image_scale: scale HDF 2D Scientific Data Set    */
  52. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  53. /* SCCS information: %W%    %G% - NCSA */
  54.  
  55. /* #include "image2.h" */
  56.  
  57. int
  58. image_scale (xdim, ydim, fbuf, pr)
  59.                     /* returns: 0 - success
  60.                            -1 - error */
  61.     int             xdim, ydim;
  62.                 /* input: dimensions of image */
  63.     float          *fbuf;
  64.                 /* input: floating array of size xdim, ydim */
  65.     struct pixrect *pr;
  66.                 /* input: memory pixrect for scaled image */
  67. {
  68.     int        scale_toggle;
  69.     float           vmax, vmin, vtmax, vtmin;
  70.     unsigned char  *s;
  71.     int             filter = 0, answer, scale_toggel;
  72.     struct mpr_data *mpr;
  73.  
  74.         /* find maximum and minimum */
  75.     find_max (fbuf, xdim * ydim, &vmax, &vmin);
  76.  
  77.     vtmin = vmin;
  78.     vtmax = vmax;
  79.         /* check toggle if dialog box is required */
  80.     scale_toggle = (int) panel_get_value (SDS_toggle);
  81.     switch (scale_toggle)
  82.     {
  83.         case 0:        /* display a dialog box to get input from user */
  84.             sprintf (msgstr, "Data (Min, Max): (%e, %e)", vmin, vmax);
  85.             while ((answer = para_dialog (&vtmax, &vtmin, msgstr)))
  86.             {
  87.                 if (vtmax < vtmin)
  88.                     msg_write ("Error: Minimum is greater than maximum. Please fix or cancel.");
  89.                 else if (flinear == 0 && (vmin < 0.0 || vmax < 0.0))
  90.                     msg_write ("Error: Log scaling requires positive minimum and maximum. Please fix or cancel.");
  91.                 else
  92.                     break;
  93.             }
  94.             if (!answer)
  95.             {        /* operation cancelled */
  96.                 msg_write ("Note: Image not loaded.");
  97.                 return (-1);
  98.             }
  99.             break;
  100.  
  101.         case 1:     /* linear scaling - use data max and min */
  102.             flinear = 1;
  103.             break;
  104.         case 2:        /* log scaling - use data max and min */
  105.             flinear = 0;
  106.             if (vmin < 0.0 || vmax < 0.0)
  107.                 sprintf (wkstr, "Error: (min, max) = (%.2f, %.2f) cannot be log scaled due to negative numbers.", vmin, vmax);
  108.                 msg_write (wkstr);
  109.                 msg_write ("Note: Image not loaded.");
  110.                 return (-1);
  111.             break;
  112.     }
  113.  
  114.     if (vtmin != vmin || vtmax != vmax)
  115.         filter = 1;
  116.  
  117.         /* scale */
  118.     mpr = mpr_d (pr);
  119.     s = (unsigned char *) mpr->md_image;
  120.     if (flinear)        /* linear scale */
  121.         ln_compute (fbuf, s, xdim * ydim, vtmax, vtmin, filter);
  122.     else            /* log scale */
  123.         log_compute (fbuf, s, xdim * ydim, vtmax, vtmin, filter);
  124.     return (0);
  125. }
  126. /* EOF */
  127. /* cat > src+obj/image2/init_parabox.c << "EOF" */
  128. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  129. /* init_parabox: (static) initialize parabox        */
  130. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  131. /* SCCS information: %W%    %G% - NCSA */
  132.  
  133. /* #include "image2.h" */
  134.  
  135. static          Frame
  136. init_parabox (s)
  137. {
  138.     Frame           para_box;
  139.     Panel           panel;
  140.     int             left, top, w, h;
  141.  
  142.     left = (int) window_get (base, WIN_X);
  143.     top = (int) window_get (base, WIN_Y);
  144.     w = (int) window_get (base, WIN_WIDTH);
  145.     h = (int) window_get (base, WIN_HEIGHT);
  146.  
  147.     para_box = window_create (NULL, FRAME,
  148.                   FRAME_SHOW_LABEL, TRUE,
  149.                   FRAME_LABEL, "Scaling Information for 2D Scientific Data Set",
  150.                   WIN_X, (left + w) / 4,
  151.                   WIN_Y, (top + h) / 4,
  152.                   0);
  153.     panel = window_create (para_box, PANEL,
  154.                    PANEL_FONT, font_panel,
  155.                    0);
  156.     panel_create_item (panel, PANEL_MESSAGE,
  157.                PANEL_ITEM_X, ATTR_COL (2),
  158.                PANEL_ITEM_Y, ATTR_ROW (0),
  159.                PANEL_LABEL_STRING, s,
  160.                PANEL_LABEL_BOLD, FALSE,
  161.                0);
  162. /*
  163.     panel_create_item(panel, PANEL_MESSAGE,
  164.             PANEL_ITEM_X, ATTR_COL(2),
  165.             PANEL_ITEM_Y, ATTR_ROW(1),
  166.             PANEL_LABEL_STRING, "Your choice please (or cancel): ",
  167.             PANEL_LABEL_BOLD, FALSE,
  168.             0);
  169. */
  170.     fmin_item = panel_create_item (panel, PANEL_TEXT,
  171.                        PANEL_ITEM_X, ATTR_COL (5),
  172.                        PANEL_ITEM_Y, ATTR_ROW (1),
  173.                        PANEL_VALUE_STORED_LENGTH, 20,
  174.                        PANEL_VALUE_DISPLAY_LENGTH, 20,
  175.                        PANEL_LABEL_STRING, "Minimum: ",
  176.                        PANEL_LABEL_BOLD, FALSE,
  177.                        0);
  178.     fmax_item = panel_create_item (panel, PANEL_TEXT,
  179.                        PANEL_ITEM_X, ATTR_COL (5),
  180.                        PANEL_ITEM_Y, ATTR_ROW (2),
  181.                        PANEL_VALUE_STORED_LENGTH, 20,
  182.                        PANEL_VALUE_DISPLAY_LENGTH, 20,
  183.                        PANEL_LABEL_STRING, "Maximum: ",
  184.                        PANEL_LABEL_BOLD, FALSE,
  185.                        0);
  186.     panel_create_item (panel, PANEL_CYCLE,
  187.                PANEL_ITEM_X, ATTR_COL (3),
  188.                PANEL_ITEM_Y, ATTR_ROW (3),
  189.                PANEL_LABEL_STRING, "Scaling type: ",
  190.                PANEL_CHOICE_STRINGS,
  191.                    "Logarithmic",
  192.                    "Linear",
  193.                    0,
  194.                PANEL_VALUE, 1,
  195.                PANEL_LABEL_BOLD, FALSE,
  196.                PANEL_NOTIFY_PROC, set_scale_type,
  197.                0);
  198.     panel_create_item (panel, PANEL_BUTTON,
  199.                PANEL_ITEM_X, ATTR_COL (35),
  200.                PANEL_ITEM_Y, ATTR_ROW (3),
  201.                PANEL_LABEL_IMAGE, panel_button_image (panel, "Ok", 3, NULL),
  202.                PANEL_CLIENT_DATA, 1,
  203.                PANEL_NOTIFY_PROC, ok_cancel,
  204.                0);
  205.     panel_create_item (panel, PANEL_BUTTON,
  206.                PANEL_ITEM_X, ATTR_COL (42),
  207.                PANEL_ITEM_Y, ATTR_ROW (3),
  208.                PANEL_LABEL_IMAGE, panel_button_image (panel, "Cancel", 6, NULL),
  209.                PANEL_CLIENT_DATA, 0,
  210.                PANEL_NOTIFY_PROC, ok_cancel,
  211.                0);
  212.     window_fit (panel);
  213.     window_fit (para_box);
  214.  
  215.     return para_box;
  216. }
  217. /* EOF */
  218. /* cat > src+obj/image2/ln_compute.c << "EOF" */
  219. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  220. /* ln_compute: compute linear scaling with threshold    */
  221. /*           filtering.                */
  222. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  223. /* SCCS information: %W%    %G% - NCSA */
  224.  
  225. /* #include "image2.h" */
  226.  
  227. ln_compute (f, s, n, fmax, fmin, filter)
  228.     float          *f, fmax, fmin;
  229.     unsigned char  *s;
  230.     int             n, filter;
  231. {
  232.     float           ftmp;
  233.     int             itmp, i;
  234.  
  235.     ftmp = fmax - fmin;
  236.     for (i = 0; i < n; i++, f++, s++)
  237.     {
  238.         if (filter)
  239.         {
  240.             if (*f > fmax)
  241.                 *f = fmax;
  242.             else if (*f < fmin)
  243.                 *f = fmin;
  244.         }
  245.         itmp = (int) ((*f - fmin) * (color_index - 1) / ftmp);
  246.         *s = (unsigned char) itmp;
  247.         if (*s < 1)
  248.             *s = 1;
  249.         else if (*s > color_index - 4)
  250.             *s = color_index - 4;
  251.     }
  252.     return;
  253. }
  254. /* EOF */
  255. /* cat > src+obj/image2/log_compute.c << "EOF" */
  256. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  257. /* log_compute: compute log scaling with threshold    */
  258. /*            filtering.                */
  259. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  260. /* SCCS information: %W%    %G% - NCSA */
  261.  
  262. /* #include "image2.h" */
  263.  
  264. log_compute (fbuf, s, n, fmax, fmin, filter)
  265.     float          *fbuf, fmax, fmin;
  266.     unsigned char  *s;
  267.     int             n, filter;
  268. {
  269.     float           ftmp, lmax, lmin, f;
  270.     int             itmp, i;
  271.  
  272.     lmax = (float) log10 ((double) fmax);
  273.     lmin = (float) log10 ((double) fmin);
  274.     ftmp = lmax - lmin;
  275.     for (i = 0; i < n; i++, fbuf++, s++)
  276.     {
  277.         if (filter)
  278.         {
  279.             if (*fbuf > fmax)
  280.                 *fbuf = fmax;
  281.             else if (*fbuf < fmin)
  282.                 *fbuf = fmin;
  283.         }
  284.         f = (float) log10 ((double) (*fbuf));
  285.         itmp = (int) ((f - lmin) * (color_index - 1) / ftmp);
  286.         *s = (unsigned char) itmp;
  287.         if (*s < 1)
  288.             *s = 1;
  289.         else if (*s > color_index - 4)
  290.             *s = color_index - 4;
  291.     }
  292.     return;
  293. }
  294. /* EOF */
  295. /* cat > src+obj/image2/ok_cancel.c << "EOF" */
  296. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  297. /* ok_cancel: (static) ok to cancel?            */
  298. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  299. /* SCCS information: %W%    %G% - NCSA */
  300.  
  301. /* #include "image2.h" */
  302.  
  303. static int
  304. ok_cancel (item, event)
  305.     Panel_item      item;
  306.     Event          *event;
  307. {
  308.     window_return ((int) panel_get (item, PANEL_CLIENT_DATA));
  309. }
  310. /* EOF */
  311. /* cat > src+obj/image2/para_dialog.c << "EOF" */
  312. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  313. /* para_dialog: scaling dialog box window loop code.    */
  314. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  315. /* SCCS information: %W%    %G% - NCSA */
  316.  
  317. /* #include "image2.h" */
  318.  
  319. para_dialog (pmax, pmin, msg)
  320.     float          *pmax, *pmin;
  321.     char           *msg;
  322. {
  323.     Frame           para_box;
  324.     int             answer;
  325.     char            str[MAXNAMELEN];
  326.  
  327.     flinear = 1;
  328.     para_box = init_parabox (msg);
  329.  
  330.         /* set min and max values */
  331.     sprintf (wkstr, "%e", *pmin);
  332.     panel_set_value (fmin_item, wkstr);
  333.     sprintf (wkstr, "%e", *pmax);
  334.     panel_set_value (fmax_item, wkstr);
  335.  
  336.     answer = (int) window_loop (para_box);
  337.     if (answer)
  338.     {
  339.         strcpy (str, (char *) panel_get_value (fmax_item));
  340.         *pmax = (float) atof (str);
  341.         strcpy (str, (char *) panel_get_value (fmin_item));
  342.         *pmin = (float) atof (str);
  343.     }
  344.  
  345.     window_set (para_box, FRAME_NO_CONFIRM, TRUE, 0);
  346.     window_destroy (para_box);
  347.     return (answer);
  348. }
  349. /* EOF */
  350. /* cat > src+obj/image2/set_scale_type.c << "EOF" */
  351. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  352. /* set_scale_type: (static) set the type of scale    */
  353. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  354. /* SCCS information: %W%    %G% - NCSA */
  355.  
  356. /* #include "image2.h" */
  357.  
  358. static void
  359. set_scale_type (item, value, event)
  360.     Panel_item      item;
  361.     int             value;
  362.     Event          *event;
  363. {
  364.         /* linear = 1, logarithmic = 0 */
  365.     flinear = value;
  366. }
  367. /* EOF */
  368.