Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
show_yuv.cpp
1 
2 /***************************************************************************
3  * show_yuv.cpp - Show YUV color space
4  *
5  * Created: Tue Feb 23 13:49:38 2005
6  * Copyright 2005-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.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include <unistd.h>
24 #include <iostream>
25 
26 #include <fvwidgets/image_display.h>
27 #include <fvutils/color/conversions.h>
28 #include <fvutils/color/yuv.h>
29 
30 #include <SDL.h>
31 
32 using namespace std;
33 using namespace firevision;
34 
35 /** YUV color space demo.
36  * This class fills the given buffer of the size 512x512.
37  * @author Tim Niemueller
38  */
40 {
41  public:
42  /** Constructor.
43  * @param yuv_buffer YUV422_PLANAR encoded buffer.
44  */
45  YUVSpaceDemo(unsigned char *yuv_buffer)
46  {
47  brightness = 128;
48  buffer = yuv_buffer;
49  }
50 
51  /** Fill buffer. */
52  void
53  fill()
54  {
55  unsigned char *yp = buffer;
56  unsigned char *up = YUV422_PLANAR_U_PLANE(buffer, 512, 512);
57  unsigned char *vp = YUV422_PLANAR_V_PLANE(buffer, 512, 512);
58 
59  for (int v = 255; v >= 0 ; --v) {
60  for (int u = 0; u < 256; ++u) {
61  *yp++ = brightness;
62  *yp++ = brightness;
63  *up++ = u;
64  *vp++ = v;
65  }
66  // Double line
67  memcpy(yp, (yp - 512), 512);
68  yp += 512;
69  memcpy(up, (up - 256), 256);
70  memcpy(vp, (vp - 256), 256);
71  up += 256;
72  vp += 256;
73  }
74  }
75 
76  /** Increase brightness.
77  * @param val value to increase brightness by
78  */
79  void brightness_up(unsigned int val = 1)
80  {
81  if ( brightness != 255 ) {
82  if ( (brightness + val) < 255 ) {
83  brightness += val;
84  } else {
85  brightness = 255;
86  }
87  printf("New brightness: %i\n", brightness);
88  fill();
89  }
90  }
91 
92  /** Decrease brightness.
93  * @param val value to decrease brightness by
94  */
95  void brightness_down(unsigned int val = 1) {
96  if ( brightness != 0 ) {
97  if ( (brightness - (int)val) > 0 ) {
98  brightness -= val;
99  } else {
100  brightness = 0;
101  }
102  printf("New brightness: %i\n", brightness);
103  fill();
104  }
105  }
106 
107  private:
108  unsigned char *buffer;
109  int brightness;
110 
111 };
112 
113 
114 int
115 main( int argc, char **argv )
116 {
117 
118  unsigned int width = 512;
119  unsigned int height = 512;
120 
121  unsigned char *yuv_buffer = malloc_buffer(YUV422_PLANAR, width, height);
122  YUVSpaceDemo *yuvspace = new YUVSpaceDemo(yuv_buffer);
123  ImageDisplay *display = new ImageDisplay(width, height);
124 
125  cout << endl << endl
126  << " V" << endl
127  << " ^" << endl
128  << " |" << endl
129  << " +--> U" << endl << endl;
130 
131  yuvspace->fill();
132  display->show(yuv_buffer);
133 
134  SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
135 
136  bool quit = false;
137  while (! quit) {
138  SDL_Event event;
139  if ( SDL_WaitEvent(&event) ) {
140  switch (event.type) {
141  case SDL_QUIT:
142  quit = true;
143  break;
144  case SDL_KEYDOWN:
145  if ( event.key.keysym.sym == SDLK_UP ) {
146  yuvspace->brightness_up();
147  display->show(yuv_buffer);
148  } else if ( event.key.keysym.sym == SDLK_DOWN ) {
149  yuvspace->brightness_down();
150  display->show(yuv_buffer);
151  } else if ( event.key.keysym.sym == SDLK_PAGEUP ) {
152  yuvspace->brightness_up(20);
153  display->show(yuv_buffer);
154  } else if ( event.key.keysym.sym == SDLK_PAGEDOWN ) {
155  yuvspace->brightness_down(20);
156  display->show(yuv_buffer);
157  } else if ( event.key.keysym.sym == SDLK_ESCAPE ) {
158  quit = true;
159  } else if ( event.key.keysym.sym == SDLK_q ) {
160  quit = true;
161  }
162  break;
163  default:
164  break;
165  }
166  }
167  }
168 
169  free(yuv_buffer);
170  delete display;
171  delete yuvspace;
172 
173  return 0;
174 }