Skip to content

add color type conversion #2444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
change to trait method
  • Loading branch information
Its-Just-Nans committed Apr 23, 2025
commit ed0fb680f5dd88334388ebf46cd426dea9552b09
36 changes: 36 additions & 0 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,34 @@ impl ExtendedColorType {
}
}

/// Returns the ColorType that is equivalent to this ExtendedColorType.
pub fn color_type(&self) -> Option<ColorType> {
match *self {
ExtendedColorType::A8 => Some(ColorType::L8),
ExtendedColorType::L1 => Some(ColorType::L8),
ExtendedColorType::La1 => Some(ColorType::La8),
ExtendedColorType::Rgb1 => Some(ColorType::Rgb8),
ExtendedColorType::Rgba1 => Some(ColorType::Rgba8),
ExtendedColorType::L2 => Some(ColorType::L8),
ExtendedColorType::La2 => Some(ColorType::La8),
ExtendedColorType::Rgb2 => Some(ColorType::Rgb8),
ExtendedColorType::Rgba2 => Some(ColorType::Rgba8),
ExtendedColorType::L4 => Some(ColorType::L8),
ExtendedColorType::La4 => Some(ColorType::La8),
ExtendedColorType::Rgb4 => Some(ColorType::Rgb8),
ExtendedColorType::Rgba4 => Some(ColorType::Rgba8),
ExtendedColorType::L8 => Some(ColorType::L8),
ExtendedColorType::La8 => Some(ColorType::La8),
ExtendedColorType::Rgb8 => Some(ColorType::Rgb8),
ExtendedColorType::Rgba8 => Some(ColorType::Rgba8),
ExtendedColorType::L16 => Some(ColorType::L16),
ExtendedColorType::La16 => Some(ColorType::La16),
ExtendedColorType::Rgb16 => Some(ColorType::Rgb16),
ExtendedColorType::Rgba16 => Some(ColorType::Rgba16),
_ => None,
}
}

/// Returns the number of bytes required to hold a width x height image of this color type.
pub(crate) fn buffer_size(self, width: u32, height: u32) -> u64 {
let bpp = self.bits_per_pixel() as u64;
Expand All @@ -252,6 +280,14 @@ impl From<ColorType> for ExtendedColorType {
}
}

impl TryInto<ColorType> for ExtendedColorType {
type Error = ();

fn try_into(self) -> Result<ColorType, Self::Error> {
self.color_type().ok_or(())
}
}

macro_rules! define_colors {
{$(
$(#[$doc:meta])*
Expand Down
14 changes: 1 addition & 13 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use num_traits::{Bounded, Num, NumCast};
use std::ops::AddAssign;

use crate::color::{Luma, LumaA, Rgb, Rgba};
use crate::{ColorType, ExtendedColorType};
use crate::ExtendedColorType;

/// Types which are safe to treat as an immutable byte slice in a pixel layout
/// for image encoding.
Expand Down Expand Up @@ -175,51 +175,39 @@ pub trait PixelWithColorType: Pixel + private::SealedPixelWithColorType {
/// This is needed for automatically detecting
/// a color format when saving an image as a file.
const COLOR_TYPE: ExtendedColorType;
/// The simple `ColorType` of this pixel.
const SIMPLE_COLOR_TYPE: ColorType;
}

impl PixelWithColorType for Rgb<u8> {
const COLOR_TYPE: ExtendedColorType = ExtendedColorType::Rgb8;
const SIMPLE_COLOR_TYPE: ColorType = ColorType::Rgb8;
}
impl PixelWithColorType for Rgb<u16> {
const COLOR_TYPE: ExtendedColorType = ExtendedColorType::Rgb16;
const SIMPLE_COLOR_TYPE: ColorType = ColorType::Rgb16;
}
impl PixelWithColorType for Rgb<f32> {
const COLOR_TYPE: ExtendedColorType = ExtendedColorType::Rgb32F;
const SIMPLE_COLOR_TYPE: ColorType = ColorType::Rgb32F;
}

impl PixelWithColorType for Rgba<u8> {
const COLOR_TYPE: ExtendedColorType = ExtendedColorType::Rgba8;
const SIMPLE_COLOR_TYPE: ColorType = ColorType::Rgba8;
}
impl PixelWithColorType for Rgba<u16> {
const COLOR_TYPE: ExtendedColorType = ExtendedColorType::Rgba16;
const SIMPLE_COLOR_TYPE: ColorType = ColorType::Rgba16;
}
impl PixelWithColorType for Rgba<f32> {
const COLOR_TYPE: ExtendedColorType = ExtendedColorType::Rgba32F;
const SIMPLE_COLOR_TYPE: ColorType = ColorType::Rgba32F;
}

impl PixelWithColorType for Luma<u8> {
const COLOR_TYPE: ExtendedColorType = ExtendedColorType::L8;
const SIMPLE_COLOR_TYPE: ColorType = ColorType::L8;
}
impl PixelWithColorType for Luma<u16> {
const COLOR_TYPE: ExtendedColorType = ExtendedColorType::L16;
const SIMPLE_COLOR_TYPE: ColorType = ColorType::L16;
}
impl PixelWithColorType for LumaA<u8> {
const COLOR_TYPE: ExtendedColorType = ExtendedColorType::La8;
const SIMPLE_COLOR_TYPE: ColorType = ColorType::La8;
}
impl PixelWithColorType for LumaA<u16> {
const COLOR_TYPE: ExtendedColorType = ExtendedColorType::La16;
const SIMPLE_COLOR_TYPE: ColorType = ColorType::La16;
}

/// Prevents down-stream users from implementing the `Primitive` trait
Expand Down
Loading