Skip to content

Commit

Permalink
ycbcr -> rgb simd
Browse files Browse the repository at this point in the history
  • Loading branch information
friendlymatthew committed Jun 18, 2024
1 parent 3733490 commit be95539
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
36 changes: 36 additions & 0 deletions src/color_spaces.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::simd::Simd;

use crate::color_spaces::ColorSpace::RGB;

type MCU = (Simd<f32, 64>, Simd<f32, 64>, Simd<f32, 64>);

#[derive(Debug, PartialEq, Clone, Copy)]
pub(crate) enum ColorSpace {
YCbCr(f32, f32, f32),
RGB(f32, f32, f32),
}

impl ColorSpace {
pub(crate) fn convert_ycbcr_to_rgb(image_data: Vec<MCU>) -> Vec<Self> {
let mut rgbs = vec![];

image_data.iter().for_each(|(ys, cbs, crs)| {
let cbs = cbs - Simd::splat(128.0);
let crs = crs - Simd::splat(128.0);

let rs = ys + Simd::splat(1.402) * crs;
let gs = ys - Simd::splat(0.344136) * cbs - Simd::splat(0.714136) * crs;
let bs = ys + Simd::splat(1.772) * cbs;

rgbs = rs
.to_array()
.iter()
.zip(gs.to_array().iter())
.zip(bs.to_array().iter())
.map(|((r, g), b)| RGB(*r, *g, *b))
.collect();
});

rgbs
}
}
14 changes: 4 additions & 10 deletions src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use rayon::iter::ParallelIterator;

use crate::bitreader::BitReader;
use crate::coding::{CodingProcess, EntropyCoding};
use crate::color_spaces::ColorSpace;
use crate::dequantizer::Dequantizer;
use crate::entropy_decoder::EntropyDecoder;
use crate::format::Format;
use crate::frame_header::Component;
use crate::huffman_tree::HuffmanClass;
use crate::idct::IDCT;
Expand Down Expand Up @@ -167,7 +167,7 @@ impl Decoder {
Ok(Parser::new(self.mmap.to_vec(), marlen_map, self.encoding))
}

pub fn decode(&mut self) -> Result<Vec<Format>> {
pub fn decode(&mut self) -> Result<Vec<ColorSpace>> {
let parser = self.setup()?;

let code_schema = self.encoding.schema();
Expand Down Expand Up @@ -287,15 +287,9 @@ impl Decoder {
image_data.push((res[0], res[1], res[2]))
}

let mut ycbcrs = vec![];
let raw_image_data = ColorSpace::convert_ycbcr_to_rgb(image_data);

for (y, cb, cr) in image_data {
for i in 0..64 {
ycbcrs.push(Format::YCbCr(y[i], cb[i], cr[i]));
}
}

Ok(ycbcrs)
Ok(raw_image_data)
}
_ => todo!(),
}
Expand Down
5 changes: 0 additions & 5 deletions src/format.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ pub mod decoder;

mod bitreader;
mod coding;
mod color_spaces;
mod dequantizer;
mod entropy_decoder;
mod format;
pub(crate) mod frame_header;
pub(crate) mod huffman_tree;
mod idct;
Expand Down

0 comments on commit be95539

Please sign in to comment.