summaryrefslogtreecommitdiff
path: root/tvg/src/colors.rs
diff options
context:
space:
mode:
authorMicha White <botahamec@outlook.com>2023-02-13 00:40:32 -0500
committerMicha White <botahamec@outlook.com>2023-02-13 00:40:32 -0500
commit89c4981fad88c0ae8353f6e934c1ec10d51b0fd7 (patch)
tree35e3161fb03ca72aa944b39b7f1f6c2ad6463adb /tvg/src/colors.rs
parent861b467b95be55db3a42182b77dba944869bf49f (diff)
Support custom color encodings
Diffstat (limited to 'tvg/src/colors.rs')
-rw-r--r--tvg/src/colors.rs29
1 files changed, 24 insertions, 5 deletions
diff --git a/tvg/src/colors.rs b/tvg/src/colors.rs
index 10bc41c..9f4e7e5 100644
--- a/tvg/src/colors.rs
+++ b/tvg/src/colors.rs
@@ -3,6 +3,8 @@ use std::io::{self, Read};
use byteorder::{BigEndian, LittleEndian, ReadBytesExt};
use num_enum::TryFromPrimitive;
+use crate::TvgError;
+
/// The color table encodes the palette for this file.
///
/// It’s binary content is defined by the `color_encoding` field in the header.
@@ -14,12 +16,14 @@ pub struct ColorTable<C: Color> {
}
impl<C: Color> ColorTable<C> {
- /// Read in one encoding, and convert it to another
+ /// Read in one encoding, and convert it to another. If `encoding` is
+ /// [`ColorEncoding::Custom`], then return
+ /// [`TvgError::UnsupportedColorEncoding`].
pub fn read_from_encoding(
reader: &mut impl Read,
color_count: u32,
encoding: ColorEncoding,
- ) -> io::Result<Self> {
+ ) -> Result<Self, TvgError> {
Ok(match encoding {
ColorEncoding::Rgba8888 => (&ColorTable::<Rgba8888>::read(reader, color_count)?).into(),
ColorEncoding::Rgb565 => (&ColorTable::<Rgb565>::read(reader, color_count)?).into(),
@@ -28,6 +32,21 @@ impl<C: Color> ColorTable<C> {
})
}
+ /// Read in one encoding, and convert it into another. If `encoding` is
+ /// [`ColorEncoding::Custom`], then use the given encoding.
+ pub fn read_from_encoding_with_custom<Custom: Color>(
+ reader: &mut impl Read,
+ color_count: u32,
+ encoding: ColorEncoding,
+ ) -> io::Result<Self> {
+ Ok(match encoding {
+ ColorEncoding::Rgba8888 => (&ColorTable::<Rgba8888>::read(reader, color_count)?).into(),
+ ColorEncoding::Rgb565 => (&ColorTable::<Rgb565>::read(reader, color_count)?).into(),
+ ColorEncoding::RgbaF32 => (&ColorTable::<RgbaF32>::read(reader, color_count)?).into(),
+ ColorEncoding::Custom => (&ColorTable::<Custom>::read(reader, color_count)?).into(),
+ })
+ }
+
/// Parse a color table.
fn read(reader: &mut impl Read, color_count: u32) -> io::Result<Self> {
let mut colors = Vec::with_capacity(color_count as usize);
@@ -117,7 +136,7 @@ pub trait Color: Sized {
/// Each color value is encoded as a sequence of four bytes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
-struct Rgba8888 {
+pub struct Rgba8888 {
/// Red color channel between 0 and 100% intensity, mapped to byte values 0
/// to 255.
red: u8,
@@ -172,7 +191,7 @@ impl Color for Rgba8888 {
/// Each color value is encoded as a sequence of 2 bytes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
-struct Rgb565 {
+pub struct Rgb565 {
/// Red color channel between 0 and 100% intensity, mapped to integer
/// values 0 to 31.
red: u8,
@@ -224,7 +243,7 @@ impl Color for Rgb565 {
/// Each color value is encoded as a sequence of 16 bytes.
#[derive(Debug, Clone, Copy, PartialEq)]
-struct RgbaF32 {
+pub struct RgbaF32 {
/// Red color channel, using 0.0 for 0% intensity and 1.0 for 100%
/// intensity.
red: f32,