raw img + background png convert

This commit is contained in:
2026-03-08 15:17:30 +01:00
parent 05ced3b7ea
commit e038981d7f
7 changed files with 86 additions and 57 deletions

View File

@ -1,4 +1,6 @@
use crate::models::{ClipboardData, ClipboardEntry, Image};
use image::codecs::png::PngEncoder;
use image::{ExtendedColorType, ImageEncoder};
use rusqlite::Connection;
use std::error::Error;
use std::fs;
@ -43,11 +45,17 @@ impl Database {
let (entry_type, content) = match &entry.content {
ClipboardData::Text(text) => ("text", text.clone()),
ClipboardData::Image(img) => {
if let Some(bytes) = &img.bytes {
let img_path = Path::new(&self.dir_path)
.join("images")
.join(format!("{}.png", img.id));
fs::write(&img_path, bytes)?;
if let Some(raw_pixels) = &img.raw_pixels {
let img_path = img.file_path(&self.dir_path);
let file = fs::File::create(&img_path)?;
let encoder = PngEncoder::new(file);
encoder.write_image(
raw_pixels,
img.width,
img.height,
ExtendedColorType::Rgba8,
)?;
}
("image", img.id.to_string())
}
@ -83,7 +91,12 @@ impl Database {
ClipboardData::Text(content)
} else {
let id = Uuid::parse_str(&content)?;
ClipboardData::Image(Image { id, bytes: None })
ClipboardData::Image(Image {
id,
raw_pixels: None,
width: 0,
height: 0,
})
};
entries.push(ClipboardEntry {