comparison Samples/Sdl/SimpleViewer/SdlSimpleViewerApplication.h @ 1358:4287eaabe490 broker

Sdl simple viewer application
author Benjamin Golinvaux <bgo@osimis.io>
date Wed, 15 Apr 2020 15:23:30 +0200
parents
children
comparison
equal deleted inserted replaced
1357:0dc5b8a4b3a0 1358:4287eaabe490
1 #pragma once
2
3 #include <Framework/Viewport/IViewport.h>
4 #include <Framework/Loaders/DicomResourcesLoader.h>
5 #include <Framework/Loaders/ILoadersContext.h>
6 #include <Framework/Loaders/SeriesFramesLoader.h>
7 #include <Framework/Loaders/SeriesThumbnailsLoader.h>
8
9 #include <boost/make_shared.hpp>
10
11
12 using OrthancStone::ILoadersContext;
13 using OrthancStone::ObserverBase;
14 using OrthancStone::IViewport;
15 using OrthancStone::DicomResourcesLoader;
16 using OrthancStone::SeriesFramesLoader;
17 using OrthancStone::TextureBaseSceneLayer;
18 using OrthancStone::DicomSource;
19 using OrthancStone::SeriesThumbnailsLoader;
20 using OrthancStone::LoadedDicomResources;
21 using OrthancStone::SeriesThumbnailType;
22 using OrthancStone::OracleScheduler;
23 using OrthancStone::OrthancRestApiCommand;
24 using OrthancStone::OracleScheduler;
25 using OrthancStone::OracleScheduler;
26 using OrthancStone::OracleScheduler;
27
28
29 class SdlSimpleViewerApplication : public ObserverBase<SdlSimpleViewerApplication>
30 {
31
32 public:
33 static boost::shared_ptr<SdlSimpleViewerApplication> Create(ILoadersContext& context, boost::shared_ptr<IViewport> viewport)
34 {
35 boost::shared_ptr<SdlSimpleViewerApplication> application(new SdlSimpleViewerApplication(context, viewport));
36
37 {
38 std::auto_ptr<ILoadersContext::ILock> lock(context.Lock());
39 DicomResourcesLoader::Factory f;
40 application->dicomLoader_ = boost::dynamic_pointer_cast<DicomResourcesLoader>(f.Create(*lock));
41 }
42
43 application->Register<DicomResourcesLoader::SuccessMessage>(*application->dicomLoader_, &SdlSimpleViewerApplication::Handle);
44
45 return application;
46 }
47
48 void LoadOrthancFrame(const DicomSource& source, const std::string& instanceId, unsigned int frame)
49 {
50 std::auto_ptr<ILoadersContext::ILock> lock(context_.Lock());
51
52 dicomLoader_->ScheduleLoadOrthancResource(boost::make_shared<LoadedDicomResources>(Orthanc::DICOM_TAG_SOP_INSTANCE_UID),
53 0, source, Orthanc::ResourceType_Instance, instanceId,
54 new Orthanc::SingleValueObject<unsigned int>(frame));
55 }
56
57 #if 0
58 void LoadDicomWebFrame(const DicomSource& source,
59 const std::string& studyInstanceUid,
60 const std::string& seriesInstanceUid,
61 const std::string& sopInstanceUid,
62 unsigned int frame)
63 {
64 std::auto_ptr<ILoadersContext::ILock> lock(context_.Lock());
65
66 // We first must load the "/metadata" to know the number of frames
67 dicomLoader_->ScheduleGetDicomWeb(
68 boost::make_shared<LoadedDicomResources>(Orthanc::DICOM_TAG_SOP_INSTANCE_UID), 0, source,
69 "/studies/" + studyInstanceUid + "/series/" + seriesInstanceUid + "/instances/" + sopInstanceUid + "/metadata",
70 new Orthanc::SingleValueObject<unsigned int>(frame));
71 }
72 #endif
73
74 void FitContent()
75 {
76 std::auto_ptr<IViewport::ILock> lock(viewport_->Lock());
77 lock->GetCompositor().FitContent(lock->GetController().GetScene());
78 lock->Invalidate();
79 }
80
81 private:
82 ILoadersContext& context_;
83 boost::shared_ptr<IViewport> viewport_;
84 boost::shared_ptr<DicomResourcesLoader> dicomLoader_;
85 boost::shared_ptr<SeriesFramesLoader> framesLoader_;
86
87 SdlSimpleViewerApplication(ILoadersContext& context,
88 boost::shared_ptr<IViewport> viewport) :
89 context_(context),
90 viewport_(viewport)
91 {
92 }
93
94 void Handle(const SeriesFramesLoader::FrameLoadedMessage& message)
95 {
96 LOG(INFO) << "Frame decoded! "
97 << message.GetImage().GetWidth() << "x" << message.GetImage().GetHeight()
98 << " " << Orthanc::EnumerationToString(message.GetImage().GetFormat());
99
100 std::auto_ptr<TextureBaseSceneLayer> layer(
101 message.GetInstanceParameters().CreateTexture(message.GetImage()));
102 layer->SetLinearInterpolation(true);
103
104 {
105 std::auto_ptr<IViewport::ILock> lock(viewport_->Lock());
106 lock->GetController().GetScene().SetLayer(0, layer.release());
107 lock->GetCompositor().FitContent(lock->GetController().GetScene());
108 lock->Invalidate();
109 }
110 }
111
112 void Handle(const DicomResourcesLoader::SuccessMessage& message)
113 {
114 if (message.GetResources()->GetSize() != 1)
115 {
116 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
117 }
118
119 //message.GetResources()->GetResource(0).Print(stdout);
120
121 {
122 std::auto_ptr<ILoadersContext::ILock> lock(context_.Lock());
123 SeriesFramesLoader::Factory f(*message.GetResources());
124
125 framesLoader_ = boost::dynamic_pointer_cast<SeriesFramesLoader>(
126 f.Create(*lock));
127
128 Register<SeriesFramesLoader::FrameLoadedMessage>(
129 *framesLoader_, &SdlSimpleViewerApplication::Handle);
130
131 assert(message.HasUserPayload());
132
133 const Orthanc::SingleValueObject<unsigned int>& payload =
134 dynamic_cast<const Orthanc::SingleValueObject<unsigned int>&>(
135 message.GetUserPayload());
136
137 LOG(INFO) << "Loading pixel data of frame: " << payload.GetValue();
138 framesLoader_->ScheduleLoadFrame(
139 0, message.GetDicomSource(), payload.GetValue(),
140 message.GetDicomSource().GetQualityCount() - 1 /* download best quality available */,
141 NULL);
142 }
143 }
144
145 };
146