Mercurial > hg > orthanc-stone
annotate Framework/Oracle/GetOrthancWebViewerJpegCommand.cpp @ 1236:358461330978 broker
global function GetWebGLViewportsRegistry()
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 09 Dec 2019 20:45:47 +0100 |
parents | a0a33e5ea5bb |
children | 0ca50d275b9a |
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::GetOrthancWebViewerJpegCommand() : | |
38 frame_(0), | |
39 quality_(95), | |
956
a7351ad54960
Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents:
846
diff
changeset
|
40 timeout_(600), |
746 | 41 expectedFormat_(Orthanc::PixelFormat_Grayscale8) |
42 { | |
43 } | |
44 | |
45 | |
46 void GetOrthancWebViewerJpegCommand::SetQuality(unsigned int quality) | |
47 { | |
48 if (quality <= 0 || | |
49 quality > 100) | |
50 { | |
51 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
52 } | |
53 else | |
54 { | |
55 quality_ = quality; | |
56 } | |
57 } | |
58 | |
59 | |
60 std::string GetOrthancWebViewerJpegCommand::GetUri() const | |
61 { | |
62 return ("/web-viewer/instances/jpeg" + boost::lexical_cast<std::string>(quality_) + | |
63 "-" + instanceId_ + "_" + boost::lexical_cast<std::string>(frame_)); | |
64 } | |
65 | |
66 | |
1134
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
67 void GetOrthancWebViewerJpegCommand::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
|
68 IMessageEmitter& emitter, |
1135
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
69 const std::string& answer) const |
746 | 70 { |
71 // This code comes from older "OrthancSlicesLoader::ParseSliceImageJpeg()" | |
72 | |
73 Json::Value encoded; | |
74 | |
75 { | |
76 Json::Reader reader; | |
77 if (!reader.parse(answer, encoded)) | |
78 { | |
79 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
80 } | |
81 } | |
82 | |
83 if (encoded.type() != Json::objectValue || | |
84 !encoded.isMember("Orthanc") || | |
85 encoded["Orthanc"].type() != Json::objectValue) | |
86 { | |
87 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
88 } | |
89 | |
90 const Json::Value& info = encoded["Orthanc"]; | |
91 if (!info.isMember("PixelData") || | |
92 !info.isMember("Stretched") || | |
93 !info.isMember("Compression") || | |
94 info["Compression"].type() != Json::stringValue || | |
95 info["PixelData"].type() != Json::stringValue || | |
96 info["Stretched"].type() != Json::booleanValue || | |
97 info["Compression"].asString() != "Jpeg") | |
98 { | |
99 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
100 } | |
101 | |
102 bool isSigned = false; | |
103 bool isStretched = info["Stretched"].asBool(); | |
104 | |
105 if (info.isMember("IsSigned")) | |
106 { | |
107 if (info["IsSigned"].type() != Json::booleanValue) | |
108 { | |
109 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
110 } | |
111 else | |
112 { | |
113 isSigned = info["IsSigned"].asBool(); | |
114 } | |
115 } | |
116 | |
117 std::auto_ptr<Orthanc::ImageAccessor> reader; | |
118 | |
119 { | |
120 std::string jpeg; | |
121 Orthanc::Toolbox::DecodeBase64(jpeg, info["PixelData"].asString()); | |
122 | |
123 reader.reset(new Orthanc::JpegReader); | |
124 dynamic_cast<Orthanc::JpegReader&>(*reader).ReadFromMemory(jpeg); | |
125 } | |
126 | |
127 if (reader->GetFormat() == Orthanc::PixelFormat_RGB24) // This is a color image | |
128 { | |
129 if (expectedFormat_ != Orthanc::PixelFormat_RGB24) | |
130 { | |
131 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
132 } | |
133 | |
134 if (isSigned || isStretched) | |
135 { | |
136 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
137 } | |
138 else | |
139 { | |
1134
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
140 SuccessMessage message(*this, *reader); |
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
141 emitter.EmitMessage(receiver, message); |
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
142 return; |
746 | 143 } |
144 } | |
145 | |
146 if (reader->GetFormat() != Orthanc::PixelFormat_Grayscale8) | |
147 { | |
148 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
149 } | |
150 | |
151 if (!isStretched) | |
152 { | |
153 if (expectedFormat_ != reader->GetFormat()) | |
154 { | |
155 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
156 } | |
157 else | |
158 { | |
1134
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
159 SuccessMessage message(*this, *reader); |
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
160 emitter.EmitMessage(receiver, message); |
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
161 return; |
746 | 162 } |
163 } | |
164 | |
165 int32_t stretchLow = 0; | |
166 int32_t stretchHigh = 0; | |
167 | |
168 if (!info.isMember("StretchLow") || | |
169 !info.isMember("StretchHigh") || | |
170 info["StretchLow"].type() != Json::intValue || | |
171 info["StretchHigh"].type() != Json::intValue) | |
172 { | |
173 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
174 } | |
175 | |
176 stretchLow = info["StretchLow"].asInt(); | |
177 stretchHigh = info["StretchHigh"].asInt(); | |
178 | |
179 if (stretchLow < -32768 || | |
180 stretchHigh > 65535 || | |
181 (stretchLow < 0 && stretchHigh > 32767)) | |
182 { | |
183 // This range cannot be represented with a uint16_t or an int16_t | |
184 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
185 } | |
186 | |
187 // Decode a grayscale JPEG 8bpp image coming from the Web viewer | |
188 std::auto_ptr<Orthanc::ImageAccessor> image | |
189 (new Orthanc::Image(expectedFormat_, reader->GetWidth(), reader->GetHeight(), false)); | |
190 | |
191 Orthanc::ImageProcessing::Convert(*image, *reader); | |
192 reader.reset(); | |
193 | |
194 float scaling = static_cast<float>(stretchHigh - stretchLow) / 255.0f; | |
195 | |
196 if (!LinearAlgebra::IsCloseToZero(scaling)) | |
197 { | |
198 float offset = static_cast<float>(stretchLow) / scaling; | |
199 Orthanc::ImageProcessing::ShiftScale(*image, offset, scaling, true); | |
200 } | |
1098
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1075
diff
changeset
|
201 |
1134
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
202 SuccessMessage message(*this, *image); |
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
203 emitter.EmitMessage(receiver, message); |
746 | 204 } |
205 } |