forked from dkogan/horizonator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
standalone.c
377 lines (334 loc) · 11.6 KB
/
standalone.c
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <getopt.h>
#include <string.h>
#include <FreeImage.h>
#include <math.h>
#include "horizonator.h"
#include "util.h"
int main(int argc, char* argv[])
{
const char* usage =
"%s [--width WIDTH_PIXELS] [--height HEIGHT_PIXELS]\n"
" [--image OUT.png] [--ranges RANGES.DAT]\n"
" [--radius RENDER_RADIUS_CELLS]\n"
" [--texture]\n"
" [--allow-tile-downloads]\n"
" [--znear ZNEAR]\n"
" [--zfar ZFAR]\n"
" [--znear-color ZNEARCOLOR]\n"
" [--zfar-color ZFARCOLOR]\n"
" [--dirdems DIRECTORY]\n"
" [--dirtiles DIRECTORY]\n"
" LAT LON AZ_DEG0 AZ_DEG1\n"
"\n"
"By default, we render to a window. If --width is given, we render\n"
"to an image (--image) and/or a binary range table (--ranges) instead.\n"
"--height applies only if --width is given, and is optional; a reasonable\n"
"field-of-view will be assumed if --height is omitted."
"The image filename MUST be a .png file\n"
"\n"
"I load RENDER_RADIUS_CELLS of the DEM in each direction off the center\n"
"point. If --radius is omitted, a reasonable default is chosen\n"
"\n"
"When plotting to a window, AZ_DEG are the azimuth bounds of the\n"
"VIEWPORT. When rendering to an image, AZ_DEG are the\n"
"centers of the first and last pixels. This is slightly smaller\n"
"than the whole viewport: there's one extra pixel on each side\n"
"\n"
"The extents of ranges that we render are given by --zmin and --zmax,\n"
"in meters. Anything closer than --zmin and further out than --zmax will\n"
"not be rendered. The color-coding extents are given by --zmin-color and\n"
"--zmax-color. Anything at or closer than --zmin-color will be rendered\n"
"as black, and anything at or further than --zmax-color will be rendered\n"
"as red. All 4 of these have reasonable defaults, and may be omitted\n"
"\n"
"By default we colorcode the renders by range. If --texture, we\n"
"use a set of image tiles to texture the render instead\n"
"\n"
"The DEMs are in the directory given by --dirdems, or in\n"
"~/.horizonator/DEMs_SRTM3/ if omitted.\n"
"\n"
"The tiles are in the directory given by --dirtiles, or in\n"
"~/.horizonator/tiles if omitted.\n";
struct option opts[] = {
{ "width", required_argument, NULL, 'w' },
{ "height", required_argument, NULL, 'H' },
{ "image", required_argument, NULL, 'i' },
{ "radius", required_argument, NULL, 'R' },
{ "ranges", required_argument, NULL, 'r' },
{ "dirdems", required_argument, NULL, 'd' },
{ "dirtiles", required_argument, NULL, 't' },
{ "texture", no_argument, NULL, 'T' },
{ "allow-tile-downloads",no_argument, NULL, 'a' },
{ "znear", required_argument, NULL, '1' },
{ "zfar", required_argument, NULL, '2' },
{ "znear-color", required_argument, NULL, '3' },
{ "zfar-color", required_argument, NULL, '4' },
{ "help", no_argument, NULL, 'h' },
{}
};
int width = 0;
int height = 0;
const char* filename_image = NULL;
const char* filename_ranges = NULL;
const char* dir_dems = NULL;
const char* dir_tiles = NULL;
bool render_texture = false;
bool allow_downloads = false;
int render_radius_cells = 1000;
float znear = -1.0f;
float zfar = -1.0f;
float znear_color = -1.0f;
float zfar_color = -1.0f;
int opt;
do
{
// "h" means -h does something
opt = getopt_long(argc, argv, "+h", opts, NULL);
switch(opt)
{
case -1:
break;
case 'h':
printf(usage, argv[0]);
return 0;
case 'w':
width = atoi(optarg);
if(width <= 0)
{
fprintf(stderr, "--width must have an integer argument > 0\n");
return 1;
}
break;
case 'H':
height = atoi(optarg);
if(height <= 0)
{
fprintf(stderr, "--height must have an integer argument > 0\n");
return 1;
}
break;
case 'R':
render_radius_cells = atoi(optarg);
if(height <= 0)
{
fprintf(stderr, "--radius must have an integer argument > 0\n");
return 1;
}
break;
case '1':
znear = (float)atof(optarg);
if(znear <= 0.0f)
{
fprintf(stderr, "--znear must have an float argument > 0\n");
return 1;
}
break;
case '2':
zfar = (float)atof(optarg);
if(zfar <= 0.0f)
{
fprintf(stderr, "--zfar must have an float argument > 0\n");
return 1;
}
break;
case '3':
znear_color = (float)atof(optarg);
if(znear_color <= 0.0f)
{
fprintf(stderr, "--znear-color must have an float argument > 0\n");
return 1;
}
break;
case '4':
zfar_color = (float)atof(optarg);
if(zfar_color <= 0.0f)
{
fprintf(stderr, "--zfar-color must have an float argument > 0\n");
return 1;
}
break;
case 'i':
filename_image = optarg;
break;
case 'r':
filename_ranges = optarg;
break;
case 'd':
dir_dems = optarg;
break;
case 't':
dir_tiles = optarg;
break;
case 'T':
render_texture = true;
break;
case 'a':
allow_downloads = true;
break;
case '?':
fprintf(stderr, "Unknown option\n\n");
fprintf(stderr, usage, argv[0]);
return 1;
}
} while( opt != -1 );
int Nargs_remaining = argc-optind;
if( Nargs_remaining != 4 )
{
fprintf(stderr, "Need exactly 4 non-option arguments. Got %d\n\n",Nargs_remaining);
fprintf(stderr, usage, argv[0]);
return 1;
}
if(width > 0 && !(filename_image != NULL || filename_ranges != NULL))
{
fprintf(stderr, "--width makes sense only with (--image or --ranges)\n\n");
fprintf(stderr, usage, argv[0]);
return 1;
}
if(width <= 0 && (filename_image != NULL || filename_ranges != NULL))
{
fprintf(stderr, "--width required if (--image or --ranges)\n\n");
fprintf(stderr, usage, argv[0]);
return 1;
}
if( height > 0 && width <= 0 )
{
fprintf(stderr, "--height makes sense only with --width\n\n");
fprintf(stderr, usage, argv[0]);
return 1;
}
if(filename_image != NULL)
{
int l = strlen(filename_image);
if(l < 5 || 0 != strcasecmp(".png", &filename_image[l-4]))
{
fprintf(stderr, "--image MUST be given a '.png' filename\n\n");
fprintf(stderr, usage, argv[0]);
return 1;
}
}
float lat = (float)atof(argv[optind+0]);
float lon = (float)atof(argv[optind+1]);
float az_deg0 = (float)atof(argv[optind+2]);
float az_deg1 = (float)atof(argv[optind+3]);
if( lat < -80.f || lat > 80.f )
{
fprintf(stderr, "Got invalid latitude");
return false;
}
if( lon < -180.f || lon > 180.f )
{
fprintf(stderr, "Got invalid longitude");
return false;
}
if(az_deg0 > az_deg1)
{
fprintf(stderr, "MUST have az_deg0 < az_deg1\n");
return 1;
}
if(filename_image == NULL && filename_ranges == NULL)
{
horizonator_allinone_glut_loop(render_texture,
lat, lon, az_deg0, az_deg1,
render_radius_cells,
znear,zfar,znear_color,zfar_color,
dir_dems, dir_tiles,
allow_downloads);
return 0;
}
// The user gave me az_deg referring to the center of the pixels at the
// edge. I need to convert them to represent the edges of the viewport.
// That's 0.5 pixels extra on either side
float az_per_pixel = (az_deg1 - az_deg0) / (float)(width-1);
az_deg0 -= az_per_pixel/2.f;
az_deg1 += az_per_pixel/2.f;
if(height <= 0)
{
// Assume a 20deg fov if no height requested
const float fovy_deg = 20.0f;
height = (int)roundf( (float)width * fovy_deg / (az_deg1-az_deg0));
}
char* image = NULL;
float* ranges = NULL;
if(filename_image != NULL)
{
image = malloc( width*height * 3 );
if(image == NULL)
{
MSG("image buffer malloc() failed");
return 1;
}
}
if(filename_ranges != NULL)
{
ranges = malloc( width*height * sizeof(float) );
if(ranges == NULL)
{
MSG("ranges buffer malloc() failed");
return 1;
}
}
horizonator_context_t ctx;
if( !horizonator_init( &ctx,
lat, lon,
width, height,
render_radius_cells,
true,
render_texture,
dir_dems,
dir_tiles,
allow_downloads) )
{
fprintf(stderr, "horizonator_init() failed\n");
return false;
}
if(!horizonator_set_zextents(&ctx,
znear, zfar, znear_color, zfar_color))
return false;
if(!horizonator_pan_zoom( &ctx, az_deg0, az_deg1))
{
fprintf(stderr, "horizonator_pan_zoom() failed");
return false;
}
if(!horizonator_render_offscreen(&ctx, image, ranges))
{
fprintf(stderr, "render failed\n");
return 1;
}
if(filename_image != NULL)
{
FreeImage_Initialise(true);
FIBITMAP* fib = FreeImage_ConvertFromRawBits((BYTE*)image, width, height,
3*width, 24,
0,0,0,
// Top row is stored first
true);
if(!FreeImage_Save(FIF_PNG, fib, filename_image, 0))
{
fprintf(stderr, "Couldn't save to '%s'\n", filename_image);
return 1;
}
FreeImage_Unload(fib);
FreeImage_DeInitialise();
free(image);
}
if(filename_ranges != NULL)
{
FILE* fp = fopen(filename_ranges, "w");
if(fp == NULL)
{
MSG("Cannot open ranges image at '%s'. Giving up", filename_ranges);
return 1;
}
if((unsigned int)(width*height) != fwrite(ranges,
sizeof(float), width*height, fp))
{
MSG("Failed to write complete 'ranges' data");
return 1;
}
fclose(fp);
}
return 0;
}