use super::constants::{
COLOR_TYPE_RGB8, COLOR_TYPE_RGBA8, WEBP_ALPHA_FLAG, WEBP_VP8, WEBP_VP8L, WEBP_VP8X,
has_webp_signature,
};
pub fn extract_compression(webp_data_ref: &[u8]) -> Option<String> {
if webp_data_ref.len() >= 16 && has_webp_signature(webp_data_ref) {
let chunk_type = &webp_data_ref[12..16];
match chunk_type {
chunk if chunk == WEBP_VP8 => Some("VP8 (lossy)".to_string()),
chunk if chunk == WEBP_VP8L => Some("VP8L (lossless)".to_string()),
chunk if chunk == WEBP_VP8X => Some("VP8X (extended)".to_string()),
_ => Some("WebP".to_string()), }
} else {
None
}
}
pub fn extract_color_type(webp_data_ref: &[u8]) -> Option<String> {
if webp_data_ref.len() < 16 {
return None;
}
if !has_webp_signature(webp_data_ref) {
return None;
}
let chunk_type = &webp_data_ref[12..16];
match chunk_type {
chunk if chunk == WEBP_VP8 => Some(COLOR_TYPE_RGB8.to_string()), chunk if chunk == WEBP_VP8L => {
if webp_data_ref.len() >= 21 {
let flags = webp_data_ref[20];
if (flags & WEBP_ALPHA_FLAG) != 0 {
Some(COLOR_TYPE_RGBA8.to_string())
} else {
Some(COLOR_TYPE_RGB8.to_string())
}
} else {
Some(COLOR_TYPE_RGB8.to_string()) }
}
chunk if chunk == WEBP_VP8X => {
if webp_data_ref.len() >= 21 {
let flags = webp_data_ref[20];
if (flags & WEBP_ALPHA_FLAG) != 0 {
Some(COLOR_TYPE_RGBA8.to_string())
} else {
Some(COLOR_TYPE_RGB8.to_string())
}
} else {
Some(COLOR_TYPE_RGB8.to_string()) }
}
_ => Some(COLOR_TYPE_RGB8.to_string()), }
}