ironrdp_graphics/
image_processing.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
use core::{cmp, fmt};
use std::io;

use byteorder::WriteBytesExt;
use ironrdp_pdu::geometry::{InclusiveRectangle, Rectangle as _};
use num_derive::ToPrimitive;
use num_traits::ToPrimitive as _;

const MIN_ALPHA: u8 = 0x00;
const MAX_ALPHA: u8 = 0xff;

pub struct ImageRegionMut<'a> {
    pub region: InclusiveRectangle,
    pub step: u16,
    pub pixel_format: PixelFormat,
    pub data: &'a mut [u8],
}

impl fmt::Debug for ImageRegionMut<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ImageRegionMut")
            .field("region", &self.region)
            .field("step", &self.step)
            .field("pixel_format", &self.pixel_format)
            .field("data_len", &self.data.len())
            .finish()
    }
}

pub struct ImageRegion<'a> {
    pub region: InclusiveRectangle,
    pub step: u16,
    pub pixel_format: PixelFormat,
    pub data: &'a [u8],
}

impl fmt::Debug for ImageRegion<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ImageRegion")
            .field("region", &self.region)
            .field("step", &self.step)
            .field("pixel_format", &self.pixel_format)
            .field("data_len", &self.data.len())
            .finish()
    }
}

impl ImageRegion<'_> {
    pub fn copy_to(&self, other: &mut ImageRegionMut<'_>) -> io::Result<()> {
        let width = cmp::min(self.region.width(), other.region.width());
        let height = cmp::min(self.region.height(), other.region.height());
        let width = usize::from(width);
        let height = usize::from(height);

        let dst_point = Point {
            x: usize::from(other.region.left),
            y: usize::from(other.region.top),
        };
        let src_point = Point {
            x: usize::from(self.region.left),
            y: usize::from(self.region.top),
        };

        let src_byte = usize::from(self.pixel_format.bytes_per_pixel());
        let dst_byte = usize::from(other.pixel_format.bytes_per_pixel());

        let src_step = if self.step == 0 {
            usize::from(self.region.width()) * src_byte
        } else {
            usize::from(self.step)
        };
        let dst_step = if other.step == 0 {
            width * dst_byte
        } else {
            usize::from(other.step)
        };

        if self.pixel_format.eq_no_alpha(other.pixel_format) {
            let width = width * dst_byte;
            for y in 0..height {
                let src_start = (y + src_point.y) * src_step + src_point.x * src_byte;
                let dst_start = (y + dst_point.y) * dst_step + dst_point.x * dst_byte;
                other.data[dst_start..dst_start + width].clone_from_slice(&self.data[src_start..src_start + width]);
            }
        } else {
            for y in 0..height {
                let src = &self.data[((y + src_point.y) * src_step)..];
                let dst = &mut other.data[((y + dst_point.y) * dst_step)..];

                for x in 0..width {
                    let color = self.pixel_format.read_color(&src[((x + src_point.x) * src_byte)..])?;
                    other
                        .pixel_format
                        .write_color(color, &mut dst[((x + dst_point.x) * dst_byte)..])?;
                }
            }
        }

        Ok(())
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, ToPrimitive)]
pub enum PixelFormat {
    ARgb32 = 536_971_400,
    XRgb32 = 536_938_632,
    ABgr32 = 537_036_936,
    XBgr32 = 537_004_168,
    BgrA32 = 537_168_008,
    BgrX32 = 537_135_240,
    RgbA32 = 537_102_472,
    RgbX32 = 537_069_704,
}

impl TryFrom<u32> for PixelFormat {
    type Error = ();

    fn try_from(value: u32) -> Result<Self, Self::Error> {
        match value {
            536_971_400 => Ok(PixelFormat::ARgb32),
            536_938_632 => Ok(PixelFormat::XRgb32),
            537_036_936 => Ok(PixelFormat::ABgr32),
            537_004_168 => Ok(PixelFormat::XBgr32),
            537_168_008 => Ok(PixelFormat::BgrA32),
            537_135_240 => Ok(PixelFormat::BgrX32),
            537_102_472 => Ok(PixelFormat::RgbA32),
            537_069_704 => Ok(PixelFormat::RgbX32),
            _ => Err(()),
        }
    }
}

