rfb_encodings/
raw.rs

1// Copyright 2025 Dustin McAfee
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! VNC Raw encoding implementation.
16//!
17//! The simplest encoding that sends pixel data directly without compression.
18//! High bandwidth but universally supported.
19
20use crate::Encoding;
21use bytes::{BufMut, BytesMut};
22
23/// Implements the VNC "Raw" encoding, which sends pixel data directly without compression.
24///
25/// This encoding is straightforward but can be very bandwidth-intensive as it transmits
26/// the raw framebuffer data in RGB format (without alpha channel).
27pub struct RawEncoding;
28
29impl Encoding for RawEncoding {
30    fn encode(
31        &self,
32        data: &[u8],
33        _width: u16,
34        _height: u16,
35        _quality: u8,
36        _compression: u8,
37    ) -> BytesMut {
38        // For 32bpp clients: convert RGBA to client pixel format (RGBX where X is padding)
39        // Client format: R at bits 0-7, G at bits 8-15, B at bits 16-23, padding at bits 24-31
40        let mut buf = BytesMut::with_capacity(data.len()); // Same size: 4 bytes per pixel
41        for chunk in data.chunks_exact(4) {
42            buf.put_u8(chunk[0]); // R at byte 0
43            buf.put_u8(chunk[1]); // G at byte 1
44            buf.put_u8(chunk[2]); // B at byte 2
45            buf.put_u8(0); // Padding at byte 3 (not alpha)
46        }
47        buf
48    }
49}