STIR 6.4.0
OutputFileFormat.txx
1//
2//
3/*
4 Copyright (C) 2003- 2009-10-08, Hammersmith Imanet Ltd
5 Copyright (C) 2011-07-01 - 2011, Kris Thielemans
6 This file is part of STIR.
7 SPDX-License-Identifier: Apache-2.0
8
9
10 See STIR/LICENSE.txt for details
11*/
12/*!
13 \file
14 \ingroup IO
15
16 \brief implementation of the stir::OutputFileFormat class
17 \author Kris Thielemans
18
19*/
20
21#include "stir/IO/OutputFileFormat.h"
22#include "stir/Succeeded.h"
23#include "stir/warning.h"
24
25START_NAMESPACE_STIR
26
27template <typename DataT>
28ASCIIlist_type OutputFileFormat<DataT>::number_format_values;
29
30template <typename DataT>
31ASCIIlist_type OutputFileFormat<DataT>::byte_order_values;
32
33template <typename DataT>
34shared_ptr<OutputFileFormat<DataT>>
35OutputFileFormat<DataT>::default_sptr()
36{
37 return OutputFileFormat<DataT>::_default_sptr;
38}
39
40template <typename DataT>
41OutputFileFormat<DataT>::OutputFileFormat(const NumericType& type, const ByteOrder& byte_order)
42 : type_of_numbers(type),
43 file_byte_order(byte_order)
44{}
45
46template <typename DataT>
47void
48OutputFileFormat<DataT>::set_defaults()
49{
50 file_byte_order = ByteOrder::native;
51 type_of_numbers = NumericType::FLOAT;
52 scale_to_write_data = 0.F;
53
54 set_key_values();
55}
56
57template <typename DataT>
58void
59OutputFileFormat<DataT>::initialise_keymap()
60{
61
62 if (number_format_values.size() == 0)
63 {
64 number_format_values.push_back("bit");
65 number_format_values.push_back("ascii");
66 number_format_values.push_back("signed integer");
67 number_format_values.push_back("unsigned integer");
68 number_format_values.push_back("float");
69
70 assert(byte_order_values.size() == 0);
71 byte_order_values.push_back("LITTLEENDIAN");
72 byte_order_values.push_back("BIGENDIAN");
73 }
74 this->parser.add_key("byte order", &byte_order_index, &byte_order_values);
75 this->parser.add_key("number format", &number_format_index, &number_format_values);
76 this->parser.add_key("number of bytes per pixel", &bytes_per_pixel);
77 this->parser.add_key("scale_to_write_data", &scale_to_write_data);
78}
79
80template <typename DataT>
81void
82OutputFileFormat<DataT>::set_key_values()
83{
84 byte_order_index = file_byte_order == ByteOrder::little_endian ? 0 : 1;
85
86 std::string number_format;
87 std::size_t bytes_per_pixel_in_size_t;
88 type_of_numbers.get_Interfile_info(number_format, bytes_per_pixel_in_size_t);
89 bytes_per_pixel = static_cast<int>(bytes_per_pixel_in_size_t);
90 number_format_index = this->parser.find_in_ASCIIlist(number_format, number_format_values);
91}
92
93template <typename DataT>
94bool
95OutputFileFormat<DataT>::post_processing()
96{
97 if (number_format_index < 0 || static_cast<ASCIIlist_type::size_type>(number_format_index) >= number_format_values.size())
98 {
99 warning("OutputFileFormat parser internal error: "
100 "'number_format_index' out of range\n");
101 return true;
102 }
103 // check if bytes_per_pixel is set if the data type is not 'bit'
104 if (number_format_index != 0 && bytes_per_pixel <= 0)
105 {
106 warning("OutputFileFormat parser: "
107 "'number_of_bytes_per_pixel' should be > 0\n");
108 return true;
109 }
110
111 type_of_numbers = NumericType(number_format_values[number_format_index], bytes_per_pixel);
112
113 if (type_of_numbers == NumericType::UNKNOWN_TYPE)
114 {
115 warning("OutputFileFormat parser: "
116 "unrecognised data type. Check either "
117 "'number_of_bytes_per_pixel' or 'number format'\n");
118 return true;
119 }
120
121 switch (byte_order_index)
122 {
123 case -1:
124 file_byte_order = ByteOrder::native;
125 break;
126 case 0:
127 file_byte_order = ByteOrder::little_endian;
128 break;
129 case 1:
130 file_byte_order = ByteOrder::big_endian;
131 break;
132 default:
133 warning("OutputFileFormat parser internal error: "
134 "'byte_order_index' out of range\n");
135 return true;
136 }
137
138 set_byte_order(file_byte_order, true);
139 set_type_of_numbers(type_of_numbers, true);
140
141 return false;
142}
143
144template <typename DataT>
145NumericType
146OutputFileFormat<DataT>::get_type_of_numbers() const
147{
148 return type_of_numbers;
149}
150
151template <typename DataT>
152ByteOrder
153OutputFileFormat<DataT>::get_byte_order()
154{
155 return file_byte_order;
156}
157
158template <typename DataT>
159float
160OutputFileFormat<DataT>::get_scale_to_write_data() const
161{
162 return scale_to_write_data;
163}
164
165template <typename DataT>
166NumericType
167OutputFileFormat<DataT>::set_type_of_numbers(const NumericType& new_type, const bool warn)
168{
169 return type_of_numbers = new_type;
170}
171
172template <typename DataT>
173ByteOrder
174OutputFileFormat<DataT>::set_byte_order(const ByteOrder& new_byte_order, const bool warn)
175{
176 return file_byte_order = new_byte_order;
177}
178
179template <typename DataT>
180float
181OutputFileFormat<DataT>::set_scale_to_write_data(const float new_scale_to_write_data, const bool warn)
182{
183 return scale_to_write_data = new_scale_to_write_data;
184}
185
186template <typename DataT>
187void
188OutputFileFormat<DataT>::set_byte_order_and_type_of_numbers(ByteOrder& new_byte_order, NumericType& new_type, const bool warn)
189{
190 new_byte_order = set_byte_order(new_byte_order, warn);
191 new_type = set_type_of_numbers(new_type, warn);
192}
193
194template <typename DataT>
195Succeeded
196OutputFileFormat<DataT>::write_to_file(std::string& filename, const DataT& density) const
197{
198 return actual_write_to_file(filename, density);
199}
200
201template <typename DataT>
202Succeeded
203OutputFileFormat<DataT>::write_to_file(const std::string& filename, const DataT& density) const
204{
205 std::string filename_to_use = filename;
206 return write_to_file(filename_to_use, density);
207};
208
209END_NAMESPACE_STIR