Just a piece of code generating video frames of adding random points to an image. LodePNG is used to save the frames. Have fun!

void rand_stars() {
// Seed the random-number generator with the current time so that
// the numbers will be different every time we run.
srand((unsigned)time(NULL));
// Image size
int w = 1024;
int h = 576;
// Initialize background color
vector buffer;
unsigned char background[] = {0, 0, 0, 255};
buffer.resize(4 * h * w);
for (int i = 0; i < w * h; ++i) {
int idx = i * 4;
for (int channel = 0; channel < 4; ++channel)
buffer[idx + channel] = background[channel];
}
// number of stars (points)
int n = 1000 * (double)rand() / (RAND_MAX + 1);
cout << n << ” stars” << endl;
// add random starts to image
for (int i = 0; i < n; ++i) {
// pixel coords
int x = w * (double)rand() / (RAND_MAX + 1);
int y = h * (double)rand() / (RAND_MAX + 1);
// star size
int size = 10 * (double)rand() / (RAND_MAX + 1);
// star color
unsigned char c1[] = { 255 * (double)rand() / (RAND_MAX + 1), 255 * (double)rand() / (RAND_MAX + 1), 255 * (double)rand() / (RAND_MAX + 1) };
// draw the star
for (int k = -size; k <= size; ++k) {
for (int t = -size; t <= size; ++t) {
if (k*k + t*t > size*size) continue;
if ((k + y) < 0 || (k + y) >= h || (t + x) < 0 || (t + x) >= w) continue;
float factor = 1.f – float(sqrt(k*k + t*t)) / size;
int idx = (k + y) * 4 * w + (t + x) * 4;
unsigned char c0[] = { buffer[idx], buffer[idx + 1], buffer[idx + 2] };
for (int channel = 0; channel < 3; ++channel)
buffer[idx + channel] = c0[channel] * (1 – factor) + c1[channel] * factor;
}
}
// encode
vector png;
lodepng::State state; //optionally customize this one
unsigned error = lodepng::encode(png, buffer, w, h, state);
// save
char filename[50];
if (i < 10) sprintf(filename, “out_00%d.png”, i);
else if (i < 100) sprintf(filename, “out_0%d.png”, i);
else sprintf(filename, “out_%d.png”, i);
if (!error) {
lodepng::save_file(png, filename);
cout << filename << ” saved!” << endl;
}
else cout << “encode ” << filename << ” failed!” << endl;
}
}
Advertisements