Skip to content

Commit

Permalink
fix stride bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Gabriel committed Mar 4, 2024
1 parent 5e5b5d1 commit 57ca79e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -26,4 +27,4 @@
"label": "rust: run swappy"
}
]
}
}
51 changes: 34 additions & 17 deletions src/femtovg_area/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u8>::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::<RGBA<u8>>().1.into(),
image.width() as usize,
image.height() as usize,
(image.rowstride() / 4) as usize,
let img = Img::new_stride(
dst_buffer.align_to::<RGBA<u8>>().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::<RGB<u8>>().1.into(),
image.width() as usize,
image.height() as usize,
(image.rowstride() / 3) as usize,
let img = Img::new_stride(
dst_buffer.align_to::<RGB<u8>>().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)
}
Expand Down

0 comments on commit 57ca79e

Please sign in to comment.