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 <core/exception.h>
25#include <fvutils/color/conversions.h>
26#include <fvutils/color/yuv.h>
27#include <fvwidgets/image_display.h>
28#include <fvwidgets/sdl_keeper.h>
29
30#include <SDL.h>
31
32using namespace fawkes;
33
34namespace firevision {
35
36/** @class ImageDisplay <fvwidgets/image_display.h>
37 * Simple image display.
38 * This is a simple thin wrapper around the SDL to display images in a standalone
39 * window. Use this for instance for easy verification of vision results.
40 * @author Tim Niemueller
41 */
42
43/** Constructor.
44 * @param width width of image
45 * @param height height of image
46 * @param title window title
47 */
48ImageDisplay::ImageDisplay(unsigned int width, unsigned int height, const char *title)
49{
50 SDLKeeper::init(SDL_INIT_VIDEO);
51 if (title)
52 SDL_WM_SetCaption(title, NULL);
53
54 _width = width;
55 _height = height;
56
57 int bpp = SDL_VideoModeOK(_width, _height, 16, SDL_ANYFORMAT);
58 _surface = SDL_SetVideoMode(width, height, bpp, /* flags */ SDL_HWSURFACE | SDL_ANYFORMAT);
59 if (!_surface) {
60 throw Exception("SDL: cannot create surface");
61 }
62
63 // SDL_UYVY_OVERLAY
64 _overlay = SDL_CreateYUVOverlay(width, height, SDL_UYVY_OVERLAY, _surface);
65 if (!_overlay) {
66 throw Exception("Cannot create overlay");
67 }
68
69 _rect = new SDL_Rect;
70
71 _rect->x = 0;
72 _rect->y = 0;
73 _rect->w = _width;
74 _rect->h = _height;
75}
76
77/** Destructor. */
79{
80 delete _rect;
81
82 SDL_FreeYUVOverlay(_overlay);
83 SDL_FreeSurface(_surface);
84
86}
87
88/** Show image from given colorspace.
89 * @param colorspace colorspace of the supplied buffer
90 * @param buffer image buffer
91 */
92void
93ImageDisplay::show(colorspace_t colorspace, unsigned char *buffer)
94{
95 SDL_LockYUVOverlay(_overlay);
96 convert(colorspace, YUV422_PACKED, buffer, _overlay->pixels[0], _width, _height);
97 SDL_UnlockYUVOverlay(_overlay);
98 SDL_DisplayYUVOverlay(_overlay, _rect);
99}
100
101/** Show image from YUV422_PLANAR colorspace.
102 * @param yuv422_planar_buffer YUV422_PLANAR encoded image.
103 */
104void
105ImageDisplay::show(unsigned char *yuv422_planar_buffer)
106{
107 SDL_LockYUVOverlay(_overlay);
108
109 yuv422planar_to_yuv422packed(yuv422_planar_buffer, _overlay->pixels[0], _width, _height);
110
111 SDL_UnlockYUVOverlay(_overlay);
112 SDL_DisplayYUVOverlay(_overlay, _rect);
113}
114
115/** Process a few SDL events.
116 * @param max_num_events maximum number of events to process.
117 */
118void
119ImageDisplay::process_events(unsigned int max_num_events)
120{
121 unsigned int proc = 0;
122 SDL_Event event;
123 while ((proc++ < max_num_events) && (SDL_PollEvent(&event))) {
124 // nothing to do here
125 }
126}
127
128/** Process SDL events until quit.
129 * Process SDL events and keeps the window responsive until either
130 * the key "q" or "Esc" are pressed.
131 */
132void
134{
135 bool quit = false;
136 while (!quit) {
137 SDL_Event event;
138 if (SDL_WaitEvent(&event)) {
139 switch (event.type) {
140 case SDL_QUIT: quit = true; break;
141 case SDL_KEYUP:
142 if ((event.key.keysym.sym == SDLK_ESCAPE) || (event.key.keysym.sym == SDLK_q)) {
143 quit = true;
144 }
145 break;
146 }
147 }
148 }
149}
150
151} // end namespace firevision
Base class for exceptions in Fawkes.
Definition: exception.h:36
ImageDisplay(unsigned int width, unsigned int height, const char *title=0)
Constructor.
void loop_until_quit()
Process SDL events until quit.
void show(colorspace_t colorspace, unsigned char *buffer)
Show image from given colorspace.
void process_events(unsigned int max_num_events=10)
Process a few SDL events.
static void quit() noexcept
Conditionally quit SDL.
Definition: sdl_keeper.cpp:85
static void init(unsigned int flags)
Init SDL.
Definition: sdl_keeper.cpp:58
Fawkes library namespace.