Fawkes API  Fawkes Development Version
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  /** Get Brightness.
108  * @return current brightness
109  */
110  int get_brightness() const
111  {
112  return brightness;
113  }
114 
115  private:
116  unsigned char *buffer;
117  int brightness;
118 
119 };
120 
121 
122 int
123 main( int argc, char **argv )
124 {
125 
126  unsigned int width = 512;
127  unsigned int height = 512;
128 
129  unsigned char *yuv_buffer = malloc_buffer(YUV422_PLANAR, width, height);
130  YUVSpaceDemo *yuvspace = new YUVSpaceDemo(yuv_buffer);
131  ImageDisplay *display = new ImageDisplay(width, height);
132 
133  cout << endl << endl
134  << " V" << endl
135  << " ^" << endl
136  << " |" << endl
137  << " +--> U" << endl << endl;
138 
139  yuvspace->fill();
140  display->show(yuv_buffer);
141 
142  SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
143 
144  bool quit = false;
145  while (! quit) {
146  SDL_Event event;
147  if ( SDL_WaitEvent(&event) ) {
148  switch (event.type) {
149  case SDL_QUIT:
150  quit = true;
151  break;
152  case SDL_KEYDOWN:
153  if ( event.key.keysym.sym == SDLK_UP ) {
154  yuvspace->brightness_up();
155  display->show(yuv_buffer);
156  } else if ( event.key.keysym.sym == SDLK_DOWN ) {
157  yuvspace->brightness_down();
158  display->show(yuv_buffer);
159  } else if ( event.key.keysym.sym == SDLK_PAGEUP ) {
160  yuvspace->brightness_up(20);
161  display->show(yuv_buffer);
162  } else if ( event.key.keysym.sym == SDLK_PAGEDOWN ) {
163  yuvspace->brightness_down(20);
164  display->show(yuv_buffer);
165 
166  } else if ( event.key.keysym.sym == SDLK_ESCAPE ) {
167  quit = true;
168  } else if ( event.key.keysym.sym == SDLK_q ) {
169  quit = true;
170  }
171  break;
172 
173  case SDL_MOUSEBUTTONDOWN:
174  {
175  int x = event.button.x;
176  int y = event.button.y;
177 
178  printf("YUV: %i %u %u\n", yuvspace->get_brightness(),
179  x / 2, y / 2);
180  }
181  break;
182 
183  default:
184  break;
185  }
186  }
187  }
188 
189  free(yuv_buffer);
190  delete display;
191  delete yuvspace;
192 
193  return 0;
194 }
Simple image display.
Definition: image_display.h:38
void fill()
Fill buffer.
Definition: show_yuv.cpp:53
YUVSpaceDemo(unsigned char *yuv_buffer)
Constructor.
Definition: show_yuv.cpp:45
STL namespace.
void brightness_up(unsigned int val=1)
Increase brightness.
Definition: show_yuv.cpp:79
YUV color space demo.
Definition: show_yuv.cpp:39
void brightness_down(unsigned int val=1)
Decrease brightness.
Definition: show_yuv.cpp:95
int get_brightness() const
Get Brightness.
Definition: show_yuv.cpp:110
void show(colorspace_t colorspace, unsigned char *buffer)
Show image from given colorspace.