rfb/
encodings.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4//
5// Copyright 2022 Oxide Computer Company
6
7use crate::{
8    pixel_formats::rgb_888,
9    rfb::{PixelFormat, Position, Resolution},
10};
11
12use EncodingType::*;
13
14#[derive(Debug)]
15#[allow(unused)]
16pub enum EncodingType {
17    Raw,
18    CopyRect,
19    RRE,
20    Hextile,
21    TRLE,
22    ZRLE,
23    CursorPseudo,
24    DesktopSizePseudo,
25    JRLE,
26    ZRLE2,
27    JPEG,
28    Zlib,
29    CursorWithAlpha,
30    Other(i32),
31}
32
33pub trait Encoding
34where
35    Self: Send,
36{
37    fn get_type(&self) -> EncodingType;
38
39    /// Transform this encoding from its representation into a byte vector that can be passed to the client.
40    fn encode(&self) -> &Vec<u8>;
41
42    /// Translates this encoding type from an input pixel format to an output format.
43    fn transform(&self, input: &PixelFormat, output: &PixelFormat) -> Box<dyn Encoding>;
44}
45
46impl From<EncodingType> for i32 {
47    fn from(e: EncodingType) -> Self {
48        match e {
49            Raw => 0,
50            CopyRect => 1,
51            RRE => 2,
52            Hextile => 5,
53            TRLE => 15,
54            ZRLE => 16,
55            CursorPseudo => -239,
56            DesktopSizePseudo => -223,
57            JRLE => 22,
58            ZRLE2 => 24,
59            JPEG => 21,
60            Zlib => 6,
61            CursorWithAlpha => -314,
62            Other(n) => n,
63        }
64    }
65}
66
67impl From<i32> for EncodingType {
68    fn from(value: i32) -> Self {
69        match value {
70            0 => Raw,
71            1 => CopyRect,
72            2 => RRE,
73            5 => Hextile,
74            15 => TRLE,
75            16 => ZRLE,
76            -239 => CursorPseudo,
77            -223 => DesktopSizePseudo,
78            22 => JRLE,
79            24 => ZRLE2,
80            21 => JPEG,
81            6 => Zlib,
82            -314 => CursorWithAlpha,
83            v => EncodingType::Other(v),
84        }
85    }
86}
87
88/// Section 7.7.1
89pub struct RawEncoding {
90    pixels: Vec<u8>,
91}
92
93impl RawEncoding {
94    pub fn new(pixels: Vec<u8>) -> Self {
95        Self { pixels }
96    }
97}
98
99impl Encoding for RawEncoding {
100    fn get_type(&self) -> EncodingType {
101        EncodingType::Raw
102    }
103
104    fn encode(&self) -> &Vec<u8> {
105        &self.pixels
106    }
107
108    fn transform(&self, input: &PixelFormat, output: &PixelFormat) -> Box<dyn Encoding> {
109        // XXX: This assumes the pixel formats are both rgb888. The server code verifies this
110        // before calling.
111        assert!(input.is_rgb_888());
112        assert!(output.is_rgb_888());
113
114        Box::new(Self {
115            pixels: rgb_888::transform(&self.pixels, &input, &output),
116        })
117    }
118}
119
120#[allow(dead_code)]
121struct RREncoding {
122    background_pixel: Pixel,
123    sub_rectangles: Vec<RRESubrectangle>,
124}
125
126#[allow(dead_code)]
127struct Pixel {
128    bytes: Vec<u8>,
129}
130
131#[allow(dead_code)]
132struct RRESubrectangle {
133    pixel: Pixel,
134    position: Position,
135    dimensions: Resolution,
136}
137
138#[allow(dead_code)]
139struct HextileEncoding {
140    tiles: Vec<Vec<HextileTile>>,
141}
142
143#[allow(dead_code)]
144enum HextileTile {
145    Raw(Vec<u8>),
146    Encoded(HextileTileEncoded),
147}
148
149#[allow(dead_code)]
150struct HextileTileEncoded {
151    background: Option<Pixel>,
152    foreground: Option<Pixel>,
153    // TODO: finish this
154}