-
Notifications
You must be signed in to change notification settings - Fork 95
/
interfile.cxx
1524 lines (1322 loc) · 60.3 KB
/
interfile.cxx
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
/*
Copyright (C) 2000 PARAPET partners
Copyright (C) 2000- 2011, Hammersmith Imanet Ltd
Copyright (C) 2013, 2018, 2023, 2024 University College London
Copyright 2017 ETH Zurich, Institute of Particle Physics and Astrophysics
This file is part of STIR.
SPDX-License-Identifier: Apache-2.0 AND License-ref-PARAPET-license
See STIR/LICENSE.txt for details
*/
/*!
\file
\ingroup InterfileIO
\brief Implementation of functions which read/write Interfile data
\author Kris Thielemans
\author Sanida Mustafovic
\author PARAPET project
\author Richard Brown
\author Parisa Khateri
*/
// Pretty horrible implementations at the moment...
#include "stir/ExamInfo.h"
#include "stir/TimeFrameDefinitions.h"
#include "stir/PatientPosition.h"
#include "stir/ImagingModality.h"
#include "stir/IO/interfile.h"
#include "stir/interfile_keyword_functions.h"
#include "stir/IO/InterfileHeader.h"
#include "stir/IO/InterfileHeaderSiemens.h"
#include "stir/IO/InterfilePDFSHeaderSPECT.h"
#include "stir/IndexRange3D.h"
#include "stir/utilities.h"
#include "stir/CartesianCoordinate3D.h"
#include "stir/VoxelsOnCartesianGrid.h"
#include "stir/ProjDataFromStream.h"
#include "stir/ProjDataInfoCylindricalArcCorr.h"
#include "stir/Scanner.h"
#include "stir/Succeeded.h"
#include "stir/IO/write_data.h"
#include "stir/IO/read_data.h"
#include "stir/is_null_ptr.h"
#include "stir/Bin.h"
#include "stir/stream.h"
#include "stir/error.h"
#include "stir/warning.h"
#include "stir/DynamicDiscretisedDensity.h"
#include "stir/modelling/ParametricDiscretisedDensity.h"
#include "stir/date_time_functions.h"
#include <boost/format.hpp>
#include <fstream>
#include <algorithm>
#include "stir/ProjDataInfoBlocksOnCylindricalNoArcCorr.h"
#include "stir/ProjDataInfoGenericNoArcCorr.h"
#include "stir/ProjDataInfoSubsetByView.h"
using std::cerr;
using std::endl;
using std::ofstream;
using std::ifstream;
using std::iostream;
using std::fstream;
using std::string;
using std::vector;
using std::istream;
using std::string;
using std::ios;
START_NAMESPACE_STIR
bool
is_interfile_signature(const char* const signature)
{
// checking for "interfile :"
const char* pos_of_colon = strchr(signature, ':');
if (pos_of_colon == 0)
return false;
string keyword(signature, pos_of_colon - signature);
return (standardise_interfile_keyword(keyword) == standardise_interfile_keyword("interfile"));
}
// help function
static VoxelsOnCartesianGrid<float>*
create_image_and_header_from(InterfileImageHeader& hdr,
char* full_data_file_name, // preallocated
istream& input,
const string& directory_for_data)
{
if (!hdr.parse(input))
{
return 0; // KT 10/12/2001 do not call ask_parameters anymore
}
// prepend directory_for_data to the data_file_name from the header
strcpy(full_data_file_name, hdr.data_file_name.c_str());
prepend_directory_name(full_data_file_name, directory_for_data.c_str());
CartesianCoordinate3D<float> voxel_size(
static_cast<float>(hdr.pixel_sizes[2]), static_cast<float>(hdr.pixel_sizes[1]), static_cast<float>(hdr.pixel_sizes[0]));
const int z_size = hdr.matrix_size[2][0];
const int y_size = hdr.matrix_size[1][0];
const int x_size = hdr.matrix_size[0][0];
const BasicCoordinate<3, int> min_indices = make_coordinate(0, -y_size / 2, -x_size / 2);
const BasicCoordinate<3, int> max_indices = min_indices + make_coordinate(z_size, y_size, x_size) - 1;
CartesianCoordinate3D<float> origin(0, 0, 0);
if (hdr.first_pixel_offsets[2] != InterfileHeader::double_value_not_set)
{
// make sure that origin is such that
// first_pixel_offsets = min_indices*voxel_size + origin
origin = make_coordinate(
float(hdr.first_pixel_offsets[2]), float(hdr.first_pixel_offsets[1]), float(hdr.first_pixel_offsets[0]))
- voxel_size * BasicCoordinate<3, float>(min_indices);
}
return new VoxelsOnCartesianGrid<float>(hdr.get_exam_info_sptr(), IndexRange<3>(min_indices, max_indices), origin, voxel_size);
}
VoxelsOnCartesianGrid<float>*
read_interfile_image(istream& input, const string& directory_for_data)
{
InterfileImageHeader hdr;
char full_data_file_name[max_filename_length];
VoxelsOnCartesianGrid<float>* image_ptr = create_image_and_header_from(hdr, full_data_file_name, input, directory_for_data);
ifstream data_in;
open_read_binary(data_in, full_data_file_name);
data_in.seekg(hdr.data_offset_each_dataset[0]);
if (hdr.data_offset_each_dataset[0] > 0)
data_in.seekg(hdr.data_offset_each_dataset[0]);
// read into image_sptr first
float scale = float(1);
if (read_data(data_in, *image_ptr, hdr.type_of_numbers, scale, hdr.file_byte_order) == Succeeded::no || scale != 1)
{
warning("read_interfile_image: error reading data or scale factor returned by read_data not equal to 1\n");
return 0;
}
for (int i = 0; i < hdr.matrix_size[2][0]; i++)
if (hdr.image_scaling_factors[0][i] != 1)
(*image_ptr)[i] *= static_cast<float>(hdr.image_scaling_factors[0][i]);
// Check number of time frames
if (image_ptr->get_exam_info().get_time_frame_definitions().get_num_frames() > 1)
{
warning(str(boost::format("Discretised density should contain 1 time frame, but this image contains %1%. "
"Only the first will be kept, and the rest discarded.")
% image_ptr->get_exam_info().get_time_frame_definitions().get_num_frames()));
ExamInfo exam_info = image_ptr->get_exam_info();
exam_info.time_frame_definitions.set_num_time_frames(1);
image_ptr->set_exam_info(exam_info);
}
else if (image_ptr->get_exam_info().get_time_frame_definitions().get_num_frames() == 0)
warning("DiscretisedDensity does not contain any time frames. This might cause an error.");
return image_ptr;
}
DynamicDiscretisedDensity*
read_interfile_dynamic_image(istream& input, const string& directory_for_data)
{
InterfileImageHeader hdr;
char full_data_file_name[max_filename_length];
shared_ptr<DiscretisedDensity<3, float>> image_sptr(
create_image_and_header_from(hdr, full_data_file_name, input, directory_for_data));
if (is_null_ptr(image_sptr))
error("Error parsing dynamic image");
shared_ptr<Scanner> scanner_sptr(Scanner::get_scanner_from_name(hdr.get_exam_info().originating_system));
DynamicDiscretisedDensity* dynamic_dens_ptr = new DynamicDiscretisedDensity(
hdr.get_exam_info().time_frame_definitions, hdr.get_exam_info().start_time_in_secs_since_1970, scanner_sptr, image_sptr);
ifstream data_in;
open_read_binary(data_in, full_data_file_name);
data_in.seekg(hdr.data_offset_each_dataset[0]);
ExamInfo _exam_info(hdr.get_exam_info());
for (unsigned int frame_num = 1; frame_num <= dynamic_dens_ptr->get_num_time_frames(); ++frame_num)
{
data_in.seekg(hdr.data_offset_each_dataset[frame_num - 1]);
// read into image_sptr first
float scale = float(1);
if (read_data(data_in, *image_sptr, hdr.type_of_numbers, scale, hdr.file_byte_order) == Succeeded::no
|| fabs(scale - float(1)) > float(1e-10))
{
warning("read_interfile_dynamic_image: error reading data or scale factor returned by read_data not equal to 1");
return 0;
}
for (int i = 0; i < hdr.matrix_size[2][0]; i++)
if (fabs(hdr.image_scaling_factors[frame_num - 1][i] - double(1)) > double(1e-10))
(*image_sptr)[i] *= static_cast<float>(hdr.image_scaling_factors[frame_num - 1][i]);
// Set the time frame of the individual frame
_exam_info.time_frame_definitions = TimeFrameDefinitions(hdr.get_exam_info().time_frame_definitions, frame_num);
image_sptr->set_exam_info(_exam_info);
// now stick into the dynamic image
dynamic_dens_ptr->set_density(*image_sptr, frame_num);
}
return dynamic_dens_ptr;
}
ParametricVoxelsOnCartesianGrid*
read_interfile_parametric_image(istream& input, const string& directory_for_data)
{
InterfileImageHeader hdr;
char full_data_file_name[max_filename_length];
shared_ptr<DiscretisedDensity<3, float>> image_sptr(
create_image_and_header_from(hdr, full_data_file_name, input, directory_for_data));
if (is_null_ptr(image_sptr))
error("Error parsing parametric image");
shared_ptr<Scanner> scanner_sptr(Scanner::get_scanner_from_name(hdr.get_exam_info().originating_system));
BasicCoordinate<3, float> voxel_size;
voxel_size[1] = hdr.pixel_sizes[2];
voxel_size[2] = hdr.pixel_sizes[1];
voxel_size[3] = hdr.pixel_sizes[0];
ParametricVoxelsOnCartesianGrid* parametric_dens_ptr
= new ParametricVoxelsOnCartesianGrid(ParametricVoxelsOnCartesianGridBaseType(
hdr.get_exam_info_sptr(), image_sptr->get_index_range(), image_sptr->get_origin(), voxel_size));
ifstream data_in;
open_read_binary(data_in, full_data_file_name);
data_in.seekg(hdr.data_offset_each_dataset[0]);
// loop over each of the parametric image types (e.g., slope, intercept)
for (int kin_param = 1; kin_param <= hdr.num_image_data_types; kin_param++)
{
data_in.seekg(hdr.data_offset_each_dataset[kin_param - 1]);
// read into image_sptr first
float scale = float(1);
if (read_data(data_in, *image_sptr, hdr.type_of_numbers, scale, hdr.file_byte_order) == Succeeded::no || scale != 1)
{
warning("read_interfile_parametric_image: error reading data or scale factor returned by read_data not equal to 1");
return 0;
}
for (int i = 0; i < hdr.matrix_size[2][0]; i++)
if (hdr.image_scaling_factors[kin_param - 1][i] != 1)
(*image_sptr)[i] *= static_cast<float>(hdr.image_scaling_factors[kin_param - 1][i]);
// Check that we're dealing with VoxelsOnCartesianGrid
if (dynamic_cast<const VoxelsOnCartesianGrid<float>*>(image_sptr.get()) == 0)
error("ParametricDiscretisedDensity::read_from_file only supports VoxelsOnCartesianGrid");
// Set the image for the given kinetic parameter
ParametricVoxelsOnCartesianGrid::SingleDiscretisedDensityType::const_full_iterator single_density_iter
= image_sptr->begin_all();
ParametricVoxelsOnCartesianGrid::SingleDiscretisedDensityType::const_full_iterator end_single_density_iter
= image_sptr->end_all();
ParametricVoxelsOnCartesianGrid::full_densel_iterator parametric_density_iter = parametric_dens_ptr->begin_all_densel();
while (single_density_iter != end_single_density_iter)
{
(*parametric_density_iter)[kin_param] = *single_density_iter;
++single_density_iter;
++parametric_density_iter;
}
}
return parametric_dens_ptr;
}
VoxelsOnCartesianGrid<float>*
read_interfile_image(const string& filename)
{
ifstream image_stream(filename.c_str());
if (!image_stream)
{
error("read_interfile_image: couldn't open file %s\n", filename.c_str());
}
char directory_name[max_filename_length];
get_directory_name(directory_name, filename.c_str());
return read_interfile_image(image_stream, directory_name);
}
DynamicDiscretisedDensity*
read_interfile_dynamic_image(const string& filename)
{
ifstream image_stream(filename.c_str());
if (!image_stream)
{
error("read_interfile_dynamic_image: couldn't open file %s\n", filename.c_str());
}
char directory_name[max_filename_length];
get_directory_name(directory_name, filename.c_str());
return read_interfile_dynamic_image(image_stream, directory_name);
}
ParametricVoxelsOnCartesianGrid*
read_interfile_parametric_image(const string& filename)
{
ifstream image_stream(filename.c_str());
if (!image_stream)
{
error("read_interfile_parametric_image: couldn't open file %s\n", filename.c_str());
}
char directory_name[max_filename_length];
get_directory_name(directory_name, filename.c_str());
return read_interfile_parametric_image(image_stream, directory_name);
}
#if 0
const VectorWithOffset<std::streamoff>
compute_file_offsets(int number_of_time_frames,
const NumericType output_type,
const CartesianCoordinate3D<int>& dim,
std::streamoff initial_offset)
{
const std::size_t a= dim.x()*dim.y()*dim.z()*output_type.size_in_bytes();
VectorWithOffset<std::streamoff> temp(number_of_time_frames);
{
for (int i=0; i<=number_of_time_frames-1;i++)
temp[i]= initial_offset +i*a;
}
return temp;
}
#endif
/* A function that finds the appropriate filename for the binary data
to write in the header. It tries to cut the directory part of
data_file_name if it's the same as the directory part of the header.
*/
static string
interfile_get_data_file_name_in_header(const string& header_file_name, const string& data_file_name)
{
const string dir_name_of_binary_data = get_directory_name(data_file_name);
if (dir_name_of_binary_data.size() == 0)
{
// data_dirname is empty
return data_file_name;
}
const string dir_name_of_header = get_directory_name(header_file_name);
if (dir_name_of_header == dir_name_of_binary_data)
{
// dirnames are the same, so strip from data_file_name
return string(data_file_name, find_pos_of_filename(data_file_name), string::npos);
}
else
{
// just copy, what else to do?
return data_file_name;
}
}
//// some static helper functions for writing
// probably should be moved to InterfileHeader
static void
write_interfile_patient_position(std::ostream& output_header, const ExamInfo& exam_info)
{
string orientation;
switch (exam_info.patient_position.get_orientation())
{
case PatientPosition::head_in:
orientation = "head_in";
break;
case PatientPosition::feet_in:
orientation = "feet_in";
break;
case PatientPosition::other_orientation:
orientation = "other";
break;
default:
orientation = "unknown";
break;
}
string rotation;
switch (exam_info.patient_position.get_rotation())
{
case PatientPosition::prone:
rotation = "prone";
break;
case PatientPosition::supine:
rotation = "supine";
break;
case PatientPosition::other_rotation:
case PatientPosition::left:
case PatientPosition::right:
rotation = "other";
break;
default:
rotation = "unknown";
break;
}
if (orientation != "unknown")
output_header << "patient orientation := " << orientation << '\n';
if (rotation != "unknown")
output_header << "patient rotation := " << rotation << '\n';
}
static void
write_interfile_time_frame_definitions(std::ostream& output_header, const ExamInfo& exam_info)
// TODO this is according to the proposed interfile standard for PET. Interfile 3.3 is different
{
const TimeFrameDefinitions& frame_defs(exam_info.time_frame_definitions);
if (frame_defs.get_num_time_frames() > 0)
{
output_header << "number of time frames := " << frame_defs.get_num_time_frames() << '\n';
for (unsigned int frame_num = 1; frame_num <= frame_defs.get_num_time_frames(); ++frame_num)
{
if (frame_defs.get_duration(frame_num) > 0)
{
output_header << "image duration (sec)[" << frame_num << "] := " << frame_defs.get_duration(frame_num) << '\n';
output_header << "image relative start time (sec)[" << frame_num << "] := " << frame_defs.get_start_time(frame_num)
<< '\n';
}
}
}
else
{
// need to write this anyway to allow vectored keys below
output_header << "number of time frames := 1\n";
}
}
// Write energy window lower and upper thresholds, if they are not -1
static void
write_interfile_energy_windows(std::ostream& output_header, const ExamInfo& exam_info)
{
if (exam_info.get_high_energy_thres() > 0 && exam_info.get_low_energy_thres() >= 0)
{
output_header << "number of energy windows := 1\n";
output_header << "energy window lower level[1] := " << exam_info.get_low_energy_thres() << '\n';
output_header << "energy window upper level[1] := " << exam_info.get_high_energy_thres() << '\n';
}
}
// Write data type descriptions (if there are any)
static void
write_interfile_image_data_descriptions(std::ostream& output_header, const std::vector<std::string>& data_type_descriptions)
{
if (data_type_descriptions.size() == 0)
return;
output_header << "number of image data types := " << data_type_descriptions.size() << '\n';
output_header << "index nesting level := {data type}\n";
for (unsigned int i = 0; i < data_type_descriptions.size(); i++)
output_header << "image data type description[" << i + 1 << "] := " << data_type_descriptions[i] << "\n";
}
static void
write_interfile_modality(std::ostream& output_header, const ExamInfo& exam_info)
{
/*
// default modality is PET
ImagingModality imaging_modality(ImagingModality::PT);
if (!is_null_ptr(exam_info_ptr))
imaging_modality=exam_info_ptr->imaging_modality;
*/
if (exam_info.imaging_modality.get_modality() != ImagingModality::Unknown)
output_header << "!imaging modality := " << exam_info.imaging_modality.get_name() << '\n';
}
static void
write_interfile_radionuclide_info(std::ostream& output_header, const ExamInfo& exam_info)
{
const auto radionuclide = exam_info.get_radionuclide();
// const bool is_spect = exam_info.imaging_modality.get_modality() == ImagingModality::NM;
// TODO we only support one
output_header << "number of radionuclides := 1\n";
if (!radionuclide.get_name().empty() && radionuclide.get_name() != "Unknown")
output_header << "radionuclide name[1] := " << radionuclide.get_name() << '\n';
if (radionuclide.get_half_life(false) > 0)
{
output_header << "radionuclide halflife (sec)[1] := " << radionuclide.get_half_life() << '\n';
}
if (radionuclide.get_branching_ratio(false) > 0)
{
output_header << "radionuclide branching factor[1] := " << radionuclide.get_branching_ratio() << '\n';
}
}
static void
interfile_create_filenames(const std::string& filename, std::string& data_name, std::string& header_name)
{
data_name = filename;
string::size_type pos = find_pos_of_extension(filename);
if (pos != string::npos && filename.substr(pos) == ".hv")
replace_extension(data_name, ".v");
else
add_extension(data_name, ".v");
header_name = filename;
replace_extension(header_name, ".hv");
}
////// end static functions
Succeeded
write_basic_interfile_image_header(const string& header_file_name,
const string& image_file_name,
const ExamInfo& exam_info,
const IndexRange<3>& index_range,
const CartesianCoordinate3D<float>& voxel_size,
const CartesianCoordinate3D<float>& origin,
const NumericType output_type,
const ByteOrder byte_order,
const VectorWithOffset<float>& scaling_factors,
const VectorWithOffset<unsigned long>& file_offsets,
const std::vector<std::string>& data_type_descriptions)
{
CartesianCoordinate3D<int> min_indices;
CartesianCoordinate3D<int> max_indices;
if (!index_range.get_regular_range(min_indices, max_indices))
{
warning("write_basic_interfile: can handle only regular index ranges\n. No output\n");
return Succeeded::no;
}
CartesianCoordinate3D<int> dimensions = max_indices - min_indices + 1;
string header_name = header_file_name;
add_extension(header_name, ".hv");
ofstream output_header(header_name.c_str(), ios::out);
if (!output_header.good())
{
warning("Error opening Interfile header '%s' for writing\n", header_name.c_str());
return Succeeded::no;
}
// handle directory names
const string data_file_name_in_header = interfile_get_data_file_name_in_header(header_file_name, image_file_name);
output_header << "!INTERFILE :=\n";
const bool is_spect = exam_info.imaging_modality.get_modality() == ImagingModality::NM;
if (!is_spect && exam_info.imaging_modality.get_modality() != ImagingModality::PT)
warning("Writing interfile header for a modality that is neither PET nor SPECT. This isn't really defined. There will be "
"some PET keywords anyway.");
write_interfile_modality(output_header, exam_info);
if (!exam_info.originating_system.empty())
output_header << "originating system := " << exam_info.originating_system << endl;
#if 0
//we don't have a conformant implementation of Interfile 3.3, even for SPECT
if (is_spect)
output_header << "!version of keys := 3.3\n";
else
output_header << "!version of keys := STIR6.0\n";
#else
output_header << "!version of keys := STIR6.0\n";
#endif
output_header << "name of data file := " << data_file_name_in_header << endl;
output_header << "!GENERAL DATA :=\n";
write_interfile_patient_position(output_header, exam_info);
output_header << "!GENERAL IMAGE DATA :=\n";
if (exam_info.start_time_in_secs_since_1970 > 0)
{
const DateTimeStrings dt = secs_since_Unix_epoch_to_Interfile_datetime(exam_info.start_time_in_secs_since_1970);
output_header << "study date := " << dt.date << '\n';
output_header << "study time := " << dt.time << '\n';
}
output_header << "!type of data := " << (is_spect ? "Tomographic" : "PET") << '\n';
output_header << "imagedata byte order := " << (byte_order == ByteOrder::little_endian ? "LITTLEENDIAN" : "BIGENDIAN") << endl;
if (exam_info.get_calibration_factor() > 0.F)
output_header << "calibration factor := " << exam_info.get_calibration_factor() << endl;
write_interfile_radionuclide_info(output_header, exam_info);
if (is_spect)
{
output_header << "!SPECT STUDY (General) :=\n";
}
else
{
output_header << "!PET STUDY (General) :=\n";
}
if (!is_spect)
{
output_header << "!PET data type := Image\n";
}
output_header << "process status := Reconstructed\n";
output_header << "!number format := ";
if (output_type.integer_type())
output_header << (output_type.signed_type() ? "signed integer\n" : "unsigned integer\n");
else
output_header << "float\n";
output_header << "!number of bytes per pixel := " << output_type.size_in_bytes() << endl;
output_header << "number of dimensions := 3\n";
output_header << "matrix axis label [1] := x\n";
output_header << "!matrix size [1] := " << dimensions.x() << endl;
output_header << "scaling factor (mm/pixel) [1] := " << voxel_size.x() << endl;
output_header << "matrix axis label [2] := y\n";
output_header << "!matrix size [2] := " << dimensions.y() << endl;
output_header << "scaling factor (mm/pixel) [2] := " << voxel_size.y() << endl;
output_header << "matrix axis label [3] := z\n";
output_header << "!matrix size [3] := " << dimensions.z() << endl;
output_header << "scaling factor (mm/pixel) [3] := " << voxel_size.z() << endl;
if (origin.z() != InterfileHeader::double_value_not_set)
{
const CartesianCoordinate3D<float> first_pixel_offsets = voxel_size * BasicCoordinate<3, float>(min_indices) + origin;
output_header << "first pixel offset (mm) [1] := " << first_pixel_offsets.x() << '\n';
output_header << "first pixel offset (mm) [2] := " << first_pixel_offsets.y() << '\n';
output_header << "first pixel offset (mm) [3] := " << first_pixel_offsets.z() << '\n';
}
write_interfile_time_frame_definitions(output_header, exam_info);
write_interfile_energy_windows(output_header, exam_info);
write_interfile_image_data_descriptions(output_header, data_type_descriptions);
for (int i = 1; i <= scaling_factors.get_length(); i++)
{
// only write scaling factors and offset if more than 1 frame or they are not default values
if (scaling_factors[i - 1] != 1. || scaling_factors.get_length() > 1)
output_header << "image scaling factor"
<< "[" << i << "] := " << scaling_factors[i - 1] << endl;
if (file_offsets[i - 1] || scaling_factors.get_length() > 1)
output_header << "data offset in bytes"
<< "[" << i << "] := " << file_offsets[i - 1] << endl;
}
// analogue of image scaling factor[*] for Louvain la Neuve Interfile reader
{
bool output_quantification_units = true;
if (scaling_factors.get_length() > 1)
{
const float first_scaling_factor = scaling_factors[0];
for (int i = 1; i <= scaling_factors.get_max_index(); i++)
{
if (scaling_factors[i] != first_scaling_factor)
{
warning("Interfile: non-standard 'quantification units' keyword not set as not all scale factors are the same");
output_quantification_units = false;
break;
}
}
}
if (output_quantification_units)
{
// only write when not 1
output_quantification_units = (scaling_factors[0] != 1.);
}
if (output_quantification_units)
output_header << "quantification units := " << scaling_factors[0] << endl;
}
// TODO
// output_header << "maximum pixel count := " << image.find_max()/scale << endl;
output_header << "!END OF INTERFILE :=\n";
// temporary copy to make an old-style header to satisfy Analyze
{
string header_name = header_file_name;
replace_extension(header_name, ".ahv");
ofstream output_header(header_name.c_str(), ios::out);
if (!output_header.good())
{
error(boost::format("Error opening old-style Interfile header %1% for writing") % header_name);
return Succeeded::no;
}
output_header << "!INTERFILE :=\n";
output_header << "!name of data file := " << image_file_name << endl;
output_header << "!total number of images := " << dimensions.z() << endl;
for (int i = 1; i <= file_offsets.get_length(); i++)
{
output_header << "!data offset in bytes := " << file_offsets[i - 1] << endl;
}
output_header << "!imagedata byte order := " << (byte_order == ByteOrder::little_endian ? "LITTLEENDIAN" : "BIGENDIAN")
<< endl;
output_header << "!number format := ";
if (output_type.integer_type())
output_header << (output_type.signed_type() ? "signed integer\n" : "unsigned integer\n");
else
output_header << (output_type.size_in_bytes() == 4 ? "short float\n" : "long float\n");
output_header << "!number of bytes per pixel := " << output_type.size_in_bytes() << endl;
output_header << "matrix axis label [1] := x\n";
output_header << "!matrix size [1] := " << dimensions.x() << endl;
output_header << "scaling factor (mm/pixel) [1] := " << voxel_size.x() << endl;
output_header << "matrix axis label [2] := y\n";
output_header << "!matrix size [2] := " << dimensions.y() << endl;
output_header << "scaling factor (mm/pixel) [2] := " << voxel_size.y() << endl;
{
// Note: bug in current version of analyze
// if voxel_size is not an integer, it will not take the
// pixel size into account
// Work around: Always make sure it is not an integer, by
// adding a small number to it if necessary
// TODO this is horrible and not according to the Interfile standard
// so, remove for distribution purposes
float zsize = voxel_size.z();
if (floor(zsize) == zsize)
zsize += 0.00001F;
// TODO this is what it should be
// float zsize = voxel_size.z()/ voxel_size.x();
output_header << ";Correct value is of keyword (commented out)\n"
<< ";!slice thickness (pixels) := " << voxel_size.z() / voxel_size.x() << endl;
output_header << ";Value for Analyze\n"
<< "!slice thickness (pixels) := " << zsize << endl;
}
output_header << "!END OF INTERFILE :=\n";
}
return Succeeded::yes;
}
template <class elemT>
Succeeded
write_basic_interfile(const string& filename,
const Array<3, elemT>& image,
const NumericType output_type,
const float scale,
const ByteOrder byte_order)
{
CartesianCoordinate3D<float> origin;
origin.fill(static_cast<float>(InterfileHeader::double_value_not_set));
return write_basic_interfile(filename, image, CartesianCoordinate3D<float>(1, 1, 1), origin, output_type, scale, byte_order);
}
template <class NUMBER>
Succeeded
write_basic_interfile(const string& filename,
const ExamInfo& exam_info,
const Array<3, NUMBER>& image,
const CartesianCoordinate3D<float>& voxel_size,
const CartesianCoordinate3D<float>& origin,
const NumericType output_type,
const float scale,
const ByteOrder byte_order)
{
std::string data_name, header_name;
interfile_create_filenames(filename, data_name, header_name);
ofstream output_data;
open_write_binary(output_data, data_name.c_str());
float scale_to_use = scale;
write_data(output_data, image, output_type, scale_to_use, byte_order);
VectorWithOffset<float> scaling_factors(1);
scaling_factors.fill(scale_to_use);
VectorWithOffset<unsigned long> file_offsets(1);
file_offsets.fill(0);
const Succeeded success = write_basic_interfile_image_header(header_name,
data_name,
exam_info,
image.get_index_range(),
voxel_size,
origin,
output_type,
byte_order,
scaling_factors,
file_offsets);
#if 0
delete[] header_name;
delete[] data_name;
#endif
return success;
}
template <class NUMBER>
Succeeded
write_basic_interfile(const string& filename,
const Array<3, NUMBER>& image,
const CartesianCoordinate3D<float>& voxel_size,
const CartesianCoordinate3D<float>& origin,
const NumericType output_type,
const float scale,
const ByteOrder byte_order)
{
return write_basic_interfile(filename, ExamInfo(), image, voxel_size, origin, output_type, scale, byte_order);
}
Succeeded
write_basic_interfile(const string& filename,
const VoxelsOnCartesianGrid<float>& image,
const NumericType output_type,
const float scale,
const ByteOrder byte_order)
{
return write_basic_interfile(filename,
image.get_exam_info(),
image, // use automatic reference to base class
image.get_grid_spacing(),
image.get_origin(),
output_type,
scale,
byte_order);
}
Succeeded
write_basic_interfile(const string& filename,
const DiscretisedDensity<3, float>& image,
const NumericType output_type,
const float scale,
const ByteOrder byte_order)
{
// dynamic_cast will throw an exception when it's not valid
return write_basic_interfile(
filename, dynamic_cast<const VoxelsOnCartesianGrid<float>&>(image), output_type, scale, byte_order);
}
Succeeded
write_basic_interfile(const string& filename,
const ParametricVoxelsOnCartesianGrid& image,
const NumericType output_type,
const float scale,
const ByteOrder byte_order)
{
std::string data_name, header_name;
interfile_create_filenames(filename, data_name, header_name);
ofstream output_data;
open_write_binary(output_data, data_name.c_str());
VectorWithOffset<unsigned long> file_offsets(image.get_num_params());
VectorWithOffset<float> scaling_factors(image.get_num_params());
for (int i = 1; i <= static_cast<int>(image.get_num_params()); i++)
{
float scale_to_use = scale;
file_offsets[i - 1] = output_data.tellp();
write_data(output_data, image.construct_single_density(i), output_type, scale_to_use, byte_order);
scaling_factors[i - 1] = (scale_to_use);
}
// Tell it what the different kinetic parameters mean
std::vector<std::string> data_type_descriptions;
data_type_descriptions.push_back("slope");
data_type_descriptions.push_back("intercept");
const Succeeded success = write_basic_interfile_image_header(header_name,
data_name,
image.get_exam_info(),
image.get_index_range(),
image.get_voxel_size(),
image.get_origin(),
output_type,
byte_order,
scaling_factors,
file_offsets,
data_type_descriptions);
#if 0
delete[] header_name;
delete[] data_name;
#endif
return success;
}
Succeeded
write_basic_interfile(const string& filename,
const DynamicDiscretisedDensity& image,
const NumericType output_type,
const float scale,
const ByteOrder byte_order)
{
std::string data_name, header_name;
interfile_create_filenames(filename, data_name, header_name);
ofstream output_data;
open_write_binary(output_data, data_name.c_str());
VectorWithOffset<unsigned long> file_offsets(image.get_num_time_frames());
VectorWithOffset<float> scaling_factors(image.get_num_time_frames());
for (int i = 1; i <= static_cast<int>(image.get_num_time_frames()); i++)
{
float scale_to_use = scale;
file_offsets[i - 1] = output_data.tellp();
write_data(output_data, image.get_density(i), output_type, scale_to_use, byte_order);
scaling_factors[i - 1] = (scale_to_use);
}
const Succeeded success = write_basic_interfile_image_header(
header_name,
data_name,
image.get_exam_info(),
image.get_density(1).get_index_range(),
dynamic_cast<const VoxelsOnCartesianGrid<float>&>(image.get_density(1)).get_grid_spacing(),
image.get_density(1).get_origin(),
output_type,
byte_order,
scaling_factors,
file_offsets);
#if 0
delete[] header_name;
delete[] data_name;
#endif
return success;
}
static ProjDataFromStream*
read_interfile_PDFS_SPECT(istream& input, const string& directory_for_data, const ios::openmode open_mode)
{
InterfilePDFSHeaderSPECT hdr;
if (!hdr.parse(input))
{
return 0; // KT 10122001 do not call ask_parameters anymore
}
char full_data_file_name[max_filename_length];
strcpy(full_data_file_name, hdr.data_file_name.c_str());
prepend_directory_name(full_data_file_name, directory_for_data.c_str());
vector<int> segment_sequence(1);
segment_sequence[0] = 0;
for (unsigned int i = 1; i < hdr.image_scaling_factors[0].size(); i++)
if (hdr.image_scaling_factors[0][0] != hdr.image_scaling_factors[0][i])
{
error("Interfile error: all image scaling factors should be equal at the moment.");
}
assert(!is_null_ptr(hdr.data_info_sptr));
shared_ptr<iostream> data_in(new fstream(full_data_file_name, open_mode | ios::binary));
if (!data_in->good())
{
warning("interfile parsing: error opening file %s", full_data_file_name);
return 0;
}
return new ProjDataFromStream(hdr.get_exam_info_sptr(),
hdr.data_info_sptr,
data_in,
hdr.data_offset_each_dataset[0],
segment_sequence,
hdr.storage_order,
hdr.type_of_numbers,
hdr.file_byte_order,
static_cast<float>(hdr.image_scaling_factors[0][0]));
}
ProjDataFromStream*
read_interfile_PDFS_Siemens(istream& input, const string& directory_for_data, const ios::openmode open_mode)
{
InterfilePDFSHeaderSiemens hdr;
if (!hdr.parse(input))
{
warning("Interfile parsing of Siemens Interfile projection data failed");
return 0;
}
// KT 14/01/2000 added directory capability
// prepend directory_for_data to the data_file_name from the header
char full_data_file_name[max_filename_length];
strcpy(full_data_file_name, hdr.data_file_name.c_str());
prepend_directory_name(full_data_file_name, directory_for_data.c_str());
shared_ptr<iostream> data_in(new fstream(full_data_file_name, open_mode | ios::binary));
if (!data_in->good())
{
warning("interfile parsing: error opening file %s", full_data_file_name);
return 0;
}
if (hdr.compression)
warning("Siemens projection data is compressed. Reading of raw data will fail.");
auto pdfs_ptr = new ProjDataFromStream(hdr.get_exam_info_sptr(),
hdr.data_info_ptr->create_shared_clone(),
data_in,
hdr.data_offset_each_dataset[0],
hdr.segment_sequence,
hdr.storage_order,
hdr.type_of_numbers,
hdr.file_byte_order,
1.);
if (hdr.timing_poss_sequence.size() > 1)
pdfs_ptr->set_timing_poss_sequence_in_stream(hdr.timing_poss_sequence);
return pdfs_ptr;
}
ProjDataFromStream*
read_interfile_PDFS(istream& input, const string& directory_for_data, const ios::openmode open_mode)
{
{
MinimalInterfileHeader hdr;
std::ios::off_type offset = input.tellg();