Fawkes API  Fawkes Development Version
show_yuv.cpp
00001 
00002 /***************************************************************************
00003  *  show_yuv.cpp - Show YUV color space
00004  *
00005  *  Created: Tue Feb 23 13:49:38 2005
00006  *  Copyright  2005-2007  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022 
00023 #include <unistd.h>
00024 #include <iostream>
00025 
00026 #include <fvwidgets/image_display.h>
00027 #include <fvutils/color/conversions.h>
00028 #include <fvutils/color/yuv.h>
00029 
00030 #include <SDL.h>
00031 
00032 using namespace std;
00033 using namespace firevision;
00034 
00035 /** YUV color space demo.
00036  * This class fills the given buffer of the size 512x512.
00037  * @author Tim Niemueller
00038  */
00039 class YUVSpaceDemo
00040 {
00041  public:
00042   /** Constructor.
00043    * @param yuv_buffer YUV422_PLANAR encoded buffer.
00044    */
00045   YUVSpaceDemo(unsigned char *yuv_buffer)
00046   {
00047     brightness = 128;
00048     buffer = yuv_buffer;
00049   }
00050 
00051   /** Fill buffer. */
00052   void
00053   fill()
00054   {
00055     unsigned char *yp = buffer;
00056     unsigned char *up = YUV422_PLANAR_U_PLANE(buffer, 512, 512);
00057     unsigned char *vp = YUV422_PLANAR_V_PLANE(buffer, 512, 512);
00058 
00059     for (int v = 255; v >= 0 ; --v) {
00060       for (int u = 0; u < 256; ++u) {
00061         *yp++ = brightness;
00062         *yp++ = brightness;
00063         *up++ = u;
00064         *vp++ = v;
00065       }
00066       // Double line
00067       memcpy(yp, (yp - 512), 512);
00068       yp += 512;
00069       memcpy(up, (up - 256), 256);
00070       memcpy(vp, (vp - 256), 256);
00071       up += 256;
00072       vp += 256;
00073     }
00074   }
00075 
00076   /** Increase brightness.
00077    * @param val value to increase brightness by
00078    */
00079   void brightness_up(unsigned int val = 1)
00080   {
00081     if ( brightness != 255 ) {
00082       if ( (brightness + val) < 255 ) {
00083         brightness += val;
00084       } else {
00085         brightness = 255;
00086       }
00087       printf("New brightness: %i\n", brightness);
00088       fill();
00089     }
00090   }
00091 
00092   /** Decrease brightness.
00093    * @param val value to decrease brightness by
00094    */
00095   void brightness_down(unsigned int val = 1) {
00096     if ( brightness != 0 ) {
00097       if ( (brightness - (int)val) > 0 ) {
00098         brightness -= val;
00099       } else {
00100         brightness = 0;
00101       }
00102       printf("New brightness: %i\n", brightness);
00103       fill();
00104     }
00105   }
00106 
00107  private:
00108   unsigned char *buffer;
00109   int brightness;
00110 
00111 };
00112 
00113 
00114 int
00115 main( int argc, char **argv )
00116 {
00117 
00118   unsigned int width = 512;
00119   unsigned int height = 512;
00120 
00121   unsigned char *yuv_buffer = malloc_buffer(YUV422_PLANAR, width, height);
00122   YUVSpaceDemo *yuvspace = new YUVSpaceDemo(yuv_buffer);
00123   ImageDisplay *display = new ImageDisplay(width, height);
00124 
00125   cout << endl << endl
00126        << " V" << endl
00127        << " ^" << endl
00128        << " |" << endl
00129        << " +--> U" << endl << endl;
00130 
00131   yuvspace->fill();
00132   display->show(yuv_buffer);
00133 
00134   SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
00135 
00136   bool quit = false;
00137   while (! quit) {
00138     SDL_Event event;
00139     if ( SDL_WaitEvent(&event) ) {
00140       switch (event.type) {
00141       case SDL_QUIT:
00142         quit = true;
00143         break;
00144       case SDL_KEYDOWN:
00145         if ( event.key.keysym.sym == SDLK_UP ) {
00146           yuvspace->brightness_up();
00147           display->show(yuv_buffer);
00148         } else if ( event.key.keysym.sym == SDLK_DOWN ) {
00149           yuvspace->brightness_down();
00150           display->show(yuv_buffer);
00151         } else if ( event.key.keysym.sym == SDLK_PAGEUP ) {
00152           yuvspace->brightness_up(20);
00153           display->show(yuv_buffer);
00154         } else if ( event.key.keysym.sym == SDLK_PAGEDOWN ) {
00155           yuvspace->brightness_down(20);
00156           display->show(yuv_buffer);
00157         } else if ( event.key.keysym.sym == SDLK_ESCAPE ) {
00158           quit = true;
00159         } else if ( event.key.keysym.sym == SDLK_q ) {
00160           quit = true;
00161         }
00162         break;
00163       default:
00164         break;
00165       }
00166     }
00167   }
00168 
00169   free(yuv_buffer);
00170   delete display;
00171   delete yuvspace;
00172 
00173   return 0;
00174 }