Function dcv_color_primitives::convert_image
source · 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>
Expand description
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 pixelsheight
- Height of the image to convert in pixelssrc_format
- Source image formatsrc_strides
- An array of distances in bytes between starts of consecutive lines in each source image planessrc_buffers
- An array of image buffers in each source color planedst_format
- Destination image formatdst_strides
- An array of distances in bytes between starts of consecutive lines in each destination image planesdst_buffers
- An array of image buffers in each destination color plane
Errors
-
InvalidValue
ifwidth
orheight
violate thesize 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 format Supported destination image pixel formats PixelFormat::Argb
PixelFormat::I420
1
PixelFormat::Argb
PixelFormat::I444
1
PixelFormat::Argb
PixelFormat::Nv12
1
PixelFormat::Bgra
PixelFormat::I420
1
PixelFormat::Bgra
PixelFormat::I444
1
PixelFormat::Bgra
PixelFormat::Nv12
1
PixelFormat::Bgra
PixelFormat::Rgb
[4
]PixelFormat::Bgr
PixelFormat::I420
1
PixelFormat::Bgr
PixelFormat::I444
1
PixelFormat::Bgr
PixelFormat::Nv12
1
PixelFormat::Bgr
PixelFormat::Rgb
[5
]PixelFormat::I420
PixelFormat::Bgra
,PixelFormat::Rgba
2
PixelFormat::I444
PixelFormat::Bgra
,PixelFormat::Rgba
2
PixelFormat::Nv12
PixelFormat::Bgra
,PixelFormat::Rgb
,PixelFormat::Rgba
2
PixelFormat::Rgb
PixelFormat::Bgra
3
-
NotEnoughData
if the source stride array is notNone
and its length is less than the source image format number of planes -
NotEnoughData
if the destination stride array is notNone
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
If the destination image color space is Bt601FR, the following formula is applied:
```text
y = 0.299 * r + 0.587 * g + 0.114 * b
cb = -0.169 * r - 0.331 * g + 0.500 * b + 128
cr = 0.500 * r - 0.419 * g - 0.081 * b + 128
If the destination image color space is Bt709FR, the following formula is applied:
```text
y = 0.213 * r + 0.715 * g + 0.072 * b
cb = -0.115 * r - 0.385 * g + 0.500 * b + 128
cr = 0.500 * r - 0.454 * g - 0.046 * 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)
If the source image color space is Bt601FR, the following formula is applied:
```text
r = y + 1.402 * (cr - 128)
g = y - 0.714 * (cr - 128) - 0.344 * (cb - 128)
b = y + 1.772 * (cb - 128)
If the source image color space is Bt709FR, the following formula is applied:
```text
r = y + 1.575 * (cr - 128)
g = y - 0.468 * (cr - 128) - 0.187 * (cb - 128)
b = y + 1.856 * (cb - 128)
Algorithm 3
Conversion from RGB to BGRA
Algorithm 4
Conversion from BGRA to RGB
Algorithm 5
Conversion from BGR to RGB