Mercurial > hg > orthanc-stone
annotate Framework/Volumes/VolumeImageProgressivePolicy.cpp @ 83:c75377306598 wasm
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 24 May 2017 23:25:36 +0200 |
parents | 28956ed68280 |
children | 4cff7b1ed31d |
rev | line source |
---|---|
0 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
40
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
5 * Copyright (C) 2017 Osimis, Belgium |
0 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
47 | 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. | |
0 | 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 | |
47 | 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 | |
0 | 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 **/ | |
20 | |
21 | |
22 #include "VolumeImageProgressivePolicy.h" | |
23 | |
24 #include "../Toolbox/DownloadStack.h" | |
16 | 25 #include "../../Resources/Orthanc/Core/Images/ImageProcessing.h" |
0 | 26 |
27 namespace OrthancStone | |
28 { | |
29 class VolumeImageProgressivePolicy::AxialSlicesScheduler | |
30 { | |
31 private: | |
32 size_t depth_; | |
33 DownloadStack stack_; | |
34 | |
35 public: | |
36 AxialSlicesScheduler(size_t depth) : | |
37 depth_(depth), | |
38 stack_(3 * depth) // "3" stands for the number of quality levels | |
39 { | |
40 assert(depth > 0); | |
41 } | |
42 | |
43 void TagFullPriority(int z, | |
44 int neighborhood) | |
45 { | |
46 DownloadStack::Writer writer(stack_); | |
47 | |
48 // Also schedule the neighboring slices for download in medium quality | |
49 for (int offset = neighborhood; offset >= 1; offset--) | |
50 { | |
51 writer.SetTopNodePermissive((z + offset) + depth_ * Quality_Medium); | |
52 writer.SetTopNodePermissive((z - offset) + depth_ * Quality_Medium); | |
53 } | |
54 | |
55 writer.SetTopNodePermissive(z + depth_ * Quality_Full); | |
56 } | |
57 | |
58 bool LookupSlice(unsigned int& z, | |
59 Quality& quality) | |
60 { | |
61 unsigned int value; | |
62 if (stack_.Pop(value)) | |
63 { | |
64 z = value % depth_; | |
65 | |
66 switch (value / depth_) | |
67 { | |
68 case 0: | |
69 quality = Quality_Low; | |
70 break; | |
71 | |
72 case 1: | |
73 quality = Quality_Medium; | |
74 break; | |
75 | |
76 case 2: | |
77 quality = Quality_Full; | |
78 break; | |
79 | |
80 default: | |
81 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
82 } | |
83 | |
84 return true; | |
85 } | |
86 else | |
87 { | |
88 return false; | |
89 } | |
90 } | |
91 }; | |
92 | |
93 | |
94 bool VolumeImageProgressivePolicy::IsComplete() | |
95 { | |
96 boost::mutex::scoped_lock lock(qualityMutex_); | |
97 | |
98 for (size_t i = 0; i < axialSlicesQuality_.size(); i++) | |
99 { | |
100 if (axialSlicesQuality_[i] != Quality_Full) | |
101 { | |
102 return false; | |
103 } | |
104 } | |
105 | |
106 return true; | |
107 } | |
108 | |
109 | |
110 void VolumeImageProgressivePolicy::InitializeInternal(ImageBuffer3D& buffer, | |
111 ISeriesLoader& loader) | |
112 { | |
113 const size_t depth = loader.GetGeometry().GetSliceCount(); | |
114 | |
115 isJpegAvailable_ = loader.IsJpegAvailable(); | |
116 | |
117 axialSlicesQuality_.clear(); | |
118 axialSlicesQuality_.resize(depth, Quality_None); | |
119 scheduler_.reset(new AxialSlicesScheduler(depth)); | |
120 } | |
121 | |
122 | |
123 bool VolumeImageProgressivePolicy::DownloadStepInternal(bool& complete, | |
124 ImageBuffer3D& buffer, | |
125 ISeriesLoader& loader) | |
126 { | |
127 unsigned int z; | |
128 Quality quality; | |
129 | |
130 if (!scheduler_->LookupSlice(z, quality)) | |
131 { | |
132 // There is no more frame to be downloaded. Before stopping, | |
133 // each loader thread checks whether all the frames have been | |
134 // downloaded at maximum quality. | |
135 complete = IsComplete(); | |
136 return true; | |
137 } | |
138 | |
139 if (quality != Quality_Full && | |
140 !isJpegAvailable_) | |
141 { | |
142 // Cannot fulfill this command, as progressive JPEG download | |
143 // is unavailable (i.e. the Web viewer plugin is unavailable) | |
144 return false; | |
145 } | |
146 | |
147 std::auto_ptr<Orthanc::ImageAccessor> frame; | |
148 | |
149 try | |
150 { | |
151 switch (quality) | |
152 { | |
153 case Quality_Low: | |
154 frame.reset(loader.DownloadJpegFrame(z, 10)); | |
155 break; | |
156 | |
157 case Quality_Medium: | |
158 frame.reset(loader.DownloadJpegFrame(z, 90)); | |
159 break; | |
160 | |
161 case Quality_Full: | |
162 frame.reset(loader.DownloadFrame(z)); | |
163 break; | |
164 | |
165 default: | |
166 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
167 } | |
168 } | |
169 catch (Orthanc::OrthancException&) | |
170 { | |
171 // The Orthanc server cannot decode this instance | |
172 return false; | |
173 } | |
174 | |
175 if (frame.get() != NULL) | |
176 { | |
177 boost::mutex::scoped_lock lock(qualityMutex_); | |
178 | |
179 if (axialSlicesQuality_[z] == Quality_None || | |
180 axialSlicesQuality_[z] < quality) | |
181 { | |
182 axialSlicesQuality_[z] = quality; | |
183 | |
184 ImageBuffer3D::SliceWriter writer(buffer, VolumeProjection_Axial, z); | |
185 Orthanc::ImageProcessing::Convert(writer.GetAccessor(), *frame); | |
186 } | |
187 } | |
188 | |
189 return false; | |
190 } | |
191 | |
192 | |
193 bool VolumeImageProgressivePolicy::IsFullQualityAxial(size_t slice) | |
194 { | |
195 scheduler_->TagFullPriority(slice, 3); | |
196 | |
197 { | |
198 boost::mutex::scoped_lock lock(qualityMutex_); | |
199 return (axialSlicesQuality_[slice] == Quality_Full); | |
200 } | |
201 } | |
202 } |