impl PixelFormat {
    pub const fn bytes_per_pixel(self) -> u8 {
        match self {
            Self::ARgb32
            | Self::XRgb32
            | Self::ABgr32
            | Self::XBgr32
            | Self::BgrA32
            | Self::BgrX32
            | Self::RgbA32
            | Self::RgbX32 => 4,
        }
    }

    pub fn eq_no_alpha(self, other: Self) -> bool {
        let mask = !(8 << 12);

        (self.to_u32().unwrap() & mask) == (other.to_u32().unwrap() & mask)
    }

    pub fn read_color(self, buffer: &[u8]) -> io::Result<Rgba> {
        match self {
            Self::ARgb32
            | Self::XRgb32
            | Self::ABgr32
            | Self::XBgr32
            | Self::BgrA32
            | Self::BgrX32
            | Self::RgbA32
            | Self::RgbX32 => {
                if buffer.len() < 4 {
                    Err(io::Error::new(
                        io::ErrorKind::InvalidInput,
                        "input buffer is not large enough (this is a bug)",
                    ))
                } else {
                    let color = &buffer[..4];

                    match self {
                        Self::ARgb32 => Ok(Rgba {
                            a: color[0],
                            r: color[1],
                            g: color[2],
                            b: color[3],
                        }),
                        Self::XRgb32 => Ok(Rgba {
                            a: MAX_ALPHA,
                            r: color[1],
                            g: color[2],
                            b: color[3],
                        }),
                        Self::ABgr32 => Ok(Rgba {
                            a: color[0],
                            b: color[1],
                            g: color[2],
                            r: color[3],
                        }),
                        Self::XBgr32 => Ok(Rgba {
                            a: MAX_ALPHA,
                            b: color[1],
                            g: color[2],
                            r: color[3],
                        }),
                        Self::BgrA32 => Ok(Rgba {
                            b: color[0],
                            g: color[1],
                            r: color[2],
                            a: color[3],
                        }),
                        Self::BgrX32 => Ok(Rgba {
                            b: color[0],
                            g: color[1],
                            r: color[2],
                            a: MAX_ALPHA,
                        }),
                        Self::RgbA32 => Ok(Rgba {
                            r: color[0],
                            g: color[1],
                            b: color[2],
                            a: color[3],
                        }),
                        Self::RgbX32 => Ok(Rgba {
                            r: color[0],
                            g: color[1],
                            b: color[2],
                            a: MAX_ALPHA,
                        }),
                    }
                }
            }
        }
    }

    pub fn write_color(self, color: Rgba, mut buffer: &mut [u8]) -> io::Result<()> {
        match self {
            Self::ARgb32 => {
                buffer.write_u8(color.a)?;
                buffer.write_u8(color.r)?;
                buffer.write_u8(color.g)?;
                buffer.write_u8(color.b)?;
            }
            Self::XRgb32 => {
                buffer.write_u8(MIN_ALPHA)?;
                buffer.write_u8(color.r)?;
                buffer.write_u8(color.g)?;
                buffer.write_u8(color.b)?;
            }
            Self::ABgr32 => {
                buffer.write_u8(color.a)?;
                buffer.write_u8(color.b)?;
                buffer.write_u8(color.g)?;
                buffer.write_u8(color.r)?;
            }
            Self::XBgr32 => {
                buffer.write_u8(MIN_ALPHA)?;
                buffer.write_u8(color.b)?;
                buffer.write_u8(color.g)?;
                buffer.write_u8(color.r)?;
            }
            Self::BgrA32 => {
                buffer.write_u8(color.b)?;
                buffer.write_u8(color.g)?;
                buffer.write_u8(color.r)?;
                buffer.write_u8(color.a)?;
            }
            Self::BgrX32 => {
                buffer.write_u8(color.b)?;
                buffer.write_u8(color.g)?;
                buffer.write_u8(color.r)?;
                buffer.write_u8(MIN_ALPHA)?;
            }
            Self::RgbA32 => {
                buffer.write_u8(color.r)?;
                buffer.write_u8(color.g)?;
                buffer.write_u8(color.b)?;
                buffer.write_u8(color.a)?;
            }
            Self::RgbX32 => {
                buffer.write_u8(color.r)?;
                buffer.write_u8(color.g)?;
                buffer.write_u8(color.b)?;
                buffer.write_u8(MIN_ALPHA)?;
            }
        }

        Ok(())
    }
}

struct Point {
    x: usize,
    y: usize,
}

pub struct Rgba {
    pub r: u8,
    pub g: u8,
    pub b: u8,
    pub a: u8,
}