raw img + background png convert
This commit is contained in:
@ -1,28 +1,27 @@
|
||||
use arboard::{Clipboard, ImageData};
|
||||
use image::{ExtendedColorType, ImageEncoder, codecs::png::PngEncoder};
|
||||
use arboard::Clipboard;
|
||||
use std::error::Error;
|
||||
use std::time::SystemTime;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::models::{ClipboardData, ClipboardEntry, Image};
|
||||
|
||||
pub trait ImageDataExt {
|
||||
fn to_png(&self) -> Result<Vec<u8>, Box<dyn Error>>;
|
||||
}
|
||||
|
||||
impl ImageDataExt for ImageData<'_> {
|
||||
fn to_png(&self) -> Result<Vec<u8>, Box<dyn Error>> {
|
||||
let mut buffer = Vec::new();
|
||||
let encoder = PngEncoder::new(&mut buffer);
|
||||
encoder.write_image(
|
||||
&self.bytes,
|
||||
self.width as u32,
|
||||
self.height as u32,
|
||||
ExtendedColorType::Rgba8,
|
||||
)?;
|
||||
Ok(buffer)
|
||||
}
|
||||
}
|
||||
// pub trait ImageDataExt {
|
||||
// fn to_png(&self) -> Result<Vec<u8>, Box<dyn Error>>;
|
||||
// }
|
||||
//
|
||||
// impl ImageDataExt for ImageData<'_> {
|
||||
// fn to_png(&self) -> Result<Vec<u8>, Box<dyn Error>> {
|
||||
// let mut buffer = Vec::new();
|
||||
// let encoder = PngEncoder::new(&mut buffer);
|
||||
// encoder.write_image(
|
||||
// &self.bytes,
|
||||
// self.width as u32,
|
||||
// self.height as u32,
|
||||
// ExtendedColorType::Rgba8,
|
||||
// )?;
|
||||
// Ok(buffer)
|
||||
// }
|
||||
// }
|
||||
|
||||
impl ClipboardEntry {
|
||||
pub fn new(clipboard: &mut Clipboard) -> Result<ClipboardEntry, Box<dyn Error>> {
|
||||
@ -32,7 +31,9 @@ impl ClipboardEntry {
|
||||
Ok(image) => {
|
||||
let id = Uuid::new_v4();
|
||||
Some(ClipboardData::Image(Image {
|
||||
bytes: Some(image.to_png()?),
|
||||
raw_pixels: Some(image.bytes.into_owned()),
|
||||
width: image.width as u32,
|
||||
height: image.height as u32,
|
||||
id,
|
||||
}))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user