-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
1752 lines (1573 loc) · 73.4 KB
/
Copy pathmain.cpp
File metadata and controls
1752 lines (1573 loc) · 73.4 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
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
/* Developed by Jimmy Hu */
/* Refactored for CLI Application capability */
// compile command:
// clang++ -std=c++20 -Xpreprocessor -fopenmp -I/usr/local/include -L/usr/local/lib -lomp main.cpp -L /usr/local/Cellar/llvm/10.0.0_3/lib/ -lm -O3 -o main -v
// https://stackoverflow.com/a/61821729/6667035
// clear && rm -rf ./main && g++-11 -std=c++20 -O4 -ffast-math -funsafe-math-optimizations -std=c++20 -fpermissive -H --verbose -Wall main.cpp -o main
//#define USE_BOOST_ITERATOR
//#define USE_BOOST_SERIALIZATION
#include "main.h"
#include "dynamic_loader.h"
//#define BOOST_TEST_DYN_LINK
//#define BOOST_TEST_MODULE image_elementwise_tests
#ifdef BOOST_TEST_MODULE
#include <boost/test/included/unit_test.hpp>
#ifdef BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#else
#include <boost/test/included/unit_test.hpp>
#endif // BOOST_TEST_DYN_LINK
#include <boost/mpl/list.hpp>
#include <boost/mpl/vector.hpp>
#include <tao/tuple/tuple.hpp>
typedef boost::mpl::list<
byte, char, int, short, long, long long int,
unsigned int, unsigned short int, unsigned long int, unsigned long long int,
float, double, long double> test_types;
// [TODO] Avoid code duplication (https://codereview.stackexchange.com/a/267709/231235)
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_add_test, T, test_types)
{
std::size_t size_x = 10;
std::size_t size_y = 10;
T initVal = 10;
T increment = 1;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test += TinyDIP::Image<T>(size_x, size_y, increment);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal + increment));
}
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_add_test_zero_dimensions, T, test_types)
{
std::size_t size_x = 0; // Test images with both of the dimensions having size zero.
std::size_t size_y = 0; // Test images with both of the dimensions having size zero.
T initVal = 10;
T increment = 1;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test += TinyDIP::Image<T>(size_x, size_y, increment);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal + increment));
}
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_add_test_large_dimensions, T, test_types)
{
std::size_t size_x = 18446744073709551615; // Test images with very large dimensions (std::numeric_limits<std::size_t>::max()).
std::size_t size_y = 18446744073709551615; // Test images with very large dimensions (std::numeric_limits<std::size_t>::max()).
T initVal = 10;
T increment = 1;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test += TinyDIP::Image<T>(size_x, size_y, increment);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal + increment));
}
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_minus_test, T, test_types)
{
std::size_t size_x = 10;
std::size_t size_y = 10;
T initVal = 10;
T difference = 1;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test -= TinyDIP::Image<T>(size_x, size_y, difference);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal - difference));
}
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_minus_test_zero_dimensions, T, test_types)
{
std::size_t size_x = 0; // Test images with both of the dimensions having size zero.
std::size_t size_y = 0; // Test images with both of the dimensions having size zero.
T initVal = 10;
T difference = 1;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test -= TinyDIP::Image<T>(size_x, size_y, difference);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal - difference));
}
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_minus_test_large_dimensions, T, test_types)
{
std::size_t size_x = 18446744073709551615; // Test images with very large dimensions (std::numeric_limits<std::size_t>::max()).
std::size_t size_y = 18446744073709551615; // Test images with very large dimensions (std::numeric_limits<std::size_t>::max()).
T initVal = 10;
T difference = 1;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test -= TinyDIP::Image<T>(size_x, size_y, difference);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal - difference));
}
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_multiplies_test, T, test_types)
{
std::size_t size_x = 10;
std::size_t size_y = 10;
T initVal = 10;
T multiplier = 2;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test *= TinyDIP::Image<T>(size_x, size_y, multiplier);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal * multiplier));
}
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_multiplies_test_zero_dimensions, T, test_types)
{
std::size_t size_x = 0; // Test images with both of the dimensions having size zero.
std::size_t size_y = 0; // Test images with both of the dimensions having size zero.
T initVal = 10;
T multiplier = 2;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test *= TinyDIP::Image<T>(size_x, size_y, multiplier);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal * multiplier));
}
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_multiplies_test_large_dimensions, T, test_types)
{
std::size_t size_x = 18446744073709551615; // Test images with very large dimensions (std::numeric_limits<std::size_t>::max()).
std::size_t size_y = 18446744073709551615; // Test images with very large dimensions (std::numeric_limits<std::size_t>::max()).
T initVal = 10;
T multiplier = 2;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test *= TinyDIP::Image<T>(size_x, size_y, multiplier);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal * multiplier));
}
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_divides_test, T, test_types)
{
std::size_t size_x = 10;
std::size_t size_y = 10;
T initVal = 10;
T divider = 2;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test /= TinyDIP::Image<T>(size_x, size_y, divider);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal / divider));
}
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_divides_test_zero_dimensions, T, test_types)
{
std::size_t size_x = 0; // Test images with both of the dimensions having size zero.
std::size_t size_y = 0; // Test images with both of the dimensions having size zero.
T initVal = 10;
T divider = 2;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test /= TinyDIP::Image<T>(size_x, size_y, divider);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal / divider));
}
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_divides_test_large_dimensions, T, test_types)
{
std::size_t size_x = 18446744073709551615; // Test images with very large dimensions (std::numeric_limits<std::size_t>::max()).
std::size_t size_y = 18446744073709551615; // Test images with very large dimensions (std::numeric_limits<std::size_t>::max()).
T initVal = 10;
T divider = 2;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test /= TinyDIP::Image<T>(size_x, size_y, divider);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal / divider));
}
/*
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_divides_zero_test, T, test_types)
{
std::size_t size_x = 10;
std::size_t size_y = 10;
T initVal = 10;
T divider = 0;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test /= TinyDIP::Image<T>(size_x, size_y, divider);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal / divider)); // dividing by zero test
}
*/
#endif
void difference_and_enhancement(std::string input_path1, std::string input_path2, double enhancement_times)
{
if (input_path1.empty())
{
std::cerr << "Input path is empty!";
}
std::filesystem::path input1 = input_path1;
std::filesystem::path input2 = input_path2;
}
#ifndef BOOST_TEST_MODULE
void addLeadingZeros(std::string input_path, std::string output_path);
void print(auto comment, auto const& seq, char term = ' ') {
for (std::cout << comment << '\n'; auto const& elem : seq)
std::cout << elem << term;
std::cout << '\n';
}
auto myHighLightRegion_parameters(const std::size_t index = 0)
{
std::vector<std::tuple<
std::string, // filenames
std::size_t, // start_index
std::size_t, // end_index
std::size_t, // startx
std::size_t, // endx
std::size_t, // starty
std::size_t, // endy
std::string // output_location
>> collection;
}
namespace handlers
{
// info template function implementation
template <
typename ImageLoaderFun = MetaImageIO::Loader
>
requires (std::invocable<ImageLoaderFun, const std::string_view, Workspace&>)
constexpr void info(
Workspace& workspace,
std::span<const std::string_view> args,
std::ostream& os = std::cout,
ImageLoaderFun&& image_loader_fun = ImageLoaderFun{})
{
if (std::ranges::empty(args))
{
os << "Usage: info <input_bmp | $var>\n";
return;
}
const std::string_view input_arg = args[0];
// Polymorphic lambda to cleanly print dimensions dynamically independent of image type
auto process_info = [&]<typename ImageType>(const ImageType& img)
requires (TinyDIP::is_Image<std::remove_cvref_t<ImageType>>::value)
{
os << "Image Info:\n";
os << " Source: " << input_arg << "\n";
os << " Width: " << img.getWidth() << "\n";
os << " Height: " << img.getHeight() << "\n";
};
if (!dispatch_data_operation<master_image_types>(input_arg, workspace, image_loader_fun, process_info))
{
os << "Error: Memory variable not found or unsupported type.\n";
}
}
// lanczos_resample function implementation
constexpr void lanczos_resample(
Workspace& workspace,
std::span<const std::string_view> args,
std::ostream& os = std::cout
)
{
auto transform_handler = make_meta_transform_handler<4>(
"lanczos_resample [execution_policy] <input_img | $var> <output_img | $var> <width> <height> [a=3]",
[](const auto& filtered_args, const std::string_view policy_str, std::ostream& os)
{
const std::size_t width = parse_arg<std::size_t>(filtered_args[2]);
const std::size_t height = parse_arg<std::size_t>(filtered_args[3]);
std::size_t a = 3;
if (std::ranges::size(filtered_args) >= 5)
{
a = parse_arg<std::size_t>(filtered_args[4]);
}
os << "Resizing " << filtered_args[0] << " to " << width << "x" << height << " with Lanczos radius " << a;
if (!std::ranges::empty(policy_str))
{
os << " (Policy: " << policy_str << ")";
}
os << "...\n";
return [width, height, a, policy_str, &os]<typename ImageType>(ImageType&& img) -> std::any
{
auto exec_default = [&]() -> std::any
{
return TinyDIP::lanczos_resample(std::forward<ImageType>(img), width, height, static_cast<int>(a));
};
auto exec_policy = [&]<typename ExecPolicy>(ExecPolicy&& exec_policy) -> std::any
requires std::is_execution_policy_v<std::remove_cvref_t<ExecPolicy>>
{
if constexpr (requires { TinyDIP::lanczos_resample(std::forward<ExecPolicy>(exec_policy), std::forward<ImageType>(img), width, height, static_cast<int>(a)); })
{
return TinyDIP::lanczos_resample(std::forward<ExecPolicy>(exec_policy), std::forward<ImageType>(img), width, height, static_cast<int>(a));
}
else
{
if (!std::ranges::empty(policy_str))
{
os << "Warning: Execution policy requested but not supported for this image type/operation. Falling back to default.\n";
}
return exec_default();
}
};
return dispatch_policy_string(policy_str, exec_policy, exec_default, os);
};
}
);
transform_handler(workspace, args, os);
}
// ones template function implementation
template <
std::invocable<const std::string_view, Workspace&, TinyDIP::Image<double>&&> ImageSaverFun = MetaImageIO::Saver
>
constexpr void ones(
Workspace& workspace,
std::span<const std::string_view> args,
std::ostream& os = std::cout,
ImageSaverFun&& image_saver_fun = ImageSaverFun{})
{
create_image_with_initial_value(
workspace, args, 1.0, "ones", os, std::forward<ImageSaverFun>(image_saver_fun)
);
}
// print template function implementation
template <
typename ImageLoaderFun = MetaImageIO::Loader
>
requires (std::invocable<ImageLoaderFun, const std::string_view, Workspace&>)
constexpr void print(
Workspace& workspace,
std::span<const std::string_view> args,
std::ostream& os = std::cout,
ImageLoaderFun&& image_loader_fun = ImageLoaderFun{})
{
if (std::ranges::empty(args))
{
os << "Usage: print <input_bmp | $var>\n";
return;
}
const std::string_view input_arg = args[0];
// Polymorphic lambda to cleanly print image content dynamically independent of image type
auto process_print = [&]<typename ImageType>(const ImageType& img)
requires (TinyDIP::is_Image<std::remove_cvref_t<ImageType>>::value)
{
os << "Printing image content for " << input_arg << ":\n";
img.print(",");
os << "Done.\n";
};
if (!dispatch_data_operation<master_image_types>(input_arg, workspace, image_loader_fun, process_print))
{
// If dispatch_data_operation returns false, it must be a $ variable holding a scalar or unsupported type
const std::string_view var_name = input_arg.substr(1);
// Polymorphic lambda returning true if the complex custom scalar type matched
auto try_print_complex_scalar = [&]<typename T>() -> bool
{
if (workspace.retrieve<T>(var_name))
{
os << "Printing scalar value for " << input_arg << ":\n";
if constexpr (is_vector_v<T> || is_deque_v<T> || is_list_v<T> || is_std_array_v<T>)
{
os << "container value = {";
bool first = true;
const auto* container_ptr = workspace.retrieve<T>(var_name);
for (const auto& elem : *container_ptr)
{
if (!first)
{
os << ", ";
}
os << +elem;
first = false;
}
os << "}\nDone.\n";
}
else
{
os << *workspace.retrieve<T>(var_name) << "\nDone.\n";
}
return true;
}
return false;
};
if (match_any_type<complex_scalar_types_for_printing>(try_print_complex_scalar))
{
// Handled successfully by try_print_complex_scalar short-circuit logic
}
else
{
// Polymorphic lambda returning true if the numeric type matched
auto try_print_numeric = [&]<typename T>() -> bool
{
if (workspace.retrieve<T>(var_name))
{
os << "Printing scalar value for " << input_arg << ":\n";
if constexpr (sizeof(T) == 1 && std::is_integral_v<T>) // Safely print 8-bit integer types as numbers, not unprintable chars
{
os << +(*workspace.retrieve<T>(var_name)) << "\nDone.\n";
}
else
{
os << *workspace.retrieve<T>(var_name) << "\nDone.\n";
}
return true;
}
return false;
};
if (!match_any_type<core_numeric_types>(try_print_numeric))
{
os << "Error: Memory variable not found or unsupported type.\n";
}
}
}
}
// RandomGenerator template struct implementation
template <typename Urbg, typename Dist>
requires (std::uniform_random_bit_generator<std::remove_reference_t<Urbg>> &&
std::invocable<Dist&, Urbg&>)
struct RandomGenerator
{
Urbg& urbg_;
Dist& dist_;
constexpr auto operator()()
{
return dist_(urbg_);
}
};
// rand_generator template function implementation
template <
std::invocable<const std::string_view, Workspace&, TinyDIP::Image<double>&&> ImageSaverFun = MetaImageIO::Saver
>
constexpr void rand_generator(
Workspace& workspace,
std::span<const std::string_view> args,
std::ostream& os = std::cout,
ImageSaverFun&& image_saver_fun = ImageSaverFun{})
{
auto dispatch_generation = [&]
(std::uniform_random_bit_generator auto&& urbg, const std::string_view& out_path, std::span<const std::size_t> sz)
{
std::uniform_real_distribution<double> dist{};
using UrbgType = std::remove_cvref_t<decltype(urbg)>;
using DistType = decltype(dist);
RandomGenerator<UrbgType, DistType> gen{urbg, dist};
// Calling the dynamic range-based generate overload directly from TinyDIP.
auto output_img = TinyDIP::generate(gen, sz);
// Dynamically save image via the injected saver abstraction
image_saver_fun(out_path, workspace, std::move(output_img));
os << "Saved to " << out_path << "\n";
};
std::map<std::string_view, std::function<void(const std::string_view&, std::span<const std::size_t>)>> urbg_mapping = {
{"knuth_b", [&]
(const std::string_view& out_path, std::span<const std::size_t> sz)
{ dispatch_generation(std::knuth_b{std::random_device{}()}, out_path, sz); }
},
{"minstd_rand", [&]
(const std::string_view& out_path, std::span<const std::size_t> sz)
{ dispatch_generation(std::minstd_rand{std::random_device{}()}, out_path, sz); }
},
{"minstd_rand0", [&]
(const std::string_view& out_path, std::span<const std::size_t> sz)
{ dispatch_generation(std::minstd_rand0{std::random_device{}()}, out_path, sz); }
},
{"mt19937", [&]
(const std::string_view& out_path, std::span<const std::size_t> sz)
{ dispatch_generation(std::mt19937{std::random_device{}()}, out_path, sz); }
},
{"mt19937_64", [&]
(const std::string_view& out_path, std::span<const std::size_t> sz)
{ dispatch_generation(std::mt19937_64{std::random_device{}()}, out_path, sz); }
},
{"ranlux24", [&]
(const std::string_view& out_path, std::span<const std::size_t> sz)
{ dispatch_generation(std::ranlux24{std::random_device{}()}, out_path, sz); }
},
{"ranlux24_base", [&]
(const std::string_view& out_path, std::span<const std::size_t> sz)
{ dispatch_generation(std::ranlux24_base{std::random_device{}()}, out_path, sz); }
},
{"ranlux48", [&]
(const std::string_view& out_path, std::span<const std::size_t> sz)
{ dispatch_generation(std::ranlux48{std::random_device{}()}, out_path, sz); }
},
{"ranlux48_base", [&]
(const std::string_view& out_path, std::span<const std::size_t> sz)
{ dispatch_generation(std::ranlux48_base{std::random_device{}()}, out_path, sz); }
}
};
auto print_available_urbgs = [&]()
{
os << "Available URBGs: ";
for (auto it = std::ranges::begin(urbg_mapping); it != std::ranges::end(urbg_mapping); ++it)
{
os << it->first;
if (std::next(it) != std::ranges::end(urbg_mapping))
{
os << ", ";
}
}
os << '\n';
};
if (std::ranges::size(args) < 3)
{
os << "Usage: rand <urbg_type> <output_bmp | $var> <dim1> [dim2] [dim3] ...\n";
print_available_urbgs();
return;
}
const std::string_view urbg_type = args[0];
const std::string_view output_arg = args[1];
std::vector<std::size_t> sizes;
sizes.reserve(std::ranges::size(args) - 2);
for (std::size_t i = 2; i < std::ranges::size(args); ++i)
{
sizes.emplace_back(parse_arg<std::size_t>(args[i]));
}
os << "Generating random image with dimensions: ";
for (const auto& size : sizes)
{
os << size << " ";
}
os << "using URBG '" << urbg_type << "'...\n";
if (auto it = urbg_mapping.find(urbg_type); it != std::ranges::end(urbg_mapping))
{
it->second(output_arg, sizes);
}
else
{
os << "Error: Unknown URBG type '" << urbg_type << "'.\n";
print_available_urbgs();
}
}
void remove(
Workspace& workspace,
std::span<const std::string_view> args,
std::ostream& os = std::cout)
{
if (std::ranges::empty(args))
{
os << "Usage: remove <$var1> [$var2] ... OR remove all\n";
return;
}
if (std::ranges::size(args) == 1 && std::string_view(args[0]) == "all")
{
workspace.clear();
os << "Removed all memory variables. Workspace is now empty.\n";
return;
}
for (const auto& arg : args)
{
const std::string_view var_arg = arg;
if (!var_arg.starts_with('$'))
{
os << "Error: Argument must be a memory variable starting with '$' or 'all'. Skipped " << var_arg << ".\n";
continue;
}
const std::string_view var_name = var_arg.substr(1);
if (workspace.remove(var_name))
{
os << "Removed memory variable $" << var_name << ".\n";
}
else
{
os << "Warning: Memory variable $" << var_name << " not found.\n";
}
}
}
// rename function implementation
void rename(
Workspace& workspace,
std::span<const std::string_view> args,
std::ostream& os = std::cout
)
{
if (std::ranges::size(args) < 2)
{
os << "Usage: rename <$old_var> <$new_var>\n";
return;
}
const std::string_view old_arg = args[0];
const std::string_view new_arg = args[1];
if (!old_arg.starts_with('$') || !new_arg.starts_with('$'))
{
os << "Error: Both arguments must be memory variables starting with '$'.\n";
return;
}
const std::string_view old_name = old_arg.substr(1);
const std::string_view new_name = new_arg.substr(1);
if (workspace.rename(old_name, new_name))
{
os << "Renamed variable $" << old_name << " to $" << new_name << ".\n";
}
else
{
os << "Error: Memory variable $" << old_name << " not found.\n";
}
}
// rotate function implementation
constexpr void rotate(
Workspace& workspace,
std::span<const std::string_view> args,
std::ostream& os = std::cout)
{
auto transform_handler = make_meta_transform_handler<3>(
"rotate [execution_policy] <input_img | $var> <output_img | $var> <angle>",
[](const auto& filtered_args, const std::string_view policy_str, std::ostream& os)
{
const double angle = parse_arg<double>(filtered_args[2]);
os << "Rotating " << filtered_args[0] << " by " << angle;
if (!std::ranges::empty(policy_str))
{
os << " (Policy: " << policy_str << ")";
}
os << "...\n";
return [angle, policy_str, &os]<typename ImageType>(ImageType&& img) -> std::any
{
auto exec_default = [&]() -> std::any
{
if constexpr (requires { TinyDIP::rotate_detail_shear_transformation(std::forward<ImageType>(img), angle); })
{
return TinyDIP::rotate_detail_shear_transformation(std::forward<ImageType>(img), angle);
}
else
{
throw std::invalid_argument("Input image type does not support rotate_detail_shear_transformation.");
return std::any{};
}
};
auto exec_policy = [&]<typename ExecPolicy>(ExecPolicy&& exec_policy) -> std::any
requires std::is_execution_policy_v<std::remove_cvref_t<ExecPolicy>>
{
if constexpr (requires { TinyDIP::rotate_detail_shear_transformation(std::forward<ExecPolicy>(exec_policy), std::forward<ImageType>(img), angle); })
{
return TinyDIP::rotate_detail_shear_transformation(std::forward<ExecPolicy>(exec_policy), std::forward<ImageType>(img), angle);
}
else
{
if (!std::ranges::empty(policy_str))
{
os << "Warning: Execution policy requested but not supported for this image type/operation. Falling back to default.\n";
}
return exec_default();
}
};
return dispatch_policy_string(policy_str, exec_policy, exec_default, os);
};
}
);
transform_handler(workspace, args, os);
}
// sift_generate_octave template function implementation
template <
typename ImageLoaderFun = MetaImageIO::Loader
>
requires (std::invocable<ImageLoaderFun, const std::string_view, Workspace&>)
constexpr void sift_generate_octave(
Workspace& workspace,
std::span<const std::string_view> args,
std::ostream& os = std::cout,
ImageLoaderFun&& image_loader_fun = ImageLoaderFun{})
{
if (std::ranges::size(args) < 2)
{
os << "Usage: sift_generate_octave <input_img | $var> <output_var | $var> [levels=5] [initial_sigma=1.6] [k=1.414]\n";
return;
}
const std::string_view input_arg = args[0];
const std::string_view output_arg = args[1];
if (!output_arg.starts_with('$'))
{
os << "Error: Output must be a memory variable starting with '$'.\n";
return;
}
const std::size_t levels = (std::ranges::size(args) > 2) ? parse_arg<std::size_t>(args[2]) : 5;
const double initial_sigma = (std::ranges::size(args) > 3) ? parse_arg<double>(args[3]) : 1.6;
const double k = (std::ranges::size(args) > 4) ? parse_arg<double>(args[4]) : std::numbers::sqrt2_v<double>;
os << "Generating SIFT octave from " << input_arg << " with " << levels << " levels...\n";
auto process_octave = [&]<typename ImageType>(ImageType&& input_img)
{
using DecayedImageType = std::remove_cvref_t<ImageType>;
if constexpr (TinyDIP::is_bool_data_v<DecayedImageType> || TinyDIP::is_complex_data_v<DecayedImageType>)
{
os << "Error: Input image type [" << get_type_name<DecayedImageType>() << "] does not support SIFT octave generation.\n";
return;
}
else
{
auto process_octave_impl = [&]<typename T>(T&& double_img)
{
using ImplDecayedT = std::remove_cvref_t<T>;
if constexpr (requires { TinyDIP::SIFT_impl::generate_octave(std::forward<T>(double_img), levels, initial_sigma, k); })
{
auto result = TinyDIP::SIFT_impl::generate_octave(std::forward<T>(double_img), levels, initial_sigma, k);
workspace.store(output_arg.substr(1), std::move(result));
os << "Saved SIFT octave to " << output_arg << ".\n";
}
else
{
os << "Error: Elevated input image type [" << get_type_name<ImplDecayedT>() << "] does not support generate_octave.\n";
}
};
using RawScalarT = TinyDIP::get_deep_scalar_t<DecayedImageType>;
if constexpr (std::same_as<RawScalarT, double>)
{
process_octave_impl(std::forward<ImageType>(input_img));
}
else if constexpr (requires { TinyDIP::im2double(std::forward<ImageType>(input_img)); })
{
process_octave_impl(TinyDIP::im2double(std::forward<ImageType>(input_img)));
}
else
{
os << "Error: Input image type [" << get_type_name<DecayedImageType>() << "] cannot be converted to double precision for SIFT octave generation.\n";
}
}
};
if (!dispatch_data_operation<master_image_types>(input_arg, workspace, image_loader_fun, process_octave))
{
os << "Error: Memory variable not found or unsupported type.\n";
}
}
// subimage function implementation
constexpr void subimage(
Workspace& workspace,
std::span<const std::string_view> args,
std::ostream& os = std::cout)
{
auto transform_handler = make_meta_transform_handler<6>(
"subimage [execution_policy] <input_img | $var> <output_img | $var> <x_offset> <y_offset> <width> <height>",
[](const auto& filtered_args, const std::string_view policy_str, std::ostream& os)
{
const std::size_t x_offset = parse_arg<std::size_t>(filtered_args[2]);
const std::size_t y_offset = parse_arg<std::size_t>(filtered_args[3]);
const std::size_t sub_width = parse_arg<std::size_t>(filtered_args[4]);
const std::size_t sub_height = parse_arg<std::size_t>(filtered_args[5]);
os << "Extracting subimage from " << filtered_args[0] << " at (" << x_offset << ", " << y_offset
<< ") with size " << sub_width << "x" << sub_height;
if (!std::ranges::empty(policy_str))
{
os << " (Policy: " << policy_str << ")";
}
os << "...\n";
return [x_offset, y_offset, sub_width, sub_height, policy_str, &os]<typename ImageType>(ImageType&& img) -> std::any
{
using DecayedT = std::remove_cvref_t<ImageType>;
// Mathematically guarantee the type is a 2D matrix/image
if constexpr (requires { img.getWidth(); img.getHeight(); img.at(0, 0); })
{
if (x_offset + sub_width > img.getWidth() || y_offset + sub_height > img.getHeight())
{
throw std::out_of_range("Subimage bounds exceed original image dimensions.");
return std::any{};
}
auto exec_default = [&]() -> std::any
{
DecayedT out_img(sub_width, sub_height);
for (std::size_t y = 0; y < sub_height; ++y)
{
for (std::size_t x = 0; x < sub_width; ++x)
{
out_img.at(x, y) = img.at(x + x_offset, y + y_offset);
}
}
return out_img;
};
auto exec_policy = [&]<typename ExecPolicy>(ExecPolicy&& exec_policy) -> std::any
requires std::is_execution_policy_v<std::remove_cvref_t<ExecPolicy>>
{
DecayedT out_img(sub_width, sub_height);
auto indices = std::views::iota(std::size_t{0}, sub_width * sub_height);
std::for_each(
std::forward<ExecPolicy>(exec_policy),
std::ranges::begin(indices),
std::ranges::end(indices),
[&](const std::size_t idx)
{
const std::size_t y = idx / sub_width;
const std::size_t x = idx % sub_width;
out_img.at(x, y) = img.at(x + x_offset, y + y_offset);
}
);
return out_img;
};
return dispatch_policy_string(policy_str, exec_policy, exec_default, os);
}
else
{
throw std::invalid_argument("Input type does not support subimage extraction.");
return std::any{};
}
};
}
);
transform_handler(workspace, args, os);
}
// to_complex function implementation
constexpr void to_complex(
Workspace& workspace,
std::span<const std::string_view> args,
std::ostream& os = std::cout)
{
auto transform_handler = make_meta_transform_handler<2, master_data_types>(
"to_complex [execution_policy] <input_data | $var> <output_var | $var>",
[](const auto& filtered_args, const std::string_view policy_str, std::ostream& os)
{
if (!std::ranges::empty(policy_str))
{
os << "Converting " << filtered_args[0] << " to complex (Policy: " << policy_str << ")...\n";
}
else
{
os << "Converting " << filtered_args[0] << " to complex...\n";
}
return [policy_str, &os]<typename DataT>(DataT&& data) -> std::any
{
using DecayedDataT = std::remove_cvref_t<DataT>;
if constexpr (TinyDIP::is_bool_data_v<DecayedDataT>)
{
throw std::invalid_argument("Input data type (bool) does not support to_complex conversion.");
return std::any{};
}
else if constexpr (TinyDIP::is_complex_data_v<DecayedDataT>)
{
// The complex value of an already complex type is simply the exact same value!
// Returning the forwarded data natively bypasses the C++ standard library's
// ambiguous template instantiations for complex types.
return DecayedDataT(std::forward<DataT>(data));
}
else
{
auto exec_default = [&]() -> std::any
{
if constexpr (TinyDIP::is_Image<DecayedDataT>::value)
{
if constexpr (requires { TinyDIP::to_complex(std::forward<DataT>(data)); })
{
return TinyDIP::to_complex(std::forward<DataT>(data));
}
else
{
throw std::invalid_argument("Input image type does not support to_complex conversion.");
return std::any{};
}
}
else if constexpr (std::ranges::input_range<DecayedDataT>)
{
if constexpr (requires { TinyDIP::to_complex(*std::ranges::begin(data)); })
{
return TinyDIP::recursive_transform<TinyDIP::recursive_depth<DecayedDataT>()>(
[](auto&& element)
{
return TinyDIP::to_complex(std::forward<decltype(element)>(element));
},
std::forward<DataT>(data)
);
}
else
{
throw std::invalid_argument("Input container type does not support to_complex conversion.");
return std::any{};
}
}
else
{
throw std::invalid_argument("Input data type does not support to_complex conversion.");
return std::any{};
}
};
auto exec_policy = [&]<typename ExecPolicy>(ExecPolicy&& exec_policy) -> std::any
requires std::is_execution_policy_v<std::remove_cvref_t<ExecPolicy>>
{
if constexpr (TinyDIP::is_Image<DecayedDataT>::value)
{
if constexpr (requires { TinyDIP::to_complex(std::forward<ExecPolicy>(exec_policy), std::forward<DataT>(data)); })
{
return TinyDIP::to_complex(std::forward<ExecPolicy>(exec_policy), std::forward<DataT>(data));
}
else
{
if (!std::ranges::empty(policy_str))
{
os << "Warning: Execution policy requested but not supported for this image type/operation. Falling back to default.\n";
}
return exec_default();
}
}
else if constexpr (std::ranges::input_range<DecayedDataT>)
{
if constexpr (requires { TinyDIP::to_complex(*std::ranges::begin(data)); })
{
return TinyDIP::recursive_transform<TinyDIP::recursive_depth<DecayedDataT>()>(
std::forward<ExecPolicy>(exec_policy),
[](auto&& element)
{
return TinyDIP::to_complex(std::forward<decltype(element)>(element));
},
std::forward<DataT>(data)
);
}
else
{
if (!std::ranges::empty(policy_str))
{
os << "Warning: Execution policy requested but not supported for this data type/operation. Falling back to default.\n";
}
return exec_default();
}
}
else
{
if (!std::ranges::empty(policy_str))
{
os << "Warning: Execution policy requested but not supported for this data type/operation. Falling back to default.\n";
}
return exec_default();
}
};
return dispatch_policy_string(policy_str, exec_policy, exec_default, os);
}
};
}
);
transform_handler(workspace, args, os);
}
// vars function implementation
void vars(
Workspace& workspace,
std::span<const std::string_view> args,
std::ostream& os = std::cout)
{
(void)args;
os << "Current Workspace Variables:\n";