-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
3503 lines (3173 loc) · 156 KB
/
Copy pathmain.cpp
File metadata and controls
3503 lines (3173 loc) · 156 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"
//#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;
}
// MetaScalarHandler template struct implementation
// Generic Meta Handler strictly refactoring scalar reduction commands like max, min, and sum
template <
std::size_t MinArgs,
typename SetupFun,
typename ArgsContainer = std::vector<std::string_view>,
typename CheckingTypes = master_data_types
>
requires(std::invocable<SetupFun, const ArgsContainer&, const std::string_view, std::ostream&>)
struct MetaScalarHandler
{
std::string_view usage_string_;
std::string_view op_name_;
std::string_view capitalized_op_name_;
SetupFun setup_fun_;
template <
typename ImageLoaderFun = MetaImageIO::Loader
>
requires (std::invocable<ImageLoaderFun, const std::string_view, Workspace&>)
constexpr void operator()(Workspace& workspace, std::span<const std::string_view> args, std::ostream& os = std::cout, ImageLoaderFun&& image_loader_fun = ImageLoaderFun{}) const
{
std::string_view policy_str = "";
ArgsContainer filtered_args;
filtered_args.reserve(std::ranges::size(args));
for (const auto& arg : args)
{
const std::string_view sv_arg = arg;
if (sv_arg == "seq" || sv_arg == "par" || sv_arg == "par_unseq" || sv_arg == "unseq")
{
policy_str = sv_arg;
}
else
{
filtered_args.emplace_back(sv_arg);
}
}
if (std::ranges::size(filtered_args) < MinArgs)
{
os << "Usage: " << usage_string_ << "\n";
if (usage_string_.find("[execution_policy]") != std::string_view::npos)
{
os << " Optional Execution policies: seq, par, par_unseq, unseq\n";
}
return;
}
const std::string_view input_arg = filtered_args[0];
std::string_view output_arg = "";
if (std::ranges::size(filtered_args) > 1)
{
output_arg = filtered_args[1];
}
auto core_processor = setup_fun_(filtered_args, policy_str, os);
std::optional<std::any> final_result_opt;
// Polymorphic lambda to cleanly execute the algorithm dynamically independent of data type
auto process_scalar = [&]<typename DataT>(DataT&& input_data)
{
final_result_opt = core_processor(std::forward<DataT>(input_data));
};
if (!dispatch_data_operation<CheckingTypes>(input_arg, workspace, image_loader_fun, process_scalar))
{
os << "Error: Memory variable not found or unsupported type.\n";
return;
}
if (final_result_opt.has_value())
{
std::any scalar_result_any = std::move(*final_result_opt);
bool handled = false;
auto handle_result = [&]<typename ScalarT>() -> bool
{
if (scalar_result_any.type() == typeid(ScalarT))
{
auto& scalar_result = std::any_cast<ScalarT&>(scalar_result_any);
if (!std::ranges::empty(output_arg))
{
if (output_arg.starts_with('$'))
{
workspace.store(output_arg.substr(1), scalar_result);
os << "Saved " << op_name_ << " result to " << output_arg << "\n";
}
else
{
os << "Error: Output must be a memory variable starting with '$'.\n";
}
}
else
{
if constexpr (is_vector_v<ScalarT> || is_deque_v<ScalarT> || is_list_v<ScalarT> || is_std_array_v<ScalarT>)
{
os << capitalized_op_name_ << " result: {";
bool first = true;
for (const auto& elem : scalar_result)
{
if (!first)
{
os << ", ";
}
os << +elem;
first = false;
}
os << "}\n";
}
else if constexpr (requires { os << scalar_result; })
{
if constexpr (sizeof(ScalarT) == 1 && std::is_integral_v<ScalarT>)
{
os << capitalized_op_name_ << " result: " << +scalar_result << "\n";
}
else
{
os << capitalized_op_name_ << " result: " << scalar_result << "\n";
}
}
else
{
os << capitalized_op_name_ << " result evaluated successfully (Non-printable complex type).\n";
}
}
handled = true;
return true;
}
return false;
};
if (!match_any_type<master_scalar_types>(handle_result))
{
os << "Error: Output type from processor is unknown or unsupported. Type Name: ["
<< scalar_result_any.type().name() << "]\n";
}
}
}
};
// make_meta_scalar_handler template function implementation
template <std::size_t MinArgs, typename CheckingTypes = master_data_types, typename SetupFun, typename ArgsContainer = std::vector<std::string_view>>
constexpr auto make_meta_scalar_handler(std::string_view usage, std::string_view op_name, std::string_view capitalized_op_name, SetupFun&& setup)
{
return MetaScalarHandler<MinArgs, std::remove_cvref_t<SetupFun>, ArgsContainer, CheckingTypes>{
usage, op_name, capitalized_op_name, std::forward<SetupFun>(setup)
};
}
namespace handlers
{
// bicubic_resize function implementation
constexpr auto bicubic_resize(
Workspace& workspace,
std::span<const std::string_view> args,
std::ostream& os = std::cout
)
{
auto transform_handler = make_meta_transform_handler<4>(
"bicubic_resize [execution_policy] <input_img | $var> <output_img | $var> <width> <height>",
[](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]);
os << "Resizing " << filtered_args[0] << " to " << width << "x" << height;
if (!std::ranges::empty(policy_str))
{
os << " (Policy: " << policy_str << ")";
}
os << "...\n";
return [width, height, policy_str, &os]<typename ImageType>(ImageType && img) -> std::any
{
auto exec_default = [&]() -> std::any
{
return TinyDIP::copyResizeBicubic(std::forward<ImageType>(img), width, height);
};
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::copyResizeBicubic(std::forward<ExecPolicy>(exec_policy), std::forward<ImageType>(img), width, height); })
{
return TinyDIP::copyResizeBicubic(std::forward<ExecPolicy>(exec_policy), std::forward<ImageType>(img), width, height);
}
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);
}
// construct_rgb template function implementation
template <
typename ImageLoaderFun = MetaImageIO::Loader,
typename ImageSaverFun = MetaImageIO::Saver
>
requires (std::invocable<ImageLoaderFun, const std::string_view, Workspace&> &&
std::invocable<ImageSaverFun, const std::string_view, Workspace&, TinyDIP::Image<TinyDIP::RGB>&&>)
constexpr void construct_rgb(
Workspace& workspace,
std::span<const std::string_view> args,
std::ostream& os = std::cout,
ImageLoaderFun&& image_loader_fun = ImageLoaderFun{},
ImageSaverFun&& image_saver_fun = ImageSaverFun{})
{
std::string_view policy_str = "";
std::vector<std::string_view> filtered_args;
filtered_args.reserve(std::ranges::size(args));
for (const auto& arg : args)
{
const std::string_view sv_arg = arg;
if (sv_arg == "seq" || sv_arg == "par" || sv_arg == "par_unseq" || sv_arg == "unseq")
{
policy_str = sv_arg;
}
else
{
filtered_args.emplace_back(sv_arg);
}
}
if (std::ranges::size(filtered_args) < 4)
{
os << "Usage: constructRGB [execution_policy] <R_img | $var> <G_img | $var> <B_img | $var> <output_img | $var>\n";
os << " Optional Execution policies: seq, par, par_unseq, unseq\n";
return;
}
const std::string_view r_arg = filtered_args[0];
const std::string_view g_arg = filtered_args[1];
const std::string_view b_arg = filtered_args[2];
const std::string_view output_arg = filtered_args[3];
os << "Constructing RGB image from " << r_arg << ", " << g_arg << ", " << b_arg;
if (!std::ranges::empty(policy_str))
{
os << " (Policy: " << policy_str << ")";
}
os << "...\n";
// Restrict allowed types to strictly 8-bit unsigned integers to mathematically prevent template combinatorial explosions
using AllowedTypes = std::tuple<TinyDIP::Image<std::uint8_t>, TinyDIP::Image<unsigned char>>;
auto process_r = [&]<typename ImgR>(ImgR&& img_r)
{
auto process_g = [&]<typename ImgG>(ImgG&& img_g)
{
auto process_b = [&]<typename ImgB>(ImgB&& img_b)
{
if constexpr (
(!std::same_as<std::remove_cvref_t<ImgR>, TinyDIP::Image<std::uint8_t>> &&
!std::same_as<std::remove_cvref_t<ImgR>, TinyDIP::Image<unsigned char>>) ||
(!std::same_as<std::remove_cvref_t<ImgG>, TinyDIP::Image<std::uint8_t>> &&
!std::same_as<std::remove_cvref_t<ImgG>, TinyDIP::Image<unsigned char>>) ||
(!std::same_as<std::remove_cvref_t<ImgB>, TinyDIP::Image<std::uint8_t>>&&
!std::same_as<std::remove_cvref_t<ImgB>, TinyDIP::Image<unsigned char>>)
)
{
throw std::invalid_argument("R / G / B plane image must be 8-bit unsigned integer type.");
}
else
{
const std::size_t width = img_r.getWidth();
const std::size_t height = img_r.getHeight();
if (width != img_g.getWidth() || height != img_g.getHeight() ||
width != img_b.getWidth() || height != img_b.getHeight())
{
throw std::invalid_argument("Dimension mismatch among R, G, B plane images.");
}
TinyDIP::Image<TinyDIP::RGB> output_image(width, height);
auto exec_default = [&]() -> std::any
{
for (std::size_t y = 0; y < height; ++y)
{
for (std::size_t x = 0; x < width; ++x)
{
TinyDIP::RGB pixel{};
pixel.channels[0] = static_cast<std::uint8_t>(img_r.at(x, y));
pixel.channels[1] = static_cast<std::uint8_t>(img_g.at(x, y));
pixel.channels[2] = static_cast<std::uint8_t>(img_b.at(x, y));
output_image.at(x, y) = pixel;
}
}
return output_image;
};
auto exec_policy = [&]<typename ExecPolicy>(ExecPolicy && exec_policy) -> std::any
requires std::is_execution_policy_v<std::remove_cvref_t<ExecPolicy>>
{
auto indices = std::views::iota(std::size_t{ 0 }, width * 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 / width;
const std::size_t x = idx % width;
TinyDIP::RGB pixel{};
pixel.channels[0] = static_cast<std::uint8_t>(img_r.at(x, y));
pixel.channels[1] = static_cast<std::uint8_t>(img_g.at(x, y));
pixel.channels[2] = static_cast<std::uint8_t>(img_b.at(x, y));
output_image.at(x, y) = pixel;
}
);
return output_image;
};
std::any final_result = dispatch_policy_string(policy_str, exec_policy, exec_default, os);
image_saver_fun(output_arg, workspace, std::move(std::any_cast<TinyDIP::Image<TinyDIP::RGB>&>(final_result)));
os << "Saved to " << output_arg << "\n";
}
};
if (!dispatch_data_operation<AllowedTypes>(b_arg, workspace, image_loader_fun, process_b))
{
os << "Error: Memory variable for B plane not found or not an 8-bit unsigned integer type. Use 'im2uint8' first.\n";
}
};
if (!dispatch_data_operation<AllowedTypes>(g_arg, workspace, image_loader_fun, process_g))
{
os << "Error: Memory variable for G plane not found or not an 8-bit unsigned integer type. Use 'im2uint8' first.\n";
}
};
if (!dispatch_data_operation<AllowedTypes>(r_arg, workspace, image_loader_fun, process_r))
{
os << "Error: Memory variable for R plane not found or not an 8-bit unsigned integer type. Use 'im2uint8' first.\n";
}
}
// create_container template function implementation
template <
typename ImageLoaderFun = MetaImageIO::Loader
>
requires (std::invocable<ImageLoaderFun, const std::string_view, Workspace&>)
constexpr void create_container(
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: create_container <prototype_element | $var> <output_container | $var>\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;
}
os << "Creating container based on type of " << input_arg << "...\n";
auto process_create = [&]<typename CandidateType>(CandidateType&& candidate)
{
using DecayedT = std::remove_cvref_t<CandidateType>;
std::vector<DecayedT> new_vec;
// Utilizing emplace_back to insert the initial candidate prototype safely and efficiently
new_vec.emplace_back(std::forward<CandidateType>(candidate));
workspace.store(output_arg.substr(1), std::move(new_vec));
os << "Created container and added initial element. Saved [std::vector<" << get_type_name<DecayedT>() << ">] to " << output_arg << ".\n";
};
using AllElementTypes = tuple_cat_t<master_data_types>;
if (!dispatch_data_operation<AllElementTypes>(input_arg, workspace, image_loader_fun, process_create))
{
os << "Error: Memory variable not found or unsupported type.\n";
}
}
// create_image_with_initial_value template function implementation
template <
std::invocable<const std::string_view, Workspace&, TinyDIP::Image<double>&&> ImageSaverFun = MetaImageIO::Saver
>
constexpr void create_image_with_initial_value(
Workspace& workspace,
std::span<const std::string_view> args,
const double initial_value,
const std::string_view command_name,
std::ostream& os = std::cout,
ImageSaverFun&& image_saver_fun = ImageSaverFun{})
{
if (std::ranges::size(args) < 2)
{
os << "Usage: " << command_name << " <output_img | $var> <dim1> [dim2] [dim3] ...\n";
return;
}
const std::string_view output_arg = args[0];
std::vector<std::size_t> sizes;
sizes.reserve(std::ranges::size(args) - 1);
std::size_t total_elements = 1;
for (std::size_t i = 1; i < std::ranges::size(args); ++i)
{
const std::size_t dim = parse_arg<std::size_t>(args[i]);
sizes.emplace_back(dim);
total_elements *= dim;
}
os << "Generating " << command_name << " image with dimensions: ";
for (std::size_t i = 0; i < std::ranges::size(sizes); ++i)
{
os << sizes[i];
if (i + 1 < std::ranges::size(sizes))
{
os << " x ";
}
}
os << "...\n";
std::vector<double> data(total_elements, initial_value);
TinyDIP::Image<double> output_img(data, sizes);
if constexpr (requires { output_img.setAllValue(initial_value); })
{
output_img.setAllValue(initial_value);
}
image_saver_fun(output_arg, workspace, std::move(output_img));
os << "Saved to " << output_arg << "\n";
}
// dct2 function implementation
constexpr void dct2(
Workspace& workspace,
std::span<const std::string_view> args,
std::ostream& os = std::cout
)
{
auto transform_handler = make_meta_transform_handler<2>(
"dct2 [execution_policy] <input_img | $var> <output_img | $var>",
[](const auto& filtered_args, const std::string_view policy_str, std::ostream& os)
{
os << "Calculating DCT-2 for " << filtered_args[0];
if (!std::ranges::empty(policy_str))
{
os << " (Policy: " << policy_str << ")";
}
os << "...\n";
return [policy_str, &os]<typename ImageType>(ImageType && img) -> std::any
{
auto exec_default = [&]() -> std::any
{
return TinyDIP::dct2(std::forward<ImageType>(img));
};
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::dct2(std::forward<ExecPolicy>(exec_policy), std::forward<ImageType>(img)); })
{
return TinyDIP::dct2(std::forward<ExecPolicy>(exec_policy), std::forward<ImageType>(img));
}
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);
}
// estimate_gaussian_params_2d function implementation
constexpr void estimate_gaussian_params_2d(
Workspace& workspace,
std::span<const std::string_view> args,
std::ostream& os = std::cout)
{
auto transform_handler = make_meta_scalar_handler<2>(
"estimate_gaussian_params_2d [execution_policy] <input_img | $var> <output_var | $var> [max_iterations=1000] [tolerance=1e-7]",
"estimate_gaussian_params_2d", "Estimate Gaussian Parameters 2D",
[](const auto& filtered_args, const std::string_view policy_str, std::ostream& os)
{
std::size_t max_iterations = 1000;
double tolerance = 1e-7;
if (std::ranges::size(filtered_args) > 2)
{
max_iterations = parse_arg<std::size_t>(filtered_args[2]);
}
if (std::ranges::size(filtered_args) > 3)
{
tolerance = parse_arg<double>(filtered_args[3]);
}
os << "Estimating 2D Gaussian parameters for " << filtered_args[0] << "...\n";
return [max_iterations, tolerance, policy_str, &os]<typename DataT>(DataT&& data) -> std::any
{
using DecayedDataT = std::remove_cvref_t<DataT>;
if constexpr (TinyDIP::is_Image<DecayedDataT>::value)
{
using ElementT = TinyDIP::get_deep_scalar_t<DecayedDataT>;
if constexpr (std::is_arithmetic_v<ElementT> && !TinyDIP::is_bool_data_v<ElementT>)
{
auto exec_default = [&]() -> std::any
{
if constexpr (requires { TinyDIP::estimate_gaussian_parameters_2d(std::execution::seq, std::forward<DataT>(data), max_iterations, tolerance); })
{
return TinyDIP::estimate_gaussian_parameters_2d(std::execution::seq, std::forward<DataT>(data), max_iterations, tolerance);
}
else if constexpr (requires { TinyDIP::estimate_gaussian_parameters_2d(std::forward<DataT>(data), max_iterations, tolerance); })
{
// Just in case the backend doesn't support execution policies natively yet
return TinyDIP::estimate_gaussian_parameters_2d(std::forward<DataT>(data), max_iterations, tolerance);
}
else
{
throw std::invalid_argument(std::string("Input image type [") + std::string(get_type_name<DecayedDataT>()) + "] does not support estimate_gaussian_parameters_2d.");
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::estimate_gaussian_parameters_2d(std::forward<ExecPolicy>(exec_policy), std::forward<DataT>(data), max_iterations, tolerance); })
{
return TinyDIP::estimate_gaussian_parameters_2d(std::forward<ExecPolicy>(exec_policy), std::forward<DataT>(data), max_iterations, tolerance);
}
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);
}
else
{
throw std::invalid_argument("Input image must have arithmetic elements to estimate Gaussian parameters.");
return std::any{};
}
}
else
{
throw std::invalid_argument("Input data type must be an Image.");
return std::any{};
}
};
}
);
transform_handler(workspace, args, os);
}
// gaussian_figure_2d template function implementation
template <
std::invocable<const std::string_view, Workspace&, TinyDIP::Image<double>&&> ImageSaverFun = MetaImageIO::Saver
>
constexpr void gaussian_figure_2d(
Workspace& workspace,
std::span<const std::string_view> args,
std::ostream& os = std::cout,
ImageSaverFun&& image_saver_fun = ImageSaverFun{})
{
if (std::ranges::size(args) < 6)
{
os << "Usage: gaussian_figure_2d <output_img | $var> <width> <height> <center_x> <center_y> <sigma>\n";
return;
}
const std::string_view output_arg = args[0];
const std::size_t width = parse_arg<std::size_t>(args[1]);
const std::size_t height = parse_arg<std::size_t>(args[2]);
const std::size_t center_x = parse_arg<std::size_t>(args[3]);
const std::size_t center_y = parse_arg<std::size_t>(args[4]);
const double sigma = parse_arg<double>(args[5]);
os << "Generating Gaussian Figure 2D with dimensions: " << width << " x " << height
<< ", center: (" << center_x << ", " << center_y << "), sigma: " << sigma << "...\n";
// Generate the 2D Gaussian Figure utilizing the native double precision wrapper directly from the TinyDIP engine
auto output_img = TinyDIP::gaussianFigure2D(width, height, center_x, center_y, sigma);
image_saver_fun(output_arg, workspace, std::move(output_img));
os << "Saved to " << output_arg << "\n";
}
// get_element template function implementation
template <
typename ImageLoaderFun = MetaImageIO::Loader
>
requires (std::invocable<ImageLoaderFun, const std::string_view, Workspace&>)
constexpr void get_element(
Workspace& workspace,
std::span<const std::string_view> args,
std::ostream& os = std::cout,
ImageLoaderFun&& image_loader_fun = ImageLoaderFun{})
{
if (std::ranges::size(args) < 3)
{
os << "Usage: get_element <input_container | $var> <output_var | $var> <index>\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 index = parse_arg<std::size_t>(args[2]);
os << "Extracting element at index " << index << " from " << input_arg << "...\n";
auto process_element = [&]<typename CandidateType>(CandidateType&& candidate)
{
using DecayedT = std::remove_cvref_t<CandidateType>;
// Verify the type is a mathematically registered container
if constexpr (is_vector_v<DecayedT> || is_deque_v<DecayedT> || is_list_v<DecayedT> || is_std_array_v<DecayedT>)
{
if (index >= std::ranges::size(candidate))
{
os << "Error: Index " << index << " is out of bounds for container of size " << std::ranges::size(candidate) << ".\n";
return;
}
// Cleanly traverse using ranges to natively support both vectors and lists
auto it = std::ranges::begin(candidate);
std::ranges::advance(it, index);
// Store the extracted element safely into the workspace (invokes copy constructor to preserve independence)
workspace.store(output_arg.substr(1), *it);
os << "Saved element to " << output_arg << ".\n";
}
else
{
os << "Error: Input type [" << get_type_name<DecayedT>() << "] is not a supported container type.\n";
}
};
// Leverage tuple_cat_t to flawlessly support both scalar containers and newly registered image containers!
using AllContainerTypes = tuple_cat_t<master_data_types, master_image_container_types>;
if (!dispatch_data_operation<AllContainerTypes>(input_arg, workspace, image_loader_fun, process_element))
{
os << "Error: Memory variable not found or unsupported type.\n";
}
}
// getPlane_channel_description function implementation
constexpr auto getPlane_channel_description(const std::size_t channel_index)
{
if (channel_index == 0)
{
"getRplane [execution_policy] <input_img | $var> <output_img | $var>";
}
else if (channel_index == 1)
{
return "getGplane [execution_policy] <input_img | $var> <output_img | $var>";
}
else if (channel_index == 2)
{
return "getBplane [execution_policy] <input_img | $var> <output_img | $var>";
}
return "";
}
// getPlane function implementation
constexpr void getPlane(
Workspace& workspace,
std::span<const std::string_view> args,
std::ostream& os = std::cout,
const std::size_t channel_index = 0
)
{
auto transform_handler = make_meta_transform_handler<2>(
getPlane_channel_description(channel_index),
[&](const auto& filtered_args, const std::string_view policy_str, std::ostream& os)
{
os << "Extracting channel " << std::to_string(channel_index) << " of " << filtered_args[0];
if (!std::ranges::empty(policy_str))
{
os << " (Policy: " << policy_str << ")";
}
os << "...\n";
return [policy_str, &os, channel_index]<typename ImageType>(ImageType&& img) -> std::any
{
auto exec_default = [&]() -> std::any
{
if constexpr (requires { TinyDIP::getPlane(std::forward<ImageType>(img), channel_index); })
{
return TinyDIP::getPlane(std::forward<ImageType>(img), channel_index);
}
else
{
throw std::invalid_argument("Input image does not support multi-channel plane extraction.");
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::getPlane(std::forward<ExecPolicy>(exec_policy), std::forward<ImageType>(img), channel_index); })
{
return TinyDIP::getPlane(std::forward<ExecPolicy>(exec_policy), std::forward<ImageType>(img), channel_index);
}
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);
}
// get_sift_potential_keypoint template function implementation
template <
typename ImageLoaderFun = MetaImageIO::Loader
>
requires (std::invocable<ImageLoaderFun, const std::string_view, Workspace&>)
constexpr void get_sift_potential_keypoint(
Workspace& workspace,