ImageDisplay.h 13 KB

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