display_utils/
pixel_format.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#![allow(non_upper_case_globals)]
6
7use std::{fmt, str};
8use thiserror::Error;
9use {fidl_fuchsia_images2 as images2, fidl_fuchsia_sysmem as sysmem};
10
11/// Pixel format definitions that bridge different versions of sysmem / images2
12/// formats Fuchsia uses for display and GPU drivers' internal image type
13/// representation.
14///
15/// NOTE: The color-components are interpreted in memory in little-endian byte-order.
16/// E.g., PixelFormat::Bgra32 as [u8; 4] would have components [blue, green, red, alpha].
17#[derive(Debug, Clone, Copy, PartialEq)]
18pub enum PixelFormat {
19    /// Equivalent to fuchsia.sysmem.PixelFormatType.Invalid and
20    /// fuchsia.images2.PixelFormat.Invalid.
21    Invalid,
22    /// RGB only, 8 bits per each of R/G/B/A sample
23    ///
24    /// Equivalent to fuchsia.sysmem.PixelFormatType.R8G8B8A8 and
25    /// fuchsia.images2.PixelFormat.R8G8B8A8.
26    R8G8B8A8,
27    /// 32bpp BGRA, 1 plane.  RGB only, 8 bits per each of B/G/R/A sample.
28    ///
29    /// Equivalent to fuchsia.sysmem.PixelFormatType.Bgra32 and
30    /// fuchsia.images2.PixelFormat.Bgra32.
31    Bgra32,
32    /// YUV only, 8 bits per Y sample
33    ///
34    /// Equivalent to fuchsia.sysmem.PixelFormatType.I420 and
35    /// fuchsia.images2.PixelFormat.I420.
36    I420,
37    /// YUV only, 8 bits per Y sample
38    ///
39    /// Equivalent to fuchsia.sysmem.PixelFormatType.M420 and
40    /// fuchsia.images2.PixelFormat.M420.
41    M420,
42    /// YUV only, 8 bits per Y sample
43    ///
44    /// Equivalent to fuchsia.sysmem.PixelFormatType.Nv12 and
45    /// fuchsia.images2.PixelFormat.Nv12.
46    Nv12,
47    /// YUV only, 8 bits per Y sample
48    ///
49    /// Equivalent to fuchsia.sysmem.PixelFormatType.Yuy2 and
50    /// fuchsia.images2.PixelFormat.Yuy2.
51    Yuy2,
52    /// This value is reserved, and not currently used.
53    ///
54    /// Equivalent to fuchsia.sysmem.PixelFormatType.Mjpeg and
55    /// fuchsia.images2.PixelFormat.Mjpeg.
56    Mjpeg,
57    /// YUV only, 8 bits per Y sample
58    ///
59    /// Equivalent to fuchsia.sysmem.PixelFormatType.Yv12 and
60    /// fuchsia.images2.PixelFormat.Yv12.
61    Yv12,
62    /// 24bpp BGR, 1 plane. RGB only, 8 bits per each of B/G/R sample
63    ///
64    /// Equivalent to fuchsia.sysmem.PixelFormatType.Bgr24 and
65    /// fuchsia.images2.PixelFormat.Bgr24.
66    Bgr24,
67    /// 16bpp RGB, 1 plane. 5 bits (most significant) R, 6 bits G, 5 bits B
68    ///
69    /// Equivalent to fuchsia.sysmem.PixelFormatType.Rgb565 and
70    /// fuchsia.images2.PixelFormat.Rgb565.
71    Rgb565,
72    /// 8bpp RGB, 1 plane. 3 bits (most significant) R, 3 bits G, 2 bits B
73    ///
74    /// Equivalent to fuchsia.sysmem.PixelFormatType.Rgb332 and
75    /// fuchsia.images2.PixelFormat.Rgb332.
76    Rgb332,
77    /// 8bpp RGB, 1 plane. 2 bits (most significant) R, 2 bits G, 2 bits B
78    ///
79    /// Equivalent to fuchsia.sysmem.PixelFormatType.Rgb2220 and
80    /// fuchsia.images2.PixelFormat.Rgb2220.
81    Rgb2220,
82    /// 8bpp, Luminance-only (red, green and blue have identical values.)
83    ///
84    /// Equivalent to fuchsia.sysmem.PixelFormatType.L8 and
85    /// fuchsia.images2.PixelFormat.L8.
86    L8,
87    /// 8bpp, Red-only (Green and Blue are to be interpreted as 0).
88    ///
89    /// Equivalent to fuchsia.sysmem.PixelFormatType.R8 and
90    /// fuchsia.images2.PixelFormat.R8.
91    R8,
92    /// 16bpp RG, 1 plane. 8 bits R, 8 bits G.
93    ///
94    /// Equivalent to fuchsia.sysmem.PixelFormatType.R8G8 and
95    /// fuchsia.images2.PixelFormat.R8G8.
96    R8G8,
97    /// 32bpp RGBA, 1 plane. 2 bits A, 10 bits R/G/B.
98    ///
99    /// Equivalent to fuchsia.sysmem.PixelFormatType.A2R10G10B10 and
100    /// fuchsia.images2.PixelFormat.A2R10G10B10.
101    A2R10G10B10,
102    /// 32bpp BGRA, 1 plane. 2 bits A, 10 bits R/G/B.
103    ///
104    /// Equivalent to fuchsia.sysmem.PixelFormatType.A2B10G10R10 and
105    /// fuchsia.images2.PixelFormat.A2B10G10R10.
106    A2B10G10R10,
107    /// A client is explicitly indicating that the client does not care which
108    /// pixel format is chosen / used.
109    ///
110    /// Equivalent to fuchsia.sysmem.PixelFormatType.DoNotCare and
111    /// fuchsia.images2.PixelFormat.DoNotCare.
112    DoNotCare,
113}
114
115impl Default for PixelFormat {
116    fn default() -> PixelFormat {
117        PixelFormat::Invalid
118    }
119}
120
121impl From<images2::PixelFormat> for PixelFormat {
122    fn from(src: images2::PixelFormat) -> Self {
123        match src {
124            images2::PixelFormat::Invalid => Self::Invalid,
125            images2::PixelFormat::R8G8B8A8 => Self::R8G8B8A8,
126            images2::PixelFormat::B8G8R8A8 => Self::Bgra32,
127            images2::PixelFormat::I420 => Self::I420,
128            images2::PixelFormat::M420 => Self::M420,
129            images2::PixelFormat::Nv12 => Self::Nv12,
130            images2::PixelFormat::Yuy2 => Self::Yuy2,
131            images2::PixelFormat::Mjpeg => Self::Mjpeg,
132            images2::PixelFormat::Yv12 => Self::Yv12,
133            images2::PixelFormat::B8G8R8 => Self::Bgr24,
134            images2::PixelFormat::R5G6B5 => Self::Rgb565,
135            images2::PixelFormat::R3G3B2 => Self::Rgb332,
136            images2::PixelFormat::R2G2B2X2 => Self::Rgb2220,
137            images2::PixelFormat::L8 => Self::L8,
138            images2::PixelFormat::R8 => Self::R8,
139            images2::PixelFormat::R8G8 => Self::R8G8,
140            images2::PixelFormat::A2R10G10B10 => Self::A2R10G10B10,
141            images2::PixelFormat::A2B10G10R10 => Self::A2B10G10R10,
142            images2::PixelFormat::DoNotCare => Self::DoNotCare,
143            _ => Self::Invalid,
144        }
145    }
146}
147
148impl From<&images2::PixelFormat> for PixelFormat {
149    fn from(src: &images2::PixelFormat) -> Self {
150        Self::from(*src)
151    }
152}
153
154impl From<PixelFormat> for images2::PixelFormat {
155    fn from(src: PixelFormat) -> Self {
156        match src {
157            PixelFormat::Invalid => Self::Invalid,
158            PixelFormat::R8G8B8A8 => Self::R8G8B8A8,
159            PixelFormat::Bgra32 => Self::B8G8R8A8,
160            PixelFormat::I420 => Self::I420,
161            PixelFormat::M420 => Self::M420,
162            PixelFormat::Nv12 => Self::Nv12,
163            PixelFormat::Yuy2 => Self::Yuy2,
164            PixelFormat::Mjpeg => Self::Mjpeg,
165            PixelFormat::Yv12 => Self::Yv12,
166            PixelFormat::Bgr24 => Self::B8G8R8,
167            PixelFormat::Rgb565 => Self::R5G6B5,
168            PixelFormat::Rgb332 => Self::R3G3B2,
169            PixelFormat::Rgb2220 => Self::R2G2B2X2,
170            PixelFormat::L8 => Self::L8,
171            PixelFormat::R8 => Self::R8,
172            PixelFormat::R8G8 => Self::R8G8,
173            PixelFormat::A2R10G10B10 => Self::A2R10G10B10,
174            PixelFormat::A2B10G10R10 => Self::A2B10G10R10,
175            PixelFormat::DoNotCare => Self::DoNotCare,
176        }
177    }
178}
179
180impl From<&PixelFormat> for images2::PixelFormat {
181    fn from(src: &PixelFormat) -> Self {
182        Self::from(*src)
183    }
184}
185
186impl From<sysmem::PixelFormatType> for PixelFormat {
187    fn from(src: sysmem::PixelFormatType) -> Self {
188        match src {
189            sysmem::PixelFormatType::Invalid => Self::Invalid,
190            sysmem::PixelFormatType::R8G8B8A8 => Self::R8G8B8A8,
191            sysmem::PixelFormatType::Bgra32 => Self::Bgra32,
192            sysmem::PixelFormatType::I420 => Self::I420,
193            sysmem::PixelFormatType::M420 => Self::M420,
194            sysmem::PixelFormatType::Nv12 => Self::Nv12,
195            sysmem::PixelFormatType::Yuy2 => Self::Yuy2,
196            sysmem::PixelFormatType::Mjpeg => Self::Mjpeg,
197            sysmem::PixelFormatType::Yv12 => Self::Yv12,
198            sysmem::PixelFormatType::Bgr24 => Self::Bgr24,
199            sysmem::PixelFormatType::Rgb565 => Self::Rgb565,
200            sysmem::PixelFormatType::Rgb332 => Self::Rgb332,
201            sysmem::PixelFormatType::Rgb2220 => Self::Rgb2220,
202            sysmem::PixelFormatType::L8 => Self::L8,
203            sysmem::PixelFormatType::R8 => Self::R8,
204            sysmem::PixelFormatType::R8G8 => Self::R8G8,
205            sysmem::PixelFormatType::A2R10G10B10 => Self::A2R10G10B10,
206            sysmem::PixelFormatType::A2B10G10R10 => Self::A2B10G10R10,
207            sysmem::PixelFormatType::DoNotCare => Self::DoNotCare,
208        }
209    }
210}
211
212impl From<&sysmem::PixelFormatType> for PixelFormat {
213    fn from(src: &sysmem::PixelFormatType) -> Self {
214        Self::from(*src)
215    }
216}
217
218impl From<PixelFormat> for sysmem::PixelFormatType {
219    fn from(src: PixelFormat) -> Self {
220        match src {
221            PixelFormat::Invalid => Self::Invalid,
222            PixelFormat::R8G8B8A8 => Self::R8G8B8A8,
223            PixelFormat::Bgra32 => Self::Bgra32,
224            PixelFormat::I420 => Self::I420,
225            PixelFormat::M420 => Self::M420,
226            PixelFormat::Nv12 => Self::Nv12,
227            PixelFormat::Yuy2 => Self::Yuy2,
228            PixelFormat::Mjpeg => Self::Mjpeg,
229            PixelFormat::Yv12 => Self::Yv12,
230            PixelFormat::Bgr24 => Self::Bgr24,
231            PixelFormat::Rgb565 => Self::Rgb565,
232            PixelFormat::Rgb332 => Self::Rgb332,
233            PixelFormat::Rgb2220 => Self::Rgb2220,
234            PixelFormat::L8 => Self::L8,
235            PixelFormat::R8 => Self::R8,
236            PixelFormat::R8G8 => Self::R8G8,
237            PixelFormat::A2R10G10B10 => Self::A2R10G10B10,
238            PixelFormat::A2B10G10R10 => Self::A2B10G10R10,
239            PixelFormat::DoNotCare => Self::DoNotCare,
240        }
241    }
242}
243
244impl From<&PixelFormat> for sysmem::PixelFormatType {
245    fn from(src: &PixelFormat) -> Self {
246        Self::from(*src)
247    }
248}
249
250impl fmt::Display for PixelFormat {
251    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
252        let string: &str = match *self {
253            PixelFormat::Invalid => "(Invalid)",
254            PixelFormat::R8G8B8A8 => "RGBA 8888",
255            PixelFormat::Bgra32 => "BGRA 8888",
256            PixelFormat::I420 => "I420",
257            PixelFormat::M420 => "M420",
258            PixelFormat::Nv12 => "NV12",
259            PixelFormat::Yuy2 => "YUY2",
260            PixelFormat::Mjpeg => "MJPEG",
261            PixelFormat::Yv12 => "YV12",
262            PixelFormat::Bgr24 => "BGR 888",
263            PixelFormat::Rgb565 => "RGB 565",
264            PixelFormat::Rgb332 => "RGB 332",
265            PixelFormat::Rgb2220 => "RGB 2220",
266            PixelFormat::L8 => "Mono L8",
267            PixelFormat::R8 => "Mono R8",
268            PixelFormat::R8G8 => "R8G8",
269            PixelFormat::A2R10G10B10 => "A2 R10 G10 B10",
270            PixelFormat::A2B10G10R10 => "A2 B10 G10 R10",
271            PixelFormat::DoNotCare => "(Do not care)",
272        };
273        write!(f, "{}", string)
274    }
275}
276
277#[derive(Debug, PartialEq)]
278pub enum ParsePixelFormatError {
279    UnsupportedFormat(String),
280}
281
282impl fmt::Display for ParsePixelFormatError {
283    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
284        match self {
285            ParsePixelFormatError::UnsupportedFormat(s) => write!(f, "Unsupported format {}", s),
286        }
287    }
288}
289
290impl str::FromStr for PixelFormat {
291    type Err = ParsePixelFormatError;
292
293    fn from_str(s: &str) -> Result<Self, Self::Err> {
294        match s.to_ascii_lowercase().as_str() {
295            "invalid" => Ok(PixelFormat::Invalid),
296            "rgba8888" | "r8g8b8a8" | "rgba32" => Ok(PixelFormat::R8G8B8A8),
297            "bgra8888" | "b8g8r8a8" | "bgra32" => Ok(PixelFormat::Bgra32),
298            "i420" => Ok(PixelFormat::I420),
299            "m420" => Ok(PixelFormat::M420),
300            "nv12" => Ok(PixelFormat::Nv12),
301            "yuy2" => Ok(PixelFormat::Yuy2),
302            "mjpeg" => Ok(PixelFormat::Mjpeg),
303            "yv12" => Ok(PixelFormat::Yv12),
304            "bgr888" | "bgr24" => Ok(PixelFormat::Bgr24),
305            "rgb565" => Ok(PixelFormat::Rgb565),
306            "rgb332" => Ok(PixelFormat::Rgb332),
307            "rgb2220" => Ok(PixelFormat::Rgb2220),
308            "monol8" => Ok(PixelFormat::L8),
309            "monor8" => Ok(PixelFormat::R8),
310            "r8g8" => Ok(PixelFormat::R8G8),
311            "a2r10g10b10" => Ok(PixelFormat::A2R10G10B10),
312            "a2b10g10r10" => Ok(PixelFormat::A2B10G10R10),
313            "donotcare" => Ok(PixelFormat::DoNotCare),
314            _ => Err(ParsePixelFormatError::UnsupportedFormat(s.to_string())),
315        }
316    }
317}
318
319#[derive(Debug, Error, PartialEq)]
320pub enum GetBytesPerPixelError {
321    #[error("Unsupported format: {0}")]
322    UnsupportedFormat(PixelFormat),
323}
324
325/// Get number of bytes per pixel for given `pixel_format`.
326/// Currently only packed pixel formats are supported. Planar formats (e.g.
327/// NV12) are not supported.
328pub fn get_bytes_per_pixel(pixel_format: PixelFormat) -> Result<usize, GetBytesPerPixelError> {
329    match pixel_format {
330        PixelFormat::R8G8B8A8
331        | PixelFormat::Bgra32
332        | PixelFormat::A2R10G10B10
333        | PixelFormat::A2B10G10R10 => Ok(4),
334        PixelFormat::Bgr24 => Ok(3),
335        PixelFormat::Rgb565 | PixelFormat::R8G8 => Ok(2),
336        PixelFormat::Rgb332 | PixelFormat::Rgb2220 | PixelFormat::L8 | PixelFormat::R8 => Ok(1),
337        _ => Err(GetBytesPerPixelError::UnsupportedFormat(pixel_format)),
338    }
339}
340
341#[cfg(test)]
342mod tests {
343    use super::*;
344    use std::str::FromStr;
345
346    #[fuchsia::test]
347    fn pixel_format_from_str_invalid() {
348        assert_eq!(
349            PixelFormat::from_str("bad"),
350            Err(ParsePixelFormatError::UnsupportedFormat("bad".to_string()))
351        );
352    }
353
354    #[fuchsia::test]
355    fn pixel_format_from_str_valid() {
356        assert_eq!(PixelFormat::from_str("invalid"), Ok(PixelFormat::Invalid));
357        assert_eq!(PixelFormat::from_str("rgba8888"), Ok(PixelFormat::R8G8B8A8));
358        assert_eq!(PixelFormat::from_str("r8g8b8a8"), Ok(PixelFormat::R8G8B8A8));
359        assert_eq!(PixelFormat::from_str("rgba32"), Ok(PixelFormat::R8G8B8A8));
360        assert_eq!(PixelFormat::from_str("bgra8888"), Ok(PixelFormat::Bgra32));
361        assert_eq!(PixelFormat::from_str("b8g8r8a8"), Ok(PixelFormat::Bgra32));
362        assert_eq!(PixelFormat::from_str("bgra32"), Ok(PixelFormat::Bgra32));
363        assert_eq!(PixelFormat::from_str("i420"), Ok(PixelFormat::I420));
364        assert_eq!(PixelFormat::from_str("m420"), Ok(PixelFormat::M420));
365        assert_eq!(PixelFormat::from_str("nv12"), Ok(PixelFormat::Nv12));
366        assert_eq!(PixelFormat::from_str("yuy2"), Ok(PixelFormat::Yuy2));
367        assert_eq!(PixelFormat::from_str("mjpeg"), Ok(PixelFormat::Mjpeg));
368        assert_eq!(PixelFormat::from_str("yv12"), Ok(PixelFormat::Yv12));
369        assert_eq!(PixelFormat::from_str("bgr888"), Ok(PixelFormat::Bgr24));
370        assert_eq!(PixelFormat::from_str("bgr24"), Ok(PixelFormat::Bgr24));
371        assert_eq!(PixelFormat::from_str("rgb565"), Ok(PixelFormat::Rgb565));
372        assert_eq!(PixelFormat::from_str("rgb332"), Ok(PixelFormat::Rgb332));
373        assert_eq!(PixelFormat::from_str("rgb2220"), Ok(PixelFormat::Rgb2220));
374        assert_eq!(PixelFormat::from_str("monol8"), Ok(PixelFormat::L8));
375        assert_eq!(PixelFormat::from_str("monor8"), Ok(PixelFormat::R8));
376        assert_eq!(PixelFormat::from_str("r8g8"), Ok(PixelFormat::R8G8));
377        assert_eq!(PixelFormat::from_str("a2r10g10b10"), Ok(PixelFormat::A2R10G10B10));
378        assert_eq!(PixelFormat::from_str("a2b10g10r10"), Ok(PixelFormat::A2B10G10R10));
379        assert_eq!(PixelFormat::from_str("donotcare"), Ok(PixelFormat::DoNotCare));
380
381        assert_eq!(PixelFormat::from_str("INVALID"), Ok(PixelFormat::Invalid));
382        assert_eq!(PixelFormat::from_str("RGBA8888"), Ok(PixelFormat::R8G8B8A8));
383        assert_eq!(PixelFormat::from_str("R8G8B8A8"), Ok(PixelFormat::R8G8B8A8));
384        assert_eq!(PixelFormat::from_str("RGBA32"), Ok(PixelFormat::R8G8B8A8));
385        assert_eq!(PixelFormat::from_str("BGRA8888"), Ok(PixelFormat::Bgra32));
386        assert_eq!(PixelFormat::from_str("B8G8R8A8"), Ok(PixelFormat::Bgra32));
387        assert_eq!(PixelFormat::from_str("BGRA32"), Ok(PixelFormat::Bgra32));
388        assert_eq!(PixelFormat::from_str("I420"), Ok(PixelFormat::I420));
389        assert_eq!(PixelFormat::from_str("M420"), Ok(PixelFormat::M420));
390        assert_eq!(PixelFormat::from_str("NV12"), Ok(PixelFormat::Nv12));
391        assert_eq!(PixelFormat::from_str("YUY2"), Ok(PixelFormat::Yuy2));
392        assert_eq!(PixelFormat::from_str("MJPEG"), Ok(PixelFormat::Mjpeg));
393        assert_eq!(PixelFormat::from_str("YV12"), Ok(PixelFormat::Yv12));
394        assert_eq!(PixelFormat::from_str("BGR888"), Ok(PixelFormat::Bgr24));
395        assert_eq!(PixelFormat::from_str("BGR24"), Ok(PixelFormat::Bgr24));
396        assert_eq!(PixelFormat::from_str("RGB565"), Ok(PixelFormat::Rgb565));
397        assert_eq!(PixelFormat::from_str("RGB332"), Ok(PixelFormat::Rgb332));
398        assert_eq!(PixelFormat::from_str("RGB2220"), Ok(PixelFormat::Rgb2220));
399        assert_eq!(PixelFormat::from_str("MONOL8"), Ok(PixelFormat::L8));
400        assert_eq!(PixelFormat::from_str("MONOR8"), Ok(PixelFormat::R8));
401        assert_eq!(PixelFormat::from_str("R8G8"), Ok(PixelFormat::R8G8));
402        assert_eq!(PixelFormat::from_str("A2R10G10B10"), Ok(PixelFormat::A2R10G10B10));
403        assert_eq!(PixelFormat::from_str("A2B10G10R10"), Ok(PixelFormat::A2B10G10R10));
404        assert_eq!(PixelFormat::from_str("DONOTCARE"), Ok(PixelFormat::DoNotCare));
405
406        assert_eq!(PixelFormat::from_str("Invalid"), Ok(PixelFormat::Invalid));
407        assert_eq!(PixelFormat::from_str("Rgba8888"), Ok(PixelFormat::R8G8B8A8));
408        assert_eq!(PixelFormat::from_str("R8g8b8a8"), Ok(PixelFormat::R8G8B8A8));
409        assert_eq!(PixelFormat::from_str("Rgba32"), Ok(PixelFormat::R8G8B8A8));
410        assert_eq!(PixelFormat::from_str("Bgra8888"), Ok(PixelFormat::Bgra32));
411        assert_eq!(PixelFormat::from_str("B8g8r8a8"), Ok(PixelFormat::Bgra32));
412        assert_eq!(PixelFormat::from_str("Bgra32"), Ok(PixelFormat::Bgra32));
413        assert_eq!(PixelFormat::from_str("I420"), Ok(PixelFormat::I420));
414        assert_eq!(PixelFormat::from_str("M420"), Ok(PixelFormat::M420));
415        assert_eq!(PixelFormat::from_str("Nv12"), Ok(PixelFormat::Nv12));
416        assert_eq!(PixelFormat::from_str("Yuy2"), Ok(PixelFormat::Yuy2));
417        assert_eq!(PixelFormat::from_str("Mjpeg"), Ok(PixelFormat::Mjpeg));
418        assert_eq!(PixelFormat::from_str("Yv12"), Ok(PixelFormat::Yv12));
419        assert_eq!(PixelFormat::from_str("Bgr888"), Ok(PixelFormat::Bgr24));
420        assert_eq!(PixelFormat::from_str("Bgr24"), Ok(PixelFormat::Bgr24));
421        assert_eq!(PixelFormat::from_str("Rgb565"), Ok(PixelFormat::Rgb565));
422        assert_eq!(PixelFormat::from_str("Rgb332"), Ok(PixelFormat::Rgb332));
423        assert_eq!(PixelFormat::from_str("Rgb2220"), Ok(PixelFormat::Rgb2220));
424        assert_eq!(PixelFormat::from_str("Monol8"), Ok(PixelFormat::L8));
425        assert_eq!(PixelFormat::from_str("Monor8"), Ok(PixelFormat::R8));
426        assert_eq!(PixelFormat::from_str("R8g8"), Ok(PixelFormat::R8G8));
427        assert_eq!(PixelFormat::from_str("A2r10g10b10"), Ok(PixelFormat::A2R10G10B10));
428        assert_eq!(PixelFormat::from_str("A2b10g10r10"), Ok(PixelFormat::A2B10G10R10));
429        assert_eq!(PixelFormat::from_str("DoNotCare"), Ok(PixelFormat::DoNotCare));
430    }
431
432    #[fuchsia::test]
433    fn get_bytes_per_pixel_valid() {
434        assert_eq!(get_bytes_per_pixel(PixelFormat::R8G8B8A8), Ok(4));
435        assert_eq!(get_bytes_per_pixel(PixelFormat::Bgra32), Ok(4));
436        assert_eq!(get_bytes_per_pixel(PixelFormat::Bgr24), Ok(3));
437        assert_eq!(get_bytes_per_pixel(PixelFormat::Rgb565), Ok(2));
438        assert_eq!(get_bytes_per_pixel(PixelFormat::Rgb332), Ok(1));
439        assert_eq!(get_bytes_per_pixel(PixelFormat::Rgb2220), Ok(1));
440        assert_eq!(get_bytes_per_pixel(PixelFormat::L8), Ok(1));
441        assert_eq!(get_bytes_per_pixel(PixelFormat::R8), Ok(1));
442        assert_eq!(get_bytes_per_pixel(PixelFormat::R8G8), Ok(2));
443        assert_eq!(get_bytes_per_pixel(PixelFormat::A2R10G10B10), Ok(4));
444        assert_eq!(get_bytes_per_pixel(PixelFormat::A2B10G10R10), Ok(4));
445    }
446
447    #[fuchsia::test]
448    fn get_bytes_per_pixel_invalid() {
449        assert_eq!(
450            get_bytes_per_pixel(PixelFormat::Invalid),
451            Err(GetBytesPerPixelError::UnsupportedFormat(PixelFormat::Invalid))
452        );
453        assert_eq!(
454            get_bytes_per_pixel(PixelFormat::I420),
455            Err(GetBytesPerPixelError::UnsupportedFormat(PixelFormat::I420))
456        );
457        assert_eq!(
458            get_bytes_per_pixel(PixelFormat::M420),
459            Err(GetBytesPerPixelError::UnsupportedFormat(PixelFormat::M420))
460        );
461        assert_eq!(
462            get_bytes_per_pixel(PixelFormat::Nv12),
463            Err(GetBytesPerPixelError::UnsupportedFormat(PixelFormat::Nv12))
464        );
465        assert_eq!(
466            get_bytes_per_pixel(PixelFormat::Yuy2),
467            Err(GetBytesPerPixelError::UnsupportedFormat(PixelFormat::Yuy2))
468        );
469        assert_eq!(
470            get_bytes_per_pixel(PixelFormat::Mjpeg),
471            Err(GetBytesPerPixelError::UnsupportedFormat(PixelFormat::Mjpeg))
472        );
473        assert_eq!(
474            get_bytes_per_pixel(PixelFormat::Yv12),
475            Err(GetBytesPerPixelError::UnsupportedFormat(PixelFormat::Yv12))
476        );
477        assert_eq!(
478            get_bytes_per_pixel(PixelFormat::DoNotCare),
479            Err(GetBytesPerPixelError::UnsupportedFormat(PixelFormat::DoNotCare))
480        );
481    }
482
483    #[fuchsia::test]
484    fn sysmem2_pixel_format_conversion() {
485        assert_eq!(
486            images2::PixelFormat::from(PixelFormat::from(images2::PixelFormat::Invalid)),
487            images2::PixelFormat::Invalid
488        );
489        assert_eq!(
490            images2::PixelFormat::from(PixelFormat::from(images2::PixelFormat::R8G8B8A8)),
491            images2::PixelFormat::R8G8B8A8
492        );
493        assert_eq!(
494            images2::PixelFormat::from(PixelFormat::from(images2::PixelFormat::B8G8R8A8)),
495            images2::PixelFormat::B8G8R8A8
496        );
497        assert_eq!(
498            images2::PixelFormat::from(PixelFormat::from(images2::PixelFormat::I420)),
499            images2::PixelFormat::I420
500        );
501        assert_eq!(
502            images2::PixelFormat::from(PixelFormat::from(images2::PixelFormat::M420)),
503            images2::PixelFormat::M420
504        );
505        assert_eq!(
506            images2::PixelFormat::from(PixelFormat::from(images2::PixelFormat::Nv12)),
507            images2::PixelFormat::Nv12
508        );
509        assert_eq!(
510            images2::PixelFormat::from(PixelFormat::from(images2::PixelFormat::Yuy2)),
511            images2::PixelFormat::Yuy2
512        );
513        assert_eq!(
514            images2::PixelFormat::from(PixelFormat::from(images2::PixelFormat::Mjpeg)),
515            images2::PixelFormat::Mjpeg
516        );
517        assert_eq!(
518            images2::PixelFormat::from(PixelFormat::from(images2::PixelFormat::Yv12)),
519            images2::PixelFormat::Yv12
520        );
521        assert_eq!(
522            images2::PixelFormat::from(PixelFormat::from(images2::PixelFormat::B8G8R8)),
523            images2::PixelFormat::B8G8R8
524        );
525        assert_eq!(
526            images2::PixelFormat::from(PixelFormat::from(images2::PixelFormat::R5G6B5)),
527            images2::PixelFormat::R5G6B5
528        );
529        assert_eq!(
530            images2::PixelFormat::from(PixelFormat::from(images2::PixelFormat::R3G3B2)),
531            images2::PixelFormat::R3G3B2
532        );
533        assert_eq!(
534            images2::PixelFormat::from(PixelFormat::from(images2::PixelFormat::R2G2B2X2)),
535            images2::PixelFormat::R2G2B2X2
536        );
537        assert_eq!(
538            images2::PixelFormat::from(PixelFormat::from(images2::PixelFormat::L8)),
539            images2::PixelFormat::L8
540        );
541        assert_eq!(
542            images2::PixelFormat::from(PixelFormat::from(images2::PixelFormat::R8)),
543            images2::PixelFormat::R8
544        );
545        assert_eq!(
546            images2::PixelFormat::from(PixelFormat::from(images2::PixelFormat::R8G8)),
547            images2::PixelFormat::R8G8
548        );
549        assert_eq!(
550            images2::PixelFormat::from(PixelFormat::from(images2::PixelFormat::A2R10G10B10)),
551            images2::PixelFormat::A2R10G10B10
552        );
553        assert_eq!(
554            images2::PixelFormat::from(PixelFormat::from(images2::PixelFormat::A2B10G10R10)),
555            images2::PixelFormat::A2B10G10R10
556        );
557        assert_eq!(
558            images2::PixelFormat::from(PixelFormat::from(images2::PixelFormat::DoNotCare)),
559            images2::PixelFormat::DoNotCare
560        );
561    }
562
563    #[fuchsia::test]
564    fn sysmem_pixel_format_conversion() {
565        assert_eq!(
566            sysmem::PixelFormatType::from(PixelFormat::from(sysmem::PixelFormatType::Invalid)),
567            sysmem::PixelFormatType::Invalid
568        );
569        assert_eq!(
570            sysmem::PixelFormatType::from(PixelFormat::from(sysmem::PixelFormatType::R8G8B8A8)),
571            sysmem::PixelFormatType::R8G8B8A8
572        );
573        assert_eq!(
574            sysmem::PixelFormatType::from(PixelFormat::from(sysmem::PixelFormatType::Bgra32)),
575            sysmem::PixelFormatType::Bgra32
576        );
577        assert_eq!(
578            sysmem::PixelFormatType::from(PixelFormat::from(sysmem::PixelFormatType::I420)),
579            sysmem::PixelFormatType::I420
580        );
581        assert_eq!(
582            sysmem::PixelFormatType::from(PixelFormat::from(sysmem::PixelFormatType::M420)),
583            sysmem::PixelFormatType::M420
584        );
585        assert_eq!(
586            sysmem::PixelFormatType::from(PixelFormat::from(sysmem::PixelFormatType::Nv12)),
587            sysmem::PixelFormatType::Nv12
588        );
589        assert_eq!(
590            sysmem::PixelFormatType::from(PixelFormat::from(sysmem::PixelFormatType::Yuy2)),
591            sysmem::PixelFormatType::Yuy2
592        );
593        assert_eq!(
594            sysmem::PixelFormatType::from(PixelFormat::from(sysmem::PixelFormatType::Mjpeg)),
595            sysmem::PixelFormatType::Mjpeg
596        );
597        assert_eq!(
598            sysmem::PixelFormatType::from(PixelFormat::from(sysmem::PixelFormatType::Yv12)),
599            sysmem::PixelFormatType::Yv12
600        );
601        assert_eq!(
602            sysmem::PixelFormatType::from(PixelFormat::from(sysmem::PixelFormatType::Bgr24)),
603            sysmem::PixelFormatType::Bgr24
604        );
605        assert_eq!(
606            sysmem::PixelFormatType::from(PixelFormat::from(sysmem::PixelFormatType::Rgb565)),
607            sysmem::PixelFormatType::Rgb565
608        );
609        assert_eq!(
610            sysmem::PixelFormatType::from(PixelFormat::from(sysmem::PixelFormatType::Rgb332)),
611            sysmem::PixelFormatType::Rgb332
612        );
613        assert_eq!(
614            sysmem::PixelFormatType::from(PixelFormat::from(sysmem::PixelFormatType::Rgb2220)),
615            sysmem::PixelFormatType::Rgb2220
616        );
617        assert_eq!(
618            sysmem::PixelFormatType::from(PixelFormat::from(sysmem::PixelFormatType::L8)),
619            sysmem::PixelFormatType::L8
620        );
621        assert_eq!(
622            sysmem::PixelFormatType::from(PixelFormat::from(sysmem::PixelFormatType::R8)),
623            sysmem::PixelFormatType::R8
624        );
625        assert_eq!(
626            sysmem::PixelFormatType::from(PixelFormat::from(sysmem::PixelFormatType::R8G8)),
627            sysmem::PixelFormatType::R8G8
628        );
629        assert_eq!(
630            sysmem::PixelFormatType::from(PixelFormat::from(sysmem::PixelFormatType::A2R10G10B10)),
631            sysmem::PixelFormatType::A2R10G10B10
632        );
633        assert_eq!(
634            sysmem::PixelFormatType::from(PixelFormat::from(sysmem::PixelFormatType::A2B10G10R10)),
635            sysmem::PixelFormatType::A2B10G10R10
636        );
637        assert_eq!(
638            sysmem::PixelFormatType::from(PixelFormat::from(sysmem::PixelFormatType::DoNotCare)),
639            sysmem::PixelFormatType::DoNotCare
640        );
641    }
642
643    #[fuchsia::test]
644    fn sysmem2_to_sysmem_pixel_format_conversion() {
645        assert_eq!(
646            sysmem::PixelFormatType::from(PixelFormat::from(images2::PixelFormat::Invalid)),
647            sysmem::PixelFormatType::Invalid
648        );
649        assert_eq!(
650            sysmem::PixelFormatType::from(PixelFormat::from(images2::PixelFormat::R8G8B8A8)),
651            sysmem::PixelFormatType::R8G8B8A8
652        );
653        assert_eq!(
654            sysmem::PixelFormatType::from(PixelFormat::from(images2::PixelFormat::B8G8R8A8)),
655            sysmem::PixelFormatType::Bgra32
656        );
657        assert_eq!(
658            sysmem::PixelFormatType::from(PixelFormat::from(images2::PixelFormat::I420)),
659            sysmem::PixelFormatType::I420
660        );
661        assert_eq!(
662            sysmem::PixelFormatType::from(PixelFormat::from(images2::PixelFormat::M420)),
663            sysmem::PixelFormatType::M420
664        );
665        assert_eq!(
666            sysmem::PixelFormatType::from(PixelFormat::from(images2::PixelFormat::Nv12)),
667            sysmem::PixelFormatType::Nv12
668        );
669        assert_eq!(
670            sysmem::PixelFormatType::from(PixelFormat::from(images2::PixelFormat::Yuy2)),
671            sysmem::PixelFormatType::Yuy2
672        );
673        assert_eq!(
674            sysmem::PixelFormatType::from(PixelFormat::from(images2::PixelFormat::Mjpeg)),
675            sysmem::PixelFormatType::Mjpeg
676        );
677        assert_eq!(
678            sysmem::PixelFormatType::from(PixelFormat::from(images2::PixelFormat::Yv12)),
679            sysmem::PixelFormatType::Yv12
680        );
681        assert_eq!(
682            sysmem::PixelFormatType::from(PixelFormat::from(images2::PixelFormat::B8G8R8)),
683            sysmem::PixelFormatType::Bgr24
684        );
685        assert_eq!(
686            sysmem::PixelFormatType::from(PixelFormat::from(images2::PixelFormat::R5G6B5)),
687            sysmem::PixelFormatType::Rgb565
688        );
689        assert_eq!(
690            sysmem::PixelFormatType::from(PixelFormat::from(images2::PixelFormat::R3G3B2)),
691            sysmem::PixelFormatType::Rgb332
692        );
693        assert_eq!(
694            sysmem::PixelFormatType::from(PixelFormat::from(images2::PixelFormat::R2G2B2X2)),
695            sysmem::PixelFormatType::Rgb2220
696        );
697        assert_eq!(
698            sysmem::PixelFormatType::from(PixelFormat::from(images2::PixelFormat::L8)),
699            sysmem::PixelFormatType::L8
700        );
701        assert_eq!(
702            sysmem::PixelFormatType::from(PixelFormat::from(images2::PixelFormat::R8)),
703            sysmem::PixelFormatType::R8
704        );
705        assert_eq!(
706            sysmem::PixelFormatType::from(PixelFormat::from(images2::PixelFormat::R8G8)),
707            sysmem::PixelFormatType::R8G8
708        );
709        assert_eq!(
710            sysmem::PixelFormatType::from(PixelFormat::from(images2::PixelFormat::A2R10G10B10)),
711            sysmem::PixelFormatType::A2R10G10B10
712        );
713        assert_eq!(
714            sysmem::PixelFormatType::from(PixelFormat::from(images2::PixelFormat::A2B10G10R10)),
715            sysmem::PixelFormatType::A2B10G10R10
716        );
717        assert_eq!(
718            sysmem::PixelFormatType::from(PixelFormat::from(images2::PixelFormat::DoNotCare)),
719            sysmem::PixelFormatType::DoNotCare
720        );
721    }
722
723    #[fuchsia::test]
724    fn sysmem_to_sysmem2_pixel_format_conversion() {
725        assert_eq!(
726            images2::PixelFormat::from(PixelFormat::from(sysmem::PixelFormatType::Invalid)),
727            images2::PixelFormat::Invalid
728        );
729        assert_eq!(
730            images2::PixelFormat::from(PixelFormat::from(sysmem::PixelFormatType::R8G8B8A8)),
731            images2::PixelFormat::R8G8B8A8
732        );
733        assert_eq!(
734            images2::PixelFormat::from(PixelFormat::from(sysmem::PixelFormatType::Bgra32)),
735            images2::PixelFormat::B8G8R8A8
736        );
737        assert_eq!(
738            images2::PixelFormat::from(PixelFormat::from(sysmem::PixelFormatType::I420)),
739            images2::PixelFormat::I420
740        );
741        assert_eq!(
742            images2::PixelFormat::from(PixelFormat::from(sysmem::PixelFormatType::M420)),
743            images2::PixelFormat::M420
744        );
745        assert_eq!(
746            images2::PixelFormat::from(PixelFormat::from(sysmem::PixelFormatType::Nv12)),
747            images2::PixelFormat::Nv12
748        );
749        assert_eq!(
750            images2::PixelFormat::from(PixelFormat::from(sysmem::PixelFormatType::Yuy2)),
751            images2::PixelFormat::Yuy2
752        );
753        assert_eq!(
754            images2::PixelFormat::from(PixelFormat::from(sysmem::PixelFormatType::Mjpeg)),
755            images2::PixelFormat::Mjpeg
756        );
757        assert_eq!(
758            images2::PixelFormat::from(PixelFormat::from(sysmem::PixelFormatType::Yv12)),
759            images2::PixelFormat::Yv12
760        );
761        assert_eq!(
762            images2::PixelFormat::from(PixelFormat::from(sysmem::PixelFormatType::Bgr24)),
763            images2::PixelFormat::B8G8R8
764        );
765        assert_eq!(
766            images2::PixelFormat::from(PixelFormat::from(sysmem::PixelFormatType::Rgb565)),
767            images2::PixelFormat::R5G6B5
768        );
769        assert_eq!(
770            images2::PixelFormat::from(PixelFormat::from(sysmem::PixelFormatType::Rgb332)),
771            images2::PixelFormat::R3G3B2
772        );
773        assert_eq!(
774            images2::PixelFormat::from(PixelFormat::from(sysmem::PixelFormatType::Rgb2220)),
775            images2::PixelFormat::R2G2B2X2
776        );
777        assert_eq!(
778            images2::PixelFormat::from(PixelFormat::from(sysmem::PixelFormatType::L8)),
779            images2::PixelFormat::L8
780        );
781        assert_eq!(
782            images2::PixelFormat::from(PixelFormat::from(sysmem::PixelFormatType::R8)),
783            images2::PixelFormat::R8
784        );
785        assert_eq!(
786            images2::PixelFormat::from(PixelFormat::from(sysmem::PixelFormatType::R8G8)),
787            images2::PixelFormat::R8G8
788        );
789        assert_eq!(
790            images2::PixelFormat::from(PixelFormat::from(sysmem::PixelFormatType::A2R10G10B10)),
791            images2::PixelFormat::A2R10G10B10
792        );
793        assert_eq!(
794            images2::PixelFormat::from(PixelFormat::from(sysmem::PixelFormatType::A2B10G10R10)),
795            images2::PixelFormat::A2B10G10R10
796        );
797        assert_eq!(
798            images2::PixelFormat::from(PixelFormat::from(sysmem::PixelFormatType::DoNotCare)),
799            images2::PixelFormat::DoNotCare
800        );
801    }
802}