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
10 See STIR/LICENSE.txt for details
16 \brief implementation of the stir::OutputFileFormat class
17 \author Kris Thielemans
21#include "stir/IO/OutputFileFormat.h"
22#include "stir/Succeeded.h"
23#include "stir/warning.h"
27template <typename DataT>
28ASCIIlist_type OutputFileFormat<DataT>::number_format_values;
30template <typename DataT>
31ASCIIlist_type OutputFileFormat<DataT>::byte_order_values;
33template <typename DataT>
34shared_ptr<OutputFileFormat<DataT>>
35OutputFileFormat<DataT>::default_sptr()
37 return OutputFileFormat<DataT>::_default_sptr;
40template <typename DataT>
41OutputFileFormat<DataT>::OutputFileFormat(const NumericType& type, const ByteOrder& byte_order)
42 : type_of_numbers(type),
43 file_byte_order(byte_order)
46template <typename DataT>
48OutputFileFormat<DataT>::set_defaults()
50 file_byte_order = ByteOrder::native;
51 type_of_numbers = NumericType::FLOAT;
52 scale_to_write_data = 0.F;
57template <typename DataT>
59OutputFileFormat<DataT>::initialise_keymap()
62 if (number_format_values.size() == 0)
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");
70 assert(byte_order_values.size() == 0);
71 byte_order_values.push_back("LITTLEENDIAN");
72 byte_order_values.push_back("BIGENDIAN");
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);
80template <typename DataT>
82OutputFileFormat<DataT>::set_key_values()
84 byte_order_index = file_byte_order == ByteOrder::little_endian ? 0 : 1;
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);
93template <typename DataT>
95OutputFileFormat<DataT>::post_processing()
97 if (number_format_index < 0 || static_cast<ASCIIlist_type::size_type>(number_format_index) >= number_format_values.size())
99 warning("OutputFileFormat parser internal error: "
100 "'number_format_index' out of range\n");
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)
106 warning("OutputFileFormat parser: "
107 "'number_of_bytes_per_pixel' should be > 0\n");
111 type_of_numbers = NumericType(number_format_values[number_format_index], bytes_per_pixel);
113 if (type_of_numbers == NumericType::UNKNOWN_TYPE)
115 warning("OutputFileFormat parser: "
116 "unrecognised data type. Check either "
117 "'number_of_bytes_per_pixel' or 'number format'\n");
121 switch (byte_order_index)
124 file_byte_order = ByteOrder::native;
127 file_byte_order = ByteOrder::little_endian;
130 file_byte_order = ByteOrder::big_endian;
133 warning("OutputFileFormat parser internal error: "
134 "'byte_order_index' out of range\n");
138 set_byte_order(file_byte_order, true);
139 set_type_of_numbers(type_of_numbers, true);
144template <typename DataT>
146OutputFileFormat<DataT>::get_type_of_numbers() const
148 return type_of_numbers;
151template <typename DataT>
153OutputFileFormat<DataT>::get_byte_order()
155 return file_byte_order;
158template <typename DataT>
160OutputFileFormat<DataT>::get_scale_to_write_data() const
162 return scale_to_write_data;
165template <typename DataT>
167OutputFileFormat<DataT>::set_type_of_numbers(const NumericType& new_type, const bool warn)
169 return type_of_numbers = new_type;
172template <typename DataT>
174OutputFileFormat<DataT>::set_byte_order(const ByteOrder& new_byte_order, const bool warn)
176 return file_byte_order = new_byte_order;
179template <typename DataT>
181OutputFileFormat<DataT>::set_scale_to_write_data(const float new_scale_to_write_data, const bool warn)
183 return scale_to_write_data = new_scale_to_write_data;
186template <typename DataT>
188OutputFileFormat<DataT>::set_byte_order_and_type_of_numbers(ByteOrder& new_byte_order, NumericType& new_type, const bool warn)
190 new_byte_order = set_byte_order(new_byte_order, warn);
191 new_type = set_type_of_numbers(new_type, warn);
194template <typename DataT>
196OutputFileFormat<DataT>::write_to_file(std::string& filename, const DataT& density) const
198 return actual_write_to_file(filename, density);
201template <typename DataT>
203OutputFileFormat<DataT>::write_to_file(const std::string& filename, const DataT& density) const
205 std::string filename_to_use = filename;
206 return write_to_file(filename_to_use, density);