Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Oracle/GetOrthancWebViewerJpegCommand.cpp @ 1859:58681a5c727b
overlay: display ContentDate/ContentTime instead of StudyDate if available + new 'TimeFormat' option
author | Alain Mazy <am@osimis.io> |
---|---|
date | Mon, 20 Sep 2021 17:08:23 +0200 |
parents | 9ac2a65d4172 |
children | 3889ae96d2e9 |
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 | |
1739
9ac2a65d4172
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1737
diff
changeset
|
5 * Copyright (C) 2017-2021 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 |
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 { | |
1571 | 48 if (quality == 0 || |
746 | 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, |
746 | 69 const std::string& answer) const |
70 { | |
71 // This code comes from older "OrthancSlicesLoader::ParseSliceImageJpeg()" | |
72 | |
73 Json::Value encoded; | |
1737
ef2f56c0311c
remove calls to deprecated classes of JsonCpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1598
diff
changeset
|
74 if (!Orthanc::Toolbox::ReadJson(encoded, answer)) |
746 | 75 { |
1737
ef2f56c0311c
remove calls to deprecated classes of JsonCpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1598
diff
changeset
|
76 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); |
746 | 77 } |
78 | |
79 if (encoded.type() != Json::objectValue || | |
80 !encoded.isMember("Orthanc") || | |
81 encoded["Orthanc"].type() != Json::objectValue) | |
82 { | |
83 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
84 } | |
85 | |
86 const Json::Value& info = encoded["Orthanc"]; | |
87 if (!info.isMember("PixelData") || | |
88 !info.isMember("Stretched") || | |
89 !info.isMember("Compression") || | |
90 info["Compression"].type() != Json::stringValue || | |
91 info["PixelData"].type() != Json::stringValue || | |
92 info["Stretched"].type() != Json::booleanValue || | |
93 info["Compression"].asString() != "Jpeg") | |
94 { | |
95 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
96 } | |
97 | |
98 bool isSigned = false; | |
99 bool isStretched = info["Stretched"].asBool(); | |
100 | |
101 if (info.isMember("IsSigned")) | |
102 { | |
103 if (info["IsSigned"].type() != Json::booleanValue) | |
104 { | |
105 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
106 } | |
107 else | |
108 { | |
109 isSigned = info["IsSigned"].asBool(); | |
110 } | |
111 } | |
112 | |
1298
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
113 std::unique_ptr<Orthanc::ImageAccessor> reader; |
746 | 114 |
115 { | |
116 std::string jpeg; | |
117 Orthanc::Toolbox::DecodeBase64(jpeg, info["PixelData"].asString()); | |
118 | |
119 reader.reset(new Orthanc::JpegReader); | |
120 dynamic_cast<Orthanc::JpegReader&>(*reader).ReadFromMemory(jpeg); | |
121 } | |
122 | |
123 if (reader->GetFormat() == Orthanc::PixelFormat_RGB24) // This is a color image | |
124 { | |
125 if (expectedFormat_ != Orthanc::PixelFormat_RGB24) | |
126 { | |
127 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
128 } | |
129 | |
130 if (isSigned || isStretched) | |
131 { | |
132 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
133 } | |
134 else | |
135 { | |
1134
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
136 SuccessMessage message(*this, *reader); |
746 | 137 emitter.EmitMessage(receiver, message); |
138 return; | |
139 } | |
140 } | |
141 | |
142 if (reader->GetFormat() != Orthanc::PixelFormat_Grayscale8) | |
143 { | |
144 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
145 } | |
146 | |
147 if (!isStretched) | |
148 { | |
149 if (expectedFormat_ != reader->GetFormat()) | |
150 { | |
151 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
152 } | |
153 else | |
154 { | |
1134
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
155 SuccessMessage message(*this, *reader); |
746 | 156 emitter.EmitMessage(receiver, message); |
157 return; | |
158 } | |
159 } | |
160 | |
161 int32_t stretchLow = 0; | |
162 int32_t stretchHigh = 0; | |
163 | |
164 if (!info.isMember("StretchLow") || | |
165 !info.isMember("StretchHigh") || | |
166 info["StretchLow"].type() != Json::intValue || | |
167 info["StretchHigh"].type() != Json::intValue) | |
168 { | |
169 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
170 } | |
171 | |
172 stretchLow = info["StretchLow"].asInt(); | |
173 stretchHigh = info["StretchHigh"].asInt(); | |
174 | |
175 if (stretchLow < -32768 || | |
176 stretchHigh > 65535 || | |
177 (stretchLow < 0 && stretchHigh > 32767)) | |
178 { | |
179 // This range cannot be represented with a uint16_t or an int16_t | |
180 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
181 } | |
182 | |
183 // 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
|
184 std::unique_ptr<Orthanc::ImageAccessor> image |
746 | 185 (new Orthanc::Image(expectedFormat_, reader->GetWidth(), reader->GetHeight(), false)); |
186 | |
187 Orthanc::ImageProcessing::Convert(*image, *reader); | |
188 reader.reset(); | |
189 | |
190 float scaling = static_cast<float>(stretchHigh - stretchLow) / 255.0f; | |
191 | |
192 if (!LinearAlgebra::IsCloseToZero(scaling)) | |
193 { | |
194 float offset = static_cast<float>(stretchLow) / scaling; | |
195 Orthanc::ImageProcessing::ShiftScale(*image, offset, scaling, true); | |
196 } | |
1098
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1075
diff
changeset
|
197 |
1134
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
198 SuccessMessage message(*this, *image); |
746 | 199 emitter.EmitMessage(receiver, message); |
200 } | |
201 } |