STIR 6.4.0
read_data.inl
Go to the documentation of this file.
1/*
2 Copyright (C) 2004- 2007, Hammersmith Imanet Ltd
3 Copyright (C) 2024, University College London
4 This file is part of STIR.
5
6 SPDX-License-Identifier: Apache-2.0
7
8 See STIR/LICENSE.txt for details
9*/
18#include "stir/Array.h"
19#include "stir/convert_array.h"
20#include "stir/NumericType.h"
21#include "stir/NumericInfo.h"
22#include "stir/Succeeded.h"
23#include "stir/ByteOrder.h"
26#include "stir/warning.h"
27#include <typeinfo>
28
29START_NAMESPACE_STIR
30
31namespace detail
32{
33/* Generic implementation of read_data(). See test_if_1d.h for info why we do this.*/
34template <int num_dimensions, class IStreamT, class elemT>
35inline Succeeded
36read_data_help(is_not_1d, IStreamT& s, ArrayType<num_dimensions, elemT>& data, const ByteOrder byte_order)
37{
38 if (data.is_contiguous())
39 return read_data_1d(s, data, byte_order);
40
41 // otherwise, recurse
42 for (typename ArrayType<num_dimensions, elemT>::iterator iter = data.begin(); iter != data.end(); ++iter)
43 {
44 if (read_data(s, *iter, byte_order) == Succeeded::no)
45 return Succeeded::no;
46 }
47 return Succeeded::yes;
48}
49
50/* 1D implementation of read_data(). See test_if_1d.h for info why we do this.*/
51// specialisation for 1D case
52template <class IStreamT, class elemT>
53inline Succeeded
54read_data_help(is_1d, IStreamT& s, ArrayType<1, elemT>& data, const ByteOrder byte_order)
55{
56 return read_data_1d(s, data, byte_order);
57}
58
59} // end of namespace detail
60
61template <int num_dimensions, class IStreamT, class elemT>
62inline Succeeded
63read_data(IStreamT& s, ArrayType<num_dimensions, elemT>& data, const ByteOrder byte_order)
64{
65 return detail::read_data_help(detail::test_if_1d<num_dimensions>(), s, data, byte_order);
66}
67
68template <int num_dimensions, class IStreamT, class elemT, class InputType, class ScaleT>
69inline Succeeded
70read_data(IStreamT& s,
72 NumericInfo<InputType> input_type,
73 ScaleT& scale_factor,
74 const ByteOrder byte_order)
75{
76 if (typeid(InputType) == typeid(elemT))
77 {
78 // TODO? you might want to use the scale even in this case,
79 // but at the moment we don't
80 scale_factor = ScaleT(1);
81 return read_data(s, data, byte_order);
82 }
83 else
84 {
85 ArrayType<num_dimensions, InputType> in_data(data.get_index_range());
86 Succeeded success = read_data(s, in_data, byte_order);
87 if (success == Succeeded::no)
88 return Succeeded::no;
89 convert_array(data, scale_factor, in_data);
90 return Succeeded::yes;
91 }
92}
93
94template <int num_dimensions, class IStreamT, class elemT, class ScaleT>
95inline Succeeded
96read_data(IStreamT& s, ArrayType<num_dimensions, elemT>& data, NumericType type, ScaleT& scale, const ByteOrder byte_order)
97{
98 switch (type.id)
99 {
100 // define macro what to do with a specific NumericType
101#define CASE(NUMERICTYPE) \
102 case NUMERICTYPE: \
103 return read_data(s, data, NumericInfo<typename TypeForNumericType<NUMERICTYPE>::type>(), scale, byte_order)
104
105 // now list cases that we want
106 CASE(NumericType::SCHAR);
107 CASE(NumericType::UCHAR);
108 CASE(NumericType::SHORT);
109 CASE(NumericType::USHORT);
110 CASE(NumericType::INT);
111 CASE(NumericType::UINT);
112 CASE(NumericType::LONG);
113 CASE(NumericType::ULONG);
114 CASE(NumericType::FLOAT);
115 CASE(NumericType::DOUBLE);
116#undef CASE
117 default:
118 warning("read_data : type not yet supported\n, at line %d in file %s", __LINE__, __FILE__);
119 return Succeeded::no;
120 }
121}
122
123END_NAMESPACE_STIR
defines the stir::Array class for multi-dimensional (numeric) arrays
This file declares the stir::ByteOrder class.
This file declares the class stir::NumericInfo.
This file declares the stir::NumericType class.
Declaration of class stir::Succeeded.
This class provides member functions to find out what byte-order your machine is and to swap numbers.
Definition ByteOrder.h:100
class NumericInfo<NUMBER> defines properties for the type NUMBER.
Definition NumericInfo.h:68
provides names for some numeric types and methods for finding their properties.
Definition NumericType.h:55
a class containing an enumeration type that can be used by functions to signal successful operation o...
Definition Succeeded.h:44
This file declares the stir::convert_array functions. This is a function to convert stir::Array objec...
Succeeded read_data_1d(std::istream &s, ArrayType< num_dimensions, elemT > &data, const ByteOrder byte_order)
This is an internal function called by read_data(). It does the actual reading to std::istream.
Definition read_data_1d.inl:33
Succeeded read_data(IStreamT &s, ArrayType< num_dimensions, elemT > &data, const ByteOrder byte_order=ByteOrder::native)
Read the data of an Array from file.
Definition read_data.inl:63
Array< num_dimensions, T2 > convert_array(scaleT &scale_factor, const Array< num_dimensions, T1 > &data_in, const NumericInfo< T2 > info2)
A function that returns a new Array (of the same dimension) with elements of type T2.
Definition convert_array.inl:35
void warning(const char *const s,...)
Print warning with format string a la printf.
Definition warning.cxx:41
Array< num_dimensions, elemT > ArrayType
type alias for future-proofing for "large" rectangular arrays
Definition ArrayFwd.h:25
Declaration of stir::read_data_1d() functions for reading 1D stir::Array objects from file.
a templated class used to check if it's a 1D array or not This class only exists to allow a work-arou...
Definition test_if_1d.h:71
Classes for use in implementation of stir::Array, stir::BasicCoordinate etc to test if it's a 1D arra...
Declaration of stir::warning()