ImageDisplay.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * NICE-Core - efficient algebra and computer vision methods
  3. * - libimagedisplay - A library for image and video display
  4. * See file License for license information.
  5. */
  6. #ifndef _IMAGEDISPLAY_IMAGEDISPLAY_H
  7. #define _IMAGEDISPLAY_IMAGEDISPLAY_H
  8. #include <qgl.h>
  9. #include <qevent.h>
  10. #include <q3popupmenu.h>
  11. //Added by qt3to4:
  12. #include <QMouseEvent>
  13. #include <QContextMenuEvent>
  14. #include <QGLWidget>
  15. #include <vector>
  16. #include <core/image/ColorImageT.h>
  17. #include <core/image/ImageT.h>
  18. #include <core/image/ImageFileListWriter.h>
  19. #include <core/basics/NonCopyable.h>
  20. #include <core/basics/FrameRateCounter.h>
  21. #include <core/imagedisplay/CaptureDialog.h>
  22. #include <memory>
  23. namespace NICE {
  24. /**
  25. * ImageDisplay is a simple image display widget.
  26. * There is also support for selecting a rectangle in the image using the mouse,
  27. * which also allows handling single clicks (see \c rectSelect()).
  28. * Please not that you can use Qt widgets (and thus \c ImageDisplay)
  29. * as part of another window, but also as a window itself.
  30. *
  31. * @author Ferid Bajramovic (ferid [dot] bajramovic [at] informatik [dot] uni-jena [dot] de)
  32. * (based on DispImgGL by ???)
  33. *
  34. * @note
  35. * This class is experimental and might change in the future.
  36. */
  37. class ImageDisplay : public QGLWidget, private NICE::NonCopyable {
  38. Q_OBJECT;
  39. public:
  40. class Text {
  41. public:
  42. Text(const int _x, const int _y, const std::string& _text,
  43. const bool _visible, const Color& _color)
  44. : x(_x), y(_y), text(_text), visible(_visible), color(_color) {
  45. }
  46. int x,y;
  47. std::string text;
  48. bool visible;
  49. Color color;
  50. };
  51. //! Default constructor
  52. ImageDisplay(QWidget* parent = NULL, const char* name = NULL,
  53. Qt::WFlags flags = 0);
  54. //! Destructor of ImageDisplay
  55. virtual ~ImageDisplay();
  56. /**
  57. * Set the image to be displayed in this window.
  58. * @param image The image to be displayed.
  59. * @param copy Create a copy of the image for the display?
  60. * If copy == false, the image must have the memory layout
  61. * \c AbstractImage::noAlignment. Otherwise the image will not
  62. * be displayed correctly. (Note that more precisely the image
  63. * must fullfill stepsize == width * 3 which is garanteed for
  64. * \c AbstractImage::noAlignment, but may be fullfilled in other
  65. * cases as well).
  66. */
  67. void setImage(const NICE::ColorImage* image, const bool copy = true);
  68. /**
  69. * Set the image to be displayed in this window.
  70. * @param image The image to be displayed.
  71. * @param copy Create a copy of the image for the display?
  72. * If copy == false, the image must have the memory layout
  73. * \c AbstractImage::noAlignment. Otherwise the image will not
  74. * be displayed correctly. (Note that more precisely the image
  75. * must fullfill stepsize == width which is garanteed for
  76. * \c AbstractImage::noAlignment, but may be fullfilled in other
  77. * cases as well).
  78. *
  79. */
  80. void setImage(const NICE::Image* image, const bool copy = true);
  81. inline unsigned int imageWidth ( void )
  82. {
  83. if ( image != NULL )
  84. {
  85. return image->width();
  86. }
  87. return 0;
  88. }
  89. inline unsigned int imageHeight ( void )
  90. {
  91. if ( image != NULL )
  92. {
  93. return image->height();
  94. }
  95. return 0;
  96. }
  97. /**
  98. * Set the overlay image to be displayed in this window.
  99. * @param image The overlay image to be displayed.
  100. */
  101. void setOverlayImage(const NICE::Image* overlayImage );
  102. /**
  103. * Draw the selection rectangle?
  104. * Typically this is turned on then connecting to the \c rectSelect signal.
  105. * @param draw The new setting
  106. */
  107. inline void setDrawSelectionRect(bool draw) {
  108. drawSelectionRect = draw;
  109. }
  110. /**
  111. * Draw the selection rectangle?
  112. * @return The current setting
  113. */
  114. inline bool isDrawSelectionRect() const {
  115. return drawSelectionRect;
  116. }
  117. virtual void setCaption(const QString& s);
  118. /**
  119. * Add a line of text to be display on the image.
  120. * @param x x-coordinate
  121. * @param y y-coordinate
  122. * @param text the text
  123. * @return id needed to change/remove the text
  124. */
  125. inline int addText(int x, int y, const std::string& text) {
  126. return addText(x, y, text, Color(255, 255, 255));
  127. }
  128. /**
  129. * Add a line of text to be display on the image.
  130. * @param x x-coordinate
  131. * @param y y-coordinate
  132. * @param text the text
  133. * @param color the color of the text
  134. * @return id needed to change/remove the text
  135. */
  136. int addText(int x, int y, const std::string& text, const Color& color);
  137. /**
  138. * Hide a line of text.
  139. */
  140. inline void hideText(int id) {
  141. texts[id].visible = false;
  142. }
  143. /**
  144. * Show a line of text, which has been hidden before.
  145. */
  146. inline void showText(int id) {
  147. texts[id].visible = true;
  148. }
  149. /**
  150. * Remove a line of text.
  151. */
  152. inline Text& text(int id) {
  153. return texts[id];
  154. }
  155. inline void removeAllText() {
  156. texts.clear();
  157. }
  158. /**
  159. * The total number of text lines.
  160. */
  161. inline uint numberOfTextLines() const {
  162. return texts.size();
  163. }
  164. signals:
  165. /**
  166. * A rectangle (\c left, \c top, \c right, \c bottom) has been drawn
  167. * by the user.
  168. * @note A single click results in a degenerate rectangle.
  169. * Thus, single clicks can also be handled:
  170. * use point (\c left, \c top).
  171. */
  172. void rectSelect(float left, float top, float right, float bottom);
  173. public slots:
  174. /**
  175. * Repaint this widget.
  176. */
  177. void repaint();
  178. /**
  179. * Save menu item clicked.
  180. */
  181. void menuSave();
  182. /**
  183. * Start video capture menu item clicked.
  184. */
  185. void menuStartCapture();
  186. /**
  187. * Stop video capture menu item clicked.
  188. */
  189. void menuStopCapture();
  190. /**
  191. * Start button clicked in video capture dialog.
  192. */
  193. void dialogStartCapture();
  194. /**
  195. * Stop button clicked in video capture dialog.
  196. */
  197. void dialogStopCapture();
  198. /**
  199. * Aspect ratio menu item clicked.
  200. */
  201. void menuAspectRatio();
  202. protected:
  203. /**
  204. * Implements QGLWidget::paintGL()
  205. */
  206. virtual void paintGL();
  207. /**
  208. * called by paintGL(), use this to change projection mode
  209. */
  210. virtual void setGLProjection(void);
  211. /**
  212. * called by paintGL(), use this to include additional openGL code
  213. */
  214. virtual void paintGLObjects(void);
  215. /**
  216. * called by contextMenuEvent(), use this to include additional
  217. * menu items
  218. */
  219. virtual void addExtraMenuItems ( Q3PopupMenu *popupMenu );
  220. //! receive mouse events
  221. virtual void mousePressEvent(QMouseEvent* event);
  222. //! receive mouse events
  223. virtual void mouseReleaseEvent(QMouseEvent* event);
  224. //! receive mouse events
  225. virtual void mouseMoveEvent(QMouseEvent* event);
  226. private:
  227. //! capture control dialog
  228. std::auto_ptr<CaptureDialog> dialog;
  229. protected:
  230. //! a pointer to the current image
  231. const NICE::GrayColorImageCommonImplementationT<Ipp8u>* image;
  232. //! color or gray?
  233. bool isColor;
  234. //! buffer for copied a \c ColorImage
  235. NICE::ColorImage* colorImageBuffer;
  236. //! buffer for copied a \c Image
  237. NICE::Image* grayImageBuffer;
  238. //! has the image been copied
  239. bool copied;
  240. private:
  241. //! Draw the selection rectangle?
  242. bool drawSelectionRect;
  243. //! Qt event handler
  244. virtual void contextMenuEvent(QContextMenuEvent* event);
  245. // handling of mouse input (rect drawing)
  246. void startDrag(int x, int y);
  247. // handling of mouse input (rect drawing)
  248. void makeDrop(int x, int y);
  249. // handling of mouse input (rect drawing)
  250. void dragging(int x, int y);
  251. //! handling of mouse input (rect drawing)
  252. bool isDragging;
  253. //! handling of mouse input (rect drawing)
  254. float dragX;
  255. //! handling of mouse input (rect drawing)
  256. float dragY;
  257. //! handling of mouse input (rect drawing)
  258. float dropX;
  259. //! handling of mouse input (rect drawing)
  260. float dropY;
  261. //! video capture
  262. std::auto_ptr<ImageFileListWriter> sequenceWriter;
  263. //! FPS Counter
  264. FrameRateCounter frameRateCounter;
  265. QString captionBuffer;
  266. void doSetCaption();
  267. std::vector<Text> texts;
  268. unsigned char *buf_overlayImage;
  269. int overlayImageWidth;
  270. int overlayImageHeight;
  271. };
  272. /**
  273. * Create an \c ImageDisplay and use it to display \c image.
  274. * @param image The image to be displayed
  275. * @param title The title of the display window
  276. * @param copy Copy the image?
  277. * @return the new \c ImageDisplay
  278. * @note This function creates a Qt widget. This means that Qt has to be
  279. * initialized before creating the widget \b and that control has
  280. * to be passed to Qt before exiting the program.
  281. * If Qt has not been initialized, yet
  282. * (i.e. no QApplication object has been created, yet),
  283. * this function will create a \c NICE::QtFramework object,
  284. * which will also initialize Qt (with empty commandline options).
  285. * If you want to properly initialize Qt,
  286. * use \c NICE::QtFramework::init(int&, char**).
  287. * To pass control to Qt before exiting, do \c NICE::QtFramework::exec().
  288. * Using \c NICE::QtFramework is opional - you can also directly use Qt.
  289. * The example code shows the simplest way to use this function.
  290. *
  291. */
  292. ImageDisplay* displayImage(const NICE::ColorImage* image,
  293. const std::string& title = "ImageDisplay",
  294. const bool copy = true);
  295. /**
  296. * @example imageDisplayExample.cpp
  297. * The example code shows the simplest way to use the functions
  298. * \c displayImage.
  299. */
  300. /**
  301. * Create an \c ImageDisplay and use it to display \c image.
  302. * @see displayImage(const NICE::ColorImage*,const std::string&,const bool)
  303. */
  304. ImageDisplay* displayImage(const NICE::Image* image,
  305. const std::string& title = "ImageDisplay",
  306. const bool copy = true);
  307. /**
  308. * Create an \c ImageDisplay and use it to display \c image with \c overlay as
  309. * overlay image.
  310. * @see displayImage(const NICE::ColorImage*,const std::string&,const bool)
  311. */
  312. ImageDisplay* displayImageOverlay(const NICE::Image* image,
  313. const NICE::Image *overlay,
  314. const std::string& title = "ImageDisplay",
  315. const bool copy = true);
  316. /**
  317. * Create an \c ImageDisplay and use it to display \c image with \c overlay as
  318. * overlay image.
  319. * @see displayImage(const NICE::ColorImage*,const std::string&,const bool)
  320. */
  321. ImageDisplay* displayImageOverlay(const NICE::ColorImage* image,
  322. const NICE::Image *overlay,
  323. const std::string& title = "ImageDisplay",
  324. const bool copy = true);
  325. /**
  326. * Display an image and five control to Qt
  327. * @see displayImage(const NICE::ColorImage*,const std::string&,const bool)
  328. */
  329. void showImage(const NICE::ColorImage & image,
  330. const std::string& title = "ImageDisplay",
  331. const bool copy = true);
  332. /**
  333. * Create an \c ImageDisplay and use it to display \c image and give control to Qt.
  334. * @see showImage(const NICE::ColorImage*,const std::string&,const bool)
  335. */
  336. void showImage(const NICE::Image & image,
  337. const std::string& title = "ImageDisplay",
  338. const bool copy = true);
  339. /**
  340. * Create an \c ImageDisplay and use it to display \c image with \c overlay
  341. * as overlay image and finally give control to Qt.
  342. * @see showImage(const NICE::ColorImage*,const std::string&,const bool)
  343. */
  344. void showImageOverlay(const NICE::Image & image,
  345. const NICE::Image & overlay,
  346. const std::string& title = "ImageDisplay",
  347. const bool copy = true);
  348. /**
  349. * Create an \c ImageDisplay and use it to display \c image with \c overlay
  350. * as overlay image and finally give control to Qt.
  351. * @see showImage(const NICE::ColorImage*,const std::string&,const bool)
  352. */
  353. void showImageOverlay(const NICE::ColorImage & image,
  354. const NICE::Image & overlay,
  355. const std::string& title = "ImageDisplay",
  356. const bool copy = true);
  357. /**
  358. * Create an \c ImageDisplay and use it to display \c image.
  359. * Difference to \c displayImage(const NICE::ColorImage*,const std::string&,const bool):
  360. * the image will always be copied.
  361. * @see displayImage(const NICE::ColorImage*,const std::string&,const bool)
  362. */
  363. inline ImageDisplay* displayImage(const NICE::ColorImage& image,
  364. const std::string& title = "ImageDisplay") {
  365. return displayImage(&image, title, true);
  366. }
  367. /**
  368. * Create an \c ImageDisplay and use it to display \c image.
  369. * Difference to \c displayImage(const NICE::ColorImage*,const std::string&,const bool):
  370. * the image will always be copied.
  371. * @see displayImage(const NICE::ColorImage*,const std::string&,const bool)
  372. */
  373. inline ImageDisplay* displayImage(const NICE::Image& image,
  374. const std::string& title = "ImageDisplay") {
  375. return displayImage(&image, title, true);
  376. }
  377. } // namespace
  378. #endif /* _IMAGEDISPLAY_IMAGEDISPLAY_H */