Trait jpeg_encoder::ImageBuffer
source · pub trait ImageBuffer {
// Required methods
fn get_jpeg_color_type(&self) -> JpegColorType;
fn width(&self) -> u16;
fn height(&self) -> u16;
fn fill_buffers(&self, y: u16, buffers: &mut [Vec<u8>; 4]);
}
Expand description
Buffer used as input value for image encoding
Image encoding with Encoder::encode_image needs an ImageBuffer as input for the image data. For convenience the Encoder::encode function contains implementations for common byte based pixel formats. Users that needs other pixel formats or don’t have the data available as byte slices can create their own buffer implementations.
Example: ImageBuffer implementation for RgbImage from the image
crate
use image::RgbImage;
use jpeg_encoder::{ImageBuffer, JpegColorType, rgb_to_ycbcr};
pub struct RgbImageBuffer {
image: RgbImage,
}
impl ImageBuffer for RgbImageBuffer {
fn get_jpeg_color_type(&self) -> JpegColorType {
// Rgb images are encoded as YCbCr in JFIF files
JpegColorType::Ycbcr
}
fn width(&self) -> u16 {
self.image.width() as u16
}
fn height(&self) -> u16 {
self.image.height() as u16
}
fn fill_buffers(&self, y: u16, buffers: &mut [Vec<u8>; 4]){
for x in 0..self.width() {
let pixel = self.image.get_pixel(x as u32 ,y as u32);
let (y,cb,cr) = rgb_to_ycbcr(pixel[0], pixel[1], pixel[2]);
// For YCbCr the 4th buffer is not used
buffers[0].push(y);
buffers[1].push(cb);
buffers[2].push(cr);
}
}
}
Required Methods§
sourcefn get_jpeg_color_type(&self) -> JpegColorType
fn get_jpeg_color_type(&self) -> JpegColorType
The color type used in the image encoding