No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ImageWidget.cs 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. //
  2. // ImageWidget.cs
  3. //
  4. // Author:
  5. // Di MERCURIO Sébastien <dimercur@insa-toulouse.fr>
  6. //
  7. // Copyright (c) 2019 INSA - DGEI
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU General Public License as published by
  11. // the Free Software Foundation, either version 3 of the License, or
  12. // (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. // 15/10/2019 dimercur
  22. // Demande #43: Migrer le code lié à la gestion des images dans sa propre classe widget
  23. using System;
  24. using Gtk;
  25. using Cairo;
  26. using Gdk;
  27. using System.Timers;
  28. namespace monitor
  29. {
  30. /// <summary>
  31. /// Class used for video display
  32. /// </summary>
  33. public class ImageWidget
  34. {
  35. /// <summary>
  36. /// Reference to GTK base widget
  37. /// </summary>
  38. private DrawingArea drawingArea;
  39. /// <summary>
  40. /// Pixbuffer used for displaying image
  41. /// </summary>
  42. private Pixbuf drawingareaCameraPixbuf;
  43. /// <summary>
  44. /// Indicate if position must be displayed or not
  45. /// </summary>
  46. public bool showPosition;
  47. /// <summary>
  48. /// Indicate if FPS must be displayed or not
  49. /// </summary>
  50. private bool showFPS;
  51. public bool ShowFPS
  52. {
  53. get
  54. {
  55. return showFPS;
  56. }
  57. set
  58. {
  59. showFPS = value;
  60. if (value == true)
  61. {
  62. imageFPS = 0;
  63. imageFPScounter = 0;
  64. fpsTimer.Start();
  65. }
  66. else
  67. {
  68. fpsTimer.Stop();
  69. Refresh();
  70. }
  71. }
  72. }
  73. /// <summary>
  74. /// Counter used for FPS computation
  75. /// </summary>
  76. private int imageFPS = 0;
  77. private int imageFPScounter = 0;
  78. /// <summary>
  79. /// Timer for FPS request
  80. /// </summary>
  81. private System.Timers.Timer fpsTimer;
  82. /// <summary>
  83. /// Hold position to be displayed
  84. /// </summary>
  85. private DestijlCommandManager.Position position;
  86. public DestijlCommandManager.Position Position
  87. {
  88. get
  89. {
  90. return position;
  91. }
  92. set
  93. {
  94. position = value;
  95. Refresh();
  96. }
  97. }
  98. /// <summary>
  99. /// Request a refresh of drawing area
  100. /// </summary>
  101. private void Refresh()
  102. {
  103. Gtk.Application.Invoke(delegate
  104. {
  105. drawingArea.QueueDraw();
  106. });
  107. }
  108. /// <summary>
  109. /// Initializes a new instance of ImageWidget class.
  110. /// </summary>
  111. /// <param name="area">Reference to GTK drawing area widget</param>
  112. public ImageWidget(DrawingArea area)
  113. {
  114. this.drawingArea = area;
  115. // create new timer for FPS , every 1s
  116. fpsTimer = new System.Timers.Timer(1000.0);
  117. fpsTimer.Elapsed += OnFpsTimerElapsed;
  118. showPosition = false;
  119. showFPS = false;
  120. drawingArea.ExposeEvent += OnDrawingAreaCameraExposeEvent;
  121. }
  122. /// <summary>
  123. /// Show an image from a ressource
  124. /// </summary>
  125. /// <param name="ressource">Ressource path</param>
  126. public void ShowImage(string ressource)
  127. {
  128. drawingareaCameraPixbuf = Pixbuf.LoadFromResource("monitor.ressources.missing_picture.png");
  129. imageFPScounter++;
  130. Refresh();
  131. }
  132. /// <summary>
  133. /// Show an image from a byte array
  134. /// </summary>
  135. /// <param name="image">Byte array containing a valid image</param>
  136. public void ShowImage(byte[] image)
  137. {
  138. drawingareaCameraPixbuf = new Pixbuf(image);
  139. imageFPScounter++;
  140. Refresh();
  141. }
  142. /// <summary>
  143. /// Callback called when drawingarea need refresh
  144. /// </summary>
  145. /// <param name="o">Sender object</param>
  146. /// <param name="args">Expose arguments</param>
  147. protected void OnDrawingAreaCameraExposeEvent(object o, ExposeEventArgs args)
  148. {
  149. //Console.WriteLine("Event expose. Args = " + args.ToString());
  150. DrawingArea area = (DrawingArea)o;
  151. Gdk.Pixbuf displayPixbuf;
  152. int areaWidth, areaHeight;
  153. // Get graphic context for background
  154. Gdk.GC gc = area.Style.BackgroundGC(Gtk.StateType.Normal);
  155. // get size of drawingarea widget
  156. area.GdkWindow.GetSize(out areaWidth, out areaHeight);
  157. int width = drawingareaCameraPixbuf.Width;
  158. int height = drawingareaCameraPixbuf.Height;
  159. float ratio = (float)width / (float)height;
  160. // if widget is smaller than image, reduce it
  161. if (areaWidth <= width)
  162. {
  163. width = areaWidth;
  164. height = (int)(width / ratio);
  165. }
  166. // if image is smaller than widget, enlarge it
  167. if (width > areaWidth)
  168. {
  169. width = areaWidth;
  170. }
  171. if (height > areaHeight)
  172. {
  173. height = areaHeight;
  174. }
  175. //scale original picture and copy result in local pixbuf
  176. displayPixbuf = drawingareaCameraPixbuf.ScaleSimple(width, height, InterpType.Bilinear);
  177. // draw local pixbuff centered on drawingarea
  178. area.GdkWindow.DrawPixbuf(gc, displayPixbuf,
  179. 0, 0,
  180. (areaWidth - displayPixbuf.Width) / 2,
  181. (areaHeight - displayPixbuf.Height) / 2,
  182. displayPixbuf.Width, displayPixbuf.Height,
  183. RgbDither.Normal, 0, 0);
  184. if (showPosition)
  185. {
  186. Cairo.Context cr = Gdk.CairoHelper.Create(area.GdkWindow);
  187. Cairo.Color textFontColor = new Cairo.Color(0.8, 0, 0);
  188. cr.SelectFontFace("Cantarell", FontSlant.Normal, FontWeight.Bold);
  189. cr.SetSourceColor(textFontColor);
  190. cr.SetFontSize(16);
  191. if (position != null)
  192. {
  193. double space = 0.0;
  194. string text = "Direction (" + position.direction.x.ToString("0.##") + " ; " + position.direction.y.ToString("0.##") + ")";
  195. TextExtents te = cr.TextExtents(text);
  196. cr.MoveTo(areaWidth - te.Width - 5,
  197. areaHeight - te.Height - 5);
  198. space = te.Height;
  199. cr.ShowText(text);
  200. text = "Centre (" + position.centre.x.ToString("0.##") + " ; " + position.centre.y.ToString("0.##") + ")";
  201. te = cr.TextExtents(text);
  202. cr.MoveTo(areaWidth - te.Width - 5,
  203. areaHeight - te.Height - 5 - space - 5);
  204. space = space + te.Height + 5;
  205. cr.ShowText(text);
  206. text = "Angle: " + position.angle.ToString("0.##");
  207. te = cr.TextExtents(text);
  208. cr.MoveTo(areaWidth - te.Width - 5,
  209. areaHeight - te.Height - 5 - space - 5);
  210. space = space + te.Height + 5;
  211. cr.ShowText(text);
  212. text = "ID: " + position.robotID;
  213. te = cr.TextExtents(text);
  214. cr.MoveTo(areaWidth - te.Width - 5,
  215. areaHeight - te.Height - 5 - space - 5);
  216. cr.ShowText(text);
  217. }
  218. else
  219. {
  220. string text = "Position (NULL)";
  221. TextExtents te = cr.TextExtents(text);
  222. cr.MoveTo(areaWidth - te.Width - 5,
  223. areaHeight - te.Height - 5);
  224. cr.ShowText(text);
  225. }
  226. ((IDisposable)cr.GetTarget()).Dispose();
  227. ((IDisposable)cr).Dispose();
  228. }
  229. if (showFPS)
  230. {
  231. Cairo.Context cr = Gdk.CairoHelper.Create(area.GdkWindow);
  232. Cairo.Color textFontColor = new Cairo.Color(0.8, 0, 0);
  233. cr.SelectFontFace("Cantarell", FontSlant.Normal, FontWeight.Bold);
  234. cr.SetSourceColor(textFontColor);
  235. cr.SetFontSize(16);
  236. string text = "FPS= " + imageFPS.ToString();
  237. TextExtents te = cr.TextExtents(text);
  238. cr.MoveTo(10, 10 + te.Height);
  239. cr.ShowText(text);
  240. ((IDisposable)cr.GetTarget()).Dispose();
  241. ((IDisposable)cr).Dispose();
  242. }
  243. }
  244. /// <summary>
  245. /// Timer used for FPS computation
  246. /// </summary>
  247. /// <param name="sender">Sender object</param>
  248. /// <param name="e">unused parameter</param>
  249. private void OnFpsTimerElapsed(object sender, ElapsedEventArgs e)
  250. {
  251. imageFPS = imageFPScounter;
  252. imageFPScounter = 0;
  253. Refresh();
  254. }
  255. }
  256. }