-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapt-encode.cpp
More file actions
139 lines (132 loc) · 3.54 KB
/
apt-encode.cpp
File metadata and controls
139 lines (132 loc) · 3.54 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
include <math.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Utils
template <typename F, typename T>
constexpr T map(F value, F f1, F t1, T f2, T t2) {
return f2 + ((t2 - f2) * (value - f1)) / (t1 - f1);
}
template <typename T> constexpr T max(T val1, T val2) {
return val1 > val2 ? val1 : val2;
}
// Constants and config
constexpr size_t CARRIER = 2400;
constexpr size_t BAUD = 4160;
constexpr size_t OVERSAMPLE = 3;
// Sync words for the left and right images
constexpr const char *SYNCA = "000011001100110011001100110011000000000";
constexpr const char *SYNCB = "000011100111001110011100111001110011100";
// Image
class Image {
public:
Image(const char *);
void free();
uint8_t getPixel(size_t, size_t) const;
size_t width() const;
size_t height() const;
private:
size_t m_height;
uint8_t *m_pixels;
};
Image::Image(const char *path) {
FILE *f = fopen(path, "r");
size_t maxValue;
size_t width;
// P2
{
char buf[2];
fread(buf, 1, 2, f);
}
fscanf(f, "%zu %zu %zu", &width, &m_height, &maxValue);
m_pixels = (uint8_t *)malloc(width * m_height);
for (size_t i = 0; i < m_height * width; i++) {
fscanf(f, "%hhu", &m_pixels[i]);
}
fclose(f);
}
void Image::free() { std::free(m_pixels); }
size_t Image::width() const { return 909; }
size_t Image::height() const { return m_height; }
uint8_t Image::getPixel(size_t x, size_t y) const {
return m_pixels[y * width() + x];
}
// Audio
void write_value(uint8_t value) {
static double sn = 0;
for (size_t i = 0; i < OVERSAMPLE; i++) {
double samp = sin(CARRIER * 2.0 * M_PI * (sn / (BAUD * OVERSAMPLE)));
samp *= map((int)value, 0, 255, 0.0, 0.7);
uint8_t buf[1];
buf[0] = map(samp, -1.0, 1.0, 0, 255);
fwrite(buf, 1, 1, stdout);
sn++;
}
}
int main(int argc, char **argv) {
// TODO: Improve command line argument parsing
// If there are no arguments, print usage and return.
if (argc < 2) {
printf("Usage: %s ./image1.pgm ./image2.pgm\n", argv[0]);
return 1;
}
// If there are two images, use them as the left and right images
// respectively, otherwise use the same image for both channels.
Image img1(argv[1]);
Image img2(argv[argc < 3 ? 1 : 2]);
auto height = max(img1.height(), img2.height());
for (size_t line = 0; line < height; line++) {
auto frame_line = line % 128;
// Sync A
for (size_t i = 0; i < strlen(SYNCA); i++)
write_value(SYNCA[i] == '0' ? 0 : 255);
// Space A
for (size_t i = 0; i < 47; i++)
write_value(0);
// Image A
for (size_t i = 0; i < 909; i++) {
if (line < img1.height())
write_value(img1.getPixel(i, line));
else
write_value(0);
}
// Telemetry A
for (size_t i = 0; i < 45; i++) {
size_t wedge = frame_line / 8;
auto v = 0;
if (wedge < 8) {
wedge++;
v = (int)(255.0 * ((wedge % 8) / 8.0));
}
write_value(v);
}
// Sync B
for (size_t i = 0; i < strlen(SYNCB); i++)
write_value(SYNCB[i] == '0' ? 0 : 255);
// Space B
for (size_t i = 0; i < 47; i++)
write_value(255);
// Image B
for (size_t i = 0; i < 909; i++) {
if (line < img2.height())
write_value(img2.getPixel(i, line));
else
write_value(0);
}
// Telemetry B
for (size_t i = 0; i < 45; i++) {
size_t wedge = frame_line / 8;
auto v = 0;
if (wedge < 8) {
wedge++;
v = (int)(255.0 * ((wedge % 8) / 8.0));
}
write_value(v);
}
}
img1.free();
img2.free();
return 0;
}