Skip to main content

vtcode_commons/
image.rs

1//! Image processing utilities
2
3use anyhow::Result;
4use base64::Engine;
5use std::path::Path;
6
7/// Represents the data from an image file ready for LLM consumption
8#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
9pub struct ImageData {
10    /// Base64-encoded image data
11    pub base64_data: String,
12
13    /// MIME type of the image (e.g., "image/png", "image/jpeg")
14    pub mime_type: String,
15
16    /// Original file path or URL
17    pub file_path: String,
18
19    /// File size in bytes
20    pub size: u64,
21}
22
23/// Detects MIME type from Content-Type header
24pub fn detect_mime_type_from_content_type(content_type: &str) -> Option<String> {
25    let content_type = content_type.to_lowercase();
26    if content_type.starts_with("image/png") {
27        Some("image/png".to_string())
28    } else if content_type.starts_with("image/jpeg") || content_type.starts_with("image/jpg") {
29        Some("image/jpeg".to_string())
30    } else if content_type.starts_with("image/gif") {
31        Some("image/gif".to_string())
32    } else if content_type.starts_with("image/webp") {
33        Some("image/webp".to_string())
34    } else if content_type.starts_with("image/bmp") {
35        Some("image/bmp".to_string())
36    } else if content_type.starts_with("image/tiff") || content_type.starts_with("image/tif") {
37        Some("image/tiff".to_string())
38    } else if content_type.starts_with("image/svg") {
39        Some("image/svg+xml".to_string())
40    } else {
41        None
42    }
43}
44
45/// Detects MIME type from file data (magic bytes)
46pub fn detect_mime_type_from_data(data: &[u8]) -> String {
47    // JPEG magic bytes: starts with FF D8
48    if data.len() >= 2 && data[0] == 0xFF && data[1] == 0xD8 {
49        return "image/jpeg".to_string();
50    }
51
52    // Need at least 8 bytes for other formats
53    if data.len() < 8 {
54        return "image/png".to_string();
55    }
56
57    match &data[..8] {
58        [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A] => "image/png".to_string(),
59        [0x47, 0x49, 0x46, 0x38, _, _, _, _] => {
60            if data.len() >= 12 && &data[8..12] == b"WEBP" {
61                "image/webp".to_string()
62            } else {
63                "image/gif".to_string()
64            }
65        }
66        [0x52, 0x49, 0x46, 0x46, _, _, _, _] => {
67            if data.len() >= 12 && &data[8..12] == b"WEBP" {
68                "image/webp".to_string()
69            } else {
70                "image/png".to_string()
71            }
72        }
73        [0x42, 0x4D, _, _] => "image/bmp".to_string(),
74        _ => "image/png".to_string(),
75    }
76}
77
78/// Detects the MIME type based on file extension
79pub fn detect_mime_type_from_extension(path: &Path) -> Result<String> {
80    let extension = path
81        .extension()
82        .and_then(|ext| ext.to_str())
83        .unwrap_or("")
84        .to_lowercase();
85
86    let mime_type = match extension.as_str() {
87        "png" => "image/png",
88        "jpg" | "jpeg" => "image/jpeg",
89        "gif" => "image/gif",
90        "webp" => "image/webp",
91        "bmp" => "image/bmp",
92        "tiff" | "tif" => "image/tiff",
93        "svg" => "image/svg+xml",
94        _ => return Err(anyhow::anyhow!("Unsupported image format: {}", extension)),
95    };
96
97    Ok(mime_type.to_string())
98}
99
100/// Validates that the image file path has a supported extension
101pub fn has_supported_image_extension(path: &Path) -> bool {
102    let extension = path
103        .extension()
104        .and_then(|ext| ext.to_str())
105        .unwrap_or("")
106        .to_lowercase();
107
108    const VALID_EXTENSIONS: &[&str] = &["png", "jpg", "jpeg", "gif", "webp", "bmp", "tiff", "svg"];
109    VALID_EXTENSIONS.contains(&extension.as_str())
110}
111
112/// Encodes binary data to base64
113pub fn encode_to_base64(data: &[u8]) -> String {
114    base64::engine::general_purpose::STANDARD.encode(data)
115}