Fawkes API  Fawkes Development Version
image_display.cpp
1 
2 /***************************************************************************
3  * image_display.cpp - widget to display an image based on SDL
4  *
5  * Created: Mon Nov 05 14:19:26 2007
6  * Copyright 2007 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <fvwidgets/image_display.h>
25 
26 #include <fvwidgets/sdl_keeper.h>
27 #include <SDL.h>
28 
29 #include <core/exception.h>
30 #include <fvutils/color/conversions.h>
31 #include <fvutils/color/yuv.h>
32 
33 using namespace fawkes;
34 
35 namespace firevision {
36 #if 0 /* just to make Emacs auto-indent happy */
37 }
38 #endif
39 
40 /** @class ImageDisplay <fvwidgets/image_display.h>
41  * Simple image display.
42  * This is a simple thin wrapper around the SDL to display images in a standalone
43  * window. Use this for instance for easy verification of vision results.
44  * @author Tim Niemueller
45  */
46 
47 /** Constructor.
48  * @param width width of image
49  * @param height height of image
50  * @param title window title
51  */
52 ImageDisplay::ImageDisplay(unsigned int width, unsigned int height, const char* title)
53 {
54 
55  SDLKeeper::init(SDL_INIT_VIDEO);
56  if (title) SDL_WM_SetCaption (title, NULL);
57 
58  _width = width;
59  _height = height;
60 
61  int bpp = SDL_VideoModeOK(_width, _height, 16, SDL_ANYFORMAT);
62  _surface = SDL_SetVideoMode(width, height, bpp, /* flags */ SDL_HWSURFACE | SDL_ANYFORMAT);
63  if ( ! _surface ) {
64  throw Exception("SDL: cannot create surface");
65  }
66 
67  // SDL_UYVY_OVERLAY
68  _overlay = SDL_CreateYUVOverlay(width, height, SDL_UYVY_OVERLAY, _surface);
69  if ( ! _overlay ) {
70  throw Exception("Cannot create overlay");
71  }
72 
73  _rect = new SDL_Rect;
74 
75  _rect->x = 0;
76  _rect->y = 0;
77  _rect->w = _width;
78  _rect->h = _height;
79 }
80 
81 
82 /** Destructor. */
83 ImageDisplay::~ImageDisplay()
84 {
85  delete _rect;
86 
87  SDL_FreeYUVOverlay(_overlay);
88  SDL_FreeSurface(_surface);
89 
90  SDLKeeper::quit();
91 }
92 
93 
94 /** Show image from given colorspace.
95  * @param colorspace colorspace of the supplied buffer
96  * @param buffer image buffer
97  */
98 void
99 ImageDisplay::show(colorspace_t colorspace, unsigned char *buffer)
100 {
101  SDL_LockYUVOverlay(_overlay);
102  convert(colorspace, YUV422_PACKED, buffer, _overlay->pixels[0], _width, _height);
103  SDL_UnlockYUVOverlay(_overlay);
104  SDL_DisplayYUVOverlay(_overlay, _rect);
105 }
106 
107 
108 /** Show image from YUV422_PLANAR colorspace.
109  * @param yuv422_planar_buffer YUV422_PLANAR encoded image.
110  */
111 void
112 ImageDisplay::show(unsigned char *yuv422_planar_buffer)
113 {
114  SDL_LockYUVOverlay(_overlay);
115 
116  yuv422planar_to_yuv422packed(yuv422_planar_buffer, _overlay->pixels[0],
117  _width, _height);
118 
119  SDL_UnlockYUVOverlay(_overlay);
120  SDL_DisplayYUVOverlay(_overlay, _rect);
121 }
122 
123 /** Process a few SDL events.
124  * @param max_num_events maximum number of events to process.
125  */
126 void
127 ImageDisplay::process_events(unsigned int max_num_events)
128 {
129  unsigned int proc = 0;
130  SDL_Event event;
131  while ( (proc++ < max_num_events) && (SDL_PollEvent(&event)) ) {
132  // nothing to do here
133  }
134 }
135 
136 
137 /** Process SDL events until quit.
138  * Process SDL events and keeps the window responsive until either
139  * the key "q" or "Esc" are pressed.
140  */
141 void
142 ImageDisplay::loop_until_quit()
143 {
144  bool quit = false;
145  while (! quit) {
146  SDL_Event event;
147  if ( SDL_WaitEvent(&event) ) {
148  switch (event.type) {
149  case SDL_QUIT:
150  quit = true;
151  break;
152  case SDL_KEYUP:
153  if ( (event.key.keysym.sym == SDLK_ESCAPE) ||
154  (event.key.keysym.sym == SDLK_q) ) {
155  quit = true;
156  }
157  break;
158  }
159  }
160  }
161 }
162 
163 } // end namespace firevision
Fawkes library namespace.
Base class for exceptions in Fawkes.
Definition: exception.h:36