diff --git a/.vscode/launch.json b/.vscode/launch.json index 8dc01cc..9fc9a02 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -21,7 +21,8 @@ }, "args": [ "--filename", - "/home/gabm/Pictures/Screenshots/satty-20240219-14:19:29.png", + "/tmp/bug.png", + //"/home/gabm/Pictures/Screenshots/satty-20240219-14:19:29.png", //"/home/gabm/Pictures/Screenshots/satty-20240109-22:19:08.png", //"/home/gabm/Pictures/Wallpaper/torres_1.jpg" "--output-filename", diff --git a/.vscode/tasks.json b/.vscode/tasks.json index c32c22b..8c05321 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -8,7 +8,8 @@ "run", "--", "--filename", - "/home/gabm/Pictures/Screenshots/satty-20240219-14:19:29.png", // small + "/tmp/bug.png", + //"/home/gabm/Pictures/Screenshots/satty-20240219-14:19:29.png", // small //"/home/gabm/Pictures/Screenshots/satty-20240109-22:19:08.png", // big //"--fullscreen", "--output-filename", @@ -26,4 +27,4 @@ "label": "rust: run swappy" } ] -} +} \ No newline at end of file diff --git a/src/femtovg_area/imp.rs b/src/femtovg_area/imp.rs index 86c284c..0a71bf8 100644 --- a/src/femtovg_area/imp.rs +++ b/src/femtovg_area/imp.rs @@ -406,33 +406,50 @@ impl FemtoVgAreaMut { ImageFlags::empty(), )?; + // extract values + let width = image.width() as usize; + let stride = image.rowstride() as usize; // stride is in bytes per row + let height = image.height() as usize; + let bytes_per_pixel = if image.has_alpha() { 4 } else { 3 }; // pixbuf supports rgb or rgba + unsafe { + let src_buffer = image.pixels(); + + let row_length = width * bytes_per_pixel; + let mut dst_buffer = if row_length == stride { + src_buffer.to_vec() + } else { + let mut dst_buffer = Vec::::with_capacity(width * height * bytes_per_pixel); + + for row in 0..height { + let src_offset = row * stride; + dst_buffer.extend_from_slice(&src_buffer[src_offset..src_offset + row_length]); + } + dst_buffer + }; + + dst_buffer.truncate(width * height * bytes_per_pixel); + if image.has_alpha() { - let mut img = Img::new_stride( - image.pixels().align_to::>().1.into(), - image.width() as usize, - image.height() as usize, - (image.rowstride() / 4) as usize, + let img = Img::new_stride( + dst_buffer.align_to::>().1.to_vec(), + width, + height, + width, ); - // this function truncates the internal buffer so that width == stride - let _ = img.as_contiguous_buf(); - canvas.update_image(background_image_id, ImageSource::Rgba(img.as_ref()), 0, 0)?; } else { - let mut img = Img::new_stride( - image.pixels().align_to::>().1.into(), - image.width() as usize, - image.height() as usize, - (image.rowstride() / 3) as usize, + let img = Img::new_stride( + dst_buffer.align_to::>().1.to_owned(), + width, + height, + width, ); - // this function truncates the internal buffer so that width == stride - let _ = img.as_contiguous_buf(); - canvas.update_image(background_image_id, ImageSource::Rgb(img.as_ref()), 0, 0)?; } - }; + } Ok(background_image_id) }