-
Notifications
You must be signed in to change notification settings - Fork 0
/
GifBuilder.cpp
68 lines (59 loc) · 2.22 KB
/
GifBuilder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// Created by Jakub Begera on 03.01.18.
//
#include <math.h>
#include <iostream>
#include "GifBuilder.h"
GifBuilder::GifBuilder(double maxAbsXY, const char *outputFile, int iterations) : maxAbsXY(maxAbsXY),
outputFile(outputFile),
iterations(iterations) {
this->writer = new GifWriter();
this->imageSize = 4 * GIF_SIZE * GIF_SIZE; // 4 bytes per pixel (RGBA)
this->frames = new uint8_t[this->imageSize];
#if GIF_GENERATE
GifBegin(this->writer, outputFile, GIF_SIZE, GIF_SIZE, GIF_DELAY);
#endif
}
void GifBuilder::addFrame(const vector<MassPoint *> &points) {
#if GIF_GENERATE
for (int i = 0; i < this->imageSize; ++i) {
this->frames[i] = 0;
}
for (int i = 0; i < points.size(); ++i) {
MassPoint *mp = points[i];
// skip out of view mass points
if (mp->x > maxAbsXY || mp->y > maxAbsXY || mp->x < -maxAbsXY || mp->y < -maxAbsXY) {
continue;
}
auto x = (int) ((((mp->x + maxAbsXY) / maxAbsXY) / 2.0) * GIF_SIZE);
auto y = (int) ((((mp->y + maxAbsXY) / maxAbsXY) / 2.0) * GIF_SIZE);
if (mp->weight > 1e35) {
// sun
for (int j = x - GIF_SUN_DIAMETER; j <= x + GIF_SUN_DIAMETER; ++j) {
for (int k = y - GIF_SUN_DIAMETER; k <= y + GIF_SUN_DIAMETER; ++k) {
if (sqrt((x-j)*(x-j) + (y-k)*(y-k)) <= GIF_SUN_DIAMETER) {
printPoint(j, k);
}
}
}
} else {
printPoint(x, y);
}
}
GifWriteFrame(writer, frames, GIF_SIZE, GIF_SIZE, GIF_DELAY, 8, true);
#endif
}
void GifBuilder::done() {
#if GIF_GENERATE
GifEnd(writer);
#endif
}
void GifBuilder::printPoint(int x, int y) {
auto index = static_cast<uint32_t>(4 * (x + y * GIF_SIZE));
if (index + 3 <= imageSize) {
frames[index] = 255;
frames[index + 1] = 255;
frames[index + 2] = 255;
frames[index + 3] = 0;
}
}