use super::color_plan::{YuvFrame, plan_yuv_frame};
use super::color_validation::{
named_planes, rgb_frame_size, rgb_shape, rgb24_frame_size, validate_out,
validate_written_planes,
};
use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::ops::traits::{ConvLayout, YuvPlanes, YuvSpec};
use crate::runtime::Runtime;
use crate::tensor::Tensor;
pub fn check_float<F: Fn(DType) -> bool>(
dtype: DType,
float_ok: F,
op: &'static str,
) -> Result<()> {
if float_ok(dtype) {
Ok(())
} else {
Err(Error::UnsupportedDType { dtype, op })
}
}
pub fn plan_decode<R: Runtime<DType = DType>, F: Fn(DType) -> bool>(
planes: &YuvPlanes<'_, R>,
spec: &YuvSpec,
out_dtype: DType,
float_ok: F,
op: &'static str,
) -> Result<YuvFrame> {
let frame = plan_yuv_frame(planes, spec, op)?;
check_float(out_dtype, float_ok, op)?;
Ok(frame)
}
pub fn plan_decode_into<R: Runtime<DType = DType>, F: Fn(DType) -> bool>(
out: &Tensor<R>,
planes: &YuvPlanes<'_, R>,
spec: &YuvSpec,
layout: ConvLayout,
float_ok: F,
op: &'static str,
) -> Result<YuvFrame> {
let frame = plan_decode(planes, spec, out.dtype(), float_ok, op)?;
let want = rgb_shape(layout, frame.height, frame.width);
validate_out(out, &want, &named_planes(planes), op)?;
Ok(frame)
}
pub fn plan_encode<R: Runtime<DType = DType>, F: Fn(DType) -> bool>(
rgb: &Tensor<R>,
planes: &YuvPlanes<'_, R>,
spec: &YuvSpec,
layout: ConvLayout,
float_ok: F,
op: &'static str,
) -> Result<YuvFrame> {
let frame = plan_yuv_frame(planes, spec, op)?;
let size = rgb_frame_size(rgb, layout, "rgb", op)?;
if size != [frame.height, frame.width] {
let want = rgb_shape(layout, frame.height, frame.width);
return Err(Error::shape_mismatch(&want, rgb.shape()));
}
check_float(rgb.dtype(), float_ok, op)?;
validate_written_planes(planes, rgb, op)?;
Ok(frame)
}
pub fn plan_unpack<R: Runtime<DType = DType>, F: Fn(DType) -> bool>(
rgb24: &Tensor<R>,
out_dtype: DType,
float_ok: F,
op: &'static str,
) -> Result<[usize; 2]> {
let size = rgb24_frame_size(rgb24, "rgb24", op)?;
check_float(out_dtype, float_ok, op)?;
Ok(size)
}
pub fn plan_unpack_into<R: Runtime<DType = DType>, F: Fn(DType) -> bool>(
out: &Tensor<R>,
rgb24: &Tensor<R>,
layout: ConvLayout,
float_ok: F,
op: &'static str,
) -> Result<[usize; 2]> {
let [h, w] = plan_unpack(rgb24, out.dtype(), float_ok, op)?;
validate_out(out, &rgb_shape(layout, h, w), &[(rgb24, "rgb24")], op)?;
Ok([h, w])
}
pub fn plan_pack<R: Runtime<DType = DType>, F: Fn(DType) -> bool>(
rgb: &Tensor<R>,
layout: ConvLayout,
float_ok: F,
op: &'static str,
) -> Result<[usize; 2]> {
let size = rgb_frame_size(rgb, layout, "rgb", op)?;
check_float(rgb.dtype(), float_ok, op)?;
Ok(size)
}
pub fn plan_pack_into<R: Runtime<DType = DType>, F: Fn(DType) -> bool>(
out: &Tensor<R>,
rgb: &Tensor<R>,
layout: ConvLayout,
float_ok: F,
op: &'static str,
) -> Result<[usize; 2]> {
let [h, w] = plan_pack(rgb, layout, float_ok, op)?;
if out.dtype() != DType::U8 {
return Err(Error::InvalidArgument {
arg: "out",
reason: format!(
"{op}: out has dtype {:?}, expected U8; pass a U8 [H, W, 3] view",
out.dtype()
),
});
}
validate_out(out, &[h, w, 3], &[(rgb, "rgb")], op)?;
Ok([h, w])
}