Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(screenshot): handle dynamic channel count in desktop pipeline

Richie McIlroyfd927b95600a
diff --git a/apps/desktop/src-tauri/src/gpu_context.rs b/apps/desktop/src-tauri/src/gpu_context.rs
index 5e11a8023..275cc48e1 100644
--- a/apps/desktop/src-tauri/src/gpu_context.rs
+++ b/apps/desktop/src-tauri/src/gpu_context.rs
@@ -10,6 +10,7 @@ pub struct PendingScreenshot {
pub data: Vec<u8>,
pub width: u32,
pub height: u32,
+ pub channels: u32,
pub created_at: Instant,
}
diff --git a/apps/desktop/src-tauri/src/recording.rs b/apps/desktop/src-tauri/src/recording.rs
index c35bf7fec..4e3852ab6 100644
--- a/apps/desktop/src-tauri/src/recording.rs
+++ b/apps/desktop/src-tauri/src/recording.rs
@@ -1374,7 +1374,16 @@ pub async fn take_screenshot(
let image_width = image.width();
let image_height = image.height();
- let image_data = image.into_raw();
+ let channels: u32 = match &image {
+ image::DynamicImage::ImageRgba8(_) => 4,
+ _ => 3,
+ };
+ let color_type = if channels == 4 {
+ image::ColorType::Rgba8
+ } else {
+ image::ColorType::Rgb8
+ };
+ let image_data = image.into_bytes();
let filename = project_name.replace(":", ".");
let filename = format!("{}.cap", sanitize_filename::sanitize(&filename));
@@ -1400,6 +1409,7 @@ pub async fn take_screenshot(
data: image_data.clone(),
width: image_width,
height: image_height,
+ channels,
created_at: Instant::now(),
},
);
@@ -1464,7 +1474,7 @@ pub async fn take_screenshot(
&image_data,
image_width,
image_height,
- image::ColorType::Rgb8.into(),
+ color_type.into(),
)
.map_err(|e| format!("Failed to encode PNG: {e}"))
})
diff --git a/apps/desktop/src-tauri/src/screenshot_editor.rs b/apps/desktop/src-tauri/src/screenshot_editor.rs
index c025e33cf..042dda22f 100644
--- a/apps/desktop/src-tauri/src/screenshot_editor.rs
+++ b/apps/desktop/src-tauri/src/screenshot_editor.rs
@@ -113,6 +113,7 @@ impl ScreenshotEditorInstances {
if let Some(frame) = pending_frame {
let width = frame.width;
let height = frame.height;
+ let channels = frame.channels;
if width > MAX_DIMENSION || height > MAX_DIMENSION {
return Err(format!(
@@ -122,7 +123,7 @@ impl ScreenshotEditorInstances {
let expected_len = width
.checked_mul(height)
- .and_then(|p| p.checked_mul(3))
+ .and_then(|p| p.checked_mul(channels))
.ok_or_else(|| {
format!("Image dimensions overflow: {width}x{height}")
})?;
@@ -133,16 +134,26 @@ impl ScreenshotEditorInstances {
if data.len() != expected_len {
return Err(format!(
- "Image data length mismatch: expected {expected_len} bytes for {width}x{height} frame, got {}",
+ "Image data length mismatch: expected {expected_len} bytes for {width}x{height}x{channels} frame, got {}",
data.len()
));
}
- let rgb_img = RgbImage::from_raw(width, height, data).ok_or_else(|| {
- format!("Invalid RGB data for {width}x{height} frame")
- })?;
- let rgba_img: image::RgbaImage = rgb_img.convert();
- (rgba_img.into_raw(), width, height)
+ let rgba_data = if channels == 4 {
+ let rgba_img = image::RgbaImage::from_raw(width, height, data)
+ .ok_or_else(|| {
+ format!("Invalid RGBA data for {width}x{height} frame")
+ })?;
+ rgba_img.into_raw()
+ } else {
+ let rgb_img =
+ RgbImage::from_raw(width, height, data).ok_or_else(|| {
+ format!("Invalid RGB data for {width}x{height} frame")
+ })?;
+ let rgba_img: image::RgbaImage = rgb_img.convert();
+ rgba_img.into_raw()
+ };
+ (rgba_data, width, height)
} else {
let image_path = if path.is_dir() {
let original = path.join("original.png");