]> git.nihav.org Git - nihav.git/commitdiff
nihav_core/formats: introduce a function to make RGB5[56]5 formats
authorKostya Shishkov <kostya.shishkov@gmail.com>
Mon, 2 Mar 2026 17:09:40 +0000 (18:09 +0100)
committerKostya Shishkov <kostya.shishkov@gmail.com>
Mon, 2 Mar 2026 18:04:59 +0000 (19:04 +0100)
And throw in RGB555_FORMAT as it seems to be used a lot.

nihav-core/src/formats.rs

index 967ac73d49c6fd992a11ca76e3c9e56dec3692ec..4c4a11639b1e3a541bd3b10df8560646007e18b3 100644 (file)
@@ -552,14 +552,11 @@ pub const PAL8_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::RG
                                             None, None],
                                         elem_size: 3, be: false, alpha: false, palette: true };
 
+/// Predefined format for RGB555 packed video.
+pub const RGB555_FORMAT: NAPixelFormaton = NAPixelFormaton::make_rgb16_fmt(5, 5, 5, true, false);
+
 /// Predefined format for RGB565 packed video.
-pub const RGB565_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::RGB(RGBSubmodel::RGB), components: 3,
-                                        comp_info: [
-                                            chromaton!(packrgb; 5, 11, 0, 2),
-                                            chromaton!(packrgb; 6,  5, 0, 2),
-                                            chromaton!(packrgb; 5,  0, 0, 2),
-                                            None, None],
-                                        elem_size: 2, be: false, alpha: false, palette: false };
+pub const RGB565_FORMAT: NAPixelFormaton = NAPixelFormaton::make_rgb16_fmt(5, 6, 5, true, false);
 
 /// Predefined format for RGB24.
 pub const RGB24_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::RGB(RGBSubmodel::RGB), components: 3,
@@ -813,6 +810,21 @@ impl NAPixelFormaton {
             _ => None,
         }
     }
+    /// Constructs a 15/16-bit RGB `NAPixelFormaton` from a compact description.
+    pub const fn make_rgb16_fmt(rbits: u8, gbits: u8, bbits: u8, rtop: bool, be: bool) -> Self {
+        let (rshift, gshift, bshift) = if rtop { (gbits + bbits, bbits, 0) } else { (0, rbits, rbits + gbits) };
+        let chromatons = [
+            Some(NAPixelChromaton { h_ss: 0, v_ss: 0, packed: true, depth: rbits, shift: rshift, comp_offs: 0, next_elem: 2}),
+            Some(NAPixelChromaton { h_ss: 0, v_ss: 0, packed: true, depth: gbits, shift: gshift, comp_offs: 0, next_elem: 2}),
+            Some(NAPixelChromaton { h_ss: 0, v_ss: 0, packed: true, depth: bbits, shift: bshift, comp_offs: 0, next_elem: 2}),
+                None, None
+        ];
+        NAPixelFormaton { model: ColorModel::RGB(RGBSubmodel::RGB),
+                          components: 3,
+                          comp_info: chromatons,
+                          elem_size: 2,
+                          be, alpha: false, palette: false }
+    }
 }
 
 impl fmt::Display for NAPixelFormaton {