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