screenshot: Replace image with png crate

This commit is contained in:
Victoria Brekenfeld 2022-08-15 20:23:27 +02:00
parent bb2c650ff7
commit 8c3c3051ac
4 changed files with 58 additions and 11 deletions

View file

@ -12,7 +12,7 @@ use smithay::{
backend::input::{Device, DeviceCapability, InputBackend, InputEvent, KeyState},
desktop::{layer_map_for_output, Kind, WindowSurfaceType},
reexports::wayland_server::{protocol::wl_surface::WlSurface, DisplayHandle, Resource},
utils::{Logical, Point, Rectangle},
utils::{Logical, Point, Rectangle, Size, Buffer},
wayland::{
data_device::set_data_device_focus,
output::Output,
@ -402,21 +402,32 @@ impl State {
break;
}
};
/*
for output in self.common.shell.outputs.clone().into_iter() {
match self.backend.offscreen_for_output(&output, &mut self.common) {
Ok(buffer) => {
Ok((buffer, size)) => {
let mut path = std::path::PathBuf::new();
path.push(&home);
path.push(format!("{}_{}.png", output.name(), timestamp));
if let Err(err) = buffer.save(&path) {
fn write_png(path: impl AsRef<std::path::Path>, data: Vec<u8>, size: Size<i32, Buffer>) -> anyhow::Result<()> {
use std::{io, fs};
let file = io::BufWriter::new(fs::File::create(&path)?);
let mut encoder = png::Encoder::new(file, size.w as u32, size.h as u32);
encoder.set_color(png::ColorType::Rgba);
encoder.set_depth(png::BitDepth::Eight);
let mut writer = encoder.write_header()?;
writer.write_image_data(&data)?;
Ok(())
}
if let Err(err) = write_png(&path, buffer, size) {
slog_scope::error!("Unable to save screenshot at {}: {}", path.display(), err);
}
},
Err(err) => slog_scope::error!("Could not save screenshot for output {}: {}", output.name(), err),
}
}
*/
}
}
}

View file

@ -32,6 +32,7 @@ use smithay::{
shm::ShmState,
viewporter::ViewporterState,
},
utils::{Size, Buffer},
};
use std::{
@ -204,12 +205,11 @@ impl BackendData {
}
}
/*
pub fn offscreen_for_output(
&mut self,
output: &Output,
state: &mut Common,
) -> anyhow::Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>> {
) -> anyhow::Result<(Vec<u8>, Size<i32, Buffer>)> {
use anyhow::Context;
use smithay::{
backend::{
@ -230,7 +230,7 @@ impl BackendData {
renderer: &mut R,
output: &Output,
state: &mut Common,
) -> anyhow::Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>>
) -> anyhow::Result<(Vec<u8>, Size<i32, Buffer>)>
where
E: std::error::Error + Send + Sync + 'static,
T: Clone + 'static,
@ -261,7 +261,7 @@ impl BackendData {
let mapping = renderer.copy_framebuffer(Rectangle::from_loc_and_size((0, 0), size))?;
let data = Vec::from(renderer.map_texture(&mapping)?);
Ok(image::ImageBuffer::from_raw(size.w as u32, size.h as u32, data).with_context(|| "buffer smaller then dimensions")?)
Ok((data, size))
}
match self {
@ -277,7 +277,6 @@ impl BackendData {
BackendData::Unset => unreachable!(),
}
}
*/
}
impl State {