comparison Samples/WebAssembly/SingleFrameViewer/SingleFrameViewer.cpp @ 1377:458ab069f313 broker

Fixed missing file in WASM sample
author Benjamin Golinvaux <bgo@osimis.io>
date Wed, 22 Apr 2020 14:02:23 +0200
parents
children 240531afdd2d
comparison
equal deleted inserted replaced
1376:dc5acb03ad93 1377:458ab069f313
1 #include "SingleFrameViewerApplication.h"
2
3 #include <Framework/Loaders/WebAssemblyLoadersContext.h>
4
5 #include <Framework/StoneException.h>
6 #include <Framework/StoneInitialization.h>
7
8 #include <Core/Toolbox.h>
9
10 #include <emscripten.h>
11 #include <emscripten/html5.h>
12
13
14 #define DISPATCH_JAVASCRIPT_EVENT(name) \
15 EM_ASM( \
16 const customEvent = document.createEvent("CustomEvent"); \
17 customEvent.initCustomEvent(name, false, false, undefined); \
18 window.dispatchEvent(customEvent); \
19 );
20
21 #define EXTERN_CATCH_EXCEPTIONS \
22 catch (Orthanc::OrthancException& e) \
23 { \
24 LOG(ERROR) << "OrthancException: " << e.What(); \
25 DISPATCH_JAVASCRIPT_EVENT("StoneException"); \
26 } \
27 catch (OrthancStone::StoneException& e) \
28 { \
29 LOG(ERROR) << "StoneException: " << e.What(); \
30 DISPATCH_JAVASCRIPT_EVENT("StoneException"); \
31 } \
32 catch (std::exception& e) \
33 { \
34 LOG(ERROR) << "Runtime error: " << e.what(); \
35 DISPATCH_JAVASCRIPT_EVENT("StoneException"); \
36 } \
37 catch (...) \
38 { \
39 LOG(ERROR) << "Native exception"; \
40 DISPATCH_JAVASCRIPT_EVENT("StoneException"); \
41 }
42
43
44
45 namespace OrthancStone
46 {
47 class Observer : public IWebViewerLoadersObserver
48 {
49 public:
50 virtual void SignalSeriesUpdated(LoadedDicomResources& series)
51 {
52 LOG(INFO) << "=====================================";
53 LOG(INFO) << series.GetSize() << " series";
54
55 /*for (size_t i = 0; i < series.GetSize(); i++)
56 {
57 series.GetResource(i).Print(stdout);
58 printf("\n");
59 }*/
60 }
61
62 virtual void SignalThumbnailLoaded(const std::string& studyInstanceUid,
63 const std::string& seriesInstanceUid,
64 SeriesThumbnailType type)
65 {
66 LOG(INFO) << "*** Thumbnail loaded: " << studyInstanceUid << " / "
67 << seriesInstanceUid << " (type " << type << ")";
68 }
69 };
70 }
71
72
73
74 static std::auto_ptr<OrthancStone::WebAssemblyLoadersContext> context_;
75 static boost::shared_ptr<OrthancStone::Application> application_;
76
77
78 extern "C"
79 {
80 int main(int argc, char const *argv[])
81 {
82 try
83 {
84 Orthanc::Logging::Initialize();
85 Orthanc::Logging::EnableInfoLevel(true);
86 //Orthanc::Logging::EnableTraceLevel(true);
87 LOG(WARNING) << "Initializing native Stone";
88
89 LOG(WARNING) << "Compiled with Emscripten " << __EMSCRIPTEN_major__
90 << "." << __EMSCRIPTEN_minor__
91 << "." << __EMSCRIPTEN_tiny__;
92
93 LOG(INFO) << "Endianness: " << Orthanc::EnumerationToString(Orthanc::Toolbox::DetectEndianness());
94 context_.reset(new OrthancStone::WebAssemblyLoadersContext(1, 4, 1));
95 context_->SetLocalOrthanc("..");
96 context_->SetDicomCacheSize(128 * 1024 * 1024); // 128MB
97
98 DISPATCH_JAVASCRIPT_EVENT("StoneInitialized");
99 }
100 EXTERN_CATCH_EXCEPTIONS;
101
102 return 0;
103 }
104
105
106 EMSCRIPTEN_KEEPALIVE
107 void InitializeViewport(const char* canvasId)
108 {
109 try
110 {
111 if (context_.get() == NULL)
112 {
113 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls,
114 "The loaders context is not available yet");
115 }
116
117 if (application_.get() != NULL)
118 {
119 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls,
120 "Only one single viewport is available for this application");
121 }
122
123 {
124 std::auto_ptr<OrthancStone::Observer> observer(new OrthancStone::Observer);
125
126 #if 1
127 OrthancStone::DicomSource source1;
128 //source1.SetDicomWebSource("http://localhost:8042/dicom-web"); source1.SetDicomWebRendered(true);
129 source1.SetDicomWebThroughOrthancSource("self"); source1.SetDicomWebRendered(true);
130 //source1.SetDicomWebThroughOrthancSource("my-google"); source1.SetDicomWebRendered(false);
131 boost::shared_ptr<OrthancStone::WebViewerLoaders> app2(
132 OrthancStone::WebViewerLoaders::Create(*context_, source1, true, observer.release()));
133 #else
134 OrthancStone::DicomSource source1;
135 source1.SetOrthancSource();
136 boost::shared_ptr<OrthancStone::WebViewerLoaders> app2(
137 OrthancStone::WebViewerLoaders::Create(*context_, source1, true, observer.release()));
138 //app2->AddOrthancStudy("27f7126f-4f66fb14-03f4081b-f9341db2-53925988");
139 //app2->AddOrthancSeries("1e2c125c-411b8e86-3f4fe68e-a7584dd3-c6da78f0");
140 #endif
141
142 // BRAINIX
143 //app2->AddDicomAllSeries();
144 //app2->AddDicomStudy("2.16.840.1.113669.632.20.1211.10000357775");
145 app2->AddDicomSeries("2.16.840.1.113669.632.20.1211.10000357775", "1.3.46.670589.11.0.0.11.4.2.0.8743.5.3800.2006120117110979000"); // Standard image: type 5
146
147 app2->AddDicomStudy("1.3.51.0.7.633920140505.6339234439.633987.633918098"); // "Video" type 4: video720p.dcm
148 app2->AddDicomStudy("1.2.276.0.7230010.3.1.2.2344313775.14992.1458058404.7528"); // "PDF" type 3: pdf.dcm
149 app2->AddDicomSeries("1.2.276.0.7230010.3.1.2.296485376.1.1568899779.944131", "1.2.276.0.7230010.3.1.3.296485376.1.1568899781.944588"); // RTSTRUCT, "Unsupported" type 2: DICOM/WebViewer2/TFE/IMAGES/IM452
150
151
152 //app2->AddDicomStudy("1.2.276.0.7230010.3.1.2.380371456.1.1544616291.954997"); // CSPO
153 }
154
155 boost::shared_ptr<OrthancStone::WebGLViewport> viewport(OrthancStone::GetWebGLViewportsRegistry().Add(canvasId));
156 application_ = OrthancStone::Application::Create(*context_, viewport);
157
158 // Paint the viewport to black
159
160 {
161 OrthancStone::WebGLViewportsRegistry::Accessor accessor(
162 OrthancStone::GetWebGLViewportsRegistry(), canvasId);
163
164 if (accessor.IsValid())
165 {
166 accessor.GetViewport().Invalidate();
167 }
168 }
169 }
170 EXTERN_CATCH_EXCEPTIONS;
171 }
172
173
174 EMSCRIPTEN_KEEPALIVE
175 void LoadOrthanc(const char* instance,
176 int frame)
177 {
178 try
179 {
180 if (application_.get() != NULL)
181 {
182 OrthancStone::DicomSource source;
183 application_->LoadOrthancFrame(source, instance, frame);
184 }
185 }
186 EXTERN_CATCH_EXCEPTIONS;
187 }
188
189
190 EMSCRIPTEN_KEEPALIVE
191 void LoadDicomWeb(const char* server,
192 const char* studyInstanceUid,
193 const char* seriesInstanceUid,
194 const char* sopInstanceUid,
195 int frame)
196 {
197 try
198 {
199 if (application_.get() != NULL)
200 {
201 OrthancStone::DicomSource source;
202 source.SetDicomWebThroughOrthancSource(server);
203 application_->LoadDicomWebFrame(source, studyInstanceUid, seriesInstanceUid,
204 sopInstanceUid, frame);
205 }
206 }
207 EXTERN_CATCH_EXCEPTIONS;
208 }
209 }