[][src]Function dcv_color_primitives::get_buffers_size

pub fn get_buffers_size(
    width: u32,
    height: u32,
    format: &ImageFormat,
    strides: Option<&[usize]>,
    buffers_size: &mut [usize]
) -> Result<(), ErrorKind>

Compute number of bytes required to store an image given its format, dimensions and optionally its strides

Arguments

  • width - Width of the image in pixels
  • height - Height of the image in pixels
  • format - Image format
  • strides - An array of distances in bytes between starts of consecutive lines in each image planes
  • buffers_size - An array describing the minimum number of bytes required in each image planes

Examples

Compute how many bytes are needed to store and image of a given format and size assuming all planes contain data which is tightly packed:

use dcv_color_primitives as dcp;
use dcp::{get_buffers_size, ColorSpace, ImageFormat, PixelFormat};
use std::error;

fn compute_size_packed() -> Result<(), Box<dyn error::Error>> {
    dcp::initialize();

    const WIDTH: u32 = 640;
    const HEIGHT: u32 = 480;
    const NUM_PLANES: u32 = 2;

    let format = ImageFormat {
        pixel_format: PixelFormat::Nv12,
        color_space: ColorSpace::Bt601,
        num_planes: NUM_PLANES,
    };

    let sizes: &mut [usize] = &mut [0usize; NUM_PLANES as usize];
    get_buffers_size(WIDTH, HEIGHT, &format, None, sizes)?;

    Ok(())
}

Compute how many bytes are needed to store and image of a given format and size in which all planes have custom strides:

use dcv_color_primitives as dcp;
use dcp::{get_buffers_size, ColorSpace, ImageFormat, PixelFormat};
use std::error;

fn compute_size_custom_strides() -> Result<(), Box<dyn error::Error>> {
    dcp::initialize();

    const WIDTH: u32 = 640;
    const HEIGHT: u32 = 480;
    const NUM_PLANES: u32 = 2;
    const Y_STRIDE: usize = (WIDTH as usize) + 1;
    const UV_STRIDE: usize = (WIDTH as usize) + 3;

    let format = ImageFormat {
        pixel_format: PixelFormat::Nv12,
        color_space: ColorSpace::Bt601,
        num_planes: NUM_PLANES,
    };

    let strides: &[usize] = &[ Y_STRIDE, UV_STRIDE, ];
    let sizes: &mut [usize] = &mut [0usize; NUM_PLANES as usize];
    get_buffers_size(WIDTH, HEIGHT, &format, Some(strides), sizes)?;

    Ok(())
}

Compute how many bytes are needed to store and image of a given format and size in which some planes have custom strides, while some other are assumed to contain data which is tightly packed:

use dcv_color_primitives as dcp;
use dcp::{get_buffers_size, ColorSpace, ImageFormat, PixelFormat, STRIDE_AUTO};
use std::error;

fn compute_size_custom_strides() -> Result<(), Box<dyn error::Error>> {
    dcp::initialize();

    const WIDTH: u32 = 640;
    const HEIGHT: u32 = 480;
    const NUM_PLANES: u32 = 2;
    const Y_STRIDE: usize = (WIDTH as usize) + 1;

    let format = ImageFormat {
        pixel_format: PixelFormat::Nv12,
        color_space: ColorSpace::Bt601,
        num_planes: NUM_PLANES,
    };

    let strides: &[usize] = &[ Y_STRIDE, STRIDE_AUTO, ];
    let sizes: &mut [usize] = &mut [0usize; NUM_PLANES as usize];
    get_buffers_size(WIDTH, HEIGHT, &format, Some(strides), sizes)?;

    Ok(())
}

Default strides (e.g. the one you would set for tightly packed data) can be set using the constant STRIDE_AUTO

Errors

  • InvalidValue if width or height violate the size constraints that might by imposed by the image pixel format

  • InvalidValue if the image format has a number of planes which is not compatible with its pixel format

  • NotEnoughData if the strides array is not None and its length is less than the image format number of planes

  • NotEnoughData if the buffers_sizes array is not None and its length is less than the image format number of planes