00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <fvwidgets/image_display.h>
00025
00026 #include <fvwidgets/sdl_keeper.h>
00027 #include <SDL.h>
00028
00029 #include <core/exception.h>
00030 #include <fvutils/color/conversions.h>
00031 #include <fvutils/color/yuv.h>
00032
00033 using namespace fawkes;
00034
00035 namespace firevision {
00036 #if 0
00037 }
00038 #endif
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 ImageDisplay::ImageDisplay(unsigned int width, unsigned int height, const char* title)
00053 {
00054
00055 SDLKeeper::init(SDL_INIT_VIDEO);
00056 if (title) SDL_WM_SetCaption (title, NULL);
00057
00058 _width = width;
00059 _height = height;
00060
00061 int bpp = SDL_VideoModeOK(_width, _height, 16, SDL_ANYFORMAT);
00062 _surface = SDL_SetVideoMode(width, height, bpp, SDL_HWSURFACE | SDL_ANYFORMAT);
00063 if ( ! _surface ) {
00064 throw Exception("SDL: cannot create surface");
00065 }
00066
00067
00068 _overlay = SDL_CreateYUVOverlay(width, height, SDL_UYVY_OVERLAY, _surface);
00069 if ( ! _overlay ) {
00070 throw Exception("Cannot create overlay");
00071 }
00072
00073 _rect = new SDL_Rect;
00074
00075 _rect->x = 0;
00076 _rect->y = 0;
00077 _rect->w = _width;
00078 _rect->h = _height;
00079 }
00080
00081
00082
00083 ImageDisplay::~ImageDisplay()
00084 {
00085 delete _rect;
00086
00087 SDL_FreeYUVOverlay(_overlay);
00088 SDL_FreeSurface(_surface);
00089
00090 SDLKeeper::quit();
00091 }
00092
00093
00094
00095
00096
00097
00098 void
00099 ImageDisplay::show(colorspace_t colorspace, unsigned char *buffer)
00100 {
00101 SDL_LockYUVOverlay(_overlay);
00102 convert(colorspace, YUV422_PACKED, buffer, _overlay->pixels[0], _width, _height);
00103 SDL_UnlockYUVOverlay(_overlay);
00104 SDL_DisplayYUVOverlay(_overlay, _rect);
00105 }
00106
00107
00108
00109
00110
00111 void
00112 ImageDisplay::show(unsigned char *yuv422_planar_buffer)
00113 {
00114 SDL_LockYUVOverlay(_overlay);
00115
00116 yuv422planar_to_yuv422packed(yuv422_planar_buffer, _overlay->pixels[0],
00117 _width, _height);
00118
00119 SDL_UnlockYUVOverlay(_overlay);
00120 SDL_DisplayYUVOverlay(_overlay, _rect);
00121 }
00122
00123
00124
00125
00126 void
00127 ImageDisplay::process_events(unsigned int max_num_events)
00128 {
00129 unsigned int proc = 0;
00130 SDL_Event event;
00131 while ( (proc++ < max_num_events) && (SDL_PollEvent(&event)) ) {
00132
00133 }
00134 }
00135
00136
00137
00138
00139
00140
00141 void
00142 ImageDisplay::loop_until_quit()
00143 {
00144 bool quit = false;
00145 while (! quit) {
00146 SDL_Event event;
00147 if ( SDL_WaitEvent(&event) ) {
00148 switch (event.type) {
00149 case SDL_QUIT:
00150 quit = true;
00151 break;
00152 case SDL_KEYUP:
00153 if ( (event.key.keysym.sym == SDLK_ESCAPE) ||
00154 (event.key.keysym.sym == SDLK_q) ) {
00155 quit = true;
00156 }
00157 break;
00158 }
00159 }
00160 }
00161 }
00162
00163 }