Skip to content

Commit 318268c

Browse files
authored
Merge pull request #7 from jspanchu/image-display-lossless
feat(RawImageDisplayArea): support rgb24 and rgba32 images
2 parents 3323724 + b77143a commit 318268c

File tree

7 files changed

+97
-1
lines changed

7 files changed

+97
-1
lines changed

trame_rca/widgets/rca.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,15 @@ def __init__(self, **kwargs):
8484
"name",
8585
"origin",
8686
]
87+
88+
89+
class RawImageDisplayArea(HtmlElement):
90+
def __init__(self, **kwargs):
91+
super().__init__(
92+
"raw-image-display-area",
93+
**kwargs,
94+
)
95+
self._attr_names += [
96+
"name",
97+
"origin",
98+
]

vue-components/src/components/DisplayArea/script.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import ImageDisplayArea from '../ImageDisplayArea';
22
import MediaSourceDisplayArea from '../MediaSourceDisplayArea';
33
import VideoDecoderDisplayArea from '../VideoDecoderDisplayArea';
4+
import RawImageDisplayArea from '../RawImageDisplayArea';
45

56
export default {
67
name: 'DisplayArea',
78
components: {
89
ImageDisplayArea,
910
MediaSourceDisplayArea,
1011
VideoDecoderDisplayArea,
12+
RawImageDisplayArea,
1113
},
1214
props: {
1315
name: {

vue-components/src/components/DisplayArea/template.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
<image-display-area v-if="display === 'image'" :name="name" :origin="origin" :poolSize="4" />
33
<media-source-display-area v-if="display === 'media-source'" :name="name" :origin="origin" />
44
<video-decoder-display-area v-if="display === 'video-decoder'" :name="name" :origin="origin" />
5+
<raw-image-display-area v-if="display === 'raw-image'" :name="name" :origin="origin" />
56
</div>
6-
`
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<template src="./template.html" />
2+
<script src="./script.js" />
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
export default {
2+
name: 'RawImageDisplayArea',
3+
props: {
4+
name: {
5+
type: String,
6+
default: 'default',
7+
},
8+
origin: {
9+
type: String,
10+
default: 'anonymous',
11+
},
12+
},
13+
data() {
14+
return {
15+
hasContent: false,
16+
};
17+
},
18+
mounted() {
19+
this.wslinkSubscription = null;
20+
const canvas = this.$el;
21+
const ctx = canvas.getContext('2d');
22+
this.onImage = ([{ name, meta, content }]) => {
23+
if (this.name === name) {
24+
if (meta.type.includes('image/rgb24')) {
25+
content.arrayBuffer().then((buffer) => {
26+
const data = new Uint8Array(buffer);
27+
canvas.width = meta.w;
28+
canvas.height = meta.h;
29+
const imageData = ctx.createImageData(meta.w, meta.h);
30+
const pixels = imageData.data;
31+
let iRGB = 0;
32+
let iRGBA = 0;
33+
while (iRGBA < pixels.length) {
34+
pixels[iRGBA++] = data[iRGB++];
35+
pixels[iRGBA++] = data[iRGB++];
36+
pixels[iRGBA++] = data[iRGB++];
37+
pixels[iRGBA++] = 255;
38+
}
39+
ctx.putImageData(imageData, 0, 0);
40+
this.hasContent = true;
41+
});
42+
} else if (meta.type.includes('image/rgba32')) {
43+
content.arrayBuffer().then((buffer) => {
44+
const data = new Uint8ClampedArray(buffer);
45+
canvas.width = meta.w;
46+
canvas.height = meta.h;
47+
const imageData = new ImageData(data, meta.w, meta.h);
48+
ctx.putImageData(imageData, 0, 0);
49+
this.hasContent = true;
50+
});
51+
} else {
52+
this.hasContent = false;
53+
}
54+
}
55+
};
56+
if (this.trame) {
57+
this.wslinkSubscription = this.trame.client
58+
.getConnection()
59+
.getSession()
60+
.subscribe('trame.rca.topic.stream', this.onImage);
61+
}
62+
},
63+
beforeUnmount() {
64+
// unsub trame.rca.topic.stream
65+
if (this.wslinkSubscription) {
66+
if (this.trame) {
67+
this.trame.client
68+
.getConnection()
69+
.getSession()
70+
.unsubscribe(this.wslinkSubscription);
71+
this.wslinkSubscription = null;
72+
}
73+
}
74+
},
75+
inject: ['trame'],
76+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<canvas class="js-canvas" v-show="hasContent">
2+
</canvas>

vue-components/src/components/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import ImageDisplayArea from './ImageDisplayArea';
44
import RemoteControlledArea from './RemoteControlledArea';
55
import MediaSourceDisplayArea from './MediaSourceDisplayArea';
66
import VideoDecoderDisplayArea from './VideoDecoderDisplayArea';
7+
import RawImageDisplayArea from './RawImageDisplayArea';
78

89
export default {
910
DisplayArea,
@@ -12,4 +13,5 @@ export default {
1213
RemoteControlledArea,
1314
MediaSourceDisplayArea,
1415
VideoDecoderDisplayArea,
16+
RawImageDisplayArea,
1517
};

0 commit comments

Comments
 (0)