pub struct YuvImage<T> {
pub pixels: T,
pub width: usize,
pub align: usize,
pub height: usize,
pub subsamp: Subsamp,
}
Expand description
A YUV (YCbCr) planar image with pixels of type T
.
This type stores an image in the JPEG color transform YCbCr (also called “YUV”). The image data first stores the Y plane, then the U (Cb) plane, and then the V (Cr) plane.
Two variants of this type are commonly used:
YuvImage<&mut [u8]>
: mutable reference to YUV image data (output image for decompression byDecompressor
).YuvImage<Vec<u8>>
: owned YUV image data (you can convert it to a reference using.as_deref()
or.as_deref_mut()
).
§Image format
The size of each image plane is determined by the width, height, chrominance subsampling and row alignment of the image:
- Luminance (Y) plane width is the image width padded to the nearest multiple of the horizontal subsampling factor.
- Luminance (Y) plane height is the image height padded to the nearest multiple of the vertical subsampling factor.
- Chrominance (U and V) plane width is the luminance plane width divided by the horizontal subsampling factor.
- Chrominance (U and V) plane height is the luminance plane height divided by the vertical subsampling factor.
- Each row is further padded to the nearest multiple of the row alignment.
§Example
For example, if the source image is 35 x 35 pixels and Sub2x1
subsampling
is used, then the luminance plane would be 36 x 35 bytes, and each of the chrominance planes
would be 18 x 35 bytes. If you specify a row alignment of 4 bytes on top of this, then the
luminance plane would be 36 x 35 bytes, and each of the chrominance planes would be 20 x 35
bytes.
let img1 = turbojpeg::YuvImage {
pixels: (),
width: 35,
align: 1,
height: 35,
subsamp: turbojpeg::Subsamp::Sub2x1,
};
assert_eq!(img1.y_size(), (36, 35));
assert_eq!(img1.uv_size(), (18, 35));
let img2 = turbojpeg::YuvImage { align: 4, ..img1 };
assert_eq!(img2.y_size(), (36, 35));
assert_eq!(img2.uv_size(), (20, 35));
Fields§
§pixels: T
Pixel data of the image (typically &mut [u8]
or Vec<u8>
).
width: usize
Width of the image in pixels (number of columns).
align: usize
Row alignment (in bytes) of the YUV image (must be a power of 2). Each row in each plane of
the YUV image will be padded to the nearest multiple of align
.
height: usize
Height of the image in pixels (number of rows).
subsamp: Subsamp
The level of chrominance subsampling used in the YUV image.
Implementations§
source§impl<T> YuvImage<T>
impl<T> YuvImage<T>
sourcepub fn as_deref(&self) -> YuvImage<&T::Target>where
T: Deref,
pub fn as_deref(&self) -> YuvImage<&T::Target>where
T: Deref,
Converts from &YuvImage<T>
to YuvImage<&T::Target>
.
In particular, you can use this to get YuvImage<&[u8]>
from YuvImage<Vec<u8>>
.
sourcepub fn as_deref_mut(&mut self) -> YuvImage<&mut T::Target>where
T: DerefMut,
pub fn as_deref_mut(&mut self) -> YuvImage<&mut T::Target>where
T: DerefMut,
Converts from &mut YuvImage<T>
to YuvImage<&mut T::Target>
.
In particular, you can use this to get YuvImage<&mut [u8]>
from YuvImage<Vec<u8>>
.
sourcepub fn y_width(&self) -> usize
pub fn y_width(&self) -> usize
Computes width of the luminance (Y) plane.
This is the image width padded to the nearest multiple of the horizontal subsampling factor and then aligned to the row alignment.
sourcepub fn y_height(&self) -> usize
pub fn y_height(&self) -> usize
Computes height of the luminance (Y) plane.
This is the image height padded to the nearest multiple of the vertical subsampling factor.
sourcepub fn uv_width(&self) -> usize
pub fn uv_width(&self) -> usize
Computes width of each chrominance (U, V) plane.
This is the Y plane width divided by the horizontal subsampling factor and then aligned to the row alignment.
sourcepub fn uv_height(&self) -> usize
pub fn uv_height(&self) -> usize
Computes height of each chrominance (U, V) plane.
This is the Y plane height divided by the vertical subsampling factor.