comparison OrthancCppClient/Instance.cpp @ 0:ebc1e38ef615

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 01 Jun 2015 09:47:32 +0200
parents
children d5027f9f676a
comparison
equal deleted inserted replaced
-1:000000000000 0:ebc1e38ef615
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * In addition, as a special exception, the copyright holders of this
12 * program give permission to link the code of its release with the
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
14 * that use the same license as the "OpenSSL" library), and distribute
15 * the linked executables. You must obey the GNU General Public License
16 * in all respects for all of the code used other than "OpenSSL". If you
17 * modify file(s) with this exception, you may extend this exception to
18 * your version of the file(s), but you are not obligated to do so. If
19 * you do not wish to do so, delete this exception statement from your
20 * version. If you delete this exception statement from all source files
21 * in the program, then also delete it here.
22 *
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 **/
31
32
33 #include "../Core/PrecompiledHeaders.h"
34 #include "Instance.h"
35
36 #include "OrthancConnection.h"
37
38 #include <boost/lexical_cast.hpp>
39
40 namespace OrthancClient
41 {
42 void Instance::DownloadImage()
43 {
44 if (reader_.get() == NULL)
45 {
46 const char* suffix;
47 switch (mode_)
48 {
49 case Orthanc::ImageExtractionMode_Preview:
50 suffix = "preview";
51 break;
52
53 case Orthanc::ImageExtractionMode_UInt8:
54 suffix = "image-uint8";
55 break;
56
57 case Orthanc::ImageExtractionMode_UInt16:
58 suffix = "image-uint16";
59 break;
60
61 case Orthanc::ImageExtractionMode_Int16:
62 suffix = "image-int16";
63 break;
64
65 default:
66 throw OrthancClientException(Orthanc::ErrorCode_NotImplemented);
67 }
68
69 Orthanc::HttpClient client(connection_.GetHttpClient());
70 client.SetUrl(std::string(connection_.GetOrthancUrl()) + "/instances/" + id_ + "/" + suffix);
71 std::string png;
72
73 if (!client.Apply(png))
74 {
75 throw OrthancClientException(Orthanc::ErrorCode_NotImplemented);
76 }
77
78 reader_.reset(new Orthanc::PngReader);
79 reader_->ReadFromMemory(png);
80 }
81 }
82
83 void Instance::DownloadDicom()
84 {
85 if (dicom_.get() == NULL)
86 {
87 Orthanc::HttpClient client(connection_.GetHttpClient());
88 client.SetUrl(std::string(connection_.GetOrthancUrl()) + "/instances/" + id_ + "/file");
89
90 dicom_.reset(new std::string);
91
92 if (!client.Apply(*dicom_))
93 {
94 throw OrthancClientException(Orthanc::ErrorCode_NetworkProtocol);
95 }
96 }
97 }
98
99 Instance::Instance(const OrthancConnection& connection,
100 const char* id) :
101 connection_(connection),
102 id_(id),
103 mode_(Orthanc::ImageExtractionMode_Int16)
104 {
105 Orthanc::HttpClient client(connection_.GetHttpClient());
106
107 client.SetUrl(std::string(connection_.GetOrthancUrl()) + "/instances/" + id_ + "/simplified-tags");
108 Json::Value v;
109 if (!client.Apply(tags_))
110 {
111 throw OrthancClientException(Orthanc::ErrorCode_NetworkProtocol);
112 }
113 }
114
115 const char* Instance::GetTagAsString(const char* tag) const
116 {
117 if (tags_.isMember(tag))
118 {
119 return tags_[tag].asCString();
120 }
121 else
122 {
123 throw OrthancClientException(Orthanc::ErrorCode_InexistentItem);
124 }
125 }
126
127 float Instance::GetTagAsFloat(const char* tag) const
128 {
129 std::string value = GetTagAsString(tag);
130
131 try
132 {
133 return boost::lexical_cast<float>(value);
134 }
135 catch (boost::bad_lexical_cast)
136 {
137 throw OrthancClientException(Orthanc::ErrorCode_BadFileFormat);
138 }
139 }
140
141 int Instance::GetTagAsInt(const char* tag) const
142 {
143 std::string value = GetTagAsString(tag);
144
145 try
146 {
147 return boost::lexical_cast<int>(value);
148 }
149 catch (boost::bad_lexical_cast)
150 {
151 throw OrthancClientException(Orthanc::ErrorCode_BadFileFormat);
152 }
153 }
154
155 unsigned int Instance::GetWidth()
156 {
157 DownloadImage();
158 return reader_->GetWidth();
159 }
160
161 unsigned int Instance::GetHeight()
162 {
163 DownloadImage();
164 return reader_->GetHeight();
165 }
166
167 unsigned int Instance::GetPitch()
168 {
169 DownloadImage();
170 return reader_->GetPitch();
171 }
172
173 Orthanc::PixelFormat Instance::GetPixelFormat()
174 {
175 DownloadImage();
176 return reader_->GetFormat();
177 }
178
179 const void* Instance::GetBuffer()
180 {
181 DownloadImage();
182 return reader_->GetConstBuffer();
183 }
184
185 const void* Instance::GetBuffer(unsigned int y)
186 {
187 DownloadImage();
188 return reader_->GetConstRow(y);
189 }
190
191 void Instance::DiscardImage()
192 {
193 reader_.reset();
194 }
195
196 void Instance::DiscardDicom()
197 {
198 dicom_.reset();
199 }
200
201
202 void Instance::SetImageExtractionMode(Orthanc::ImageExtractionMode mode)
203 {
204 if (mode_ == mode)
205 {
206 return;
207 }
208
209 DiscardImage();
210 mode_ = mode;
211 }
212
213
214 void Instance::SplitVectorOfFloats(std::vector<float>& target,
215 const char* tag)
216 {
217 const std::string value = GetTagAsString(tag);
218
219 target.clear();
220
221 try
222 {
223 std::string tmp;
224 for (size_t i = 0; i < value.size(); i++)
225 {
226 if (value[i] == '\\')
227 {
228 target.push_back(boost::lexical_cast<float>(tmp));
229 tmp.clear();
230 }
231 else
232 {
233 tmp.push_back(value[i]);
234 }
235 }
236
237 target.push_back(boost::lexical_cast<float>(tmp));
238 }
239 catch (boost::bad_lexical_cast)
240 {
241 // Unable to parse the Image Orientation Patient.
242 throw OrthancClientException(Orthanc::ErrorCode_BadFileFormat);
243 }
244 }
245
246
247 const uint64_t Instance::GetDicomSize()
248 {
249 DownloadDicom();
250 assert(dicom_.get() != NULL);
251 return dicom_->size();
252 }
253
254 const void* Instance::GetDicom()
255 {
256 DownloadDicom();
257 assert(dicom_.get() != NULL);
258
259 if (dicom_->size() == 0)
260 {
261 return NULL;
262 }
263 else
264 {
265 return &((*dicom_) [0]);
266 }
267 }
268
269
270 void Instance::LoadTagContent(const char* path)
271 {
272 Orthanc::HttpClient client(connection_.GetHttpClient());
273 client.SetUrl(std::string(connection_.GetOrthancUrl()) + "/instances/" + id_ + "/content/" + path);
274
275 if (!client.Apply(content_))
276 {
277 throw OrthancClientException(Orthanc::ErrorCode_UnknownResource);
278 }
279 }
280
281
282 const char* Instance::GetLoadedTagContent() const
283 {
284 return content_.c_str();
285 }
286 }