Mercurial > hg > orthanc-stone
comparison OrthancStone/Sources/Platforms/WebAssembly/WebAssemblyCairoViewport.cpp @ 1899:917500c46fe0
moved the Platform folder from the Applications folder to the Stone library itself
author | Alain Mazy <am@osimis.io> |
---|---|
date | Sat, 29 Jan 2022 12:47:32 +0100 |
parents | |
children | 184b0aeae1af |
comparison
equal
deleted
inserted
replaced
1898:a5e54bd87b25 | 1899:917500c46fe0 |
---|---|
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-2022 Osimis S.A., Belgium | |
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium | |
7 * | |
8 * This program is free software: you can redistribute it and/or | |
9 * modify it under the terms of the GNU Lesser General Public License | |
10 * as published by the Free Software Foundation, either version 3 of | |
11 * the License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * Lesser General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU Lesser General Public | |
19 * License along with this program. If not, see | |
20 * <http://www.gnu.org/licenses/>. | |
21 **/ | |
22 | |
23 | |
24 #if defined(ORTHANC_BUILDING_STONE_LIBRARY) && ORTHANC_BUILDING_STONE_LIBRARY == 1 | |
25 # include "WebAssemblyCairoViewport.h" | |
26 # include "../../../OrthancStone/Sources/Scene2D/CairoCompositor.h" | |
27 # include "../../../OrthancStone/Sources/Scene2DViewport/ViewportController.h" | |
28 #else | |
29 // This is the case when using the WebAssembly side module, and this | |
30 // source file must be compiled within the WebAssembly main module | |
31 # include <Viewport/WebAssemblyCairoViewport.h> | |
32 # include <Scene2D/CairoCompositor.h> | |
33 # include <Scene2DViewport/ViewportController.h> | |
34 #endif | |
35 | |
36 | |
37 #include <Images/Image.h> | |
38 | |
39 namespace OrthancStone | |
40 { | |
41 void WebAssemblyCairoViewport::Paint(ICompositor& compositor, | |
42 ViewportController& controller) | |
43 { | |
44 compositor.Refresh(controller.GetScene()); | |
45 | |
46 // Create a temporary memory buffer for the canvas in JavaScript | |
47 Orthanc::ImageAccessor cairo; | |
48 dynamic_cast<CairoCompositor&>(compositor).GetCanvas().GetReadOnlyAccessor(cairo); | |
49 | |
50 const unsigned int width = cairo.GetWidth(); | |
51 const unsigned int height = cairo.GetHeight(); | |
52 | |
53 if (javascript_.get() == NULL || | |
54 javascript_->GetWidth() != width || | |
55 javascript_->GetHeight() != height) | |
56 { | |
57 javascript_.reset(new Orthanc::Image(Orthanc::PixelFormat_RGBA32, width, height, | |
58 true /* force minimal pitch */)); | |
59 } | |
60 | |
61 // Convert from BGRA32 memory layout (only color mode supported | |
62 // by Cairo, which corresponds to CAIRO_FORMAT_ARGB32) to RGBA32 | |
63 // (as expected by HTML5 canvas). This simply amounts to | |
64 // swapping the B and R channels. Alpha channel is also set to | |
65 // full opacity (255). | |
66 uint8_t* q = reinterpret_cast<uint8_t*>(javascript_->GetBuffer()); | |
67 for (unsigned int y = 0; y < height; y++) | |
68 { | |
69 const uint8_t* p = reinterpret_cast<const uint8_t*>(cairo.GetConstRow(y)); | |
70 for (unsigned int x = 0; x < width; x++) | |
71 { | |
72 q[0] = p[2]; // R | |
73 q[1] = p[1]; // G | |
74 q[2] = p[0]; // B | |
75 q[3] = 255; // A | |
76 | |
77 p += 4; | |
78 q += 4; | |
79 } | |
80 } | |
81 | |
82 if (width != 0 && | |
83 height != 0) | |
84 { | |
85 // Execute JavaScript commands to blit the image buffer onto the | |
86 // 2D drawing context of the HTML5 canvas | |
87 EM_ASM({ | |
88 const data = new Uint8ClampedArray(Module.HEAP8.buffer, $1, 4 * $2 * $3); | |
89 const img = new ImageData(data, $2, $3); | |
90 const ctx = document.getElementById(UTF8ToString($0)).getContext('2d'); | |
91 ctx.putImageData(img, 0, 0); | |
92 }, | |
93 GetCanvasId().c_str(), // $0 | |
94 javascript_->GetBuffer(), // $1 | |
95 javascript_->GetWidth(), // $2 | |
96 javascript_->GetHeight()); // $3 | |
97 } | |
98 } | |
99 | |
100 | |
101 WebAssemblyCairoViewport::WebAssemblyCairoViewport(const std::string& canvasId, | |
102 bool enableEmscriptenMouseEvents) : | |
103 WebAssemblyViewport(canvasId,enableEmscriptenMouseEvents) | |
104 { | |
105 RefreshCanvasSize(); | |
106 AcquireCompositor(new CairoCompositor(GetCanvasWidth(), GetCanvasHeight())); | |
107 } | |
108 | |
109 | |
110 boost::shared_ptr<WebAssemblyCairoViewport> WebAssemblyCairoViewport::Create( | |
111 const std::string& canvasId, bool enableEmscriptenMouseEvents) | |
112 { | |
113 boost::shared_ptr<WebAssemblyCairoViewport> that = boost::shared_ptr<WebAssemblyCairoViewport>( | |
114 new WebAssemblyCairoViewport(canvasId, enableEmscriptenMouseEvents)); | |
115 | |
116 that->WebAssemblyViewport::PostConstructor(); | |
117 return that; | |
118 } | |
119 } |