-
Notifications
You must be signed in to change notification settings - Fork 21
/
inc.cpp
136 lines (104 loc) · 2.67 KB
/
inc.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
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
#include <stdint.h>
extern "C" void console_log(uint32_t value);
extern uint8_t memory;
static void print(uint32_t value) {
console_log(value);
}
static uint32_t dec(uint32_t x) {
return x - 1;
}
extern "C" uint32_t inc(uint32_t x) {
return dec(x) + 2;
}
extern "C" void incmem() {
uint8_t* ptr = &memory;
ptr[0] += 1;
ptr[1] += 10;
ptr[2] += 100;
}
extern "C" void gray(int width, int height) {
uint8_t* ptr = &memory;
for (int i = 0; i < width * height * 4; i += 4) {
uint32_t gray = 0.299 * ptr[i] + 0.587 * ptr[i+1] + 0.114 * ptr[i+2];
ptr[i] = gray;
ptr[i+1] = gray;
ptr[i+2] = gray;
}
}
extern "C" void swaprg(int width, int height) {
uint8_t* ptr = &memory;
for (int i = 0; i < width * height * 4; i += 4) {
uint32_t r = ptr[i];
ptr[i] = ptr[i+1];
ptr[i+1] = r;
}
}
extern "C" void swap_red(int width, int height, int color_mask) {
uint8_t* ptr = &memory;
for (int i = 0; i < width * height * 4; i += 4) {
uint32_t r = ptr[i];
if ((color_mask & 0x04) == 0) {
if (color_mask & 0x02) {
ptr[i] = ptr[i + 1];
} else {
ptr[i] = ptr[i + 2];
}
}
if (color_mask & 0x02) ptr[i + 1] = r;
if (color_mask & 0x01) ptr[i + 2] = r;
}
}
extern "C" void blur(int width, int height) {
uint8_t* tgt = &memory;
uint8_t* src = tgt + (width * height * 4);
for (int i = 0; i < (width * height * 4); i++) {
src[i] = tgt[i];
}
for (int row = 1; row < height - 1; row++) {
for (int col = 1; col < width; col++) {
int r = 0;
int g = 0;
int b = 0;
int i = (((row + 0) * width) + (col + 0)) * 4;
r += 4 * src[i + 0];
g += 4 * src[i + 1];
b += 4 * src[i + 2];
i = (((row + 1) * width) + (col + 0)) * 4;
r += 2 * src[i + 0];
g += 2 * src[i + 1];
b += 2 * src[i + 2];
i = (((row - 1) * width) + (col + 0)) * 4;
r += 2 * src[i + 0];
g += 2 * src[i + 1];
b += 2 * src[i + 2];
i = (((row + 0) * width) + (col + 1)) * 4;
r += 2 * src[i + 0];
g += 2 * src[i + 1];
b += 2 * src[i + 2];
i = (((row + 0) * width) + (col - 1)) * 4;
r += 2 * src[i + 0];
g += 2 * src[i + 1];
b += 2 * src[i + 2];
i = (((row - 1) * width) + (col - 1)) * 4;
r += 1 * src[i + 0];
g += 1 * src[i + 1];
b += 1 * src[i + 2];
i = (((row + 1) * width) + (col - 1)) * 4;
r += 1 * src[i + 0];
g += 1 * src[i + 1];
b += 1 * src[i + 2];
i = (((row - 1) * width) + (col + 1)) * 4;
r += 1 * src[i + 0];
g += 1 * src[i + 1];
b += 1 * src[i + 2];
i = (((row + 1) * width) + (col + 1)) * 4;
r += 1 * src[i + 0];
g += 1 * src[i + 1];
b += 1 * src[i + 2];
i = (((row + 0) * width) + (col + 0)) * 4;
tgt[i + 0] = r / 16;
tgt[i + 1] = g / 16;
tgt[i + 2] = b / 16;
}
}
}