summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/wasm/qwasmdom.cpp
diff options
context:
space:
mode:
authorLorn Potter <[email protected]>2023-10-05 13:37:38 +1000
committerLorn Potter <[email protected]>2023-12-21 11:04:12 +1000
commit8fe920ccb03a9264f936cfbc9c6d61687f6bb4d6 (patch)
tree1e492a5f82a7f1c8b84581b61e72cc1930c59a38 /src/plugins/platforms/wasm/qwasmdom.cpp
parent66c2dfbebab4ec632b723779a9a41896d35e84d0 (diff)
wasm: move image to web conversion to dom::
This allows other areas to utilize this Change-Id: I4bc7e8374289a19afe8b639b2b3b0dc0f8f65a3a Done-with: [email protected] Reviewed-by: Morten Johan Sørvig <[email protected]>
Diffstat (limited to 'src/plugins/platforms/wasm/qwasmdom.cpp')
-rw-r--r--src/plugins/platforms/wasm/qwasmdom.cpp40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/plugins/platforms/wasm/qwasmdom.cpp b/src/plugins/platforms/wasm/qwasmdom.cpp
index 9aca102b2e8..ebb2dd4807a 100644
--- a/src/plugins/platforms/wasm/qwasmdom.cpp
+++ b/src/plugins/platforms/wasm/qwasmdom.cpp
@@ -36,6 +36,46 @@ QPointF mapPoint(emscripten::val source, emscripten::val target, const QPointF &
return point + offset;
}
+void drawImageToWebImageDataArray(const QImage &sourceImage, emscripten::val destinationImageData,
+ const QRect &sourceRect)
+{
+ Q_ASSERT_X(destinationImageData["constructor"]["name"].as<std::string>() == "ImageData",
+ Q_FUNC_INFO, "The destination should be an ImageData instance");
+
+ constexpr int BytesPerColor = 4;
+ if (sourceRect.width() == sourceImage.width()) {
+ // Copy a contiguous chunk of memory
+ // ...............
+ // OOOOOOOOOOOOOOO
+ // OOOOOOOOOOOOOOO -> image data
+ // OOOOOOOOOOOOOOO
+ // ...............
+ auto imageMemory = emscripten::typed_memory_view(sourceRect.width() * sourceRect.height()
+ * BytesPerColor,
+ sourceImage.constScanLine(sourceRect.y()));
+ destinationImageData["data"].call<void>(
+ "set", imageMemory, sourceRect.y() * sourceImage.width() * BytesPerColor);
+ } else {
+ // Go through the scanlines manually to set the individual lines in bulk. This is
+ // marginally less performant than the above.
+ // ...............
+ // ...OOOOOOOOO... r = 0 -> image data
+ // ...OOOOOOOOO... r = 1 -> image data
+ // ...OOOOOOOOO... r = 2 -> image data
+ // ...............
+ for (int row = 0; row < sourceRect.height(); ++row) {
+ auto scanlineMemory =
+ emscripten::typed_memory_view(sourceRect.width() * BytesPerColor,
+ sourceImage.constScanLine(row + sourceRect.y())
+ + BytesPerColor * sourceRect.x());
+ destinationImageData["data"].call<void>("set", scanlineMemory,
+ (sourceRect.y() + row) * sourceImage.width()
+ * BytesPerColor
+ + sourceRect.x() * BytesPerColor);
+ }
+ }
+}
+
} // namespace dom
QT_END_NAMESPACE