Skip to content

Commit

Permalink
Added JPEG YUV Fixed point encoding with benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed Nov 29, 2024
1 parent 2c986d3 commit 1590564
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 15 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,7 @@ harness = false
path = "benches/blur.rs"
name = "blur"
harness = false

[[bench]]
name = "fixed_point"
harness = false
74 changes: 74 additions & 0 deletions benches/fixed_point.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use criterion::{criterion_group, criterion_main, Criterion};
use image::{ImageBuffer, Rgb};

pub fn bench_jpeg_fixed_point(c: &mut Criterion) {
let width = 1920;
let height = 1920;
let src = ImageBuffer::from_pixel(width, height, Rgb([213u8, 156, 64]));

c.bench_function("Test JPEG floating point", |b| {
let mut y_plane = vec![0u8; width as usize * height as usize];
let mut u_plane = vec![0u8; width as usize * height as usize];
let mut v_plane = vec![0u8; width as usize * height as usize];
b.iter(|| {
for (((rgb, y_dst), u_dst), v_dst) in src
.chunks_exact(3)
.zip(y_plane.iter_mut())
.zip(u_plane.iter_mut())
.zip(v_plane.iter_mut())
{
let r: f32 = rgb[0] as f32;
let g: f32 = rgb[1] as f32;
let b: f32 = rgb[2] as f32;

// Coefficients from JPEG File Interchange Format (Version 1.02), multiplied for 255 maximum.
let y = 0.299 * r + 0.587 * g + 0.114 * b;
let cb = -0.1687 * r - 0.3313 * g + 0.5 * b + 128.;
let cr = 0.5 * r - 0.4187 * g - 0.0813 * b + 128.;
*y_dst = y as u8;
*u_dst = cb as u8;
*v_dst = cr as u8;
}
});
});

c.bench_function("Test JPEG fixed point", |b| {
let mut y_plane = vec![0u8; width as usize * height as usize];
let mut u_plane = vec![0u8; width as usize * height as usize];
let mut v_plane = vec![0u8; width as usize * height as usize];
b.iter(|| {
for (((rgb, y_dst), u_dst), v_dst) in src
.chunks_exact(3)
.zip(y_plane.iter_mut())
.zip(u_plane.iter_mut())
.zip(v_plane.iter_mut())
{
let r = rgb[0] as i32;
let g = rgb[1] as i32;
let b = rgb[2] as i32;
const C_YR: i32 = 19595; // 0.29900 = 19595 * 2^-16
const C_YG: i32 = 38469; // 0.58700 = 38469 * 2^-16
const C_YB: i32 = 7471; // 0.11400 = 7471 * 2^-16
const Y_ROUNDING: i32 = 1 << 15; // + 0.5 to perform rounding shift right in-place
const C_UR: i32 = 11059; // 0.16874 = 11059 * 2^-16
const C_UG: i32 = 21709; // 0.33126 = 21709 * 2^-16
const C_UB: i32 = 32768; // 0.5 = 32768 * 2^-16
const UV_BIAS_ROUNDING: i32 = (128 * (1 << 16)) + (1 << 15); // 128 + 0.5 = ((128 * (1 << 16)) + (1 << 15)) * 2^-16 ; + 0.5 to perform rounding shift right in-place
const C_VR: i32 = C_UB; // 0.5 = 32768 * 2^-16
const C_VG: i32 = 27439; // 0.41869 = 27439 * 2^-16
const C_VB: i32 = 5329; // 0.08131409 = 5329 * 2^-16

let y = (C_YR * r + C_YG * g + C_YB * b + Y_ROUNDING) >> 16;
let cb = (-C_UR * r - C_UG * g + C_UB * b + UV_BIAS_ROUNDING) >> 16;
let cr = (C_VR * r - C_VG * g - C_VB * b + UV_BIAS_ROUNDING) >> 16;

*y_dst = y as u8;
*u_dst = cb as u8;
*v_dst = cr as u8;
}
});
});
}

