comparison Applications/Samples/WebAssembly/SingleFrameViewer/SingleFrameViewer.cpp @ 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/WebAssembly/SingleFrameViewer/SingleFrameViewer.cpp@244ad1e4e76a
children 4cfdaf4ef3fe
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 #include "SingleFrameViewerApplication.h"
23
24 #include "../../../Sources/Loaders/WebAssemblyLoadersContext.h"
25 #include "../../../Sources/StoneException.h"
26 #include "../../../Sources/StoneInitialization.h"
27
28 #include <Compatibility.h> // For std::unique_ptr<>
29 #include <Toolbox.h>
30
31 #include <emscripten.h>
32 #include <emscripten/html5.h>
33
34
35 #define DISPATCH_JAVASCRIPT_EVENT(name) \
36 EM_ASM( \
37 const customEvent = document.createEvent("CustomEvent"); \
38 customEvent.initCustomEvent(name, false, false, undefined); \
39 window.dispatchEvent(customEvent); \
40 );
41
42 #define EXTERN_CATCH_EXCEPTIONS \
43 catch (Orthanc::OrthancException& e) \
44 { \
45 LOG(ERROR) << "OrthancException: " << e.What(); \
46 DISPATCH_JAVASCRIPT_EVENT("StoneException"); \
47 } \
48 catch (OrthancStone::StoneException& e) \
49 { \
50 LOG(ERROR) << "StoneException: " << e.What(); \
51 DISPATCH_JAVASCRIPT_EVENT("StoneException"); \
52 } \
53 catch (std::exception& e) \
54 { \
55 LOG(ERROR) << "Runtime error: " << e.what(); \
56 DISPATCH_JAVASCRIPT_EVENT("StoneException"); \
57 } \
58 catch (...) \
59 { \
60 LOG(ERROR) << "Native exception"; \
61 DISPATCH_JAVASCRIPT_EVENT("StoneException"); \
62 }
63
64
65
66 namespace OrthancStone
67 {
68 }
69
70 static std::unique_ptr<OrthancStone::WebAssemblyLoadersContext> context_;
71 static boost::shared_ptr<OrthancStone::Application> application_;
72
73 extern "C"
74 {
75 int main(int argc, char const *argv[])
76 {
77 try
78 {
79 Orthanc::Logging::Initialize();
80 Orthanc::Logging::EnableInfoLevel(true);
81 //Orthanc::Logging::EnableTraceLevel(true);
82 LOG(WARNING) << "Initializing native Stone";
83
84 LOG(WARNING) << "Compiled with Emscripten " << __EMSCRIPTEN_major__
85 << "." << __EMSCRIPTEN_minor__
86 << "." << __EMSCRIPTEN_tiny__;
87
88 LOG(INFO) << "Endianness: " << Orthanc::EnumerationToString(Orthanc::Toolbox::DetectEndianness());
89 context_.reset(new OrthancStone::WebAssemblyLoadersContext(1, 4, 1));
90 context_->SetLocalOrthanc("..");
91 context_->SetDicomCacheSize(128 * 1024 * 1024); // 128MB
92
93 DISPATCH_JAVASCRIPT_EVENT("WasmModuleInitialized");
94 }
95 EXTERN_CATCH_EXCEPTIONS;
96
97 return 0;
98 }
99
100 EMSCRIPTEN_KEEPALIVE
101 void InitializeViewport(const char* canvasId)
102 {
103 try
104 {
105 if (context_.get() == NULL)
106 {
107 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls,
108 "The loaders context is not available yet");
109 }
110
111 if (application_.get() != NULL)
112 {
113 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls,
114 "Only one single viewport is available for this application");
115 }
116
117 boost::shared_ptr<OrthancStone::WebGLViewport> viewport(OrthancStone::GetWebGLViewportsRegistry().Add(canvasId));
118 application_ = OrthancStone::Application::Create(*context_, viewport);
119
120 {
121 OrthancStone::WebGLViewportsRegistry::Accessor accessor(
122 OrthancStone::GetWebGLViewportsRegistry(), canvasId);
123
124 if (accessor.IsValid())
125 {
126 accessor.GetViewport().Invalidate();
127 }
128 }
129 }
130 EXTERN_CATCH_EXCEPTIONS;
131 }
132
133
134 EMSCRIPTEN_KEEPALIVE
135 void LoadFromOrthanc(const char* instance,
136 int frame)
137 {
138 try
139 {
140 if (application_.get() != NULL)
141 {
142 OrthancStone::DicomSource source;
143 application_->LoadOrthancFrame(source, instance, frame);
144 }
145 }
146 EXTERN_CATCH_EXCEPTIONS;
147 }
148
149
150 EMSCRIPTEN_KEEPALIVE
151 void LoadFromDicomWeb(const char* server,
152 const char* studyInstanceUid,
153 const char* seriesInstanceUid,
154 const char* sopInstanceUid,
155 int frame)
156 {
157 try
158 {
159 if (application_.get() != NULL)
160 {
161 OrthancStone::DicomSource source;
162 source.SetDicomWebThroughOrthancSource(server);
163 application_->LoadDicomWebFrame(source, studyInstanceUid, seriesInstanceUid,
164 sopInstanceUid, frame);
165 }
166 }
167 EXTERN_CATCH_EXCEPTIONS;
168 }
169 }