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