-
Notifications
You must be signed in to change notification settings - Fork 0
/
File.cpp
211 lines (166 loc) · 5.86 KB
/
File.cpp
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
/************************************************************************
* DICOMLIB
* Copyright 2003 Sunnybrook and Women's College Health Science Center
* Implemented by Trevor Morgan (morgan@sten.sunnybrook.utoronto.ca)
*
* See LICENSE.txt for copyright and licensing info.
*************************************************************************/
#include "File.hpp"
#include "Buffer.hpp"
#include "TransferSyntax.hpp"
#include "Decoder.hpp"
#include "Encoder.hpp"
namespace dicom
{
/*
TODO
Are there more efficient ways of reading/writing bytes to a stream
than the one I'm using here? I'm sure there must be...
*/
/*!
Is there a nicer way of implementing this?
*/
size_t GetStreamSize(std::ifstream& In)
{
std::streampos CurrentPosition=In.tellg(); //get current position.
In.seekg(0,std::ios::end); //jump to end
long StreamSize=In.tellg(); //get stream length.
In.seekg(CurrentPosition); //reset
return StreamSize;
}
void ReadFileMetaFromStream(std::ifstream& In, DataSet& ds)
{//added by Sam Shen - I don't see why meta info is not part of the data set
ds.clear();
In.seekg(0);
UID TransferSyntaxUID=IMPL_VR_LE_TRANSFER_SYNTAX;//default
try
{
FileMetaInformation MetaInfo(In);
MetaInfo.MetaElements_(TAG_TRANSFER_SYNTAX_UID) >> TransferSyntaxUID;
ds = MetaInfo.MetaElements_;
}
catch (FileMetaInfoException& e)
{
/*
If FileMetaInformation constructor fails, it probably
means we're opening a KODAK file that doesn't have meta info.
(this is a violation of the DICOM standard.)
Against my better judgement I've been asked to make
the code try and recover at this point. I can't bring
myself to, but if you really need it, remove the following
re-throw command
*/
throw;
In.seekg(0);
}
return ;
}
/*
I add a limit to the buffer so that I can read the header only -Sam 29July2009
*/
void ReadFromStream(std::ifstream& In, DataSet& data,size_t max_number_of_byte_to_read)
{
data.clear();
UID TransferSyntaxUID=IMPL_VR_LE_TRANSFER_SYNTAX;//default
try
{
FileMetaInformation MetaInfo(In);
MetaInfo.MetaElements_(TAG_TRANSFER_SYNTAX_UID) >> TransferSyntaxUID;
}
catch (FileMetaInfoException& e)
{
/*
If FileMetaInformation constructor fails, it probably
means we're opening a KODAK file that doesn't have meta info.
(this is a violation of the DICOM standard.)
Against my better judgement I've been asked to make
the code try and recover at this point. I can't bring
myself to, but if you really need it, remove the following
re-throw command
*/
throw;
In.seekg(0);
}
TS ts(TransferSyntaxUID);
/*
if MetaInformation constructs correctly, then
In should be pointing at the correct point in the
file to start reading the DataSet.
*/
size_t BytesToRead=GetStreamSize(In)-In.tellg();
BytesToRead=min(BytesToRead,max_number_of_byte_to_read);
int ByteOrder=ts.isBigEndian()?
__BIG_ENDIAN:__LITTLE_ENDIAN;
Buffer buffer(ByteOrder);
//Read data from stream onto buffer.
buffer.assign(BytesToRead,0);
unsigned char* pData=&buffer.front();;
In.read((char*)pData,BytesToRead);//This is the most time intensive part. Can we speed it up any?
//Transfer data from buffer onto dataset
ReadFromBuffer(buffer,data,ts);
//I insist on putting the ts into the data so that user has an chance to interpret pixel data correctly. -Sam
if(ts.isEncapsulated() || ts.isDeflated())
data.Put<VR_UI>(TAG_TRANSFER_SYNTAX_UID,TransferSyntaxUID);
}
void WriteToStream(const DataSet& data,std::ostream& Out,TS ts, bool Tiff)
{
FileMetaInformation MetaInfo(data,ts);
int bytes_in_meta=MetaInfo.Write(Out); //We write it now to get the buffer size of MeatInfo
//rewrite when we get enough infomation to put to TIFF header
int ByteOrder=ts.isBigEndian()?
__BIG_ENDIAN:__LITTLE_ENDIAN;
Buffer buffer(ByteOrder);
UID TS_UID=MetaInfo.MetaElements_(TAG_TRANSFER_SYNTAX_UID).Get<UID>();
WriteToBuffer(data,buffer,TS(TS_UID));
/*
The following is in desperate need of optimisation!
UCDMC99 had a 'WriteToStream' version of WriteToBuffer,
that matched exactly the syntax of WriteToBuffer. Two identical
functions is bad, can we trick it with templates or
virtual functions??
*/
/*
while(buffer.position()!=buffer.end())
{
Out.put(*buffer.position());
buffer.Increment(1);
}*/
int bytes_to_write=buffer.end()-buffer.position();
if(Tiff)
{
FileMetaInformation MetaInfoTiff(data,ts,bytes_to_write + bytes_in_meta);
MetaInfoTiff.Write(Out);
}
Out.write((const char *)(&*buffer.position()),bytes_to_write);
buffer.Increment(bytes_to_write);
}
/*
I add a limit to the buffer so that I can read the header only -Sam 29July2009
*/
void Read(std::string FileName,DataSet& data,size_t max_number_of_byte_to_read)
{
std::ifstream in(FileName.c_str(),std::ios::binary);
if(in.fail())
throw dicom::exception("Couldn't open input file");
ReadFromStream(in,data,max_number_of_byte_to_read);
}
void ReadW(std::wstring FileName, DataSet& data, size_t max_number_of_byte_to_read)
{
std::ifstream in(FileName.c_str(), std::ios::binary);
if (in.fail())
throw dicom::exception("Couldn't open input file");
ReadFromStream(in, data, max_number_of_byte_to_read);
}
//supply a false if you want to write a Pure Dicom file
//here pure means NOT-TIFF-Compatible
void Write(const DataSet& data, std::string FileName, TS ts, bool Tiff)
{
std::ofstream out(FileName.c_str(),std::ios::binary);//what's default behaviour if file already exists?
WriteToStream(data,out,ts, Tiff);
}
void WriteW(const DataSet& data, std::wstring FileName, TS ts, bool Tiff)
{
std::ofstream out(FileName.c_str(), std::ios::binary);//what's default behaviour if file already exists?
WriteToStream(data, out, ts, Tiff);
}
}//namespace dicom