Lumiera  0.pre.03
»edit your freedom«
dummy-image-generator.cpp
Go to the documentation of this file.
1 /*
2  DummyImageGenerator - creating test output frames for simulated playback
3 
4  Copyright (C) Lumiera.org
5  2009, Joel Holdsworth <joel@airwebreathe.org.uk>,
6  Hermann Vosseler <Ichthyostega@web.de>
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of
11  the License, or (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 
22 * *****************************************************/
23 
24 
32 
33 
34 namespace steam {
35 namespace node {
36 
37 
38 
39  namespace { // implementation details
40 
41 
42  typedef unsigned char byte;
43 
44  inline int
45  clamp (const int &val, const int &maxval, const int &minval)
46  {
47  if(val > maxval) return maxval;
48  if(val < minval) return minval;
49  return val;
50  }
51 
52  inline void
53  rgb_to_yuv (int r, int g, int b, byte &y, byte &u, byte &v)
54  {
55  // This code isn't great, but it does the job
56  y = (byte)clamp((299 * r + 587 * g + 114 * b) / 1000, 235, 16);
57  v = (byte)clamp((500 * r - 419 * g - 81 * b) / 1000 + 127, 255, 0);
58  u = (byte)clamp((-169 * r - 331 * g + 500 * b) / 1000 + 127, 255, 0);
59  }
60 
61 
62  void
63  rgb_buffer_to_yuy2 (unsigned char *in, unsigned char *out)
64  {
65  for (uint i = 0; i < 320*240*2; i+=4)
66  {
67  byte y0, u0, v0;
68  const byte r0 = *(in++);
69  const byte g0 = *(in++);
70  const byte b0 = *(in++);
71  rgb_to_yuv(r0, g0, b0, y0, u0, v0);
72 
73  byte y1, u1, v1;
74  const byte r1 = *(in++);
75  const byte g1 = *(in++);
76  const byte b1 = *(in++);
77  rgb_to_yuv(r1, g1, b1, y1, u1, v1);
78 
79  out[i] = y0;
80  out[i + 1] = u0;
81  out[i + 2] = y1;
82  out[i + 3] = v0;
83  } }
84 
85 
86  } // (End) implementation details
87 
88 
89 
90 
91  DummyImageGenerator::DummyImageGenerator(uint fps)
92  : current_(0)
93  , frame_(0)
94  , fps_(fps)
95  { }
96 
97 
98  LumieraDisplayFrame
100  {
101 
102  ++frame_;
103  if(frame_ > 2 * fps_)
104  frame_ = 0;
105 
106  if(frame_ < 1 * fps_)
107  {
108  // create random snow...
109  for(int i = 0; i < 320*240*3; i+=3)
110  {
111  byte value ( rand() );
112  buf_[i] = value;
113  buf_[i+1] = value;
114  buf_[i+2] = value;
115  }
116  }
117  else
118  { // create a colour strip pattern
119  typedef unsigned char Row[320 * 3];
120 
121  unsigned char * row = buf_;
122 
123  // create a colour strip pattern in the first row...
124  for(int x = 0; x < 320; ++x)
125  {
126  byte &r = row[x*3];
127  byte &g = row[x*3+1];
128  byte &b = row[x*3+2];
129 
130  if (x < 1*320/7) r = 0xC0, g = 0xC0, b = 0xC0;
131  else if(x < 2*320/7) r = 0xC0, g = 0xC0, b = 0x00;
132  else if(x < 3*320/7) r = 0x00, g = 0xC0, b = 0xC0;
133  else if(x < 4*320/7) r = 0x00, g = 0xC0, b = 0x00;
134  else if(x < 5*320/7) r = 0xC0, g = 0x00, b = 0xC0;
135  else if(x < 6*320/7) r = 0xC0, g = 0x00, b = 0x00;
136  else r = 0x00, g = 0x00, b = 0xC0;
137  }
138 
139  // fill remaining rows of the frame with the same pattern
140  for(int y = 1; y < 240; ++y)
141  memcpy(buf_ + y*sizeof(Row), row, sizeof(Row));
142 
143  }
144 
145  // select output buffer to return
146  LumieraDisplayFrame outBuff;
147 
148  if (!current_)
149  {
150  outBuff = outFrame_A_;
151  current_= 1;
152  }
153  else
154  {
155  outBuff = outFrame_B_;
156  current_= 0;
157  }
158 
159  rgb_buffer_to_yuy2(buf_, outBuff);
160  return outBuff;
161 
162  }
163 
164 
165  LumieraDisplayFrame
167  {
168  if (!current_) return outFrame_A_;
169  else return outFrame_B_;
170  }
171 
172 
173 
174 }} // namespace steam::node
unsigned char buf_[320 *240 *3]
working buffer for next frame
Generator for test dummy video frames to simulate playback of rendered output.
Steam-Layer implementation namespace root.
LumieraDisplayFrame next()
generate the next frame and occupy the alternate buffer.
unsigned char outFrame_A_[320 *240 *4]
output frame 1
unsigned char outFrame_B_[320 *240 *4]
output frame 2
LumieraDisplayFrame current()
just re-return a pointer to the current frame without generating any new image data ...