-
Notifications
You must be signed in to change notification settings - Fork 19
/
julia_driver.cpp
316 lines (264 loc) · 8.79 KB
/
julia_driver.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include "julia.hpp"
#include <vector>
#include <iostream>
#include <limits.h>
#include <stdio.h>
#include <assert.h>
#include <algorithm>
#include <utility>
#include <string.h>
#include <chrono>
#include <thread>
#if !(defined(_WIN32) || defined(_WIN64))
#include <unistd.h>
void set_binary_io()
{}
#else
// http://stackoverflow.com/questions/341817/is-there-a-replacement-for-unistd-h-for-windows-visual-c
// http://stackoverflow.com/questions/13198627/using-file-descriptors-in-visual-studio-2010-and-windows
#include <io.h>
#include <fcntl.h>
#define read _read
#define write _write
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
void set_binary_io()
{
_setmode(_fileno(stdin), _O_BINARY);
_setmode(_fileno(stdout), _O_BINARY);
}
#endif
const unsigned greyLevels=9; // Ignore blank
const char *greyChars=".:-=+*#%@";
char toGrey(unsigned v, unsigned maxIter)
{
if(v>=maxIter){
return ' ';
}else{
return greyChars[v%greyLevels];
}
}
complex_t chooseC(float t)
{
return 0.9f*exp(-float(t)*0.2f*complex_t(0,1));
}
void RenderNonProgressive(julia_frame_render_proc_t julia, unsigned width, unsigned height, complex_t z, unsigned maxIter)
{
std::vector<unsigned> buffer(width*height,UINT_MAX);
julia(width, height, z, maxIter, &buffer[0]);
std::stringstream acc;
for(unsigned y=0;y<height;y++){
for(unsigned x=0;x<width;x++){
acc<<toGrey(buffer[y*width+x], maxIter);
}
acc<<"\n";
}
std::cout<<acc.str();
std::cout.flush();
}
void RenderProgressive(julia_frame_render_proc_t julia, unsigned width, unsigned height, complex_t z, unsigned maxIter)
{
std::vector<unsigned> buffer(width*height,UINT_MAX);
std::thread render([&](){
julia(width, height, z, maxIter, &buffer[0]);
});
for(unsigned y=0;y<height;y++){
for(unsigned x=0;x<width;x++){
while(buffer[y*width+x]==UINT_MAX){
std::cout.flush();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
std::cout<<toGrey(buffer[y*width+x], maxIter);
}
std::cout<<std::endl;
}
std::cout.flush();
render.join();
}
void RenderAnimation(julia_frame_render_proc_t julia, unsigned width, unsigned height, unsigned maxIter, unsigned fps, unsigned maxFrames)
{
auto start = std::chrono::system_clock::now();
double dt= fps ? 1.0/fps : 1.0/25;
unsigned frames=0;
double t=0.0;
while(maxFrames==0 || (frames<maxFrames)){
auto now = std::chrono::system_clock::now();
t= fps ? std::chrono::duration<double>(now-start).count() : t+dt;
complex_t c=chooseC(t);
RenderNonProgressive(julia, width, height, c, maxIter);
if(fps){
std::this_thread::sleep_until(now+std::chrono::milliseconds((int)(dt)));
}
frames++;
}
}
void indexToRGB32(uint8_t *rgb, unsigned index, unsigned maxIter)
{
rgb[0]=0;
rgb[1]=0;
rgb[2]=0;
rgb[3]=0;
if(index>=maxIter)
return;
index=index%12;
unsigned color=index%3;
unsigned level=index/3;
rgb[color]=level*64+63; // map to 63, 127, 191, 254
}
void RenderVideo(julia_frame_render_proc_t julia, unsigned width, unsigned height, unsigned maxIter, unsigned fps, unsigned maxFrames)
{
set_binary_io();
auto start = std::chrono::system_clock::now();
double dt= fps==0 ? 1.0/25 : 1.0/fps;
std::vector<unsigned> buffer(width*height,UINT_MAX);
std::vector<uint8_t> pixels(width*height*4);
unsigned frames=0;
double t=0.0;
while(maxFrames==0 || (frames<maxFrames)){
auto now = std::chrono::system_clock::now();
t= fps ? std::chrono::duration<double>(now-start).count() : t+dt;
complex_t c=chooseC(t);
julia(width, height, c, maxIter, &buffer[0]);
for(unsigned i=0; i<width*height; i++){
indexToRGB32(&pixels[4*i], buffer[i], maxIter);
}
int todo=pixels.size();
int done=0;
while(todo>0){
int written=write(STDOUT_FILENO, &pixels[done],todo);
if(written<=0){
fprintf(stderr, "Failed to write all pixels.\n");
exit(1);
}
todo-=written;
done+=written;
}
fprintf(stderr, " Done %d\n", frames);
if(fps){
std::this_thread::sleep_until(now+std::chrono::milliseconds((int)(dt)));
}
frames++;
}
}
void printUsage()
{
std::cout<<"pipe_driver [options]\n";
std::cout<<" -progressive : Show characters as that are rendered\n";
std::cout<<" -non-progressive : Render frame once complete\n";
std::cout<<" -animation : Simulate animated text\n";
std::cout<<" -video : Send raw pixels down stdout (Warning!)\n";
std::cout<<" -engine engineName : Name of engine to use\n";
std::cout<<" -width width : Width in characters or pixels\n";
std::cout<<" -height height : Height in characters or pixels\n";
std::cout<<" -max-iter maxIterations : How deep into image to go\n";
std::cout<<" -fps framesPerSecond : Target frame rate\n";
std::cout<<" -max-frames n : Do this many frames then quit.\n";
std::cout<<"\n";
std::cout<<"Engines:\n";
std::cout<<" reference\n";
std::cout<<" parallel_inner\n";
std::cout<<" parallel_outer\n";
std::cout<<" parallel_both\n";
}
int main(int argc, char *argv[])
{
auto juliaEngine=juliaFrameRender_Reference;
unsigned width=60, height=30;
complex_t z(-0.505f,0.505f);
unsigned maxIter=100000;
unsigned maxFrames=0;
unsigned fps=0;
enum{
Progressive,
NonProgressive,
Animation,
Video
} mode = Progressive;
int ai=1;
while(ai<argc){
if(!strcmp(argv[ai], "-help")){
printUsage();
exit(0);
}else if(!strcmp(argv[ai],"-progressive")){
mode=Progressive;
ai++;
}else if(!strcmp(argv[ai],"-non-progressive")){
mode=NonProgressive;
ai++;
}else if(!strcmp(argv[ai],"-animation")){
mode=Animation;
ai++;
}else if(!strcmp(argv[ai],"-video")){
mode=Video;
ai++;
}else if(!strcmp(argv[ai],"-width")){
if(ai+1 >= argc){
fprintf(stderr, "Missing argument to width.");
exit(1);
}
width=atoi(argv[ai+1]);
ai+=2;
}else if(!strcmp(argv[ai],"-height")){
if(ai+1 >= argc){
fprintf(stderr, "Missing argument to height.");
exit(1);
}
height=atoi(argv[ai+1]);
ai+=2;
}else if(!strcmp(argv[ai],"-max-iter")){
if(ai+1 >= argc){
fprintf(stderr, "Missing argument to max-iter.");
exit(1);
}
maxIter=atoi(argv[ai+1]);
ai+=2;
}else if(!strcmp(argv[ai],"-max-frames")){
if(ai+1 >= argc){
fprintf(stderr, "Missing argument to max-frames.");
exit(1);
}
maxFrames=atoi(argv[ai+1]);
ai+=2;
}else if(!strcmp(argv[ai],"-engine")){
if(ai+1 >= argc){
fprintf(stderr, "Missing argument to engine.");
exit(1);
}
if(!strcmp(argv[ai+1], "reference")){
juliaEngine=juliaFrameRender_Reference;
}else if(!strcmp(argv[ai+1], "parallel_inner")){
juliaEngine=juliaFrameRender_ParallelInner;
}else if(!strcmp(argv[ai+1], "parallel_outer")){
juliaEngine=juliaFrameRender_ParallelOuter;
}else if(!strcmp(argv[ai+1], "parallel_both")){
juliaEngine=juliaFrameRender_ParallelBoth;
}else{
fprintf(stderr, "Uknown argument to -engine : '%s'\n", argv[ai+1]);
exit(1);
}
ai+=2;
}else if(!strcmp(argv[ai],"-fps")){
if(ai+1 >= argc){
fprintf(stderr, "Missing argument to fps.");
exit(1);
}
fps=atoi(argv[ai+1]);
ai+=2;
}else{
fprintf(stderr, "Unknown option %s\n", argv[ai]);
exit(1);
}
}
if(mode==Progressive){
RenderProgressive(juliaEngine, width, height, z, maxIter);
}else if(mode==NonProgressive){
RenderNonProgressive(juliaEngine, width, height, z, maxIter);
}else if(mode==Animation){
RenderAnimation(juliaEngine, width, height, maxIter, fps, maxFrames);
}else if(mode==Video){
RenderVideo(juliaEngine, width, height, maxIter, fps, maxFrames);
}else{
fprintf(stderr, "Error, no mode selected.\n");
}
return 0;
}