comparison OrthancCppClient/Instance.cpp @ 479:0cd977e94479

initial commit of the c++ client
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 16 Jul 2013 09:08:09 +0200
parents
children 7f7a2d174acb
comparison
equal deleted inserted replaced
478:888f8a778e70 479:0cd977e94479
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2013 Medical Physics Department, CHU of Liege,
4 * 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 "Instance.h"
34
35 #include "OrthancConnection.h"
36 #include "../Core/OrthancException.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 Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
67 }
68
69 Orthanc::HttpClient client(connection_.GetHttpClient());
70 client.SetUrl(connection_.GetOrthancUrl() + "/instances/" + id_ + "/" + suffix);
71 std::string png;
72
73 if (!client.Apply(png))
74 {
75 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
76 }
77
78 reader_.reset(new Orthanc::PngReader);
79 reader_->ReadFromMemory(png);
80 }
81 }
82
83 Instance::Instance(const OrthancConnection& connection,
84 const std::string& id) :
85 connection_(connection),
86 id_(id),
87 mode_(Orthanc::ImageExtractionMode_Int16)
88 {
89 Orthanc::HttpClient client(connection_.GetHttpClient());
90
91 client.SetUrl(connection_.GetOrthancUrl() + "/instances/" + id_ + "/simplified-tags");
92 Json::Value v;
93 if (!client.Apply(tags_))
94 {
95 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);
96 }
97 }
98
99 std::string Instance::GetTagAsString(const char* tag)
100 {
101 if (tags_.isMember(tag))
102 {
103 return tags_[tag].asString();
104 }
105 else
106 {
107 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem);
108 }
109 }
110
111 float Instance::GetTagAsFloat(const char* tag)
112 {
113 std::string value = GetTagAsString(tag);
114
115 try
116 {
117 return boost::lexical_cast<float>(value);
118 }
119 catch (boost::bad_lexical_cast)
120 {
121 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
122 }
123 }
124
125 int Instance::GetTagAsInt(const char* tag)
126 {
127 std::string value = GetTagAsString(tag);
128
129 try
130 {
131 return boost::lexical_cast<int>(value);
132 }
133 catch (boost::bad_lexical_cast)
134 {
135 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
136 }
137 }
138
139 unsigned int Instance::GetWidth()
140 {
141 DownloadImage();
142 return reader_->GetWidth();
143 }
144
145 unsigned int Instance::GetHeight()
146 {
147 DownloadImage();
148 return reader_->GetHeight();
149 }
150
151 unsigned int Instance::GetPitch()
152 {
153 DownloadImage();
154 return reader_->GetPitch();
155 }
156
157 Orthanc::PixelFormat Instance::GetPixelFormat()
158 {
159 DownloadImage();
160 return reader_->GetFormat();
161 }
162
163 const void* Instance::GetBuffer()
164 {
165 DownloadImage();
166 return reader_->GetBuffer();
167 }
168
169 const void* Instance::GetBuffer(unsigned int y)
170 {
171 DownloadImage();
172 return reader_->GetBuffer(y);
173 }
174
175 void Instance::DiscardImage()
176 {
177 reader_.reset();
178 }
179
180
181 void Instance::SetImageExtractionMode(Orthanc::ImageExtractionMode mode)
182 {
183 if (mode_ == mode)
184 {
185 return;
186 }
187
188 DiscardImage();
189 mode_ = mode;
190 }
191
192
193 void Instance::SplitVectorOfFloats(std::vector<float>& target,
194 const char* tag)
195 {
196 const std::string value = GetTagAsString(tag);
197
198 target.clear();
199
200 try
201 {
202 std::string tmp;
203 for (size_t i = 0; i < value.size(); i++)
204 {
205 if (value[i] == '\\')
206 {
207 target.push_back(boost::lexical_cast<float>(tmp));
208 tmp.clear();
209 }
210 else
211 {
212 tmp.push_back(value[i]);
213 }
214 }
215
216 target.push_back(boost::lexical_cast<float>(tmp));
217 }
218 catch (boost::bad_lexical_cast)
219 {
220 // Unable to parse the Image Orientation Patient.
221 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
222 }
223 }
224 }