[][src]Function dcv_color_primitives::convert_image

pub fn convert_image(
    width: u32,
    height: u32,
    src_format: &ImageFormat,
    src_strides: Option<&[usize]>,
    src_buffers: &[&[u8]],
    dst_format: &ImageFormat,
    dst_strides: Option<&[usize]>,
    dst_buffers: &mut [&mut [u8]]
) -> Result<(), ErrorKind>

Converts from a color space to another one, applying downsampling/upsampling to match destination image format.

Arguments

  • width - Width of the image to convert in pixels
  • height - Height of the image to convert in pixels
  • src_format - Source image format
  • src_strides - An array of distances in bytes between starts of consecutive lines in each source image planes
  • src_buffers - An array of image buffers in each source color plane
  • dst_format - Destination image format
  • dst_strides - An array of distances in bytes between starts of consecutive lines in each destination image planes
  • dst_buffers - An array of image buffers in each destination color plane

Errors

  • NotInitialized if the library was not initialized before

  • InvalidValue if width or height violate the size constraints that might by imposed by the source and destination image pixel formats

  • InvalidValue if source or destination image formats have a number of planes which is not compatible with their pixel formats

  • InvalidOperation if there is no available method to convert the image with the source pixel format to the image with the destination pixel format.

    The list of available conversions is specified here:

    Source image pixel formatSupported destination image pixel formats
    PixelFormat::ArgbPixelFormat::Nv12 1
    PixelFormat::BgraPixelFormat::Nv12 1
    PixelFormat::BgrPixelFormat::Nv12 1
    PixelFormat::Nv12PixelFormat::Bgra 2
  • NotEnoughData if the source stride array is not None and its length is less than the source image format number of planes

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

  • NotEnoughData if one or more source/destination buffers does not provide enough data.

    The minimum number of bytes to provide for each buffer depends from the image format, dimensions, and strides (if they are not None).

    You can compute the buffers' size using get_buffers_size

Algorithm 1

Conversion from linear RGB model to YCbCr color model, with 4:2:0 downsampling

If the destination image color space is Bt601, the following formula is applied:

y  =  0.257 * r + 0.504 * g + 0.098 * b + 16
cb = -0.148 * r - 0.291 * g + 0.439 * b + 128
cr =  0.439 * r - 0.368 * g - 0.071 * b + 128

If the destination image color space is Bt709, the following formula is applied:

y  =  0.213 * r + 0.715 * g + 0.072 * b + 16
cb = -0.117 * r - 0.394 * g + 0.511 * b + 128
cr =  0.511 * r - 0.464 * g - 0.047 * b + 128

Algorithm 2

Conversion from YCbCr model to linear RGB model, with 4:4:4 upsampling

If the destination image contains an alpha channel, each component will be set to 255

If the source image color space is Bt601, the following formula is applied:

r = 1.164 * (y - 16) + 1.596 * (cr - 128)
g = 1.164 * (y - 16) - 0.813 * (cr - 128) - 0.392 * (cb - 128)
b = 1.164 * (y - 16) + 2.017 * (cb - 128)

If the source image color space is Bt709, the following formula is applied:

r = 1.164 * (y - 16) + 1.793 * (cr - 128)
g = 1.164 * (y - 16) - 0.534 * (cr - 128) - 0.213 * (cb - 128)
b = 1.164 * (y - 16) + 2.115 * (cb - 128)