Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Oracle/GetOrthancImageCommand.cpp @ 2066:cf3d85eb291c deep-learning
added class OpenGLTextureVolume
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 04 May 2023 17:18:14 +0200 |
parents | 7053b8a0aaec |
children | 07964689cb0b |
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 | |
1871
7053b8a0aaec
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1870
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
7053b8a0aaec
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1870
diff
changeset
|
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
746 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
9 * modify it under the terms of the GNU Lesser General Public License |
746 | 10 * as published by the Free Software Foundation, either version 3 of |
11 * the License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
16 * Lesser General Public License for more details. |
1596
4fb8fdf03314
removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1572
diff
changeset
|
17 * |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
18 * You should have received a copy of the GNU Lesser General Public |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
19 * License along with this program. If not, see |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
20 * <http://www.gnu.org/licenses/>. |
746 | 21 **/ |
22 | |
23 | |
24 #include "GetOrthancImageCommand.h" | |
25 | |
1455
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1352
diff
changeset
|
26 #include <Images/JpegReader.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1352
diff
changeset
|
27 #include <Images/PamReader.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1352
diff
changeset
|
28 #include <Images/PngReader.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1352
diff
changeset
|
29 #include <OrthancException.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1352
diff
changeset
|
30 #include <Toolbox.h> |
746 | 31 |
32 namespace OrthancStone | |
33 { | |
34 GetOrthancImageCommand::GetOrthancImageCommand() : | |
35 uri_("/"), | |
956
a7351ad54960
Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents:
846
diff
changeset
|
36 timeout_(600), |
1571 | 37 hasExpectedFormat_(false), |
38 expectedFormat_(Orthanc::PixelFormat_Grayscale8) | |
746 | 39 { |
40 } | |
41 | |
42 | |
43 void GetOrthancImageCommand::SetExpectedPixelFormat(Orthanc::PixelFormat format) | |
44 { | |
45 hasExpectedFormat_ = true; | |
46 expectedFormat_ = format; | |
47 } | |
48 | |
49 | |
1152
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
50 static std::string GetFormatSuffix(Orthanc::PixelFormat pixelFormat) |
746 | 51 { |
52 switch (pixelFormat) | |
53 { | |
54 case Orthanc::PixelFormat_RGB24: | |
1152
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
55 return "preview"; |
746 | 56 |
57 case Orthanc::PixelFormat_Grayscale16: | |
1152
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
58 return "image-uint16"; |
746 | 59 |
60 case Orthanc::PixelFormat_SignedGrayscale16: | |
1152
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
61 return "image-int16"; |
746 | 62 |
63 default: | |
64 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
65 } | |
66 } | |
67 | |
1152
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
68 |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
69 void GetOrthancImageCommand::SetInstanceUri(const std::string& instance, |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
70 Orthanc::PixelFormat 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 uri_ = "/instances/" + instance + "/" + GetFormatSuffix(pixelFormat); |
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 |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
75 |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
76 void GetOrthancImageCommand::SetFrameUri(const std::string& instance, |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
77 unsigned int frame, |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
78 Orthanc::PixelFormat pixelFormat) |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
79 { |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
80 uri_ = ("/instances/" + instance + "/frames/" + |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
81 boost::lexical_cast<std::string>(frame) + "/" + GetFormatSuffix(pixelFormat)); |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
82 } |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
83 |
78b8bfe154bc
GetOrthancImageCommand::SetFrameUri()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
84 |
1134
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
85 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
|
86 IMessageEmitter& emitter, |
746 | 87 const std::string& answer, |
88 const HttpHeaders& answerHeaders) const | |
89 { | |
1255
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
90 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
|
91 { |
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
92 std::string key = Orthanc::Toolbox::StripSpaces(it->first); |
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
93 Orthanc::Toolbox::ToLowerCase(key); |
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
94 |
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
95 if (key == "content-disposition" && |
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
96 it->second == "filename=\"unsupported.png\"") |
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
97 { |
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
98 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat, |
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
99 "Orthanc cannot decode this image"); |
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
100 } |
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
101 } |
c1c83c1fb837
GetOrthancImageCommand: handling of unsupported formats
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1152
diff
changeset
|
102 |
746 | 103 Orthanc::MimeType contentType = Orthanc::MimeType_Binary; |
104 | |
105 for (HttpHeaders::const_iterator it = answerHeaders.begin(); | |
106 it != answerHeaders.end(); ++it) | |
107 { | |
108 std::string s; | |
109 Orthanc::Toolbox::ToLowerCase(s, it->first); | |
110 | |
111 if (s == "content-type") | |
112 { | |
113 contentType = Orthanc::StringToMimeType(it->second); | |
114 break; | |
115 } | |
116 } | |
117 | |
1298
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
118 std::unique_ptr<Orthanc::ImageAccessor> image; |
746 | 119 |
120 switch (contentType) | |
121 { | |
122 case Orthanc::MimeType_Png: | |
123 { | |
124 image.reset(new Orthanc::PngReader); | |
125 dynamic_cast<Orthanc::PngReader&>(*image).ReadFromMemory(answer); | |
126 break; | |
127 } | |
128 | |
129 case Orthanc::MimeType_Pam: | |
130 { | |
1352
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
131 #ifdef __EMSCRIPTEN__ |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
132 // "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
|
133 // 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
|
134 // 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
|
135 // 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
|
136 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
|
137 #else |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
138 // potentially unaligned, with is faster and consumes less heap memory |
1572 | 139 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
|
140 #endif |
746 | 141 dynamic_cast<Orthanc::PamReader&>(*image).ReadFromMemory(answer); |
142 break; | |
143 } | |
144 | |
145 case Orthanc::MimeType_Jpeg: | |
146 { | |
147 image.reset(new Orthanc::JpegReader); | |
148 dynamic_cast<Orthanc::JpegReader&>(*image).ReadFromMemory(answer); | |
149 break; | |
150 } | |
151 | |
152 default: | |
153 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol, | |
154 "Unsupported HTTP Content-Type for an image: " + | |
155 std::string(Orthanc::EnumerationToString(contentType))); | |
156 } | |
157 | |
158 if (hasExpectedFormat_) | |
159 { | |
160 if (expectedFormat_ == Orthanc::PixelFormat_SignedGrayscale16 && | |
161 image->GetFormat() == Orthanc::PixelFormat_Grayscale16) | |
162 { | |
163 image->SetFormat(Orthanc::PixelFormat_SignedGrayscale16); | |
164 } | |
165 | |
166 if (expectedFormat_ != image->GetFormat()) | |
167 { | |
168 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
169 } | |
170 } | |
171 | |
1352
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
172 //{ |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
173 // // 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
|
174 // const Orthanc::ImageAccessor& source = *image; |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
175 // const void* sourceBuffer = source.GetConstBuffer(); |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
176 // 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
|
177 // int sourceWidth = source.GetWidth(); |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
178 // int sourceHeight = source.GetHeight(); |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
179 // int sourcePitch = source.GetPitch(); |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
180 |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
181 // // 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
|
182 // LOG(ERROR) << "GetOrthancImageCommand::ProcessHttpAnswer | source:" |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
183 // << " W = " << sourceWidth << " H = " << sourceHeight |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
184 // << " P = " << sourcePitch << " B = " << sourceBufferInt |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
185 // << " B % 4 == " << sourceBufferInt % 4; |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
186 //} |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
187 |
159708a38af4
Enforce alignment when using PamReader to retrieve REST images,
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
188 |
1134
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
189 SuccessMessage message(*this, *image, contentType); |
746 | 190 emitter.EmitMessage(receiver, message); |
191 } | |
192 } |