Implemented a funky test card generator

This commit is contained in:
Joel Holdsworth 2009-01-18 17:41:55 +00:00
parent b0db84ec14
commit 20b2cb1ee5

View file

@ -102,11 +102,96 @@ PlaybackController::playback_thread()
}
}
typedef unsigned char byte;
inline int
clamp(const int &val, const int &maxval, const int &minval)
{
if(val > maxval) return maxval;
if(val < minval) return minval;
return val;
}
inline void
rgb_to_yuv(int r, int g, int b, byte &y, byte &u, byte &v)
{
// This code isn't great, but it does the job
y = (byte)clamp((299 * r + 587 * g + 114 * b) / 1000, 235, 16);
v = (byte)clamp((500 * r - 419 * g - 81 * b) / 1000 + 127, 255, 0);
u = (byte)clamp((-169 * r - 331 * g + 500 * b) / 1000 + 127, 255, 0);
}
void rgb_buffer_to_yuy2(unsigned char *in, unsigned char *out)
{
for(int i = 0; i < 320*240*2; i+=4)
{
byte y0, u0, v0;
const byte r0 = *(in++);
const byte g0 = *(in++);
const byte b0 = *(in++);
rgb_to_yuv(r0, g0, b0, y0, u0, v0);
byte y1, u1, v1;
const byte r1 = *(in++);
const byte g1 = *(in++);
const byte b1 = *(in++);
rgb_to_yuv(r1, g1, b1, y1, u1, v1);
out[i] = y0;
out[i + 1] = (byte)(((int)u0 + (int)u1) / 2);
out[i + 2] = y1;
out[i + 3] = (byte)(((int)v0 + (int)v1) / 2);
}
}
void
PlaybackController::pull_frame()
{
for(int i = 0; i < 320*240*4; i++)
buffer[i] = rand();
static int frame = 0;
unsigned char in[320 * 240 * 3];
frame--;
if(frame <= 0)
frame = 200;
if(frame > 150)
{
for(int i = 0; i < 320*240*3; i+=3)
{
byte value = (byte)rand();
in[i] = value;
in[i+1] = value;
in[i+2] = value;
}
}
else
{
unsigned char row[320 * 3];
for(int x = 0; x < 320; x++)
{
byte &r = row[x*3];
byte &g = row[x*3+1];
byte &b = row[x*3+2];
if(x < 1*320/7) r = 0xC0, g = 0xC0, b = 0xC0;
else if(x < 2*320/7) r = 0xC0, g = 0xC0, b = 0x00;
else if(x < 3*320/7) r = 0x00, g = 0xC0, b = 0xC0;
else if(x < 4*320/7) r = 0x00, g = 0xC0, b = 0x00;
else if(x < 5*320/7) r = 0xC0, g = 0x00, b = 0xC0;
else if(x < 6*320/7) r = 0xC0, g = 0x00, b = 0x00;
else r = 0x00, g = 0x00, b = 0xC0;
}
for(int y = 0; y < 240; y++)
{
memcpy(in + y*320*3, row, sizeof(row));
}
}
rgb_buffer_to_yuy2(in, buffer);
dispatcher.emit();
}