-
Notifications
You must be signed in to change notification settings - Fork 0
/
envutil.cc
1619 lines (1355 loc) · 52.6 KB
/
envutil.cc
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************/
/* */
/* utility to convert and extract images from 360 degree environments */
/* */
/* Copyright 2024 by Kay F. Jahnke */
/* */
/* The git repository for this software is at */
/* */
/* https://github.com/kfjahnke/envutil */
/* */
/* Please direct questions, bug reports, and contributions to */
/* */
/* [email protected] */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files (the "Software"), to deal in the Software without */
/* restriction, including without limitation the rights to use, */
/* copy, modify, merge, publish, distribute, sublicense, and/or */
/* sell copies of the Software, and to permit persons to whom the */
/* Software is furnished to do so, subject to the following */
/* conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the */
/* Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
/* OTHER DEALINGS IN THE SOFTWARE. */
/* */
/************************************************************************/
// A utility to extract an image from an environment. This program takes
// a 2:1 lat/lon environment or a 1:6 cubemap image as input and produces
// output in the specified orientation, projection, field of view and
// extent. For CL arguments, try 'envutil --help'. The program can also
// create environment images - just pass 'spherical' or 'cubemap' as
// output projection, 360 or 90 degrees hfov, respectively, and an
// appropriate output size. This ability can be used to convert from
// one environment to another, optionally with an arbitrary 3D rotation.
//
// The output projection can be one of "spherical", "cylindrical",
// "rectilinear", "stereographic", "fisheye" or "cubemap". The geometrical
// extent of the output is set up most conveniently by passing --hfov, the
// horizontal field of view of the output. The x0, x1, y0, and y1 parameters
// allow passing specific extent values (in model space units), which should
// rarely be necessary. To specify the orientation of the 'virtual camera',
// pass Euler angles yaw, pitch and roll - they default to zero: a view
// 'straight ahead' to the point corresponding to the center of the
// environment image with no camera roll. The size of the output is
// given by --width and --height. You must pass an output filename
// with --output; --input specifies the environment image.
//
// You can choose several different interpolation methods with the --itp
// cammand line argument. The default is --itp 1, which uses bilinear
// interpolation. This is fast and often good enough, especially if there
// are no great scale changes involved - so, if the output's resolution is
// similar to the input's. --itp -1 employs OpenImageIO (OIIO for short)
// for interpolation. Without further parameters, OIIO's default mode is
// used, which uses sophisticated, but slow methods to produce the output.
// All of OIIO's interpolation, mip-mapping and wrapping modes can be
// selected by using the relevant additional parameters. Finally, --itp -2
// uses 'twining' - inlined oversampling with subsequent weighted pixel
// binning. The default with this method is to use a simple box filter
// whose specific parameterization is set up automatically. Additional
// parameters can change the amount of oversampling and add gaussian
// weights to the filter parameters. 'twining' is quite fast (if the number
// of filter taps isn't very large. When down-scaling, the parameter
// 'twine' should be at least the same as the scaling factor to avoid
// aliasing. When upscaling, larger twining values will slighly soften
// the output and suppress the star-shaped artifacts typical for bilinear
// interpolation. Twining is new and this is a first approach. The method
// is intrinsically very flexible (it's based on a generalization of
// convolution), and the full flexibility isn't accessible in 'extract'
// with the parameterization as it stands now, but it's already quite
// useful with the few parameters I offer.
//
// The program uses zimt as it's 'strip-mining' and SIMD back-end, and
// sets up the pixel pipelines using zimt's functional composition tools.
// This allows for terse programming, and the use of a functional
// paradigm allows for many features to be freely combined - a property
// which is sometimes called 'orthogonality'. What you can't combine in
// 'extract' is twining and interpolation with OIIO - this is pointless,
// because OIIO offers all the anti-aliasing and quality interpolation
// one might want, and using twining on top would not improve the
// output. Currently, the build is set up to produce binary for AVX2-
// -capable CPUs - nowadays most 'better' CPUs support this SIMD ISA.
// When building for other (and non-i86) CPUs, suitable parameters should
// be passed to the compiler (you'll have to modify the CMakeLists.txt).
// I strongly suggest you install highway on your system - the build
// will detect and use it to good effect. This is a build-time dependency
// only. Next-best (when using i86 CPUs up to AVX2) is Vc, the fall-back
// is to use std::simd, and even that can be turned off if you want to
// rely on autovectorization; zimt structures the processing so that it's
// autovectorization-friendly and performance is still quite good that way.
// I have managed to build envutil on Linux, macOS (on intel CPUs) and
// Windows. The build adapts to the given system and expects a set of
// dependencies (OpenImageIO, Imath, ffmpeg), zimt code is provided in
// this repository. The macOS build fulfilled the dependencies with
// macPorts, the Windows build used msys2/mingw64.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>
} ;
#include <fstream>
#include "stepper.h"
// To conveniently rotate with a rotational quaternion, we employ
// Imath's 'Quat' data type, packaged in a zimt::unary_functor.
// This is not factored out because it requires inclusion of
// some Imath headers, which I want to keep out of the other
// code, e.g. in geometry.h, where it would fit in nicely.
#include <Imath/ImathVec.h>
#include <Imath/ImathEuler.h>
#include <Imath/ImathQuat.h>
#include <Imath/ImathLine.h>
// rotate_3d uses a SIMDized Imath Quaternion to affect a 3D rotation
// of a 3D SIMDized coordinate. Imath::Quat<float> can't broadcast
// to handle SIMDized input, but if we use an Imath::Quat of the
// SIMDized type, we get the desired effect.
template < typename T , std::size_t L >
struct rotate_3d
: public zimt::unary_functor
< zimt::xel_t < T , 3 > , zimt::xel_t < T , 3 > , L >
{
typedef zimt::simdized_type < T , L > f_v ;
typedef zimt::xel_t < T , 3 > crd3_t ;
typedef zimt::simdized_type < crd3_t , L > crd3_v ;
Imath::Quat < T > q ;
rotate_3d ( T roll , T pitch , T yaw , bool inverse = false )
{
// set up the rotational quaternion. if 'inverse' is set, produce
// the conjugate.
if ( inverse )
{
Imath::Eulerf angles ( -yaw , -pitch , -roll , Imath::Eulerf::YXZ ) ;
q = angles.toQuat() ;
}
else
{
Imath::Eulerf angles ( roll , pitch , yaw , Imath::Eulerf::ZXY ) ;
q = angles.toQuat() ;
}
}
// eval applies the quaternion, reinterpret-casting the zimt 'xel'
// data we use throughoutthe program to Imath::Vec3 - both types
// are compatible.
template < typename U >
void eval ( const zimt::xel_t < U , 3 > & in ,
zimt::xel_t < U , 3 > & out ) const
{
auto const & in_e
= reinterpret_cast < const Imath::Vec3 < U > & > ( in ) ;
auto & out_e
= reinterpret_cast < Imath::Vec3 < U > & > ( out ) ;
out_e = in_e * Imath::Quat < U > ( q ) ;
}
// for convenience:
template < typename U >
zimt::xel_t < U , 3 > operator() ( const zimt::xel_t < U , 3 > & in )
{
zimt::xel_t < U , 3 > out ;
eval ( in , out ) ;
return out ;
}
} ;
// for image I/O, we use OpenImageIO.
#include <OpenImageIO/imageio.h>
using OIIO::ImageInput ;
using OIIO::ImageOutput ;
using OIIO::TypeDesc ;
using OIIO::ImageSpec ;
// struct image_series holds a format string for a series of numbered
// images. It's a standard printf-type format string containing precisely
// one %-sequence accepting an integer - e.g. %04d
// It has operator[] to provide the filename fixed with the given index.
struct image_series
{
std::string format_string ;
std::size_t buffer_size ;
image_series ( const std::string & _format_string )
: format_string ( _format_string )
{
// scan the format string for percent signs. Only one of them
// is allowed.
int percent_count = 0 ;
int percent_pos = 0 ;
int pos = 0 ;
for ( auto const & c : format_string )
{
if ( c == '%' )
{
percent_count++ ;
percent_pos = pos ;
}
++pos ;
}
if ( percent_count == 1 )
buffer_size = pos + 16 ;
}
bool valid()
{
return ( buffer_size != 0 ) ;
}
std::string operator[] ( const std::size_t & index )
{
if ( buffer_size )
{
char buffer [ buffer_size ] ;
snprintf ( buffer , buffer_size ,
format_string.c_str() , int(index) ) ;
return buffer ;
}
else
{
return format_string ;
}
}
} ;
// similar class, used for six cube faces
struct cubeface_series
{
std::string format_string ;
std::vector < std::string > filename ;
std::size_t buffer_size ;
cubeface_series() = default ;
cubeface_series ( const std::string & _format_string )
: format_string ( _format_string )
{
// scan the format string for percent signs. Only one of them
// is allowed.
int percent_count = 0 ;
int percent_pos = 0 ;
int pos = 0 ;
for ( auto const & c : format_string )
{
if ( c == '%' )
{
percent_count++ ;
percent_pos = pos ;
}
++pos ;
}
buffer_size = 0 ;
if ( percent_count == 1 )
{
buffer_size = pos + 16 ;
char buffer [ buffer_size ] ;
snprintf ( buffer , buffer_size , format_string.c_str() , "left" ) ;
filename.push_back ( buffer ) ;
snprintf ( buffer , buffer_size , format_string.c_str() , "right" ) ;
filename.push_back ( buffer ) ;
snprintf ( buffer , buffer_size , format_string.c_str() , "top" ) ;
filename.push_back ( buffer ) ;
snprintf ( buffer , buffer_size , format_string.c_str() , "bottom" ) ;
filename.push_back ( buffer ) ;
snprintf ( buffer , buffer_size , format_string.c_str() , "front" ) ;
filename.push_back ( buffer ) ;
snprintf ( buffer , buffer_size , format_string.c_str() , "back" ) ;
filename.push_back ( buffer ) ;
}
}
bool valid()
{
return filename.size() == 6 ;
}
// we provide two operator[] overloads. The first one accesses the
// cubeface filename by number, the second inserts a given orientation
// string.
std::string operator[] ( const std::size_t & index )
{
assert ( buffer_size != 0 && index < 6 ) ;
return filename [ index ] ;
}
std::string operator[] ( const std::string & face )
{
char buffer [ buffer_size ] ;
snprintf ( buffer , buffer_size ,
format_string.c_str() ,
face.c_str() ) ;
return buffer ;
}
// return a reference to the set of six names
const std::vector < std::string > & get_filenames()
{
return filename ;
}
} ;
// geometry.h has the coordinate transformations needed when
// dealing with environment images, especially cubemaps.
#include "geometry.h"
// a large part of the code in this file is dedicated to processing
// command line arguments. We use OpenImageIO's ArgParse object, which
// is similar to python's argparse. The result of gleaning the arguments
// is held in member variables of the 'args' object. If any command line
// arguments aren't acceptable, the program will terminate with an
// exception. Beyond simply parsing the arguments, the code in this
// object does some calculations to process the arguments further and
// provide more palatable values to the program. Note: angles are passed
// in degrees, but internally, only radians are used. The conversion is
// done right after the parameter acquisition.
#include <regex>
#include <OpenImageIO/filesystem.h>
#include <OpenImageIO/argparse.h>
using OIIO::ArgParse ;
using OIIO::Filesystem::convert_native_arguments ;
struct arguments
{
bool verbose ;
std::string input ;
std::string output ;
std::string mount_image ;
double hfov ;
float mount_hfov ;
std::size_t width ;
std::size_t height ;
std::size_t mount_width ;
std::size_t mount_height ;
std::string prj_str ;
std::string mount_prj_str ;
projection_t projection ;
projection_t mount_prj;
double cbmfov ;
std::size_t support_min ;
std::size_t tile_size ;
bool ctc ;
double yaw , pitch , roll ;
double x0 , x1 , y0 , y1 ;
std::string seqfile ;
std::string codec ;
float mbps ;
int fps ;
int itp ;
int twine ;
std::string twf_file ;
bool twine_normalize ;
bool twine_precise ;
double twine_width , twine_density , twine_sigma , twine_threshold ;
std::string swrap, twrap, mip, interp , tsoptions ;
float stwidth , stblur ;
bool conservative_filter ;
// gleaned from other parameters or input images
bool multiple_input ;
cubeface_series cfs ;
projection_t env_projection ;
double step , env_step ;
std::size_t env_width , env_height ;
std::size_t nchannels ;
std::unique_ptr<ImageInput> inp ;
std::unique_ptr<ImageInput> mount_inp ;
// technical variables for the argument parser
std::string metamatch ;
std::regex field_re ;
// the 'arguments' object's 'init' takes the main program's argc
// and argv.
void init ( int argc , const char ** argv )
{
// we're using OIIO's argparse, since we're using OIIO anyway.
// This is a convenient way to glean arguments on all supported
// platforms - getopt isn't available everywhere.
convert_native_arguments(argc, (const char**)argv);
ArgParse ap;
ap.intro("envutil: convert and create extracts from environment images\n")
.usage("envutil [options] --input INPUT --output OUTPUT");
ap.arg("-v", &verbose)
.help("Verbose output");
// the options are grouped thematically
// mandatory options
ap.separator(" mandatory options:");
ap.arg("--input INPUT")
.help("input file name (mandatory)")
.metavar("INPUT");
ap.arg("--output OUTPUT")
.help("output file name (mandatory)")
.metavar("OUTPUT");
// important options which have defaults
ap.separator(" important options which have defaults:");
ap.arg("--projection PRJ")
.help("projection used for the output image(s) (default: rectilinear)")
.metavar("PRJ");
ap.arg("--hfov ANGLE")
.help("horiziontal field of view of the output (default: 90)")
.metavar("ANGLE");
ap.arg("--width EXTENT")
.help("width of the output (default: 1024)")
.metavar("EXTENT");
ap.arg("--height EXTENT")
.help("height of the output (default: same as width)")
.metavar("EXTENT");
// additional input parameters for cubemap input
ap.separator(" additional input parameters for cubemap input:");
ap.arg("--cbmfov ANGLE")
.help("horiziontal field of view of cubemap input (default: 90)")
.metavar("ANGLE");
ap.arg("--support_min EXTENT")
.help("minimal additional support around the cube face proper")
.metavar("EXTENT");
ap.arg("--tile_size EXTENT")
.help("tile size for the internal representation image")
.metavar("EXTENT");
ap.arg("--ctc CTC")
.help("pass '1' to interpret cbmfov as center-to-center (default 0)")
.metavar("CTC");
// parameters for single-image output
ap.separator(" additional parameters for single-image output:");
ap.arg("--yaw ANGLE")
.help("yaw of the virtual camera")
.metavar("ANGLE");
ap.arg("--pitch ANGLE")
.help("pitch of the virtual camera")
.metavar("ANGLE");
ap.arg("--roll ANGLE")
.help("roll of the virtual camera")
.metavar("ANGLE");
ap.arg("--x0 EXTENT")
.help("low end of the horizontal range")
.metavar("EXTENT");
ap.arg("--x1 EXTENT")
.help("high end of the horizontal range")
.metavar("EXTENT");
ap.arg("--y0 EXTENT")
.help("low end of the vertical range")
.metavar("EXTENT");
ap.arg("--y1 EXTENT")
.help("high end of the vertical range")
.metavar("EXTENT");
// parameters for multi-image and video output
ap.separator(" additional parameters for multi-image and video output:");
ap.arg("--seqfile SEQFILE")
.help("image sequence file name (optional)")
.metavar("SEQFILE");
ap.arg("--codec CODEC")
.help("video codec for video sequence output (default: libx265)")
.metavar("CODEC");
ap.arg("--mbps MBPS")
.help("output video with MBPS Mbit/sec (default: 8)")
.metavar("MBPS");
ap.arg("--fps FPS")
.help("output video FPS frames/sec (default: 60)")
.metavar("FPS");
// interpolation options
ap.separator(" interpolation options:");
ap.arg("--itp ITP")
.help("interpolator: 1 for bilinear, -1 for OIIO, -2 bilinear+twining")
.metavar("ITP");
// parameters for twining (with --itp -2)
ap.separator(" parameters for twining (with --itp -2):");
ap.arg("--twine TWINE")
.help("use twine*twine oversampling - default: automatic settings")
.metavar("TWINE");
ap.arg("--twf_file TWF_FILE")
.help("read twining filter kernel from TWF_FILE")
.metavar("TWF_FILE");
ap.arg("--twine_normalize", &twine_normalize)
.help("normalize twining filter weights gleaned from a file");
ap.arg("--twine_precise", &twine_precise)
.help("project twining basis vectors to tangent plane");
ap.arg("--twine_width WIDTH")
.help("widen the pick-up area of the twining filter")
.metavar("WIDTH");
ap.arg("--twine_density DENSITY")
.help("increase tap count of an 'automatic' twining filter")
.metavar("DENSITY");
ap.arg("--twine_sigma SIGMA")
.help("use a truncated gaussian for the twining filter (default: don't)")
.metavar("SIGMA");
ap.arg("--twine_threshold THR")
.help("discard twining filter taps below this threshold")
.metavar("THR");
// parameters for lookup with OpenImageIO (with --itp -1)
ap.separator(" parameters for lookup with OpenImageIO (with --itp -1):");
ap.arg("--tsoptions KVLIST")
.help("OIIO TextureSystem Options: coma-separated key=value pairs")
.metavar("KVLIST");
ap.arg("--swrap WRAP")
.help("OIIO Texture System swrap mode")
.metavar("WRAP");
ap.arg("--twrap WRAP")
.help("OIIO Texture System twrap mode")
.metavar("WRAP");
ap.arg("--mip MIP")
.help("OIIO Texture System mip mode")
.metavar("MIP");
ap.arg("--interp INTERP")
.help("OIIO Texture System interp mode")
.metavar("INTERP");
ap.arg("--stwidth EXTENT")
.help("swidth and twidth OIIO Texture Options")
.metavar("EXTENT");
ap.arg("--stblur EXTENT")
.help("sblur and tblur OIIO Texture Options")
.metavar("EXTENT");
ap.arg("--conservative YESNO")
.help("OIIO conservative_filter Texture Option - pass 0 or 1")
.metavar("YESNO");
ap.separator(" parameters for mounted image input:");
// std::string mount_image , mount_prj ;
// float mount_hfov ;
ap.add_argument("--mount %s:IMAGE %s:PROJECTION %f:HFOV",
&mount_image , &mount_prj_str, &mount_hfov)
.help("load non-environment source image") ;
if (ap.parse(argc, argv) < 0 ) {
std::cerr << ap.geterror() << std::endl;
ap.print_help();
assert ( false ) ;
}
if (!metamatch.empty()) {
field_re.assign(metamatch, std::regex_constants::extended
| std::regex_constants::icase);
}
// extract the arguments from the argparser, parse the projection
input = ap["input"].as_string ( "" ) ;
output = ap["output"].as_string ( "" ) ;
seqfile = ap["seqfile"].as_string ( "" ) ;
twf_file = ap["twf_file"].as_string ( "" ) ;
codec = ap["codec"].as_string ( "libx265" ) ;
mbps = ( 1000000.0 * ap["mbps"].get<float> ( 8.0 ) ) ;
fps = ap["fps"].get<int>(60);
itp = ap["itp"].get<int>(1);
twine = ap["twine"].get<int>(0);
twine_width = ap["twine_width"].get<float>(1.0);
twine_density = ap["twine_density"].get<float>(1.0);
twine_sigma = ap["twine_sigma"].get<float>(0.0);
twine_threshold = ap["twine_threshold"].get<float>(0.0);
swrap = ap["swrap"].as_string ( "WrapDefault" ) ;
twrap = ap["twrap"].as_string ( "WrapDefault" ) ;
mip = ap["mip"].as_string ( "MipModeDefault" ) ;
interp = ap["interp"].as_string ( "InterpSmartBicubic" ) ;
tsoptions = ap["tsoptions"].as_string ( "automip=1" ) ;
conservative_filter = ap["conservative"].get<int>(1) ;
x0 = ap["x0"].get<float> ( 0.0 ) ;
x1 = ap["x1"].get<float> ( 0.0 ) ;
y0 = ap["y0"].get<float> ( 0.0 ) ;
y1 = ap["y1"].get<float> ( 0.0 ) ;
width = ap["width"].get<int> ( 0 ) ;
stwidth = ap["stwidth"].get<float> ( 1 ) ;
stblur = ap["stblur"].get<float> ( 0 ) ;
height = ap["height"].get<int> ( 0 ) ;
hfov = ap["hfov"].get<float>(90.0);
cbmfov = ap["cbmfov"].get<float>(90.0);
ctc = ap["ctc"].get<int>(0);
tile_size = ap["tile_size"].get<int> ( 64 ) ;
support_min = ap["support_min"].get<int> ( 8 ) ;
if ( hfov != 0.0 )
x0 = x1 = y0 = y1 = 0 ;
yaw = ap["yaw"].get<float>(0.0);
pitch = ap["pitch"].get<float>(0.0);
roll = ap["roll"].get<float>(0.0);
prj_str = ap["projection"].as_string ( "rectilinear" ) ;
int prj = 0 ;
for ( const auto & p : projection_name )
{
if ( p == prj_str )
break ;
++ prj ;
}
projection = projection_t ( prj ) ;
if ( mount_image != std::string() )
{
prj = 0 ;
for ( const auto & p : projection_name )
{
if ( p == mount_prj_str )
break ;
++ prj ;
}
mount_prj = projection_t ( prj ) ;
assert ( input == std::string() ) ;
}
else
{
assert ( input != std::string() ) ;
}
assert ( output != std::string() ) ;
if ( width == 0 )
width = 1024 ;
if ( projection == CUBEMAP )
{
height = 6 * width ;
assert ( hfov >= 90.0 ) ;
}
if ( projection == SPHERICAL && height == 0 )
{
if ( width & 1 )
++width ;
height = width / 2 ;
}
if ( height == 0 )
height = width ;
cbmfov *= M_PI / 180.0 ;
if ( mount_image != std::string() )
{
std::cout << "create env from mount image " << mount_image << std::endl ;
mount_inp = ImageInput::open ( mount_image ) ;
assert ( mount_inp ) ;
const ImageSpec &spec = mount_inp->spec() ;
mount_width = spec.width ;
mount_height = spec.height ;
nchannels = spec.nchannels ;
mount_hfov *= M_PI / 180.0 ;
switch ( mount_prj )
{
case SPHERICAL:
case FISHEYE:
case CYLINDRICAL:
env_step = mount_hfov / mount_width ;
break ;
case RECTILINEAR:
env_step = 2.0 * tan ( mount_hfov / 2.0 ) / mount_width ;
break ;
case STEREOGRAPHIC:
env_step = 4.0 * tan ( mount_hfov / 4.0 ) / mount_width ;
break ;
default:
break ;
}
}
else
{
// some member variables in the args object are gleaned from
// the input image.
// first we check for percent signs in the input filename. If
// we find one, we assume that the input is a cubemap consisting
// of six separate images following a naming scheme described by
// the string in 'input' which is treated as a format string.
multiple_input = false ;
auto has_percent = input.find_first_of ( "%" ) ;
if ( has_percent != std::string::npos )
{
// input must be a set of six cubeface images, that's the
// only way how we accept a format string.
cfs = cubeface_series ( input ) ;
multiple_input = cfs.valid() ;
if ( multiple_input )
{
// let's open the first cube face to extract the metrics.
// the cubemap's 'load' routine will check all images in
// turn, so we needn't do that here.
inp = ImageInput::open ( cfs[0] ) ;
assert ( inp ) ;
const ImageSpec &spec = inp->spec() ;
assert ( spec.width == spec.height ) ;
env_width = spec.width ;
env_height = spec.height * 6 ;
nchannels = spec.nchannels ;
env_projection = CUBEMAP ;
if ( ctc )
{
double half_md = tan ( cbmfov / 2.0 ) ;
half_md *= ( ( env_width + 1.0 ) / env_width ) ;
cbmfov = atan ( half_md ) * 2.0 ;
if ( verbose )
std::cout << "ctc is set, adjusted cbmfov to "
<< ( cbmfov * 180.0 / M_PI ) << std::endl ;
}
env_step = cbmfov / env_width ;
}
}
if ( ! multiple_input )
{
// we have a single image as input.
inp = ImageInput::open ( input ) ;
assert ( inp ) ;
const ImageSpec &spec = inp->spec() ;
env_width = spec.width ;
env_height = spec.height ;
nchannels = spec.nchannels ;
assert ( env_width == env_height * 2
|| env_height == env_width * 6 ) ;
if ( env_width == env_height * 2 )
{
env_projection = SPHERICAL ;
env_step = 2.0 * M_PI / env_width ;
}
else if ( env_width * 6 == env_height )
{
if ( ctc )
{
double half_md = tan ( cbmfov / 2.0 ) ;
half_md *= ( ( env_width + 1.0 ) / env_width ) ;
cbmfov = atan ( half_md ) * 2.0 ;
if ( verbose )
std::cout << "ctc is set, adjusted cbmfov to "
<< ( cbmfov * 180.0 / M_PI ) << std::endl ;
}
env_projection = CUBEMAP ;
env_step = cbmfov / env_width ;
}
else
{
std::cerr << "input image must have 2:1 or 1:6 aspect ratio"
<< std::endl ;
exit ( -1 ) ;
}
}
if ( verbose )
{
std::cout << "input: " << input << std::endl ;
std::cout << "input width: " << env_width << std::endl ;
std::cout << "input height: " << env_height << std::endl ;
std::cout << "input has " << nchannels << " channels" << std::endl ;
std::cout << "env_step: " << env_step << std::endl ;
std::cout << "interpolation: "
<< ( itp == 1 ? "direct bilinear" : "uses OIIO" )
<< std::endl ;
std::cout << "output width: " << width
<< " height: " << height << std::endl ;
}
}
if ( seqfile == std::string() )
{
// single-image output (no sequence file given)
// if there is a sequence file, the variables which are set
// here will be set for every frame specified in the sequence,
// and single-image parameters from the CL have no effect.
if ( verbose)
{
std::cout << "output: " << output << std::endl ;
std::cout << "output projection: " << prj_str << std::endl ;
if ( hfov > 0.0 )
std::cout << "output hfov: " << hfov << std::endl ;
std::cout << "virtual camera yaw: " << yaw
<< " pitch: " << pitch
<< " roll: " << roll << std::endl ;
}
// convert angles to radians
hfov *= M_PI / 180.0 ;
yaw *= M_PI / 180.0 ;
pitch *= M_PI / 180.0 ;
roll *= M_PI / 180.0 ;
if ( ( projection == CUBEMAP ) && ctc )
{
double half_md = tan ( hfov / 2.0 ) ;
half_md *= ( ( width + 1.0 ) / width ) ;
hfov = atan ( half_md ) * 2.0 ;
if ( verbose )
std::cout << "cubemap output: ctc is set, adjusted hfov to "
<< ( hfov * 180.0 / M_PI ) << std::endl ;
}
// calculate extent - a non-zero hfov overrides x0, x1, y0, and y1
step = 0.0 ;
if ( hfov != 0.0 )
{
auto extent = get_extent ( projection , width , height , hfov ) ;
x0 = extent.x0 ;
x1 = extent.x1 ;
y0 = extent.y0 ;
y1 = extent.y1 ;
}
assert ( x0 < x1 ) ;
assert ( y0 < y1 ) ;
step = ( x1 - x0 ) / width ;
if ( verbose )
{
if ( hfov == 0.0 )
{
std::cout << "extent calculated from hfov:"
<< std::endl ;
}
else
{
std::cout << "extent gleaned from command line arguments:"
<< std::endl ;
}
std::cout << "x0: " << x0 << " x1: " << x1 << std::endl ;
std::cout << "y0: " << y0 << " y1: " << y1 << std::endl ;
std::cout << "step: " << step << std::endl ;
}
}
}
} ;
// to avoid having to pass the arguments around, we us a global
// 'args' object.
arguments args ;
// helper function to save a zimt array of pixels to an image file, or
// to a set of six cube face images, if 'output' has a format string.
template < std::size_t nchannels >
void save_array ( const std::string & filename ,
const zimt::view_t
< 2 ,
zimt::xel_t < float , nchannels >
> & pixels ,
bool is_latlon = false )
{
if ( args.projection == CUBEMAP )
{
// output is a cubemap, let's see if 'output' is a format
// string for six separate cube faces
assert ( pixels.shape[1] == 6 * pixels.shape[0] ) ;
auto has_percent = filename.find_first_of ( "%" ) ;
if ( has_percent != std::string::npos )
{
// input must be a set of six cubeface images, that's the
// only way how we accept a format string.
cubeface_series cfs ( filename ) ;
if ( cfs.valid() )
{
// we'll call save_array recursively for the single images, hence:
args.projection = RECTILINEAR ;
// save six subarrays to individual images
std::size_t w = pixels.shape[0] ;
for ( std::size_t i = 0 ; i < 6 ; i++ )
{
save_array ( cfs[i] ,
pixels.window ( { 0L , long ( i * w ) } ,
{ long ( w ) , long ( ( i + 1 ) * w ) } ) ) ;
}
// restore the projection in 'args'
args.projection = CUBEMAP ;
// we're done.
return ;
}
}
}
// if we land here, we're supposed to store an ordinary single image
auto out = ImageOutput::create ( filename );
assert ( out != nullptr ) ;
ImageSpec ospec ( pixels.shape[0] , pixels.shape[1] ,
nchannels , TypeDesc::HALF ) ;
out->open ( filename , ospec ) ;
auto success = out->write_image ( TypeDesc::FLOAT , pixels.data() ) ;
assert ( success ) ;
out->close();
}
// quick shot at encoding video: I'm using an example file from the ffmpeg
// examples section, with some adaptations to bend the C code to C++,
// and additions to convert the float RGB data from the pixel pieline