Mercurial > hg > orthanc-stone
annotate Framework/Oracle/GetOrthancWebViewerJpegCommand.cpp @ 976:3abc47e051c8
Added tag toa2019083101 for changeset e75fd08d6c75
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Sat, 31 Aug 2019 13:50:11 +0200 |
parents | a7351ad54960 |
children | 81b29bc7c3d4 2d8ab34c8c91 |
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 "GetOrthancWebViewerJpegCommand.h" | |
23 | |
24 #include "../Toolbox/LinearAlgebra.h" | |
25 | |
26 #include <Core/Images/Image.h> | |
27 #include <Core/Images/ImageProcessing.h> | |
28 #include <Core/Images/JpegReader.h> | |
29 #include <Core/OrthancException.h> | |
30 #include <Core/Toolbox.h> | |
31 | |
32 #include <json/reader.h> | |
33 #include <json/value.h> | |
34 | |
35 namespace OrthancStone | |
36 { | |
37 GetOrthancWebViewerJpegCommand::SuccessMessage::SuccessMessage(const GetOrthancWebViewerJpegCommand& command, | |
38 Orthanc::ImageAccessor* image) : // Takes ownership | |
39 OriginMessage(command), | |
40 image_(image) | |
41 { | |
42 if (image == NULL) | |
43 { | |
44 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
45 } | |
46 } | |
47 | |
48 | |
49 GetOrthancWebViewerJpegCommand::GetOrthancWebViewerJpegCommand() : | |
50 frame_(0), | |
51 quality_(95), | |
956
a7351ad54960
Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents:
846
diff
changeset
|
52 timeout_(600), |
746 | 53 expectedFormat_(Orthanc::PixelFormat_Grayscale8) |
54 { | |
55 } | |
56 | |
57 | |
58 void GetOrthancWebViewerJpegCommand::SetQuality(unsigned int quality) | |
59 { | |
60 if (quality <= 0 || | |
61 quality > 100) | |
62 { | |
63 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
64 } | |
65 else | |
66 { | |
67 quality_ = quality; | |
68 } | |
69 } | |
70 | |
71 | |
72 std::string GetOrthancWebViewerJpegCommand::GetUri() const | |
73 { | |
74 return ("/web-viewer/instances/jpeg" + boost::lexical_cast<std::string>(quality_) + | |
75 "-" + instanceId_ + "_" + boost::lexical_cast<std::string>(frame_)); | |
76 } | |
77 | |
78 | |
79 void GetOrthancWebViewerJpegCommand::ProcessHttpAnswer(IMessageEmitter& emitter, | |
80 const IObserver& receiver, | |
81 const std::string& answer) const | |
82 { | |
83 // This code comes from older "OrthancSlicesLoader::ParseSliceImageJpeg()" | |
84 | |
85 Json::Value encoded; | |
86 | |
87 { | |
88 Json::Reader reader; | |
89 if (!reader.parse(answer, encoded)) | |
90 { | |
91 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
92 } | |
93 } | |
94 | |
95 if (encoded.type() != Json::objectValue || | |
96 !encoded.isMember("Orthanc") || | |
97 encoded["Orthanc"].type() != Json::objectValue) | |
98 { | |
99 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
100 } | |
101 | |
102 const Json::Value& info = encoded["Orthanc"]; | |
103 if (!info.isMember("PixelData") || | |
104 !info.isMember("Stretched") || | |
105 !info.isMember("Compression") || | |
106 info["Compression"].type() != Json::stringValue || | |
107 info["PixelData"].type() != Json::stringValue || | |
108 info["Stretched"].type() != Json::booleanValue || | |
109 info["Compression"].asString() != "Jpeg") | |
110 { | |
111 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
112 } | |
113 | |
114 bool isSigned = false; | |
115 bool isStretched = info["Stretched"].asBool(); | |
116 | |
117 if (info.isMember("IsSigned")) | |
118 { | |
119 if (info["IsSigned"].type() != Json::booleanValue) | |
120 { | |
121 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
122 } | |
123 else | |
124 { | |
125 isSigned = info["IsSigned"].asBool(); | |
126 } | |
127 } | |
128 | |
129 std::auto_ptr<Orthanc::ImageAccessor> reader; | |
130 | |
131 { | |
132 std::string jpeg; | |
133 Orthanc::Toolbox::DecodeBase64(jpeg, info["PixelData"].asString()); | |
134 | |
135 reader.reset(new Orthanc::JpegReader); | |
136 dynamic_cast<Orthanc::JpegReader&>(*reader).ReadFromMemory(jpeg); | |
137 } | |
138 | |
139 if (reader->GetFormat() == Orthanc::PixelFormat_RGB24) // This is a color image | |
140 { | |
141 if (expectedFormat_ != Orthanc::PixelFormat_RGB24) | |
142 { | |
143 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
144 } | |
145 | |
146 if (isSigned || isStretched) | |
147 { | |
148 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
149 } | |
150 else | |
151 { | |
152 SuccessMessage message(*this, reader.release()); | |
153 emitter.EmitMessage(receiver, message); | |
154 return; | |
155 } | |
156 } | |
157 | |
158 if (reader->GetFormat() != Orthanc::PixelFormat_Grayscale8) | |
159 { | |
160 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
161 } | |
162 | |
163 if (!isStretched) | |
164 { | |
165 if (expectedFormat_ != reader->GetFormat()) | |
166 { | |
167 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
168 } | |
169 else | |
170 { | |
171 SuccessMessage message(*this, reader.release()); | |
172 emitter.EmitMessage(receiver, message); | |
173 return; | |
174 } | |
175 } | |
176 | |
177 int32_t stretchLow = 0; | |
178 int32_t stretchHigh = 0; | |
179 | |
180 if (!info.isMember("StretchLow") || | |
181 !info.isMember("StretchHigh") || | |
182 info["StretchLow"].type() != Json::intValue || | |
183 info["StretchHigh"].type() != Json::intValue) | |
184 { | |
185 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
186 } | |
187 | |
188 stretchLow = info["StretchLow"].asInt(); | |
189 stretchHigh = info["StretchHigh"].asInt(); | |
190 | |
191 if (stretchLow < -32768 || | |
192 stretchHigh > 65535 || | |
193 (stretchLow < 0 && stretchHigh > 32767)) | |
194 { | |
195 // This range cannot be represented with a uint16_t or an int16_t | |
196 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
197 } | |
198 | |
199 // Decode a grayscale JPEG 8bpp image coming from the Web viewer | |
200 std::auto_ptr<Orthanc::ImageAccessor> image | |
201 (new Orthanc::Image(expectedFormat_, reader->GetWidth(), reader->GetHeight(), false)); | |
202 | |
203 Orthanc::ImageProcessing::Convert(*image, *reader); | |
204 reader.reset(); | |
205 | |
206 float scaling = static_cast<float>(stretchHigh - stretchLow) / 255.0f; | |
207 | |
208 if (!LinearAlgebra::IsCloseToZero(scaling)) | |
209 { | |
210 float offset = static_cast<float>(stretchLow) / scaling; | |
211 Orthanc::ImageProcessing::ShiftScale(*image, offset, scaling, true); | |
212 } | |
213 | |
214 SuccessMessage message(*this, image.release()); | |
215 emitter.EmitMessage(receiver, message); | |
216 } | |
217 } |