comparison Applications/Samples/Sdl/SingleFrameViewer/SdlSimpleViewerApplication.h @ 1538:d1806b4e4839

moving OrthancStone/Samples/ as Applications/Samples/
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 11 Aug 2020 13:24:38 +0200
parents OrthancStone/Samples/Sdl/SingleFrameViewer/SdlSimpleViewerApplication.h@244ad1e4e76a
children 6e0da8370270
comparison
equal deleted inserted replaced
1537:de8cf5859e84 1538:d1806b4e4839
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
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.
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
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
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #pragma once
23
24 #include "../../../Sources/Viewport/IViewport.h"
25 #include "../../../Sources/Loaders/DicomResourcesLoader.h"
26 #include "../../../Sources/Loaders/ILoadersContext.h"
27 #include "../../../Sources/Loaders/SeriesFramesLoader.h"
28 #include "../../../Sources/Loaders/SeriesThumbnailsLoader.h"
29
30 #include <Compatibility.h> // For std::unique_ptr<>
31
32 #include <boost/make_shared.hpp>
33
34
35 using OrthancStone::ILoadersContext;
36 using OrthancStone::ObserverBase;
37 using OrthancStone::IViewport;
38 using OrthancStone::DicomResourcesLoader;
39 using OrthancStone::SeriesFramesLoader;
40 using OrthancStone::TextureBaseSceneLayer;
41 using OrthancStone::DicomSource;
42 using OrthancStone::SeriesThumbnailsLoader;
43 using OrthancStone::LoadedDicomResources;
44 using OrthancStone::SeriesThumbnailType;
45 using OrthancStone::OracleScheduler;
46 using OrthancStone::OrthancRestApiCommand;
47 using OrthancStone::OracleScheduler;
48 using OrthancStone::OracleScheduler;
49 using OrthancStone::OracleScheduler;
50
51
52 class SdlSimpleViewerApplication : public ObserverBase<SdlSimpleViewerApplication>
53 {
54
55 public:
56 static boost::shared_ptr<SdlSimpleViewerApplication> Create(ILoadersContext& context, boost::shared_ptr<IViewport> viewport)
57 {
58 boost::shared_ptr<SdlSimpleViewerApplication> application(new SdlSimpleViewerApplication(context, viewport));
59
60 {
61 std::unique_ptr<ILoadersContext::ILock> lock(context.Lock());
62 application->dicomLoader_ = DicomResourcesLoader::Create(*lock);
63 }
64
65 application->Register<DicomResourcesLoader::SuccessMessage>(*application->dicomLoader_, &SdlSimpleViewerApplication::Handle);
66
67 return application;
68 }
69
70 void LoadOrthancFrame(const DicomSource& source, const std::string& instanceId, unsigned int frame)
71 {
72 std::unique_ptr<ILoadersContext::ILock> lock(context_.Lock());
73
74 dicomLoader_->ScheduleLoadOrthancResource(boost::make_shared<LoadedDicomResources>(Orthanc::DICOM_TAG_SOP_INSTANCE_UID),
75 0, source, Orthanc::ResourceType_Instance, instanceId,
76 new Orthanc::SingleValueObject<unsigned int>(frame));
77 }
78
79 #if 0
80 void LoadDicomWebFrame(const DicomSource& source,
81 const std::string& studyInstanceUid,
82 const std::string& seriesInstanceUid,
83 const std::string& sopInstanceUid,
84 unsigned int frame)
85 {
86 std::unique_ptr<ILoadersContext::ILock> lock(context_.Lock());
87
88 // We first must load the "/metadata" to know the number of frames
89 dicomLoader_->ScheduleGetDicomWeb(
90 boost::make_shared<LoadedDicomResources>(Orthanc::DICOM_TAG_SOP_INSTANCE_UID), 0, source,
91 "/studies/" + studyInstanceUid + "/series/" + seriesInstanceUid + "/instances/" + sopInstanceUid + "/metadata",
92 new Orthanc::SingleValueObject<unsigned int>(frame));
93 }
94 #endif
95
96 void FitContent()
97 {
98 std::unique_ptr<IViewport::ILock> lock(viewport_->Lock());
99 lock->GetCompositor().FitContent(lock->GetController().GetScene());
100 lock->Invalidate();
101 }
102
103 private:
104 ILoadersContext& context_;
105 boost::shared_ptr<IViewport> viewport_;
106 boost::shared_ptr<DicomResourcesLoader> dicomLoader_;
107 boost::shared_ptr<SeriesFramesLoader> framesLoader_;
108
109 SdlSimpleViewerApplication(ILoadersContext& context,
110 boost::shared_ptr<IViewport> viewport) :
111 context_(context),
112 viewport_(viewport)
113 {
114 }
115
116 void Handle(const SeriesFramesLoader::FrameLoadedMessage& message)
117 {
118 LOG(INFO) << "Frame decoded! "
119 << message.GetImage().GetWidth() << "x" << message.GetImage().GetHeight()
120 << " " << Orthanc::EnumerationToString(message.GetImage().GetFormat());
121
122 std::unique_ptr<TextureBaseSceneLayer> layer(
123 message.GetInstanceParameters().CreateTexture(message.GetImage()));
124 layer->SetLinearInterpolation(true);
125
126 {
127 std::unique_ptr<IViewport::ILock> lock(viewport_->Lock());
128 lock->GetController().GetScene().SetLayer(0, layer.release());
129 lock->GetCompositor().FitContent(lock->GetController().GetScene());
130 lock->Invalidate();
131 }
132 }
133
134 void Handle(const DicomResourcesLoader::SuccessMessage& message)
135 {
136 if (message.GetResources()->GetSize() != 1)
137 {
138 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
139 }
140
141 //message.GetResources()->GetResource(0).Print(stdout);
142
143 {
144 std::unique_ptr<ILoadersContext::ILock> lock(context_.Lock());
145 SeriesFramesLoader::Factory f(*message.GetResources());
146
147 framesLoader_ = boost::dynamic_pointer_cast<SeriesFramesLoader>(
148 f.Create(*lock));
149
150 Register<SeriesFramesLoader::FrameLoadedMessage>(
151 *framesLoader_, &SdlSimpleViewerApplication::Handle);
152
153 assert(message.HasUserPayload());
154
155 const Orthanc::SingleValueObject<unsigned int>& payload =
156 dynamic_cast<const Orthanc::SingleValueObject<unsigned int>&>(
157 message.GetUserPayload());
158
159 LOG(INFO) << "Loading pixel data of frame: " << payload.GetValue();
160 framesLoader_->ScheduleLoadFrame(
161 0, message.GetDicomSource(), payload.GetValue(),
162 message.GetDicomSource().GetQualityCount() - 1 /* download best quality available */,
163 NULL);
164 }
165 }
166
167 };
168