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)
5  2009, Joel Holdsworth <joel@airwebreathe.org.uk>,
6  Hermann Vosseler <Ichthyostega@web.de>
7 
8   **Lumiera** is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by the
10   Free Software Foundation; either version 2 of the License, or (at your
11   option) any later version. See the file COPYING for further details.
12 
13 * *****************************************************************/
14 
15 
23 
24 
25 namespace steam {
26 namespace node {
27 
28 
29 
30  namespace { // implementation details
31 
32 
33  typedef unsigned char byte;
34 
35  inline int
36  clamp (const int &val, const int &maxval, const int &minval)
37  {
38  if(val > maxval) return maxval;
39  if(val < minval) return minval;
40  return val;
41  }
42 
43  inline void
44  rgb_to_yuv (int r, int g, int b, byte &y, byte &u, byte &v)
45  {
46  // This code isn't great, but it does the job
47  y = (byte)clamp((299 * r + 587 * g + 114 * b) / 1000, 235, 16);
48  v = (byte)clamp((500 * r - 419 * g - 81 * b) / 1000 + 127, 255, 0);
49  u = (byte)clamp((-169 * r - 331 * g + 500 * b) / 1000 + 127, 255, 0);
50  }
51 
52 
53  void
54  rgb_buffer_to_yuy2 (unsigned char *in, unsigned char *out)
55  {
56  for (uint i = 0; i < 320*240*2; i+=4)
57  {
58  byte y0, u0, v0;
59  const byte r0 = *(in++);
60  const byte g0 = *(in++);
61  const byte b0 = *(in++);
62  rgb_to_yuv(r0, g0, b0, y0, u0, v0);
63 
64  byte y1, u1, v1;
65  const byte r1 = *(in++);
66  const byte g1 = *(in++);
67  const byte b1 = *(in++);
68  rgb_to_yuv(r1, g1, b1, y1, u1, v1);
69 
70  out[i] = y0;
71  out[i + 1] = u0;
72  out[i + 2] = y1;
73  out[i + 3] = v0;
74  } }
75 
76 
77  } // (End) implementation details
78 
79 
80 
81 
82  DummyImageGenerator::DummyImageGenerator(uint fps)
83  : current_(0)
84  , frame_(0)
85  , fps_(fps)
86  { }
87 
88 
89  LumieraDisplayFrame
91  {
92 
93  ++frame_;
94  if(frame_ > 2 * fps_)
95  frame_ = 0;
96 
97  if(frame_ < 1 * fps_)
98  {
99  // create random snow...
100  for(int i = 0; i < 320*240*3; i+=3)
101  {
102  byte value ( rand() );
103  buf_[i] = value;
104  buf_[i+1] = value;
105  buf_[i+2] = value;
106  }
107  }
108  else
109  { // create a colour strip pattern
110  typedef unsigned char Row[320 * 3];
111 
112  unsigned char * row = buf_;
113 
114  // create a colour strip pattern in the first row...
115  for(int x = 0; x < 320; ++x)
116  {
117  byte &r = row[x*3];
118  byte &g = row[x*3+1];
119  byte &b = row[x*3+2];
120 
121  if (x < 1*320/7) r = 0xC0, g = 0xC0, b = 0xC0;
122  else if(x < 2*320/7) r = 0xC0, g = 0xC0, b = 0x00;
123  else if(x < 3*320/7) r = 0x00, g = 0xC0, b = 0xC0;
124  else if(x < 4*320/7) r = 0x00, g = 0xC0, b = 0x00;
125  else if(x < 5*320/7) r = 0xC0, g = 0x00, b = 0xC0;
126  else if(x < 6*320/7) r = 0xC0, g = 0x00, b = 0x00;
127  else r = 0x00, g = 0x00, b = 0xC0;
128  }
129 
130  // fill remaining rows of the frame with the same pattern
131  for(int y = 1; y < 240; ++y)
132  memcpy(buf_ + y*sizeof(Row), row, sizeof(Row));
133 
134  }
135 
136  // select output buffer to return
137  LumieraDisplayFrame outBuff;
138 
139  if (!current_)
140  {
141  outBuff = outFrame_A_;
142  current_= 1;
143  }
144  else
145  {
146  outBuff = outFrame_B_;
147  current_= 0;
148  }
149 
150  rgb_buffer_to_yuy2(buf_, outBuff);
151  return outBuff;
152 
153  }
154 
155 
156  LumieraDisplayFrame
158  {
159  if (!current_) return outFrame_A_;
160  else return outFrame_B_;
161  }
162 
163 
164 
165 }} // 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 ...