/vos/gui/sub/gui/lutview/LutGraphView.cc

https://github.com/nasa/VICAR · C++ · 170 lines · 135 code · 31 blank · 4 comment · 15 complexity · 90d6177d7d2c3e481b1ffab7b9b9a886 MD5 · raw file

  1. ///////////////////////////////////////////////////////
  2. // LutGraphView.C:
  3. ////////////////////////////////////////////////////////
  4. #include "LutGraphView.h"
  5. #include "Lut.h"
  6. #include <Xm/Xm.h>
  7. #include <Xm/DrawingA.h>
  8. #include <iostream>
  9. using namespace std;
  10. #include <stdio.h>
  11. // Resources for this class
  12. XtResource LutGraphView::_resources [ ] = {
  13. {
  14. (char *)"redColor",
  15. (char *)"RedColor",
  16. XmRString,
  17. sizeof ( String ),
  18. XtOffset ( LutGraphView *, _red ),
  19. XmRImmediate,
  20. ( XtPointer ) "red",
  21. },
  22. {
  23. (char *)"greenColor",
  24. (char *)"GreenColor",
  25. XmRString,
  26. sizeof ( String ),
  27. XtOffset ( LutGraphView *, _green ),
  28. XmRImmediate,
  29. ( XtPointer ) "green",
  30. },
  31. {
  32. (char *)"blueColor",
  33. (char *)"BlueColor",
  34. XmRString,
  35. sizeof ( String ),
  36. XtOffset ( LutGraphView *, _blue ),
  37. XmRImmediate,
  38. ( XtPointer ) "blue",
  39. },
  40. };
  41. LutGraphView::LutGraphView ( Widget parent, const char *name,
  42. Lut *lut, Lut *lut1, Lut *lut2 )
  43. : LutView (name, lut, lut1, lut2)
  44. {
  45. _w = XtVaCreateWidget ( _name,
  46. xmDrawingAreaWidgetClass, parent,
  47. NULL );
  48. installDestroyHandler ();
  49. getResources ( _resources, XtNumber ( _resources ) );
  50. XtAddCallback ( _w, XmNexposeCallback,
  51. &LutGraphView::displayCallback,
  52. ( XtPointer ) this );
  53. _gc = XtGetGC ( _w, 0, 0 );
  54. if (_lut) _lut->attachView(this);
  55. if (_lut1) _lut1->attachView(this);
  56. if (_lut2) _lut2->attachView(this);
  57. }
  58. LutGraphView::LutGraphView ( Widget parent, const char *name, Lut *lut )
  59. : LutView (name, lut)
  60. {
  61. _w = XtVaCreateWidget ( _name,
  62. xmDrawingAreaWidgetClass, parent,
  63. NULL );
  64. installDestroyHandler ();
  65. getResources ( _resources, XtNumber ( _resources ) );
  66. XtAddCallback ( _w, XmNexposeCallback,
  67. &LutGraphView::displayCallback,
  68. ( XtPointer ) this );
  69. _gc = XtGetGC ( _w, 0, 0 );
  70. if (_lut) _lut->attachView(this);
  71. }
  72. LutGraphView::~LutGraphView()
  73. {
  74. if ( _w && _gc )
  75. XtReleaseGC ( _w, _gc );
  76. }
  77. void LutGraphView::displayCallback ( Widget,
  78. XtPointer client_data,
  79. XtPointer )
  80. {
  81. LutGraphView *obj;
  82. obj = ( LutGraphView * ) client_data;
  83. if (obj != NULL)
  84. obj->update();
  85. }
  86. void LutGraphView::update ( )
  87. {
  88. if ( ! XtIsRealized(_w) ) return;
  89. XSetWindowAttributes attrs;
  90. attrs.bit_gravity = ForgetGravity;
  91. XChangeWindowAttributes ( XtDisplay(_w),
  92. XtWindow(_w), CWBitGravity, &attrs );
  93. Colormap cmap = DefaultColormap ( XtDisplay(_w), DefaultScreen ( XtDisplay(_w) ) );
  94. XColor exact;
  95. XtVaGetValues ( _w, XmNwidth, &_width,
  96. XmNheight, &_height,
  97. NULL );
  98. _height--;
  99. XPoint graph [256];
  100. double horScale = (double)_width / (double)256;
  101. double verScale = (double)_height / (double)256;
  102. XClearWindow ( XtDisplay(_w), XtWindow(_w) ); // Always clear the window before drawing
  103. int x;
  104. if (_lut)
  105. {
  106. for ( x=0; x<256; x++ )
  107. {
  108. graph[x].x = (int) (x * horScale);
  109. graph[x].y = _height - (int) ( (*_lut)[x] * verScale );
  110. }
  111. XAllocNamedColor ( XtDisplay(_w), cmap, "red", &colorR, &exact);
  112. XSetForeground ( XtDisplay(_w), _gc, colorR.pixel );
  113. XDrawLines ( XtDisplay(_w), XtWindow(_w), _gc, graph, 256, CoordModeOrigin );
  114. }
  115. if (_lut1)
  116. {
  117. for ( x=0; x<256; x++ )
  118. {
  119. graph[x].x = (int) (x * horScale);
  120. graph[x].y = _height - (int) ( (*_lut1)[x] * verScale );
  121. }
  122. XAllocNamedColor ( XtDisplay(_w), cmap, "green", &colorG, &exact);
  123. XSetForeground ( XtDisplay(_w), _gc, colorG.pixel );
  124. XDrawLines ( XtDisplay(_w), XtWindow(_w), _gc, graph, 256, CoordModeOrigin );
  125. }
  126. if (_lut2)
  127. {
  128. for ( x=0; x<256; x++ )
  129. {
  130. graph[x].x = (int) (x * horScale);
  131. graph[x].y = _height - (int) ( (*_lut2)[x] * verScale );
  132. }
  133. XAllocNamedColor ( XtDisplay(_w), cmap, "blue", &colorB, &exact);
  134. XSetForeground ( XtDisplay(_w), _gc, colorB.pixel );
  135. XDrawLines ( XtDisplay(_w), XtWindow(_w), _gc, graph, 256, CoordModeOrigin );
  136. }
  137. XSetForeground ( XtDisplay(_w), _gc, WhitePixel( XtDisplay(_w),
  138. DefaultScreen(XtDisplay(_w)) ) );
  139. }