-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhpc_ds_api.hpp
1366 lines (1245 loc) · 51.3 KB
/
hpc_ds_api.hpp
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
#pragma once
#include "hpc_ds_details.hpp"
#include "hpc_ds_structs.hpp"
#include <fmt/core.h>
#include <i3d/image3d.h>
#include <i3d/transform.h>
#include <memory>
#include <ranges>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <vector>
namespace ds {
/**
* @brief Get the dataset properties object
*
* Creates a HTTP requests to server <ip>:<port> and collects data about dataset
* <uuid>. If some property is not found, atribute is left at its default value
* (Warning is emmited if in DEBUG).
*
* @param ip IP address of server (http:// at the beginning is not necessary)
* @param port Port, where the server is listening for requests
* @param uuid Unique identifier of dataset
* @return DatasetProperties
*/
inline dataset_props_ptr get_dataset_properties(const std::string& ip,
int port,
const std::string& uuid);
/**
* @brief Read full image
*
* Creates (several if needed) HTTP requests and collects whole image and
* returns it. All image properties are collected from server side.
*
* As there is no (meaningfull) way for C++ to choose correct underlying type in
* runtime, make sure to specify correct template type.
*
* @tparam T Scalar used as underlying type for image representation
* @param ip IP address of server (http:// at the beginning is not necessary)
* @param port Port, where the server is listening for requests
* @param uuid Unique identifier of dataset
* @param channel Channel, at which the image is located
* @param timepoint Timepoint, at which the image is located
* @param angle Angle, at which the image is located
* @param resolution Resolution, at which the image is located
* @param version Version, at which the image is located (integer identifier or
* "latest")
* @param props [Optional] cached dataset properties
* @return i3d::Image3d<T>
*/
template <cnpts::Scalar T>
i3d::Image3d<T> read_image(const std::string& ip,
int port,
const std::string& uuid,
int channel = 0,
int timepoint = 0,
int angle = 0,
i3d::Vector3d<int> resolution = {1, 1, 1},
const std::string& version = "latest",
dataset_props_ptr props = nullptr);
/**
* @brief Write full image
*
* Creates (several if needed) HTTP requests and sends whole image to datastore.
*
* @tparam T Scalar used as underlying type for image representation
* @param img Input image to be send to server
* @param ip IP address of server (http:// at the beginning is not necessary)
* @param port Port, where the server is listening for requests
* @param uuid Unique identifier of dataset
* @param channel Channel, at which the image is located
* @param timepoint Timepoint, at which the image is located
* @param angle Angle, at which the image is located
* @param resolution Resolution, at which the image is located
* @param version Version, at which the image is located (integer identifier or
* "latest")
* @param props [Optional] cached dataset properties
*/
template <cnpts::Scalar T>
void write_image(const i3d::Image3d<T>& img,
const std::string& ip,
int port,
const std::string& uuid,
int channel = 0,
int timepoint = 0,
int angle = 0,
i3d::Vector3d<int> resolution = {1, 1, 1},
const std::string version = "latest",
dataset_props_ptr props = nullptr);
/**
* @brief Write full image and generate pyramids
*
* Creates (several if needed) HTTP requests and sends whole image to datastore.
* Input image is considered to be full-resolution (that is: {1, 1, 1}).
* All other resolutions will be generated with selected <ResamplingMode>
* and uploaded to server as well.
*
* @tparam T Scalar used as underlying type for image representation
* @param img Input image in original resolution
* @param ip IP address of server (http:// at the beginning is not necessary)
* @param port Port, where the server is listening for requests
* @param uuid Unique identifier of dataset
* @param channel Channel, at which the image is located
* @param timepoint Timepoint, at which the image is located
* @param angle Angle, at which the image is located
* @param version Version, at which the image is located (integer identifier or
* "latest")
* @param m Sampling mode used for image resampling
* @param props [Optional] cached dataset properties
*/
template <cnpts::Scalar T>
void write_with_pyramids(const i3d::Image3d<T>& img,
const std::string& ip,
int port,
const std::string& uuid,
int channel = 0,
int timepoint = 0,
int angle = 0,
const std::string& version = "latest",
SamplingMode m = SamplingMode::NEAREST_NEIGHBOUR,
dataset_props_ptr props = nullptr);
/**
* @brief Representation of connection to specific image
*
* Class representing connection to specific image on the server.
* This class provides basic methods for read/write operations necessary to
* transfer images from/to server. This class does not cache or precollect
* any data, so the first HTTP request will be send only when corresponding
* function is called.
*/
class ImageView {
public:
/**
* @brief Construct a new Image View object
*
* @param ip IP address of server (http:// at the beginning is not
* necessary)
* @param port Port, where the server is listening for requests
* @param uuid Unique identifier of dataset
* @param channel Channel, at which the image is located
* @param timepoint Timepoint, at which the image is located
* @param angle Angle, at which the image is located
* @param resolution Resolution, at which the image is located
* @param version Version, at which the image is located (integer identifier
* or "latest")
* @param props [Optional] cached dataset properties
*/
ImageView(std::string ip,
int port,
std::string uuid,
int channel,
int timepoint,
int angle,
i3d::Vector3d<int> resolution,
std::string version);
/**
* @brief Get dataset properties
*
* @return DatasetProperties
*/
dataset_props_ptr get_properties() const;
/**
* @brief Read one block from server
*
* Reads one block of image located at <coord> and returns it.
* The information about size of the image is collected from the server.
*
* If in DEBUG, function will check wheter given coordinate corresponds
* to valid block.
*
* As there is no (meaningfull) way for C++ to choose correct underlying
* type in runtime, make sure to specify correct template type.
*
* @tparam T Scalar used as underlying type for image representation
* @param coord Block coordinate
* @param props [Optional] cached dataset properties
* @return Image containing selected block
*/
template <cnpts::Scalar T>
i3d::Image3d<T> read_block(i3d::Vector3d<int> coord,
dataset_props_ptr props = nullptr) const;
/**
* @brief Read one block from server to image
*
* Reads one block of image located at <coord> and saves it to <dest> with
* offset <dest_offset>.
*
* If in DEBUG, function will check wheter given coordinate corresponds
* to valid block as well as wheter the block fits into the image (taking
* offset into account).
*
* @tparam T Scalar used as underlying type for image representation
* @param coord Block coordinate
* @param dest Image to write data to
* @param dest_offset Offset by which the corresponding write should be
* moved
* @param props [Optional] cached dataset properties
*/
template <cnpts::Scalar T>
void read_block(i3d::Vector3d<int> coord,
i3d::Image3d<T>& dest,
i3d::Vector3d<int> dest_offset = {0, 0, 0},
dataset_props_ptr props = nullptr) const;
/**
* @brief Read blocks from server and return them
*
* Reads blocks specified in <coords> and returns them. Corresponding
* sizes are collected from server and calculated specificaly for each
* block.
*
* This function is not optimized, meaning that for each coord in <coord>,
* one HTTP request will be sent out to the server. This can heavily slow
* down speed of the application as communication via network is not cheap.
* If you do not have specific needs,most of the time it will be faster
* to collect blocks into prealocated image (second overload of
* read_blocks), however it will eat more RAM.
*
* If in DEBUG, the fucntion checks if coordinates given in <coords> points
* to a valid blocks.
*
* As there is no (meaningfull) way for C++ to choose correct underlying
* type in runtime, make sure to specify correct template type.
*
* @tparam T Scalar used as underlying type for image representation
* @param coords Block coordinates
* @param props [Optional] cached dataset properties
* @return Vector of fetched blocks (the order is the same as given in
* <coords>)
*/
template <cnpts::Scalar T>
std::vector<i3d::Image3d<T>>
read_blocks(const std::vector<i3d::Vector3d<int>>& coords,
dataset_props_ptr props = nullptr) const;
/**
* @brief Read blocks from server and saves them into prealocated image
*
* Read blocks specified in <coords> and saves them into locations given in
* <offsets>.
*
* If in DEBUG, the function checks if coordinates given in <coords> points
* to a valid blocks.
*
* @tparam T Scalar used as underlying type for image representation
* @param coords Block coordinates
* @param dest Prealocated destination image
* @param offsets Offsets at wich the corresponding blocks should be saved
* @param props [Optional] cached dataset properties
*/
template <cnpts::Scalar T>
void read_blocks(const std::vector<i3d::Vector3d<int>>& coords,
i3d::Image3d<T>& dest,
const std::vector<i3d::Vector3d<int>>& offsets,
dataset_props_ptr props = nullptr) const;
/**
* @brief Read region of interest from the server
*
* Read all neccessary blocks intersecting with chosen region from the
* server. It is neccessary, that start_point < end_point (elem-wise)..
*
* @tparam T Scalar used as underlying type for image representation
* @param start_point smallest point of the region
* @param end_point largest point of the region
* @param props [Optional] cached dataset properties
* @return i3d::Image3d<T> Selected region
*/
template <cnpts::Scalar T>
i3d::Image3d<T> read_region(i3d::Vector3d<int> start_point,
i3d::Vector3d<int> end_point,
dataset_props_ptr props = nullptr) const;
/**
* @brief Read region of interest from the server
*
* Read all neccessary blocks intersecting with chosen region from the
* server and insert region into preallocated image <dest> at <offset>.
*
* It is neccessary, that start_point < end_point (elem-wise)..
*
* @tparam T Scalar used as underlying type for image representation
* @param start_point smallest point of the region
* @param end_point largest point of the region
* @param dest destination image
* @param offset offset to destination image
* @param props [Optional] cached dataset properties
*/
template <cnpts::Scalar T>
void read_region(i3d::Vector3d<int> start_point,
i3d::Vector3d<int> end_point,
i3d::Image3d<T>& dest,
i3d::Vector3d<int> offset = {0, 0, 0},
dataset_props_ptr props = nullptr) const;
/**
* @brief Read full image
*
* Read full image from the server and return it. The information about
* dimensions are fetched from the server.
*
* As there is no (meaningfull) way for C++ to choose correct underlying
* type in runtime, make sure to specify correct template type.
*
* @tparam T Scalar used as underlying type for image representation
* @param props [Optional] cached dataset properties
* @return i3d::Image3d<T> Fetched image
*/
template <cnpts::Scalar T>
i3d::Image3d<T> read_image(dataset_props_ptr props = nullptr) const;
/**
* @brief Write block to server
*
* Write block from source image to server. The information about
* dimensions are fetched from the server.
*
* If in DEBUG, the function checks if coordinate given in <coord> points to
* a valid block, as well as wheter the offset specified for block is within
* image boundaries.
*
* @tparam T Scalar used as underlying type for image representation
* @param src Source image to collect block from
* @param coord Block coordinates
* @param src_offset Offset of given block in source image
* @param props [Optional] cached dataset properties
*/
template <cnpts::Scalar T>
void write_block(const i3d::Image3d<T>& src,
i3d::Vector3d<int> coord,
i3d::Vector3d<int> src_offset = {0, 0, 0},
dataset_props_ptr props = nullptr) const;
/**
* @brief Write blocks to server
*
* Write blocks from source image to server. The information about
* dimensions are fetched from the server.
*
* If in DEBUG, the function checks if coordinates given in <coords> points
* to a valid block, as well as wheter the offsets specified for each block
* is within image boundaries.
*
* @tparam T Scalar used as underlying type for image representation
* @param src Source image to collect blocks from
* @param coords Vector of block coordinates
* @param src_offsets Offsets of corresponding blocks in source image
* @param props [Optional] cached dataset properties
*/
template <cnpts::Scalar T>
void write_blocks(const i3d::Image3d<T>& src,
const std::vector<i3d::Vector3d<int>>& coords,
const std::vector<i3d::Vector3d<int>>& src_offsets,
dataset_props_ptr props = nullptr) const;
/**
* @brief Write image to server
*
* Write full image to server.
*
* It is recommended to make sure that the dimension of the source image is
* the same as the dimension of image at server side.
*
* Mostly, given smaller source image will emit error and fail to upload.
* Given larger source image will result in cropping.
*
* @tparam T Scalar used as underlying type for image representation
* @param img Source image
* @param props [Optional] cached dataset properties
*/
template <cnpts::Scalar T>
void write_image(const i3d::Image3d<T>& img,
dataset_props_ptr props = nullptr) const;
private:
std::string _ip;
int _port;
std::string _uuid;
int _channel;
int _timepoint;
int _angle;
i3d::Vector3d<int> _resolution;
std::string _version;
};
/**
* @brief Representation of connection to dataset
*
* Class representing connection to specific dataset on the server.
* It provides basic methods for read/write operations necessary to tranfser
* images (in the dataset) from/to server. This class does not cache or
* precollect any data, so the first HTTP request will be send only when
* corresponding function is called.
*
* All of the methods accepts arguments that uniquely identifies requested
* image. At the backend, this class tranfsers commands into ImageView objects.
*/
class Connection {
public:
/**
* @brief Construct a new Connection object
*
* @param ip IP address of server (http:// at the beginning is not
* necessary)
* @param port Port, where the server is listening for requests
* @param uuid Unique identifier of dataset
*/
Connection(std::string ip, int port, std::string uuid);
/**
* @brief Get ImageView of specified image.
*
* @param channel Channel, at which the image is located
* @param timepoint Timepoint, at which the image is located
* @param angle Angle, at which the image is located
* @param resolution Resolution, at which the image is located
* @param version Version, at which the image is located (integer identifier
* or "latest")
* @return ImageView
*/
ImageView get_view(int channel,
int timepoint,
int angle,
i3d::Vector3d<int> resolution,
const std::string& version) const;
/**
* @brief Get dataset properties
*
* @return DatasetProperties
*/
dataset_props_ptr get_properties() const;
/**
* @brief Read one block from server to image
*
* Reads one block of image located at <coord> and saves it to <dest> with
* offset <dest_offset>.
*
* If in DEBUG, function will check wheter given coordinate corresponds
* to valid block as well as wheter the block fits into the image (taking
* offset into account).
*
* @tparam T Scalar used as underlying type for image representation
* @param coord Block coordinate
* @param channel Channel, at which the image is located
* @param timepoint Timepoint, at which the image is located
* @param angle Angle, at which the image is located
* @param resolution Resolution, at which the image is located
* @param version Version, at which the image is located (integer identifier
* or "latest")
* @param props [Optional] cached dataset properties
* @return Image containing selected block
*/
template <cnpts::Scalar T>
i3d::Image3d<T> read_block(i3d::Vector3d<int> coord,
int channel,
int timepoint,
int angle,
i3d::Vector3d<int> resolution,
const std::string& version,
dataset_props_ptr props = nullptr) const;
/**
* @brief Read one block from server to image
*
* Reads one block of image located at <coord> and saves it to <dest> with
* offset <dest_offset>.
*
* If in DEBUG, function will check wheter given coordinate corresponds
* to valid block as well as wheter the block fits into the image (taking
* offset into account).
*
* @tparam T Scalar used as underlying type for image representation
* @param coord Block coordinate
* @param dest Image to write data to
* @param dest_offset Offset by which the corresponding write should be
* moved
* @param channel Channel, at which the image is located
* @param timepoint Timepoint, at which the image is located
* @param angle Angle, at which the image is located
* @param resolution Resolution, at which the image is located
* @param version Version, at which the image is located (integer identifier
* or "latest")
* @param props [Optional] cached dataset properties
*/
template <cnpts::Scalar T>
void read_block(i3d::Vector3d<int> coord,
i3d::Image3d<T>& dest,
i3d::Vector3d<int> dest_offset,
int channel,
int timepoint,
int angle,
i3d::Vector3d<int> resolution,
const std::string& version,
dataset_props_ptr props = nullptr) const;
/**
* @brief Read blocks from server and return them
*
* Reads blocks specified in <coords> and returns them. Corresponding
* sizes are collected from server and calculated specificaly for each
* block.
*
* This function is not optimized, meaning that for each coord in <coord>,
* one HTTP request will be sent out to the server. This can heavily slow
* down speed of the application as communication via network is not cheap.
* If you do not have specific needs,most of the time it will be faster
* to collect blocks into prealocated image (second overload of
* read_blocks), however it will eat more RAM.
*
* If in DEBUG, the fucntion checks if coordinates given in <coords> points
* to a valid blocks.
*
* As there is no (meaningfull) way for C++ to choose correct underlying
* type in runtime, make sure to specify correct template type.
*
* @tparam T Scalar used as underlying type for image representation
* @param coords Block coordinates
* @param channel Channel, at which the image is located
* @param timepoint Timepoint, at which the image is located
* @param angle Angle, at which the image is located
* @param resolution Resolution, at which the image is located
* @param version Version, at which the image is located (integer identifier
* or "latest")
* @param props [Optional] cached dataset properties
* @return Vector of fetched blocks (the order is the same as given in
* <coords>)
*/
template <cnpts::Scalar T>
std::vector<i3d::Image3d<T>>
read_blocks(const std::vector<i3d::Vector3d<int>>& coords,
int channel,
int timepoint,
int angle,
i3d::Vector3d<int> resolution,
const std::string& version,
dataset_props_ptr props = nullptr) const;
/**
* @brief Read blocks from server and saves them into prealocated image
*
* Read blocks specified in <coords> and saves them into locations given in
* <offsets>.
*
* If in DEBUG, the function checks if coordinates given in <coords> points
* to a valid blocks, as well as wheter the offsets specified for each block
* are within image boundaries.
*
* @tparam T Scalar used as underlying type for image representation
* @param coords Block coordinates
* @param dest Prealocated destination image
* @param dest_offsets Offsets at wich the corresponding blocks should be
* saved
* @param channel Channel, at which the image is located
* @param timepoint Timepoint, at which the image is located
* @param angle Angle, at which the image is located
* @param resolution Resolution, at which the image is located
* @param version Version, at which the image is located (integer identifier
* or "latest")
* @param props [Optional] cached dataset properties
*/
template <cnpts::Scalar T>
void read_blocks(const std::vector<i3d::Vector3d<int>>& coords,
i3d::Image3d<T>& dest,
const std::vector<i3d::Vector3d<int>>& dest_offsets,
int channel,
int timepoint,
int angle,
i3d::Vector3d<int> resolution,
const std::string& version,
dataset_props_ptr props = nullptr) const;
/**
* @brief Read region of interest from the server
*
* Read all neccessary blocks intersecting with chosen region from the
* server. It is neccessary, that start_point < end_point (elem-wise)..
*
* @tparam T Scalar used as underlying type for image representation
* @param start_point smallest point of the region
* @param end_point largest point of the region
* @param channel Channel, at which the image is located
* @param timepoint Timepoint, at which the image is located
* @param angle Angle, at which the image is located
* @param resolution Resolution, at which the image is located
* @param version Version, at which the image is located (integer identifier
* or "latest")
* @param props [Optional] cached dataset properties
* @return Image containing selected block
*/
template <cnpts::Scalar T>
i3d::Image3d<T> read_region(i3d::Vector3d<int> start_point,
i3d::Vector3d<int> end_point,
int channel,
int timepoint,
int angle,
i3d::Vector3d<int> resolution,
const std::string& version,
dataset_props_ptr props = nullptr) const;
/**
* @brief Read region of interest from the server
*
* Read all neccessary blocks intersecting with chosen region from the
* server and insert region into preallocated image <dest> at <offset>.
*
* It is neccessary, that start_point < end_point (elem-wise)..
*
* @tparam T Scalar used as underlying type for image representation
* @param start_point smallest point of the region
* @param end_point largest point of the region
* @param dest destination image
* @param offset offset to destination image
* @param channel Channel, at which the image is located
* @param timepoint Timepoint, at which the image is located
* @param angle Angle, at which the image is located
* @param resolution Resolution, at which the image is located
* @param version Version, at which the image is located (integer identifier
* or "latest")
* @param props [Optional] cached dataset properties
*/
template <cnpts::Scalar T>
void read_region(i3d::Vector3d<int> start_point,
i3d::Vector3d<int> end_point,
i3d::Image3d<T>& dest,
i3d::Vector3d<int> offset,
int channel,
int timepoint,
int angle,
i3d::Vector3d<int> resolution,
const std::string& version,
dataset_props_ptr props = nullptr) const;
/**
* @brief Read full image
*
* Read full image from the server and return it. The information about
* dimensions are fetched from the server.
*
* As there is no (meaningfull) way for C++ to choose correct underlying
* type in runtime, make sure to specify correct template type.
*
* @tparam T Scalar used as underlying type for image representation
* @param channel Channel, at which the image is located
* @param timepoint Timepoint, at which the image is located
* @param angle Angle, at which the image is located
* @param resolution Resolution, at which the image is located
* @param version Version, at which the image is located (integer identifier
* or "latest")
* @param props [Optional] cached dataset properties
* @return i3d::Image3d<T> etched image
*/
template <cnpts::Scalar T>
i3d::Image3d<T> read_image(int channel,
int timepoint,
int angle,
i3d::Vector3d<int> resolution,
const std::string& version,
dataset_props_ptr props = nullptr) const;
/**
* @brief Write block to server
*
* Write block from source image to server. The information about
* dimensions are fetched from the server.
*
* If in DEBUG, the function checks if coordinate given in <coord> points to
* a valid block, as well as wheter the offset specified for block is within
* image boundaries.
*
* @tparam T Scalar used as underlying type for image representation
* @param src Source image to collect block from
* @param coord Block coordinates
* @param src_offset Offset of given block in source image
* @param channel Channel, at which the image is located
* @param timepoint Timepoint, at which the image is located
* @param angle Angle, at which the image is located
* @param resolution Resolution, at which the image is located
* @param version Version, at which the image is located (integer identifier
* or "latest")
* @param props [Optional] cached dataset properties
*/
template <cnpts::Scalar T>
void write_block(const i3d::Image3d<T>& src,
i3d::Vector3d<int> coord,
i3d::Vector3d<int> src_offset,
int channel,
int timepoint,
int angle,
i3d::Vector3d<int> resolution,
const std::string& version,
dataset_props_ptr props = nullptr) const;
/**
* @brief Write blocks to server
*
* Write blocks from source image to server. The information about
* dimensions are fetched from the server.
*
* If in DEBUG, the function checks if coordinates given in <coords> points
* to a valid block, as well as wheter the offsets specified for each block
* is within image boundaries.
*
* @tparam T Scalar used as underlying type for image representation
* @param src Source image to collect blocks from
* @param coords Vector of block coordinates
* @param src_offsets Offsets of corresponding blocks in source image
* @param channel Channel, at which the image is located
* @param timepoint Timepoint, at which the image is located
* @param angle Angle, at which the image is located
* @param resolution Resolution, at which the image is located
* @param version Version, at which the image is located (integer identifier
* or "latest")
* @param props [Optional] cached dataset properties
*/
template <cnpts::Scalar T>
void write_blocks(const i3d::Image3d<T>& src,
const std::vector<i3d::Vector3d<int>>& coords,
const std::vector<i3d::Vector3d<int>>& src_offsets,
int channel,
int timepoint,
int angle,
i3d::Vector3d<int> resolution,
const std::string& version,
dataset_props_ptr props = nullptr) const;
/**
* @brief Write image to server
*
* Write full image to server.
*
* It is recommended to make sure that the dimension of the source image is
* the same as the dimension of image at server side.
*
* Mostly, given smaller source image will emit error and fail to upload.
* Given larger source image will result in cropping.
*
* @tparam T Scalar used as underlying type for image representation
* @param img Source image
* @param channel Channel, at which the image is located
* @param timepoint Timepoint, at which the image is located
* @param angle Angle, at which the image is located
* @param resolution Resolution, at which the image is located
* @param version Version, at which the image is located (integer identifier
* or "latest")
* @param props [Optional] cached dataset properties
*/
template <cnpts::Scalar T>
void write_image(const i3d::Image3d<T>& img,
int channel,
int timepoint,
int angle,
i3d::Vector3d<int> resolution,
const std::string& version,
dataset_props_ptr props = nullptr) const;
/**
* @brief Write full image and generate pyramids
*
* Creates (several if needed) HTTP requests and sends whole image to
datastore.
* Input image is considered to be full-resolution (that is: {1, 1, 1}).
* All other resolutions will be generated with selected <ResamplingMode>
* and uploaded to server as well.
* @tparam T Scalar used as underlying type for image representation
* @param img Input image in original resolution
* @param channel Channel, at which the image is located
* @param timepoint Timepoint, at which the image is located
* @param angle Angle, at which the image is located
* @param version Version, at which the image is located (integer identifier
or
* "latest")
* @param m Sampling mode used for image resampling
* @param props [Optional] cached dataset properties
*/
template <cnpts::Scalar T>
void write_with_pyramids(const i3d::Image3d<T>& img,
int channel,
int timepoint,
int angle,
const std::string& version,
SamplingMode m,
dataset_props_ptr props = nullptr) const;
private:
std::string _ip;
int _port;
std::string _uuid;
};
} // namespace ds
/* ================= IMPLEMENTATION FOLLOWS ======================== */
namespace ds {
/* ===================================== Global space */
/* inline */ dataset_props_ptr get_dataset_properties(const std::string& ip,
int port,
const std::string& uuid) {
std::string dataset_url = details::get_dataset_url(ip, port, uuid);
return std::make_unique<DatasetProperties>(
details::get_dataset_properties(dataset_url));
}
template <cnpts::Scalar T>
i3d::Image3d<T> read_image(const std::string& ip,
int port,
const std::string& uuid,
int channel /* = 0 */,
int timepoint /* = 0 */,
int angle /* = 0 */,
i3d::Vector3d<int> resolution /* = {1, 1, 1} */,
const std::string& version /* = "latest" */,
dataset_props_ptr props /* = nullptr */) {
return ImageView(ip, port, uuid, channel, timepoint, angle, resolution,
version)
.read_image<T>(props);
}
template <cnpts::Scalar T>
void write_image(const i3d::Image3d<T>& img,
const std::string& ip,
int port,
const std::string& uuid,
int channel /* = 0 */,
int timepoint /* = 0 */,
int angle /* = 0 */,
i3d::Vector3d<int> resolution /* = {1, 1, 1} */,
const std::string version /* = "latest" */,
dataset_props_ptr props /* = nullptr */) {
ImageView(ip, port, uuid, channel, timepoint, angle, resolution, version)
.write_image(img, props);
}
template <cnpts::Scalar T>
void write_with_pyramids(const i3d::Image3d<T>& img,
const std::string& ip,
int port,
const std::string& uuid,
int channel /* = 0 */,
int timepoint /* = 0 */,
int angle /* = 0 */,
const std::string& version /* = "latest"*/,
SamplingMode m /* = SamplingMode::NEAREST_NEIGHBOUR */,
dataset_props_ptr props /* = nullptr */) {
Connection(ip, port, uuid)
.write_with_pyramids(img, channel, timepoint, angle, version, m, props);
}
/* ===================================== ImageView */
ImageView::ImageView(std::string ip,
int port,
std::string uuid,
int channel,
int timepoint,
int angle,
i3d::Vector3d<int> resolution,
std::string version)
: _ip(std::move(ip)), _port(port), _uuid(std::move(uuid)),
_channel(channel), _timepoint(timepoint), _angle(angle),
_resolution(resolution), _version(std::move(version)) {}
dataset_props_ptr ImageView::get_properties() const {
return get_dataset_properties(_ip, _port, _uuid);
}
template <cnpts::Scalar T>
i3d::Image3d<T>
ImageView::read_block(i3d::Vector3d<int> coord,
dataset_props_ptr props /* = nullptr */) const {
/* Fetch properties from server */
if (!props)
props = get_properties();
i3d::Vector3d<int> block_dim = props->get_block_dimensions(_resolution);
/* Prepare output image */
i3d::Image3d<T> img;
i3d::Vector3d<int> block_size = details::data_manip::get_block_size(
coord, block_dim, props->get_img_dimensions(_resolution));
img.MakeRoom(block_size);
/* Fetch and return */
read_block(coord, img);
return img;
}
template <cnpts::Scalar T>
void ImageView::read_block(i3d::Vector3d<int> coord,
i3d::Image3d<T>& dest,
i3d::Vector3d<int> dest_offset /* = {0, 0, 0} */,
dataset_props_ptr props /* = nullptr */) const {
read_blocks({coord}, dest, {dest_offset}, props);
}
template <cnpts::Scalar T>
std::vector<i3d::Image3d<T>>
ImageView::read_blocks(const std::vector<i3d::Vector3d<int>>& coords,
dataset_props_ptr props /* = nullptr */) const {
if (!props)
props = get_properties();
/* Process blocks one by one */
std::vector<i3d::Image3d<T>> out;
for (auto coord : coords)
out.push_back(read_block<T>(coord));
return out;
}
template <cnpts::Scalar T>
void ImageView::read_blocks(const std::vector<i3d::Vector3d<int>>& coords,
i3d::Image3d<T>& dest,
const std::vector<i3d::Vector3d<int>>& offsets,
dataset_props_ptr props /* = nullptr */
) const {
if (!props)
props = get_properties();
if (!details::matches_image_type(dest, props->voxel_type))
throw std::logic_error("Server and i3d image type does not match\n");
auto resolutions = props->get_all_resolutions();
if (std::ranges::find(resolutions, _resolution) == end(resolutions))
throw std::logic_error(
fmt::format("Resolution {} not supported by server\n",
details::to_string(_resolution))
.c_str());
if (!props->timepoint_ids.contains(_timepoint))
throw std::logic_error(
fmt::format("Timepoint {} not supported by server\n",
details::to_string(_timepoint))
.c_str());
if (_channel >= props->channels)
throw std::logic_error(
fmt::format("Channel {} not supported by server\n",
details::to_string(_channel))
.c_str());
if (_angle >= props->angles)
throw std::logic_error(fmt::format("Angle {} not supported by server\n",
details::to_string(_angle))
.c_str());
/* Fetched properties from server */
std::string dataset_url = details::get_dataset_url(_ip, _port, _uuid);
i3d::Vector3d<int> block_dim = props->get_block_dimensions(_resolution);
i3d::Vector3d<int> img_dim = props->get_img_dimensions(_resolution);
if (coords.size() != offsets.size())
throw std::logic_error("Count of coordinates != count of offsets");
if (!details::check_block_coords(coords, img_dim, block_dim))
throw std::out_of_range("Blocks out of range");
/* prepare request url */
std::string session_url = details::requests::session_url_request(
dataset_url, _resolution, _version);
if (session_url.ends_with('/'))
session_url.pop_back();
std::vector<std::pair<std::string, std::vector<std::size_t>>> requests =
details::create_requests(coords, session_url, _timepoint, _channel,
_angle);
for (const auto& [req, idxs] : requests) {
auto [data, response] = details::requests::make_request(req);
std::size_t start_i = 0;
for (std::size_t i : idxs) {
i3d::Vector3d<int> block_size =
props->get_block_size(coords[i], _resolution);
std::size_t data_size = details::data_manip::get_block_data_size(
block_size, props->voxel_type);
details::data_manip::read_data(
std::span(data.begin() + start_i, data_size), props->voxel_type,
dest, offsets[i], block_size);
start_i += data_size;
}
}
}
template <cnpts::Scalar T>
i3d::Image3d<T>
ImageView::read_region(i3d::Vector3d<int> start_point,