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 
00034 /** YUV color space demo.
00035  * This class fills the given buffer of the size 512x512.
00036  * @author Tim Niemueller
00037  */
00038 class YUVSpaceDemo
00039 {
00040  public:
00041   /** Constructor.
00042    * @param yuv_buffer YUV422_PLANAR encoded buffer.
00043    */
00044   YUVSpaceDemo(unsigned char *yuv_buffer)
00045   {
00046     brightness = 128;
00047     buffer = yuv_buffer;
00048   }
00049 
00050   /** Fill buffer. */
00051   void
00052   fill()
00053   {
00054     unsigned char *yp = buffer;
00055     unsigned char *up = YUV422_PLANAR_U_PLANE(buffer, 512, 512);
00056     unsigned char *vp = YUV422_PLANAR_V_PLANE(buffer, 512, 512);
00057
00058     for (int v = 255; v >= 0 ; --v) {
00059       for (int u = 0; u < 256; ++u) {
00060         *yp++ = brightness;
00061         *yp++ = brightness;
00062         *up++ = u;
00063         *vp++ = v;
00064       }
00065       // Double line
00066       memcpy(yp, (yp - 512), 512);
00067       yp += 512;
00068       memcpy(up, (up - 256), 256);
00069       memcpy(vp, (vp - 256), 256);
00070       up += 256;
00071       vp += 256;
00072     }
00073   }
00074 
00075   /** Increase brightness.
00076    * @param val value to increase brightness by
00077    */
00078   void brightness_up(unsigned int val = 1)
00079   {
00080     if ( brightness != 255 ) {
00081       if ( (brightness + val) < 255 ) {
00082         brightness += val;
00083       } else {
00084         brightness = 255;
00085       }
00086       printf("New brightness: %i\n", brightness);
00087       fill();
00088     }
00089   }
00090 
00091   /** Decrease brightness.
00092    * @param val value to decrease brightness by
00093    */
00094   void brightness_down(unsigned int val = 1) {
00095     if ( brightness != 0 ) {
00096       if ( (brightness - (int)val) > 0 ) {
00097         brightness -= val;
00098       } else {
00099         brightness = 0;
00100       }
00101       printf("New brightness: %i\n", brightness);
00102       fill();
00103     }
00104   }
00105
00106  private:
00107   unsigned char *buffer;
00108   int brightness;
00109
00110 };
00111
00112
00113 int
00114 main( int argc, char **argv )
00115 {
00116
00117   unsigned int width = 512;
00118   unsigned int height = 512;
00119
00120   unsigned char *yuv_buffer = malloc_buffer(YUV422_PLANAR, width, height);
00121   YUVSpaceDemo *yuvspace = new YUVSpaceDemo(yuv_buffer);
00122   ImageDisplay *display = new ImageDisplay(width, height);
00123
00124   cout << endl << endl
00125        << " V" << endl
00126        << " ^" << endl
00127        << " |" << endl
00128        << " +--> U" << endl << endl;
00129
00130   yuvspace->fill();
00131   display->show(yuv_buffer);
00132
00133   SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
00134
00135   bool quit = false;
00136   while (! quit) {
00137     SDL_Event event;
00138     if ( SDL_WaitEvent(&event) ) {
00139       switch (event.type) {
00140       case SDL_QUIT:
00141         quit = true;
00142         break;
00143       case SDL_KEYDOWN:
00144         if ( event.key.keysym.sym == SDLK_UP ) {
00145           yuvspace->brightness_up();
00146           display->show(yuv_buffer);
00147         } else if ( event.key.keysym.sym == SDLK_DOWN ) {
00148           yuvspace->brightness_down();
00149           display->show(yuv_buffer);
00150         } else if ( event.key.keysym.sym == SDLK_PAGEUP ) {
00151           yuvspace->brightness_up(20);
00152           display->show(yuv_buffer);
00153         } else if ( event.key.keysym.sym == SDLK_PAGEDOWN ) {
00154           yuvspace->brightness_down(20);
00155           display->show(yuv_buffer);
00156         } else if ( event.key.keysym.sym == SDLK_ESCAPE ) {
00157           quit = true;
00158         } else if ( event.key.keysym.sym == SDLK_q ) {
00159           quit = true;
00160         }
00161         break;
00162       default:
00163         break;
00164       }
00165     }
00166   }
00167
00168   free(yuv_buffer);
00169   delete display;
00170   delete yuvspace;
00171 }