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