home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Graphics / Graphics.zip / xfitsvew.zip / XFITSview / blinkbox.c < prev    next >
C/C++ Source or Header  |  1998-04-02  |  12KB  |  393 lines

  1. /* image blink control box  for XFITSview */
  2. /*-----------------------------------------------------------------------
  3. *  Copyright (C) 1996
  4. *  Associated Universities, Inc. Washington DC, USA.
  5. *  This program is free software; you can redistribute it and/or
  6. *  modify it under the terms of the GNU General Public License as
  7. *  published by the Free Software Foundation; either version 2 of
  8. *  the License, or (at your option) any later version.
  9. *
  10. *  This program is distributed in the hope that it will be useful,
  11. *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. *  GNU General Public License for more details.
  14. *-----------------------------------------------------------------------*/
  15. #include <Xm/Xm.h> 
  16. #include <Xm/DialogS.h> 
  17. #include <Xm/MainW.h> 
  18. #include <Xm/Scale.h>
  19. #include <Xm/Form.h>
  20. #include <Xm/PushB.h>
  21. #include <Xm/Label.h>
  22. #include <Xm/MessageB.h>
  23. #include <Xm/TextF.h>
  24. #include <Xm/Text.h>
  25. #include <time.h>
  26. #include <math.h>
  27. #include "imagedisp.h"
  28. #include "color.h"
  29.  
  30. /* is the blink box active? */
  31. int BlinkBoxActive = 0;
  32.  
  33. /* global structures for things to talk to each other */
  34. typedef struct {
  35.   ImageDisplay *BoxData;   /* image display structure pointer */
  36.   int    oldImag;          /* CurImag at start */
  37.   int    Stop;             /* true if blinking terminated */
  38.   float  Dwell;            /* dwell time on each image */
  39.   Widget dialog;           /* box, start, end planes */
  40.   Widget DwellScroll;      /* Dwell time scroll */
  41.   Widget DwellScrollLabel; /* label for scroll */
  42.   Widget QuitButton;       /* quit button window */
  43. } BlinkBoxStuff;
  44.  
  45. typedef struct { /* define DisplayInfo structure */
  46.   short          value[2];  /* Brightness and contrast scroll bar values */
  47.   unsigned short red[MAXCOLOR], green[MAXCOLOR], blue[MAXCOLOR]; /* colors */
  48.   int            zoom;      /* zoom factor, neg = zoom out */
  49.   int            scrollx;   /* center "x" pixel in display */
  50.   int            scrolly;   /* center "y" pixel in display */
  51. }  BlinkDisplayInfo;
  52.  
  53. /* file global variables */
  54. BlinkBoxStuff BlinkDia;
  55. BlinkDisplayInfo info[2];
  56. int validInfo[2]={0,0};
  57.  
  58. /* internal functions */
  59. void SaveBlinkInfo(ImageDisplay *IDdata);
  60. void RestoreBlinkInfo(ImageDisplay *IDdata);
  61. void AdjustBlinkInfo(void);
  62. int BlinkAreAligned (void);
  63.  
  64.  
  65. Boolean BlinkCheckQuit()
  66. /* searches event queue for click of Quit button */
  67. /* returns true if found */
  68. {
  69.   XButtonPressedEvent QuitEvent;
  70.   Boolean ret=0;
  71.  
  72.   if (XCheckTypedEvent(XtDisplay(BlinkDia.dialog), ButtonPress, 
  73.                (XEvent*)&QuitEvent))
  74.     {
  75.       if (QuitEvent.window==XtWindow(BlinkDia.QuitButton)) { /* found it */
  76.           BlinkDia.Stop = 1; ret=1;}
  77.       else /* it's not yours - put it back */
  78.           {XPutBackEvent (XtDisplay(BlinkDia.dialog), 
  79.             (XEvent*)&QuitEvent); return 0;}
  80.     }
  81.   return ret;
  82. } /* end BlinkCheckQuit */
  83.  
  84. void BlinkLabelDwell(void)
  85. {
  86. /* tell dwell time */
  87.   char      string[30];
  88.   XmString  WierdString = NULL;
  89.  
  90.    /* value label */
  91.   sprintf (string,"    Dwell Time %4.1f sec.", BlinkDia.Dwell);
  92.   WierdString = XmStringCreateSimple (string);
  93.   XtVaSetValues(BlinkDia.DwellScrollLabel, 
  94.         XmNlabelString,   WierdString,
  95.         NULL);
  96.   if (WierdString) XmStringFree(WierdString);
  97. } /* end BlinkLabelDwell */
  98.  
  99. void BlinkShutDown (void)
  100. /* shutdown blink operation and restore to previous condition */
  101. {
  102. /* restore original condition */
  103.   CurImag = BlinkDia.oldImag; /* reset original image */
  104.   RestoreBlinkInfo(BlinkDia.BoxData); /* reset display */
  105. /* show current image */
  106.   ResetDisplay(BlinkDia.BoxData);
  107. /* reset color table */
  108.   SetColorTable (BlinkDia.BoxData);
  109.  
  110. /* I could just die */
  111.   BlinkBoxActive = 0; /* mark as inactive */
  112.   XtDestroyWidget (BlinkDia.dialog); /* your wish is granted */
  113.   } /* end BlinkShutDown */
  114.  
  115.  
  116. void BlinkScrollDwellCB (Widget w, XtPointer clientData, XtPointer callData)
  117. {
  118. /* scroll bar used to select plane */
  119.     ImageDisplay *IDdata = (ImageDisplay *)clientData;
  120.     XmScaleCallbackStruct *call_data = (XmScaleCallbackStruct *) callData;
  121.  
  122. /* read value of scrollbar */
  123.     BlinkDia.Dwell = call_data->value * 0.1; /* 10th of sec */
  124. /* update label */
  125.     BlinkLabelDwell();
  126. } /* end BlinkScrollPlaneCB */
  127.  
  128. void BlinkPlay (Widget w, XtPointer clientData, XtPointer callData)
  129. /* Display next image and set next call to itself until quit hit */
  130. {
  131.   unsigned long interval;
  132.  
  133.  /* Done? */
  134.   if (BlinkDia.Stop || BlinkCheckQuit()) /* quit button? */
  135.     {BlinkShutDown();
  136.      return;}
  137.  
  138. /* toggle image */
  139.   if (CurImag) CurImag = 0;
  140.   else CurImag = 1;
  141.  
  142. /* start timer */
  143.   interval = 1000.0 * BlinkDia.Dwell;
  144.   XtAppAddTimeOut(BlinkDia.BoxData->app, interval, 
  145.           (XtTimerCallbackProc)BlinkPlay, NULL);
  146.  
  147.   BlinkDia.BoxData->showInfo = 0; /* turn off display of pixel information */
  148. /* show other image */
  149.   ResetDisplay(BlinkDia.BoxData);
  150. /* reset display info */
  151.   RestoreBlinkInfo(BlinkDia.BoxData); 
  152. /* sync events */
  153.   XSync (XtDisplay(BlinkDia.dialog), False);
  154.  
  155. /* reset color table */
  156.   SetColorTable (BlinkDia.BoxData);
  157. } /* end BlinkPlay */
  158.  
  159. void BlinkQuitButCB (Widget w, XtPointer clientData, XtPointer callData)
  160. /* Quit button hit */
  161. {
  162. /* just mark as stopped and BlinkPlay will shut down */
  163.   BlinkDia.Stop = 1;
  164.  
  165. } /* end BlinkQuitButCB */
  166.  
  167. void BlinkSwapCB (Widget parent, XtPointer clientData, XtPointer callData)
  168. /* swap current and "Blink" store images */
  169. /* client data is ImageDisplay pointer */
  170. {
  171.   ImageDisplay *IDdata = (ImageDisplay*)clientData;
  172.  
  173.   if (!IDdata) return; /* validity check */
  174.    
  175. /* register IDdata */
  176.   BlinkDia.BoxData = IDdata;
  177.  
  178.   IDdata->showInfo = 0; /* turn off display of pixel information */
  179.   SaveBlinkInfo(IDdata); /* save display info */
  180.   if (CurImag==0) CurImag = 1; /* swap images */
  181.   else CurImag = 0;
  182.   if (validInfo[CurImag]) RestoreBlinkInfo(IDdata);
  183.   else /* default setup */
  184.     {
  185.       IDdata->zoom = 1;
  186.       IDdata->scrollx = 0;
  187.       IDdata->scrolly = 0;
  188.       IDdata->iXCorn = 0;
  189.       IDdata->iYCorn = 0;
  190.       IDdata->value[0] = 128;
  191.       IDdata->value[1] = 128;
  192.       InitColorTable(IDdata);
  193.     }
  194. /* show other image */
  195.   ResetDisplay(IDdata);
  196. /* reset color table */
  197.   SetColorTable (IDdata);
  198. } /* end BlinkSwapCB */
  199.  
  200. void BlinkBlinkCB (Widget parent, XtPointer clientData, XtPointer callData)
  201. /* create dialog box for "Blink" display of planes */
  202. /* client data is ImageDisplay pointer */
  203. {
  204.   Widget       form;
  205.   XmString     WierdString = NULL;
  206.   char         valuestr[30];
  207.   ImageDisplay *IDdata = (ImageDisplay*)clientData;
  208.   int          start;
  209.   short        xpos, ypos;
  210.  
  211.  
  212. /* register IDdata */
  213.   BlinkDia.BoxData = IDdata;
  214.  
  215.   /* don't make another one */
  216.   if (BlinkBoxActive) {
  217.     if (XtIsRealized (BlinkDia.dialog))
  218.     XMapRaised (XtDisplay(IDdata->shell), XtWindow(BlinkDia.dialog));
  219.     return;
  220.   }
  221.  
  222. /* mark as active */
  223.   BlinkBoxActive = 1;
  224.  
  225. /* other initialization */
  226.   BlinkDia.Dwell = 2.0;
  227.   BlinkDia.Stop = 0;
  228.  
  229. /* save display states */
  230.   SaveBlinkInfo(IDdata); /* save display info */
  231.   AdjustBlinkInfo(); /* adjust zoom scroll if they are the same on the sky */
  232.   BlinkDia.oldImag = CurImag;
  233.  
  234.   BlinkDia.dialog = XtVaCreatePopupShell ("BlinkBox",
  235.                  xmDialogShellWidgetClass, 
  236.                  IDdata->shell, 
  237.                  XmNautoUnmanage, False,
  238.                  XmNwidth,     200,
  239.                  XmNheight,     60,
  240.                  XmNdeleteResponse, XmDESTROY,
  241.                  NULL);
  242.  
  243. /* make Form widget to stick things on */
  244.   form = XtVaCreateManagedWidget ("BlinkForm", xmFormWidgetClass,
  245.                   BlinkDia.dialog,
  246.                   XmNautoUnmanage, False,
  247.                   XmNwidth,     200,
  248.                   XmNheight,    60,
  249.                   XmNx,           0,
  250.                   XmNy,           0,
  251.                   NULL);
  252.  
  253. /* plane scroll */
  254.   BlinkDia.DwellScroll = XtVaCreateManagedWidget ("BlinkDwellScroll", 
  255.                                     xmScaleWidgetClass, 
  256.                     form,
  257.                     XmNwidth,            200,
  258.                     XmNheight,           15,
  259.                     XmNmaximum,          100,
  260.                     XmNminimum,           10,
  261.                     XmNvalue,             20,
  262.                     XmNshowValue,       True,
  263.                     XmNorientation, XmHORIZONTAL,
  264.                     XmNprocessingDirection, XmMAX_ON_RIGHT,
  265.                     XmNleftAttachment,  XmATTACH_FORM,
  266.                     XmNtopAttachment, XmATTACH_FORM,
  267.                     NULL);
  268.   
  269.   XtAddCallback(BlinkDia.DwellScroll, XmNvalueChangedCallback, 
  270.                 BlinkScrollDwellCB, (XtPointer)IDdata);
  271.  
  272. /* scroll label */
  273.   sprintf (valuestr, "dummy string");
  274.   WierdString = XmStringCreateSimple (valuestr);
  275.   BlinkDia.DwellScrollLabel = XtVaCreateManagedWidget ("BlinkScrollLabel", 
  276.                                     xmLabelWidgetClass, 
  277.                     form, 
  278.                     XmNwidth,           200,
  279.                     XmNtopAttachment, XmATTACH_WIDGET,
  280.                     XmNtopWidget,     BlinkDia.DwellScroll,
  281.                     XmNleftAttachment,  XmATTACH_FORM,
  282.                     NULL);
  283.   if (WierdString) XmStringFree(WierdString);
  284.   BlinkLabelDwell();
  285.  
  286. /* Quit button */
  287.   BlinkDia.QuitButton = 
  288.     XtVaCreateManagedWidget ("Quit", xmPushButtonWidgetClass, 
  289.                  form, 
  290.                  XmNbottomAttachment, XmATTACH_FORM,
  291.                  XmNleftAttachment,  XmATTACH_FORM,
  292.                  XmNrightAttachment, XmATTACH_FORM,
  293.                  NULL);
  294.   XtAddCallback (BlinkDia.QuitButton, XmNactivateCallback, BlinkQuitButCB, 
  295.          (XtPointer)IDdata);
  296.  
  297. /* put it some place reasonable */
  298. /*  where is parent? */
  299.      XtVaGetValues (IDdata->shell,
  300.             XmNx, &xpos,
  301.             XmNy, &ypos,
  302.             NULL);
  303.   xpos -= 50;
  304.   if (xpos<0) xpos = 0;
  305.   ypos += 160;
  306.   XMoveWindow (XtDisplay(IDdata->shell), XtWindow(BlinkDia.dialog),
  307.            xpos, ypos);
  308.  
  309. /* set it up */
  310.   XtManageChild (BlinkDia.dialog);
  311.  
  312. /* start it going */
  313.   BlinkPlay (BlinkDia.dialog, clientData, callData);
  314. } /* end BlinkBox */
  315.  
  316. void SaveBlinkInfo(ImageDisplay *IDdata)
  317. /* save image display info */
  318. {
  319. int i;
  320.    if (!IDdata) return; /* validity check */
  321.    
  322.    validInfo[CurImag] = 1;
  323.    info[CurImag].value[0] = IDdata->value[0];
  324.    info[CurImag].value[1] = IDdata->value[1];
  325.    if (IDdata->zoom==0) IDdata->zoom = 1;
  326.    info[CurImag].zoom = IDdata->zoom;
  327.    info[CurImag].scrollx = IDdata->scrollx;
  328.    info[CurImag].scrolly = IDdata->scrolly;
  329.    for (i=0;i<MAXCOLOR;i++) {
  330.       info[CurImag].red[i] = IDdata->red[i];
  331.       info[CurImag].blue[i] = IDdata->blue[i];
  332.       info[CurImag].green[i] = IDdata->green[i];}
  333. } /* end SaveBlinkInfo */
  334.  
  335. void RestoreBlinkInfo(ImageDisplay *IDdata)
  336. /* restore image display info */
  337. {
  338. int i;
  339.    if (!IDdata) return; /* validity check */
  340.    
  341.    IDdata->value[0] = info[CurImag].value[0];
  342.    IDdata->value[1] = info[CurImag].value[1];
  343.    if (info[CurImag].zoom==0) info[CurImag].zoom = 1;
  344.    IDdata->zoom = info[CurImag].zoom;
  345.    IDdata->scrollx = info[CurImag].scrollx;
  346.    IDdata->scrolly = info[CurImag].scrolly;
  347.    for (i=0;i<MAXCOLOR;i++) {
  348.       IDdata->red[i] = info[CurImag].red[i];
  349.       IDdata->blue[i] = info[CurImag].blue[i];
  350.       IDdata->green[i] = info[CurImag].green[i];}
  351. } /* end RestoreBlinkInfo */
  352.  
  353. void AdjustBlinkInfo(void)
  354. /* set zoom and scroll if the two blink images are the same part of the sky */
  355. { int other;
  356.  
  357.   if (!BlinkAreAligned()) return;
  358.   if (CurImag) other = 0;
  359.   else other = 1;
  360.   info[other].zoom = info[CurImag].zoom;
  361.   info[other].scrollx = info[CurImag].scrollx;
  362.   info[other].scrolly = info[CurImag].scrolly;
  363. }  /* end AdjustBlinkInfo */
  364.  
  365. int BlinkAreAligned (void)
  366. /* routine to determine if the two blink images have coincident pixels  */
  367. /* return 1 if so else 0  */
  368. {  int same, i;
  369.    double arg1, arg2;
  370.  
  371. /* this is easy if they are the same file.  */
  372.    same = !strcmp(image[0].FileName->sp, image[1].FileName->sp);
  373.    if (same) return 1;
  374.    if (image[0].dim[0]!=image[1].dim[0]) return 0;
  375.    if (image[0].dim[1]!=image[1].dim[1]) return 0;
  376.    arg1 = (image[0].crpix[0]-image[1].crpix[0]);
  377.    arg2 = (image[1].crpix[0]);
  378.    if (fabs(arg1) > 0.1 * fabs(arg2)) return 0;
  379.    arg1 = (image[0].crpix[1]-image[1].crpix[1]);
  380.    arg2 = (image[1].crpix[1]);
  381.    if (fabs(arg1) > 0.1 * fabs(arg2)) return 0;
  382.    if (fabs(image[0].crval[0]-image[1].crval[0]) >
  383.       0.1 * fabs(image[1].crpix[0])) return 0;
  384.    if (fabs(image[0].crval[1]-image[1].crval[1]) >
  385.       0.1 * fabs(image[1].crpix[1])) return 0;
  386.    arg1 = (image[0].crot[1]-image[1].crot[1]);
  387.    if (fabs(arg1) > 1.0) return 0;
  388.    if (!StringComp(image[0].cname[0],image[1].cname[0])) return 0;
  389.    if (!StringComp(image[0].cname[1],image[1].cname[1])) return 0;
  390.    return 1;  /* Seems OK  */
  391. } /* end BlinkAreAligned  */
  392.  
  393.