Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Oracle/GetOrthancImageCommand.cpp @ 1582:cdd1932c530c
fix cppcheck
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 16 Oct 2020 17:34:53 +0200 |
parents | 1b3039384972 |
children | 4fb8fdf03314 |
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 | |
1270
2d8ab34c8c91
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
956
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
746 | 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 | |
1455
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1352
diff
changeset
|
24 #include <Images/JpegReader.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1352
diff
changeset
|
25 #include <Images/PamReader.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1352
diff
changeset
|
26 #include <Images/PngReader.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1352
diff
changeset
|
27 #include <OrthancException.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1352
diff
changeset
|
28 #include <Toolbox.h> |
746 | 29 |
30 namespace OrthancStone | |
31 { | |
32 GetOrthancImageCommand::GetOrthancImageCommand() : | |
33 uri_("/"), | |
956
a7351ad54960
Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents:
846
diff
changeset
|
34 timeout_(600), |
1571 | 35 hasExpectedFormat_(false), |
36 expectedFormat_(Orthanc::PixelFormat_Grayscale8) | |
746 | 37 { |
38 } | |
39 | |
40 | |
41 void GetOrthancImageCommand::SetExpectedPixelFormat(Orthanc::PixelFormat format) | |
42 { | |
43 hasExpectedFormat_ = true; | |
44 expectedFormat_ = format; | |
45 } | |
46 | |
47 | |
1152
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
48 static std::string GetFormatSuffix(Orthanc::PixelFormat pixelFormat) |
746 | 49 { |
50 switch (pixelFormat) | |
51 { | |
52 case Orthanc::PixelFormat_RGB24: | |
1152
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
53 return "preview"; |
746 | 54 |
55 case Orthanc::PixelFormat_Grayscale16: | |
1152
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
56 return "image-uint16"; |
746 | 57 |
58 case Orthanc::PixelFormat_SignedGrayscale16: | |
1152
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
59 return "image-int16"; |
746 | 60 |
61 default: | |
62 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
63 } | |
64 } | |
65 | |
1152
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
66 |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
67 void GetOrthancImageCommand::SetInstanceUri(const std::string& instance, |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
68 Orthanc::PixelFormat pixelFormat) |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
69 { |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
70 uri_ = "/instances/" + instance + "/" + GetFormatSuffix(pixelFormat); |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
71 } |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
72 |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
73 |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
74 void GetOrthancImageCommand::SetFrameUri(const std::string& instance, |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
75 unsigned int frame, |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
76 Orthanc::PixelFormat pixelFormat) |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
77 { |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
78 uri_ = ("/instances/" + instance + "/frames/" + |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
79 boost::lexical_cast<std::string>(frame) + "/" + GetFormatSuffix(pixelFormat)); |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
80 } |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
81 |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
82 |
1134
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
83 void GetOrthancImageCommand::ProcessHttpAnswer(boost::weak_ptr<IObserver> receiver, |
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
84 IMessageEmitter& emitter, |
746 | 85 const std::string& answer, |
86 const HttpHeaders& answerHeaders) const | |
87 { | |
1255
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
88 for (HttpHeaders::const_iterator it = answerHeaders.begin(); it != answerHeaders.end(); ++it) |
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
89 { |
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
90 std::string key = Orthanc::Toolbox::StripSpaces(it->first); |
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
91 Orthanc::Toolbox::ToLowerCase(key); |
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
92 |
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
93 if (key == "content-disposition" && |
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
94 it->second == "filename=\"unsupported.png\"") |
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
95 { |
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
96 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat, |
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
97 "Orthanc cannot decode this image"); |
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
98 } |
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
99 } |
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
100 |
746 | 101 Orthanc::MimeType contentType = Orthanc::MimeType_Binary; |
102 | |
103 for (HttpHeaders::const_iterator it = answerHeaders.begin(); | |
104 it != answerHeaders.end(); ++it) | |
105 { | |
106 std::string s; | |
107 Orthanc::Toolbox::ToLowerCase(s, it->first); | |
108 | |
109 if (s == "content-type") | |
110 { | |
111 contentType = Orthanc::StringToMimeType(it->second); | |
112 break; | |
113 } | |
114 } | |
115 | |
1298
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
116 std::unique_ptr<Orthanc::ImageAccessor> image; |
746 | 117 |
118 switch (contentType) | |
119 { | |
120 case Orthanc::MimeType_Png: | |
121 { | |
122 image.reset(new Orthanc::PngReader); | |
123 dynamic_cast<Orthanc::PngReader&>(*image).ReadFromMemory(answer); | |
124 break; | |
125 } | |
126 | |
127 case Orthanc::MimeType_Pam: | |
128 { | |
1352
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
129 #ifdef __EMSCRIPTEN__ |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
130 // "true" means we ask the PamReader to make an extra copy so that |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
131 // the resulting Orthanc::ImageAccessor is aligned (as malloc is). |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
132 // Indeed, even though alignment is not required in Web Assembly, |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
133 // Emscripten seems to check it and bail out if addresses are "odd" |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
134 image.reset(new Orthanc::PamReader(true)); |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
135 #else |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
136 // potentially unaligned, with is faster and consumes less heap memory |
1572 | 137 image.reset(new Orthanc::PamReader(false)); |
1352
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
138 #endif |
746 | 139 dynamic_cast<Orthanc::PamReader&>(*image).ReadFromMemory(answer); |
140 break; | |
141 } | |
142 | |
143 case Orthanc::MimeType_Jpeg: | |
144 { | |
145 image.reset(new Orthanc::JpegReader); | |
146 dynamic_cast<Orthanc::JpegReader&>(*image).ReadFromMemory(answer); | |
147 break; | |
148 } | |
149 | |
150 default: | |
151 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol, | |
152 "Unsupported HTTP Content-Type for an image: " + | |
153 std::string(Orthanc::EnumerationToString(contentType))); | |
154 } | |
155 | |
156 if (hasExpectedFormat_) | |
157 { | |
158 if (expectedFormat_ == Orthanc::PixelFormat_SignedGrayscale16 && | |
159 image->GetFormat() == Orthanc::PixelFormat_Grayscale16) | |
160 { | |
161 image->SetFormat(Orthanc::PixelFormat_SignedGrayscale16); | |
162 } | |
163 | |
164 if (expectedFormat_ != image->GetFormat()) | |
165 { | |
166 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
167 } | |
168 } | |
169 | |
1352
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
170 //{ |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
171 // // DEBUG DISPLAY IMAGE PROPERTIES BGO 2020-04-11 |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
172 // const Orthanc::ImageAccessor& source = *image; |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
173 // const void* sourceBuffer = source.GetConstBuffer(); |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
174 // intptr_t sourceBufferInt = reinterpret_cast<intptr_t>(sourceBuffer); |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
175 // int sourceWidth = source.GetWidth(); |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
176 // int sourceHeight = source.GetHeight(); |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
177 // int sourcePitch = source.GetPitch(); |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
178 |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
179 // // TODO: turn error into trace below |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
180 // LOG(ERROR) << "GetOrthancImageCommand::ProcessHttpAnswer | source:" |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
181 // << " W = " << sourceWidth << " H = " << sourceHeight |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
182 // << " P = " << sourcePitch << " B = " << sourceBufferInt |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
183 // << " B % 4 == " << sourceBufferInt % 4; |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
184 //} |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
185 |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
186 |
1134
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
187 SuccessMessage message(*this, *image, contentType); |
746 | 188 emitter.EmitMessage(receiver, message); |
189 } | |
190 } |