annotate Framework/Oracle/GetOrthancWebViewerJpegCommand.cpp @ 976:3abc47e051c8

Added tag toa2019083101 for changeset e75fd08d6c75
author Benjamin Golinvaux <bgo@osimis.io>
date Sat, 31 Aug 2019 13:50:11 +0200
parents a7351ad54960
children 81b29bc7c3d4 2d8ab34c8c91
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
746
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Stone of Orthanc
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Affero General Public License for more details.
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 **/
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 #include "GetOrthancWebViewerJpegCommand.h"
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "../Toolbox/LinearAlgebra.h"
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include <Core/Images/Image.h>
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include <Core/Images/ImageProcessing.h>
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 #include <Core/Images/JpegReader.h>
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 #include <Core/OrthancException.h>
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 #include <Core/Toolbox.h>
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 #include <json/reader.h>
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 #include <json/value.h>
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 namespace OrthancStone
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 GetOrthancWebViewerJpegCommand::SuccessMessage::SuccessMessage(const GetOrthancWebViewerJpegCommand& command,
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 Orthanc::ImageAccessor* image) : // Takes ownership
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 OriginMessage(command),
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 image_(image)
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 if (image == NULL)
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 GetOrthancWebViewerJpegCommand::GetOrthancWebViewerJpegCommand() :
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 frame_(0),
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 quality_(95),
956
a7351ad54960 Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents: 846
diff changeset
52 timeout_(600),
746
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 expectedFormat_(Orthanc::PixelFormat_Grayscale8)
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 void GetOrthancWebViewerJpegCommand::SetQuality(unsigned int quality)
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 if (quality <= 0 ||
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 quality > 100)
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 else
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 quality_ = quality;
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 std::string GetOrthancWebViewerJpegCommand::GetUri() const
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 return ("/web-viewer/instances/jpeg" + boost::lexical_cast<std::string>(quality_) +
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 "-" + instanceId_ + "_" + boost::lexical_cast<std::string>(frame_));
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 void GetOrthancWebViewerJpegCommand::ProcessHttpAnswer(IMessageEmitter& emitter,
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 const IObserver& receiver,
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 const std::string& answer) const
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 // This code comes from older "OrthancSlicesLoader::ParseSliceImageJpeg()"
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 Json::Value encoded;
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 Json::Reader reader;
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 if (!reader.parse(answer, encoded))
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 if (encoded.type() != Json::objectValue ||
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 !encoded.isMember("Orthanc") ||
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 encoded["Orthanc"].type() != Json::objectValue)
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 const Json::Value& info = encoded["Orthanc"];
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 if (!info.isMember("PixelData") ||
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 !info.isMember("Stretched") ||
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 !info.isMember("Compression") ||
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 info["Compression"].type() != Json::stringValue ||
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 info["PixelData"].type() != Json::stringValue ||
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 info["Stretched"].type() != Json::booleanValue ||
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 info["Compression"].asString() != "Jpeg")
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 bool isSigned = false;
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 bool isStretched = info["Stretched"].asBool();
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 if (info.isMember("IsSigned"))
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 if (info["IsSigned"].type() != Json::booleanValue)
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 else
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 isSigned = info["IsSigned"].asBool();
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 std::auto_ptr<Orthanc::ImageAccessor> reader;
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 std::string jpeg;
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 Orthanc::Toolbox::DecodeBase64(jpeg, info["PixelData"].asString());
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 reader.reset(new Orthanc::JpegReader);
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 dynamic_cast<Orthanc::JpegReader&>(*reader).ReadFromMemory(jpeg);
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 if (reader->GetFormat() == Orthanc::PixelFormat_RGB24) // This is a color image
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 if (expectedFormat_ != Orthanc::PixelFormat_RGB24)
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 if (isSigned || isStretched)
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 else
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 SuccessMessage message(*this, reader.release());
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 emitter.EmitMessage(receiver, message);
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 return;
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 if (reader->GetFormat() != Orthanc::PixelFormat_Grayscale8)
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 if (!isStretched)
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 if (expectedFormat_ != reader->GetFormat())
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 else
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 SuccessMessage message(*this, reader.release());
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 emitter.EmitMessage(receiver, message);
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 return;
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 int32_t stretchLow = 0;
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 int32_t stretchHigh = 0;
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 if (!info.isMember("StretchLow") ||
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 !info.isMember("StretchHigh") ||
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 info["StretchLow"].type() != Json::intValue ||
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 info["StretchHigh"].type() != Json::intValue)
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 stretchLow = info["StretchLow"].asInt();
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 stretchHigh = info["StretchHigh"].asInt();
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 if (stretchLow < -32768 ||
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 stretchHigh > 65535 ||
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 (stretchLow < 0 && stretchHigh > 32767))
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 // This range cannot be represented with a uint16_t or an int16_t
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 // Decode a grayscale JPEG 8bpp image coming from the Web viewer
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 std::auto_ptr<Orthanc::ImageAccessor> image
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 (new Orthanc::Image(expectedFormat_, reader->GetWidth(), reader->GetHeight(), false));
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 Orthanc::ImageProcessing::Convert(*image, *reader);
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 reader.reset();
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 float scaling = static_cast<float>(stretchHigh - stretchLow) / 255.0f;
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 if (!LinearAlgebra::IsCloseToZero(scaling))
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 {
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 float offset = static_cast<float>(stretchLow) / scaling;
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 Orthanc::ImageProcessing::ShiftScale(*image, offset, scaling, true);
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 SuccessMessage message(*this, image.release());
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 emitter.EmitMessage(receiver, message);
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 }
d716bfb3e07c reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 }