ISMRMRD: serialization.h Source File

ISMRMRD ISMRM Raw Data Format

Loading...
Searching...
No Matches
serialization.h
Go to the documentation of this file.
1#ifndef ISMRMRDSERIALIZATION_H
2#define ISMRMRDSERIALIZATION_H
3
4#include <exception>
5#include <iostream>
6
7#include "ismrmrd/export.h"
8#include "ismrmrd/ismrmrd.h"
9#include "ismrmrd/waveform.h"
10#include "ismrmrd/xml.h"
11
24
25namespace ISMRMRD {
26
27enum ISMRMRD_MESSAGE_ID {
28 ISMRMRD_MESSAGE_UNPEEKED = 0,
29 ISMRMRD_MESSAGE_CONFIG_FILE = 1,
30 ISMRMRD_MESSAGE_CONFIG_TEXT = 2,
31 ISMRMRD_MESSAGE_HEADER = 3,
32 ISMRMRD_MESSAGE_CLOSE = 4,
33 ISMRMRD_MESSAGE_TEXT = 5,
34 ISMRMRD_MESSAGE_ACQUISITION = 1008,
35 ISMRMRD_MESSAGE_IMAGE = 1022,
36 ISMRMRD_MESSAGE_WAVEFORM = 1026,
37 ISMRMRD_MESSAGE_NDARRAY = 1030
38};
39
40// A wrapper interface, which we can implement, e.g., for std::istream
41 class ReadableStreamView {
42public:
43 virtual void read(char *buffer, size_t count) = 0;
44
45 virtual bool eof() = 0;
46};
47
48// A wrapper interface, which we can implement, e.g., for std::ostream
49 class WritableStreamView {
50public:
51 virtual void write(const char *buffer, size_t count) = 0;
52
53 virtual bool bad() = 0;
54};
55
56// We define a few wrapper structs here to make the serialization code a bit
57// more readable.
58 struct ConfigFile {
59 char config[1024];
60};
61
62 struct ConfigText {
63 std::string config_text;
64};
65
66 struct TextMessage {
67 std::string message;
68};
69
70// serialize Acquisition to ostream
71EXPORTISMRMRD void serialize(const Acquisition &acq, WritableStreamView &ws);
72
73// serialize Image<T> to ostream
74template <typename T>
75EXPORTISMRMRD void serialize(const Image<T> &img, WritableStreamView &ws);
76
77// serialize Waveform to ostream
78EXPORTISMRMRD void serialize(const Waveform &wfm, WritableStreamView &ws);
79
80// serialize const length (1024) char array to ostream. Used for CONFIG FILE
81EXPORTISMRMRD void serialize(const ConfigFile &cfg, WritableStreamView &ws);
82
83// serialize a string
84EXPORTISMRMRD void serialize(const std::string &str, WritableStreamView &ws);
85
86// serialize a NDArray
87template <typename T>
88EXPORTISMRMRD void serialize(const NDArray<T> &arr, WritableStreamView &ws);
89
90// deserialize Acquisition from istream
91EXPORTISMRMRD void deserialize(Acquisition &acq, ReadableStreamView &rs);
92
93// deserialize Image<T> from istream
94template <typename T>
95EXPORTISMRMRD void deserialize(Image<T> &img, ReadableStreamView &rs);
96
97// deserialize Waveform from istream
98EXPORTISMRMRD void deserialize(Waveform &wfm, ReadableStreamView &rs);
99
100// deserialize const length (1024) char array from istream. Used for CONFIG FILE
101EXPORTISMRMRD void deserialize(ConfigFile &cfg, ReadableStreamView &rs);
102
103// deserialize a string
104EXPORTISMRMRD void deserialize(std::string &str, ReadableStreamView &rs);
105
106// deserialize a NDArray
107template <typename T>
108EXPORTISMRMRD void deserialize(NDArray<T> &arr, ReadableStreamView &rs);
109
110 class ProtocolStreamClosed : public std::exception {};
111
112 class EXPORTISMRMRD ProtocolSerializer {
113public:
114 ProtocolSerializer(WritableStreamView &ws);
115 void serialize(const ConfigFile &cf);
116 void serialize(const ConfigText &ct);
117 void serialize(const TextMessage &tm);
118 void serialize(const IsmrmrdHeader &hdr);
119 void serialize(const Acquisition &acq);
120 template <typename T> void serialize(const Image<T> &img);
121 void serialize(const Waveform &wfm);
122 template <typename T> void serialize(const NDArray<T> &arr);
123 void close();
124
125protected:
126 void write_msg_id(uint16_t id);
127 WritableStreamView &_ws;
128};
129
130 class EXPORTISMRMRD ProtocolDeserializer {
131public:
132 ProtocolDeserializer(ReadableStreamView &rs);
133 void deserialize(ConfigFile &cf);
134 void deserialize(ConfigText &ct);
135 void deserialize(TextMessage &tm);
136 void deserialize(IsmrmrdHeader &hdr);
137 void deserialize(Acquisition &acq);
138 template <typename T> void deserialize(Image<T> &img);
139 void deserialize(Waveform &wfm);
140 template <typename T> void deserialize(NDArray<T> &arr);
141
142 // Peek at the next data type in the stream
143 uint16_t peek();
144 int peek_image_data_type();
145 int peek_ndarray_data_type();
146
147protected:
148 ReadableStreamView &_rs;
149 uint16_t _peeked;
150 ImageHeader _peeked_image_header;
151 uint16_t _peeked_ndarray_data_type;
152};
153
154} // namespace ISMRMRD
155
156#endif // ISMRMRDSERIALIZATION_H
ISMRMRD::Acquisition
MR Acquisition type.
Definition ismrmrd.h:605
ISMRMRD::ImageHeader
Header for MR Image type.
Definition ismrmrd.h:753
ISMRMRD::Image
MR Image type.
Definition ismrmrd.h:772
ISMRMRD::NDArray
N-Dimensional array type.
Definition ismrmrd.h:934
ISMRMRD::ProtocolStreamClosed
Definition serialization.h:110
ISMRMRD::ReadableStreamView
Definition serialization.h:41
ISMRMRD::WritableStreamView
Definition serialization.h:49
ismrmrd.h
ISMRMRD::ConfigFile
Definition serialization.h:58
ISMRMRD::ConfigText
Definition serialization.h:62
ISMRMRD::IsmrmrdHeader
Definition xml.h:553
ISMRMRD::TextMessage
Definition serialization.h:66
ISMRMRD::Waveform
Definition waveform.h:96
xml.h