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 <fvutils/color/conversions.h>
24 #include <fvutils/color/yuv.h>
25 #include <fvwidgets/image_display.h>
26 
27 #include <SDL.h>
28 #include <iostream>
29 #include <unistd.h>
30 
31 using namespace std;
32 using namespace firevision;
33 
34 /** YUV color space demo.
35  * This class fills the given buffer of the size 512x512.
36  * @author Tim Niemueller
37  */
38 class YUVSpaceDemo
39 {
40 public:
41  /** Constructor.
42  * @param yuv_buffer YUV422_PLANAR encoded buffer.
43  */
44  explicit YUVSpaceDemo(unsigned char *yuv_buffer)
45  {
46  brightness = 128;
47  buffer = yuv_buffer;
48  }
49 
50  /** Fill buffer. */
51  void
52  fill()
53  {
54  unsigned char *yp = buffer;
55  unsigned char *up = YUV422_PLANAR_U_PLANE(buffer, 512, 512);
56  unsigned char *vp = YUV422_PLANAR_V_PLANE(buffer, 512, 512);
57 
58  for (int v = 255; v >= 0; --v) {
59  for (int u = 0; u < 256; ++u) {
60  *yp++ = brightness;
61  *yp++ = brightness;
62  *up++ = u;
63  *vp++ = v;
64  }
65  // Double line
66  memcpy(yp, (yp - 512), 512);
67  yp += 512;
68  memcpy(up, (up - 256), 256);
69  memcpy(vp, (vp - 256), 256);
70  up += 256;
71  vp += 256;
72  }
73  }
74 
75  /** Increase brightness.
76  * @param val value to increase brightness by
77  */
78  void
79  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
96  brightness_down(unsigned int val = 1)
97  {
98  if (brightness != 0) {
99  if ((brightness - (int)val) > 0) {
100  brightness -= val;
101  } else {
102  brightness = 0;
103  }
104  printf("New brightness: %i\n", brightness);
105  fill();
106  }
107  }
108 
109  /** Get Brightness.
110  * @return current brightness
111  */
112  int
113  get_brightness() const
114  {
115  return brightness;
116  }
117 
118 private:
119  unsigned char *buffer;
120  int brightness;
121 };
122 
123 int
124 main(int argc, char **argv)
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 << " V" << endl << " ^" << endl << " |" << endl << " +--> U" << endl << endl;
134 
135  yuvspace->fill();
136  display->show(yuv_buffer);
137 
138  SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
139 
140  bool quit = false;
141  while (!quit) {
142  SDL_Event event;
143  if (SDL_WaitEvent(&event)) {
144  switch (event.type) {
145  case SDL_QUIT: quit = true; break;
146  case SDL_KEYDOWN:
147  if (event.key.keysym.sym == SDLK_UP) {
148  yuvspace->brightness_up();
149  display->show(yuv_buffer);
150  } else if (event.key.keysym.sym == SDLK_DOWN) {
151  yuvspace->brightness_down();
152  display->show(yuv_buffer);
153  } else if (event.key.keysym.sym == SDLK_PAGEUP) {
154  yuvspace->brightness_up(20);
155  display->show(yuv_buffer);
156  } else if (event.key.keysym.sym == SDLK_PAGEDOWN) {
157  yuvspace->brightness_down(20);
158  display->show(yuv_buffer);
159 
160  } else if (event.key.keysym.sym == SDLK_ESCAPE) {
161  quit = true;
162  } else if (event.key.keysym.sym == SDLK_q) {
163  quit = true;
164  }
165  break;
166 
167  case SDL_MOUSEBUTTONDOWN: {
168  int x = event.button.x;
169  int y = event.button.y;
170 
171  printf("YUV: %i %i %i\n", yuvspace->get_brightness(), x / 2, y / 2);
172  } break;
173 
174  default: break;
175  }
176  }
177  }
178 
179  free(yuv_buffer);
180  delete display;
181  delete yuvspace;
182 
183  return 0;
184 }
firevision::ImageDisplay
Definition: image_display.h:34
YUVSpaceDemo::get_brightness
int get_brightness() const
Get Brightness.
Definition: show_yuv.cpp:112
YUVSpaceDemo::brightness_down
void brightness_down(unsigned int val=1)
Decrease brightness.
Definition: show_yuv.cpp:95
YUVSpaceDemo
YUV color space demo.
Definition: show_yuv.cpp:37
YUVSpaceDemo::brightness_up
void brightness_up(unsigned int val=1)
Increase brightness.
Definition: show_yuv.cpp:78
YUVSpaceDemo::fill
void fill()
Fill buffer.
Definition: show_yuv.cpp:51