ImageDisplay.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. NICE::ColorImage * getColorImageBuffer()
  111. {
  112. return this->colorImageBuffer;
  113. }
  114. /**
  115. * Draw the selection rectangle?
  116. * @return The current setting
  117. */
  118. inline bool isDrawSelectionRect() const {
  119. return drawSelectionRect;
  120. }
  121. virtual void setCaption(const QString& s);
  122. /**
  123. * Add a line of text to be display on the image.
  124. * @param x x-coordinate
  125. * @param y y-coordinate
  126. * @param text the text
  127. * @return id needed to change/remove the text
  128. */
  129. inline int addText(int x, int y, const std::string& text) {
  130. return addText(x, y, text, Color(255, 255, 255));
  131. }
  132. /**
  133. * Add a line of text to be display on the image.
  134. * @param x x-coordinate
  135. * @param y y-coordinate
  136. * @param text the text
  137. * @param color the color of the text
  138. * @return id needed to change/remove the text
  139. */
  140. int addText(int x, int y, const std::string& text, const Color& color);
  141. /**
  142. * Hide a line of text.
  143. */
  144. inline void hideText(int id) {
  145. texts[id].visible = false;
  146. }
  147. /**
  148. * Show a line of text, which has been hidden before.
  149. */
  150. inline void showText(int id) {
  151. texts[id].visible = true;
  152. }
  153. /**
  154. * Remove a line of text.
  155. */
  156. inline Text& text(int id) {
  157. return texts[id];
  158. }
  159. inline void removeAllText() {
  160. texts.clear();
  161. }
  162. /**
  163. * The total number of text lines.
  164. */
  165. inline uint numberOfTextLines() const {
  166. return texts.size();
  167. }
  168. signals:
  169. /**
  170. * A rectangle (\c left, \c top, \c right, \c bottom) has been drawn
  171. * by the user.
  172. * @note A single click results in a degenerate rectangle.
  173. * Thus, single clicks can also be handled:
  174. * use point (\c left, \c top).
  175. */
  176. void rectSelect(float left, float top, float right, float bottom);
  177. public slots:
  178. /**
  179. * Repaint this widget.
  180. */
  181. void repaint();
  182. /**
  183. * Save menu item clicked.
  184. */
  185. void menuSave();
  186. /**
  187. * Start video capture menu item clicked.
  188. */
  189. void menuStartCapture();
  190. /**
  191. * Stop video capture menu item clicked.
  192. */
  193. void menuStopCapture();
  194. /**
  195. * Start button clicked in video capture dialog.
  196. */
  197. void dialogStartCapture();
  198. /**
  199. * Stop button clicked in video capture dialog.
  200. */
  201. void dialogStopCapture();
  202. /**
  203. * Aspect ratio menu item clicked.
  204. */
  205. void menuAspectRatio();
  206. protected:
  207. /**
  208. * Implements QGLWidget::paintGL()
  209. */
  210. virtual void paintGL();
  211. /**
  212. * called by paintGL(), use this to change projection mode
  213. */
  214. virtual void setGLProjection(void);
  215. /**
  216. * called by paintGL(), use this to include additional openGL code
  217. */
  218. virtual void paintGLObjects(void);
  219. /**
  220. * called by contextMenuEvent(), use this to include additional
  221. * menu items
  222. */
  223. virtual void addExtraMenuItems ( Q3PopupMenu *popupMenu );
  224. //! receive mouse events
  225. virtual void mousePressEvent(QMouseEvent* event);
  226. //! receive mouse events
  227. virtual void mouseReleaseEvent(QMouseEvent* event);
  228. //! receive mouse events
  229. virtual void mouseMoveEvent(QMouseEvent* event);
  230. private:
  231. //! capture control dialog
  232. std::auto_ptr<CaptureDialog> dialog;
  233. protected:
  234. //! a pointer to the current image
  235. const NICE::GrayColorImageCommonImplementationT<Ipp8u>* image;
  236. //! color or gray?
  237. bool isColor;
  238. //! buffer for copied a \c ColorImage
  239. NICE::ColorImage* colorImageBuffer;
  240. //! buffer for copied a \c Image
  241. NICE::Image* grayImageBuffer;
  242. //! has the image been copied
  243. bool copied;
  244. private:
  245. //! Draw the selection rectangle?
  246. bool drawSelectionRect;
  247. //! Qt event handler
  248. virtual void contextMenuEvent(QContextMenuEvent* event);
  249. // handling of mouse input (rect drawing)
  250. void startDrag(int x, int y);
  251. // handling of mouse input (rect drawing)
  252. void makeDrop(int x, int y);
  253. // handling of mouse input (rect drawing)
  254. void dragging(int x, int y);
  255. //! handling of mouse input (rect drawing)
  256. bool isDragging;
  257. //! handling of mouse input (rect drawing)
  258. float dragX;
  259. //! handling of mouse input (rect drawing)
  260. float dragY;
  261. //! handling of mouse input (rect drawing)
  262. float dropX;
  263. //! handling of mouse input (rect drawing)
  264. float dropY;
  265. //! video capture
  266. std::auto_ptr<ImageFileListWriter> sequenceWriter;
  267. //! FPS Counter
  268. FrameRateCounter frameRateCounter;
  269. QString captionBuffer;
  270. void doSetCaption();
  271. std::vector<Text> texts;
  272. unsigned char *buf_overlayImage;
  273. int overlayImageWidth;
  274. int overlayImageHeight;
  275. };
  276. /**
  277. * Create an \c ImageDisplay and use it to display \c image.
  278. * @param image The image to be displayed
  279. * @param title The title of the display window
  280. * @param copy Copy the image?
  281. * @return the new \c ImageDisplay
  282. * @note This function creates a Qt widget. This means that Qt has to be
  283. * initialized before creating the widget \b and that control has
  284. * to be passed to Qt before exiting the program.
  285. * If Qt has not been initialized, yet
  286. * (i.e. no QApplication object has been created, yet),
  287. * this function will create a \c NICE::QtFramework object,
  288. * which will also initialize Qt (with empty commandline options).
  289. * If you want to properly initialize Qt,
  290. * use \c NICE::QtFramework::init(int&, char**).
  291. * To pass control to Qt before exiting, do \c NICE::QtFramework::exec().
  292. * Using \c NICE::QtFramework is opional - you can also directly use Qt.
  293. * The example code shows the simplest way to use this function.
  294. *
  295. */
  296. ImageDisplay* displayImage(const NICE::ColorImage* image,
  297. const std::string& title = "ImageDisplay",
  298. const bool copy = true);
  299. /**
  300. * @example imageDisplayExample.cpp
  301. * The example code shows the simplest way to use the functions
  302. * \c displayImage.
  303. */
  304. /**
  305. * Create an \c ImageDisplay and use it to display \c image.
  306. * @see displayImage(const NICE::ColorImage*,const std::string&,const bool)
  307. */
  308. ImageDisplay* displayImage(const NICE::Image* image,
  309. const std::string& title = "ImageDisplay",
  310. const bool copy = true);
  311. /**
  312. * Create an \c ImageDisplay and use it to display \c image with \c overlay as
  313. * overlay image.
  314. * @see displayImage(const NICE::ColorImage*,const std::string&,const bool)
  315. */
  316. ImageDisplay* displayImageOverlay(const NICE::Image* image,
  317. const NICE::Image *overlay,
  318. const std::string& title = "ImageDisplay",
  319. const bool copy = true);
  320. /**
  321. * Create an \c ImageDisplay and use it to display \c image with \c overlay as
  322. * overlay image.
  323. * @see displayImage(const NICE::ColorImage*,const std::string&,const bool)
  324. */
  325. ImageDisplay* displayImageOverlay(const NICE::ColorImage* image,
  326. const NICE::Image *overlay,
  327. const std::string& title = "ImageDisplay",
  328. const bool copy = true);
  329. /**
  330. * Display an image and five control to Qt
  331. * @see displayImage(const NICE::ColorImage*,const std::string&,const bool)
  332. */
  333. void showImage(const NICE::ColorImage & image,
  334. const std::string& title = "ImageDisplay",
  335. const bool copy = true);
  336. /**
  337. * Create an \c ImageDisplay and use it to display \c image and give control to Qt.
  338. * @see showImage(const NICE::ColorImage*,const std::string&,const bool)
  339. */
  340. void showImage(const NICE::Image & image,
  341. const std::string& title = "ImageDisplay",
  342. const bool copy = true);
  343. /**
  344. * Create an \c ImageDisplay and use it to display \c image with \c overlay
  345. * as overlay image and finally give control to Qt.
  346. * @see showImage(const NICE::ColorImage*,const std::string&,const bool)
  347. */
  348. void showImageOverlay(const NICE::Image & image,
  349. const NICE::Image & overlay,
  350. const std::string& title = "ImageDisplay",
  351. const bool copy = true);
  352. /**
  353. * Create an \c ImageDisplay and use it to display \c image with \c overlay
  354. * as overlay image and finally give control to Qt.
  355. * @see showImage(const NICE::ColorImage*,const std::string&,const bool)
  356. */
  357. void showImageOverlay(const NICE::ColorImage & image,
  358. const NICE::Image & overlay,
  359. const std::string& title = "ImageDisplay",
  360. const bool copy = true);
  361. /**
  362. * Create an \c ImageDisplay and use it to display \c image.
  363. * Difference to \c displayImage(const NICE::ColorImage*,const std::string&,const bool):
  364. * the image will always be copied.
  365. * @see displayImage(const NICE::ColorImage*,const std::string&,const bool)
  366. */
  367. inline ImageDisplay* displayImage(const NICE::ColorImage& image,
  368. const std::string& title = "ImageDisplay") {
  369. return displayImage(&image, title, true);
  370. }
  371. /**
  372. * Create an \c ImageDisplay and use it to display \c image.
  373. * Difference to \c displayImage(const NICE::ColorImage*,const std::string&,const bool):
  374. * the image will always be copied.
  375. * @see displayImage(const NICE::ColorImage*,const std::string&,const bool)
  376. */
  377. inline ImageDisplay* displayImage(const NICE::Image& image,
  378. const std::string& title = "ImageDisplay") {
  379. return displayImage(&image, title, true);
  380. }
  381. } // namespace
  382. #endif /* _IMAGEDISPLAY_IMAGEDISPLAY_H */