-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrainPaint.cpp
More file actions
271 lines (245 loc) · 5.61 KB
/
BrainPaint.cpp
File metadata and controls
271 lines (245 loc) · 5.61 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
// CREDITS: http://www.cplusplus.com/forum/general/6194/
typedef unsigned char byte;
typedef struct
{
byte red, green, blue;
}
RGB_t;
// It is presumed that the image is stored in memory as
// RGB_t data[ height ][ width ]
// where lines are top to bottom and columns are left to right
// (the same way you view the image on the display)
// The routine makes all the appropriate adjustments to match the TGA format specification.
bool write_truecolor_tga(const std::string& filename, RGB_t* data, unsigned width, unsigned height)
{
std::ofstream tgafile(filename.c_str(), std::ios::binary);
if (!tgafile) return false;
// The image header
byte header[18] = { 0 };
header[2] = 1; // truecolor
header[12] = width & 0xFF;
header[13] = (width >> 8) & 0xFF;
header[14] = height & 0xFF;
header[15] = (height >> 8) & 0xFF;
header[16] = 24; // bits per pixel
tgafile.write((const char*)header, 18);
// The image data is stored bottom-to-top, left-to-right
for (int y = height - 1; y >= 0; y--)
for (int x = 0; x < width; x++)
{
tgafile.put((char)data[(y * width) + x].blue);
tgafile.put((char)data[(y * width) + x].green);
tgafile.put((char)data[(y * width) + x].red);
}
// The file footer. This part is totally optional.
static const char footer[26] =
"\0\0\0\0" // no extension area
"\0\0\0\0" // no developer directory
"TRUEVISION-XFILE" // yep, this is a TGA file
".";
tgafile.write(footer, 26);
tgafile.close();
return true;
}
// end Credits
//next byte
void next();
//last byte
void last();
//add one to current byte
void add();
//subtract one from current byte
void substract();
//start a loop
void beginLoop();
//end of a loop, repeat if not ending yet
void endLoop();
//draw a pixel using the next 5 bytes [x,y,r,g,b]
void drawPixel();
//get Input from console
void getInput();
//print current byte char value
void printChar();
//the current position of the byte we want to use
int curByte;
//the bytes we can use
int bytes[1024];
//the size and datot of the image we will generate
RGB_t data[100][100];
//all the starts of the loops
std::vector<int> loopBegins;
//a string which will be filled with all the commands
std::string commands;
//the current position of the command we are executing in the string
int commandIndex;
int main(int argc, char const *argv[])
{
std::cout << "output" << std::endl;
//variable inits
commandIndex = 0;
commands = "";
char c = 0;
for (int i = 0; i < 1024; i++)
{
bytes[i] = 0;
}
for (unsigned int i = 0; i < 100; i++)
{
for (unsigned int j = 0; j < 100; j++)
{
data[i][j].red = 255;
data[i][j].green = 255;
data[i][j].blue = 255;
}
}
//open the file
std::ifstream file(argv[1]);
//return if there is no file present
if (!file.is_open())
{
std::cout << "File not found, outputting hello world!" << std::endl;
commands = ";H $>+<$>+<$>+<$>+<$+>--<$+$+>---<$>+<$>+<$>+<$>+<$>+<$++>----< ;E $>+<$>+<$>+<$>+<$+>----<$+$->++<$+$->++<$+$++>----< ;L $>+<$>+<$>+<$>+<$+$+$+$++>----< ;L $>+<$>+<$>+<$>+<$+$+$+$++>----< ;O $>+<$>+<$>+<$>+<$+$+$+$>-<$>-<$>-<$>-<$-$-$+++++ ;W $>+<$>+<$>+<$>+<+$>-<+$+>+<$+>-<$>-<$>-<$>-<$++ ;o $>+<$>+<$>+<$>+<$+$+$+$>-<$>-<$>-<$>-<$-$-$++++ ;R $>+<$>+<$>+<$>+<$>----<+$+>+<$->+<$+>+<$+>+<$++>----< ;L $>+<$>+<$>+<$>+<$+$+$+$++>----< ;D $>+<$>+<$>+<$>+<$+$+$+>-<$>-<$>-<$>-<-$-$++++ ;! $>+<$>+<$>++<$++++>----<";
}
//add all commands to the commans strings
else
while (file.get(c))
{
switch (c)
{
case '+':
commands += c;
break;
case '-':
commands += c;
break;
case '>':
commands += c;
break;
case '<':
commands += c;
break;
case '[':
commands += c;
break;
case ']':
commands += c;
break;
case ',':
commands += c;
break;
case '.':
commands += c;
break;
case '$':
commands += c;
break;
}
}
//close the file
file.close();
//start executing the commands
for (commandIndex = 0; commandIndex < commands.length(); commandIndex++)
{
switch (commands.at(commandIndex))
{
case '+':
add();
break;
case '-':
substract();
break;
case '>':
next();
break;
case '<':
last();
break;
case '[':
beginLoop();
break;
case ']':
endLoop();
break;
case ',':
getInput();
break;
case '.':
printChar();
break;
case '$':
drawPixel();
break;
}
}
std::cout << "outputting image" << std::endl;
//output the image
write_truecolor_tga("output.tga", (*data), 100, 100);
return 0;
}
void next()
{
if (curByte < 1024)
{
curByte++;
}
}
void last()
{
if (curByte >= 0)
{
curByte--;
}
}
void add()
{
bytes[curByte]++;
}
void substract()
{
bytes[curByte]--;
}
void beginLoop()
{
loopBegins.push_back(commandIndex);
}
void endLoop()
{
//if the current byte is 0 we can leave the loop
if (bytes[curByte] == 0)
{
loopBegins.pop_back();
}
else
{
//if the loop isnt done, repeat
commandIndex = (loopBegins.back());
}
}
void drawPixel()
{
//if the variables get out of range dont try to use them
if (curByte + 4 >= 1024)
return;
int y = bytes[curByte];
int x = bytes[curByte + 1];
int r = bytes[curByte + 2];
int g = bytes[curByte + 3];
int b = bytes[curByte + 4];
data[x][y].red = r;
data[x][y].blue = b;
data[x][y].green = g;
}
void getInput()
{
char c;
c = std::getchar();
bytes[curByte] = (int)c;
}
void printChar()
{
std::cout << (char)bytes[curByte];
}