comparison Framework/Oracle/GetOrthancImageCommand.cpp @ 746:d716bfb3e07c

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 22 May 2019 12:48:57 +0200
parents
children 17423d072a95
comparison
equal deleted inserted replaced
745:c44c1d2d3598 746:d716bfb3e07c
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_("/"),
48 timeout_(10),
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
85 void GetOrthancImageCommand::ProcessHttpAnswer(IMessageEmitter& emitter,
86 const IObserver& receiver,
87 const std::string& answer,
88 const HttpHeaders& answerHeaders) const
89 {
90 Orthanc::MimeType contentType = Orthanc::MimeType_Binary;
91
92 for (HttpHeaders::const_iterator it = answerHeaders.begin();
93 it != answerHeaders.end(); ++it)
94 {
95 std::string s;
96 Orthanc::Toolbox::ToLowerCase(s, it->first);
97
98 if (s == "content-type")
99 {
100 contentType = Orthanc::StringToMimeType(it->second);
101 break;
102 }
103 }
104
105 std::auto_ptr<Orthanc::ImageAccessor> image;
106
107 switch (contentType)
108 {
109 case Orthanc::MimeType_Png:
110 {
111 image.reset(new Orthanc::PngReader);
112 dynamic_cast<Orthanc::PngReader&>(*image).ReadFromMemory(answer);
113 break;
114 }
115
116 case Orthanc::MimeType_Pam:
117 {
118 image.reset(new Orthanc::PamReader);
119 dynamic_cast<Orthanc::PamReader&>(*image).ReadFromMemory(answer);
120 break;
121 }
122
123 case Orthanc::MimeType_Jpeg:
124 {
125 image.reset(new Orthanc::JpegReader);
126 dynamic_cast<Orthanc::JpegReader&>(*image).ReadFromMemory(answer);
127 break;
128 }
129
130 default:
131 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol,
132 "Unsupported HTTP Content-Type for an image: " +
133 std::string(Orthanc::EnumerationToString(contentType)));
134 }
135
136 if (hasExpectedFormat_)
137 {
138 if (expectedFormat_ == Orthanc::PixelFormat_SignedGrayscale16 &&
139 image->GetFormat() == Orthanc::PixelFormat_Grayscale16)
140 {
141 image->SetFormat(Orthanc::PixelFormat_SignedGrayscale16);
142 }
143
144 if (expectedFormat_ != image->GetFormat())
145 {
146 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat);
147 }
148 }
149
150 SuccessMessage message(*this, image.release(), contentType);
151 emitter.EmitMessage(receiver, message);
152 }
153 }