Mercurial > hg > orthanc-stone
annotate Framework/Oracle/GetOrthancImageCommand.cpp @ 1098:17660df24c36 broker
simplification of IOracleRunner
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 25 Oct 2019 13:01:24 +0200 |
parents | 81b29bc7c3d4 |
children | 87fbeb823375 |
rev | line source |
---|---|
746 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "GetOrthancImageCommand.h" | |
23 | |
24 #include <Core/Images/JpegReader.h> | |
25 #include <Core/Images/PamReader.h> | |
26 #include <Core/Images/PngReader.h> | |
27 #include <Core/OrthancException.h> | |
28 #include <Core/Toolbox.h> | |
29 | |
30 namespace OrthancStone | |
31 { | |
32 GetOrthancImageCommand::SuccessMessage::SuccessMessage(const GetOrthancImageCommand& command, | |
33 Orthanc::ImageAccessor* image, // Takes ownership | |
34 Orthanc::MimeType mime) : | |
35 OriginMessage(command), | |
36 image_(image), | |
37 mime_(mime) | |
38 { | |
39 if (image == NULL) | |
40 { | |
41 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
42 } | |
43 } | |
44 | |
45 | |
46 GetOrthancImageCommand::GetOrthancImageCommand() : | |
47 uri_("/"), | |
956
a7351ad54960
Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents:
846
diff
changeset
|
48 timeout_(600), |
746 | 49 hasExpectedFormat_(false) |
50 { | |
51 } | |
52 | |
53 | |
54 void GetOrthancImageCommand::SetExpectedPixelFormat(Orthanc::PixelFormat format) | |
55 { | |
56 hasExpectedFormat_ = true; | |
57 expectedFormat_ = format; | |
58 } | |
59 | |
60 | |
61 void GetOrthancImageCommand::SetInstanceUri(const std::string& instance, | |
62 Orthanc::PixelFormat pixelFormat) | |
63 { | |
64 uri_ = "/instances/" + instance; | |
65 | |
66 switch (pixelFormat) | |
67 { | |
68 case Orthanc::PixelFormat_RGB24: | |
69 uri_ += "/preview"; | |
70 break; | |
71 | |
72 case Orthanc::PixelFormat_Grayscale16: | |
73 uri_ += "/image-uint16"; | |
74 break; | |
75 | |
76 case Orthanc::PixelFormat_SignedGrayscale16: | |
77 uri_ += "/image-int16"; | |
78 break; | |
79 | |
80 default: | |
81 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
82 } | |
83 } | |
84 | |
1098
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1075
diff
changeset
|
85 IMessage* GetOrthancImageCommand::ProcessHttpAnswer(const std::string& answer, |
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1075
diff
changeset
|
86 const HttpHeaders& answerHeaders) const |
746 | 87 { |
88 Orthanc::MimeType contentType = Orthanc::MimeType_Binary; | |
89 | |
90 for (HttpHeaders::const_iterator it = answerHeaders.begin(); | |
91 it != answerHeaders.end(); ++it) | |
92 { | |
93 std::string s; | |
94 Orthanc::Toolbox::ToLowerCase(s, it->first); | |
95 | |
96 if (s == "content-type") | |
97 { | |
98 contentType = Orthanc::StringToMimeType(it->second); | |
99 break; | |
100 } | |
101 } | |
102 | |
103 std::auto_ptr<Orthanc::ImageAccessor> image; | |
104 | |
105 switch (contentType) | |
106 { | |
107 case Orthanc::MimeType_Png: | |
108 { | |
109 image.reset(new Orthanc::PngReader); | |
110 dynamic_cast<Orthanc::PngReader&>(*image).ReadFromMemory(answer); | |
111 break; | |
112 } | |
113 | |
114 case Orthanc::MimeType_Pam: | |
115 { | |
116 image.reset(new Orthanc::PamReader); | |
117 dynamic_cast<Orthanc::PamReader&>(*image).ReadFromMemory(answer); | |
118 break; | |
119 } | |
120 | |
121 case Orthanc::MimeType_Jpeg: | |
122 { | |
123 image.reset(new Orthanc::JpegReader); | |
124 dynamic_cast<Orthanc::JpegReader&>(*image).ReadFromMemory(answer); | |
125 break; | |
126 } | |
127 | |
128 default: | |
129 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol, | |
130 "Unsupported HTTP Content-Type for an image: " + | |
131 std::string(Orthanc::EnumerationToString(contentType))); | |
132 } | |
133 | |
134 if (hasExpectedFormat_) | |
135 { | |
136 if (expectedFormat_ == Orthanc::PixelFormat_SignedGrayscale16 && | |
137 image->GetFormat() == Orthanc::PixelFormat_Grayscale16) | |
138 { | |
139 image->SetFormat(Orthanc::PixelFormat_SignedGrayscale16); | |
140 } | |
141 | |
142 if (expectedFormat_ != image->GetFormat()) | |
143 { | |
144 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
145 } | |
146 } | |
147 | |
1098
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1075
diff
changeset
|
148 return new SuccessMessage(*this, image.release(), contentType); |
746 | 149 } |
150 } |