home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / desklib / examples / DeskLib / Examples / Widget5 / !Widget5 / c / GraphWin < prev    next >
Encoding:
Text File  |  1995-05-03  |  9.1 KB  |  357 lines

  1. /************************************************************************
  2.  *                                                                      *
  3.  *        File: GraphWin.c                                              *
  4.  *                                                                      *
  5.  *     Purpose: Draws graphs for greylevel and cumulative histograms    *
  6.  *                                                                      *
  7.  ************************************************************************/
  8.  
  9. #include "DeskLib:WimpSWIs.h"          /* Low-level WIMP commands         */
  10. #include "DeskLib:Window.h"            /* Window handling automation      */
  11. #include "DeskLib:Core.h"              /* usefull core functions          */
  12. #include "DeskLib:Error.h"             /* Error despatcher                */
  13. #include "DeskLib:Event.h"             /* Event despatcher                */
  14. #include "DeskLib:EventMsg.h"          /* Wimp Message event dispatcher   */
  15. #include "DeskLib:File.h"              /* Low level file handling         */
  16. #include "DeskLib:Msgs.h"              /* Message translation code        */
  17. #include "DeskLib:Resource.h"          /* Handles finding resource files  */
  18. #include "DeskLib:Sound.h"             /* Beep!                           */
  19. #include "DeskLib:Template.h"          /* Template loading and caching    */
  20. #include "DeskLib:Icon.h"              /* Icon handling automation        */
  21. #include "DeskLib:Screen.h"            /* Caching screen info             */
  22. #include "DeskLib:Sprite.h"            /* Sprite handling routines        */
  23. #include "DeskLib:File.h"              /* OS file IO                      */
  24. #include "DeskLib:KeyCodes.h"          /* Codes for wimp returned keys    */
  25. #include "DeskLib:Hourglass.h"         /* control hourglass               */
  26.  
  27. #include "SpriteWin.h"
  28. #include "Process.h"
  29. #include "Configure.h"
  30.  
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34.  
  35. extern window_handle db;
  36.  
  37. /* definitions for 'graph' */
  38. #define graph_GREYHISTOGRAM 0
  39. #define graph_CUMUHISTOGRAM 1
  40.  
  41. /* icon defs */
  42. #define icon_MAXY       4
  43. #define icon_LABELX     10
  44. #define icon_BAR        36
  45. #define icon_FREQMIN    27
  46. #define icon_FREQMAX    35
  47. #define icon_CUMUPIXMIN 11
  48. #define icon_CUMUPIXMAX 26
  49.  
  50. /* icon defs for small graph window */
  51. #define icon_SMAXY       4
  52. #define icon_SLABELX     10
  53. #define icon_SBAR        26
  54. #define icon_SFREQMIN    17
  55. #define icon_SFREQMAX    25
  56. #define icon_SCUMUPIXMIN 11
  57. #define icon_SCUMUPIXMAX 16
  58.  
  59. void open_greyhistogram(int sourceimage);
  60. void open_cumulativehistogram(int sourceimage);
  61. void draw_graph(window_handle window, int (*histogram)[256]);
  62. static BOOL Close_window(event_pollblock *event, void *reference);
  63.  
  64. /******************************************************************************
  65.  *                            extern routines                                 *
  66.  ******************************************************************************/
  67.  
  68. extern void GraphWin_Init(void)
  69. {
  70.  /*
  71.   * initialisation to be done on startup
  72.   */
  73.  
  74. }
  75.  
  76. /******************************************************************************/
  77.  
  78. extern void GraphWin_Start(int graph, int sourceimage)
  79. {
  80.  
  81.   switch(graph)
  82.   {
  83.     case graph_GREYHISTOGRAM:
  84.       open_greyhistogram(sourceimage);
  85.       break;
  86.  
  87.     case graph_CUMUHISTOGRAM:
  88.       open_cumulativehistogram(sourceimage);
  89.       break;
  90.  
  91.     default:
  92.       Error_Report(1, "graph %i   image %i", graph, sourceimage);
  93.       break;
  94.   }
  95.  
  96. }
  97.  
  98. /******************************************************************************
  99.  *                                open handlers                               *
  100.  ******************************************************************************/
  101.  
  102. void open_greyhistogram(int sourceimage)
  103. {
  104.   window_handle window;
  105.   int histogram[256];
  106.   wimp_point *size;
  107.   char number[5];
  108.   char title[50];
  109.   icon_handle icon;
  110.  
  111.  /*
  112.   * get size of image
  113.   */
  114.   size = SpriteWin_GetSize(sourceimage);
  115.  
  116.  /*
  117.   * calculate greylevel histogram
  118.   */
  119.   Process_GreyHistorgram(sourceimage, size, &histogram);
  120.  
  121.  /*
  122.   * create graph window
  123.   */
  124.   switch(graphtype)
  125.   {
  126.     case graphtype_LARGE:
  127.        window = Window_Create("Graph", template_TITLEMIN);
  128.        break;
  129.  
  130.     case graphtype_SMALL:
  131.        window = Window_Create("SmallGraph", template_TITLEMIN);
  132.        break;
  133.   }
  134.  
  135.  /*
  136.   * remove y label that is not needed
  137.   */
  138.   switch(graphtype)
  139.   {
  140.     case graphtype_LARGE:
  141.        for(icon = icon_CUMUPIXMIN; icon <= icon_CUMUPIXMAX; icon++)
  142.            Wimp_DeleteIcon(window, icon);
  143.        break;
  144.  
  145.     case graphtype_SMALL:
  146.        for(icon = icon_SCUMUPIXMIN; icon <= icon_SCUMUPIXMAX; icon++)
  147.            Wimp_DeleteIcon(window, icon);
  148.        break;
  149.   }
  150.  
  151.  /*
  152.   * set text for window title
  153.   */
  154.   Msgs_Lookup("txt.hisgra", title, 49);
  155.   sprintf(number, " %i", sourceimage);
  156.   strcat(title, number);
  157.   Window_SetTitle(window, title);
  158.  
  159.  /*
  160.   * draw bars on graph
  161.   */
  162.   draw_graph(window, &histogram);
  163.  
  164.  /*
  165.   * install event handlers
  166.   */
  167.   Event_Claim(event_CLOSE, window, event_ANY, Close_window, (void *)window);
  168.  
  169.  /*
  170.   * display window
  171.   */
  172.   Window_Show(window, open_WHEREVER);
  173.  
  174. }
  175.  
  176. /******************************************************************************/
  177.  
  178. void open_cumulativehistogram(int sourceimage)
  179. {
  180.   window_handle window;
  181.   int greyhist[256];
  182.   int cuhist[256];
  183.   wimp_point *size;
  184.   char number[5];
  185.   char title[50];
  186.   icon_handle icon;
  187.  
  188.  /*
  189.   * get size of image
  190.   */
  191.   size = SpriteWin_GetSize(sourceimage);
  192.  
  193.  /*
  194.   * calculate greylevel histogram
  195.   */
  196.   Process_GreyHistorgram(sourceimage, size, &greyhist);
  197.  
  198.  /*
  199.   * calculate cumulative grey level histogram
  200.   */
  201.   Process_CumulativeHistorgram(&greyhist, &cuhist);
  202.  
  203.  /*
  204.   * create graph window
  205.   */
  206.   switch(graphtype)
  207.   {
  208.     case graphtype_LARGE:
  209.        window = Window_Create("Graph", template_TITLEMIN);
  210.        break;
  211.  
  212.     case graphtype_SMALL:
  213.        window = Window_Create("SmallGraph", template_TITLEMIN);
  214.        break;
  215.   }
  216.  
  217.  /*
  218.   * remove y label that is not needed
  219.   */
  220.   switch(graphtype)
  221.   {
  222.     case graphtype_LARGE:
  223.        for(icon = icon_FREQMIN; icon <= icon_FREQMAX; icon++)
  224.            Wimp_DeleteIcon(window, icon);
  225.        break;
  226.  
  227.     case graphtype_SMALL:
  228.        for(icon = icon_SFREQMIN; icon <= icon_SFREQMAX; icon++)
  229.            Wimp_DeleteIcon(window, icon);
  230.        break;
  231.   }
  232.  
  233.  /*
  234.   * set text for window title
  235.   */
  236.   Msgs_Lookup("txt.cugra", title, 49);
  237.   sprintf(number, " %i", sourceimage);
  238.   strcat(title, number);
  239.   Window_SetTitle(window, title);
  240.  
  241.  /*
  242.   * draw bars on graph
  243.   */
  244.   draw_graph(window, &cuhist);
  245.  
  246.  /*
  247.   * install event handlers
  248.   */
  249.   Event_Claim(event_CLOSE, window, event_ANY, Close_window, (void *)window);
  250.  
  251.  /*
  252.   * display window
  253.   */
  254.   Window_Show(window, open_WHEREVER);
  255.  
  256. }
  257.  
  258. /******************************************************************************
  259.  *                               close handlers                               *
  260.  ******************************************************************************/
  261.  
  262. static BOOL Close_window(event_pollblock *event, void *reference)
  263. {
  264.   window_handle window = (window_handle) reference;
  265.  
  266.   Window_Delete(window);
  267.  
  268.   return(TRUE);
  269. }
  270.  
  271. /******************************************************************************
  272.  *                             misc routines                                  *
  273.  ******************************************************************************/
  274.  
  275. void draw_graph(window_handle window, int (*histogram)[256])
  276. {
  277.  /*
  278.   * draws graph on window of histogram in array 'histogram'
  279.   */
  280.   icon_createblock icreate;
  281.   icon_handle icon;
  282.   int loop;
  283.   int maxheight;
  284.   float heightscale;
  285.   int barheight;
  286.  
  287.   icon_handle baricon;
  288.   icon_handle ylabel;
  289.   int horizbarsep;
  290.  
  291.   switch(graphtype)
  292.   {
  293.     case graphtype_LARGE:
  294.        baricon = icon_BAR;
  295.        ylabel = icon_MAXY;
  296.        horizbarsep = 4;
  297.        break;
  298.  
  299.     case graphtype_SMALL:
  300.        baricon = icon_SBAR;
  301.        ylabel = icon_SMAXY;
  302.        horizbarsep = 2;
  303.        break;
  304.   }
  305.  
  306.  /*
  307.   * setup icon block for first bar and remove guide bar from window
  308.   */
  309.   icreate.window = window;
  310.   Wimp_GetIconState(window, baricon, &icreate.icondata);
  311.   icreate.icondata.workarearect.min.x = icreate.icondata.workarearect.max.x - 2;
  312.   Wimp_DeleteIcon(window, baricon);
  313.  
  314.  /*
  315.   * find maximum height for bars
  316.   */
  317.   maxheight = 0;
  318.   for(loop = 0; loop < 256; loop++)
  319.       if( (*histogram)[loop] > maxheight )
  320.          maxheight = (*histogram)[loop];
  321.  
  322.  /*
  323.   * set max y label
  324.   */
  325.   Icon_SetInteger(window, ylabel, maxheight);
  326.  
  327.  /*
  328.   * calculate scaling factor for height of bars
  329.   */
  330.   heightscale = ( (float) icreate.icondata.workarearect.max.y -
  331.                   (float) icreate.icondata.workarearect.min.y) / (float) maxheight;
  332.  /*
  333.   * add bars for histogram
  334.   */
  335.   for(loop = 0; loop < 256; loop++){
  336.  
  337.      barheight = (int) ( (float) (*histogram)[loop] * heightscale );
  338.  
  339.      if(barheight > 0){ /* no bar if zero */
  340.  
  341.         if(barheight < 4) /* min size of bar */
  342.            barheight = 4;
  343.  
  344.         icreate.icondata.workarearect.max.y = icreate.icondata.workarearect.min.y +
  345.                                               barheight;
  346.          
  347.         Wimp_CreateIcon(&icreate, &icon);
  348.      }
  349.  
  350.      icreate.icondata.workarearect.min.x += horizbarsep;
  351.      icreate.icondata.workarearect.max.x += horizbarsep;
  352.  
  353.   }
  354.  
  355. }
  356.  
  357.