criterion_group!(benches, bench_jpeg_fixed_point);
criterion_main!(benches);
47 changes: 32 additions & 15 deletions src/codecs/jpeg/encoder.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#![allow(clippy::too_many_arguments)]

use std::borrow::Cow;
use std::io::{self, Write};

use crate::error::{
ImageError, ImageResult, ParameterError, ParameterErrorKind, UnsupportedError,
UnsupportedErrorKind,
};
use crate::image::{ImageEncoder, ImageFormat};
use crate::utils::clamp;
use crate::{ExtendedColorType, GenericImageView, ImageBuffer, Luma, Pixel, Rgb};
use num_traits::ToPrimitive;
use std::borrow::Cow;
use std::io::{self, Write};

use super::entropy::build_huff_lut_const;
use super::transform;
Expand Down Expand Up @@ -771,19 +771,36 @@ fn encode_coefficient(coefficient: i32) -> (u8, u16) {

#[inline]
fn rgb_to_ycbcr<P: Pixel>(pixel: P) -> (u8, u8, u8) {
use crate::traits::Primitive;
use num_traits::cast::ToPrimitive;

let [r, g, b] = pixel.to_rgb().0;
let max: f32 = P::Subpixel::DEFAULT_MAX_VALUE.to_f32().unwrap();
let r: f32 = r.to_f32().unwrap();
let g: f32 = g.to_f32().unwrap();
let b: f32 = b.to_f32().unwrap();

// Coefficients from JPEG File Interchange Format (Version 1.02), multiplied for 255 maximum.
let y = 76.245 / max * r + 149.685 / max * g + 29.07 / max * b;
let cb = -43.0185 / max * r - 84.4815 / max * g + 127.5 / max * b + 128.;
let cr = 127.5 / max * r - 106.7685 / max * g - 20.7315 / max * b + 128.;
let r: i32 = r.to_u8().unwrap() as i32;
let g: i32 = g.to_u8().unwrap() as i32;
let b: i32 = b.to_u8().unwrap() as i32;

/*
JPEG RGB -> YCbCr is defined as following equations using Bt.601 Full Range matrix:
Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + 128
Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + 128
To avoid using slow floating point conversion is done in fixed point,
using following coefficients with rounding to nearest integer mode:
*/

const C_YR: i32 = 19595; // 0.29900 = 19595 * 2^-16
const C_YG: i32 = 38469; // 0.58700 = 38469 * 2^-16
const C_YB: i32 = 7471; // 0.11400 = 7471 * 2^-16
const Y_ROUNDING: i32 = 1 << 15; // + 0.5 to perform rounding shift right in-place
const C_UR: i32 = 11059; // 0.16874 = 11059 * 2^-16
const C_UG: i32 = 21709; // 0.33126 = 21709 * 2^-16
const C_UB: i32 = 32768; // 0.5 = 32768 * 2^-16
const UV_BIAS_ROUNDING: i32 = (128 * (1 << 16)) + (1 << 15); // 128 + 0.5 = ((128 * (1 << 16)) + (1 << 15)) * 2^-16 ; + 0.5 to perform rounding shift right in-place
const C_VR: i32 = C_UB; // 0.5 = 32768 * 2^-16
const C_VG: i32 = 27439; // 0.41869 = 27439 * 2^-16
const C_VB: i32 = 5329; // 0.08131409 = 5329 * 2^-16

let y = (C_YR * r + C_YG * g + C_YB * b + Y_ROUNDING) >> 16;
let cb = (-C_UR * r - C_UG * g + C_UB * b + UV_BIAS_ROUNDING) >> 16;
let cr = (C_VR * r - C_VG * g - C_VB * b + UV_BIAS_ROUNDING) >> 16;

(y as u8, cb as u8, cr as u8)
}
Expand Down

0 comments on commit 1590564

Please sign in to comment.