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