SharedImageBuffer/SharedPixelBuffer: use u32 instead of usize

For width, height and stride that's a more natural choice that's also consistent
with what the image crate uses.
This commit is contained in:
Simon Hausmann 2022-01-27 16:51:20 +01:00 committed by Simon Hausmann
parent 8d4927bac2
commit d16a335bc4
6 changed files with 37 additions and 25 deletions

View file

@ -15,6 +15,7 @@ This version changes some APIs in incompatible ways. For details how to migrate
- The deprecated methods `Model::attach_peer` and `ModelNotify::attach` were removed.
- The interpreter does not differentiate anymore between `Value::Array` and `Value::Model`
everything is a `Value::Model`, which now contains a `ModelHandle`
- In Rust, `sixtyfps::SharedPixelBuffer` and `sixtyfps::SharedImageBuffer` now use a `u32` instead of `usize` for `width`, `height` and `stride`.
### Added

View file

@ -95,8 +95,8 @@ pub fn main() {
main_window.set_original_image(sixtyfps::Image::from_rgba8(
sixtyfps::SharedPixelBuffer::clone_from_slice(
source_image.as_raw(),
source_image.width() as _,
source_image.height() as _,
source_image.width(),
source_image.height(),
),
));
@ -147,8 +147,8 @@ pub fn main() {
let filtered_image = filter_fn(&source_image);
sixtyfps::Image::from_rgba8(sixtyfps::SharedPixelBuffer::clone_from_slice(
filtered_image.as_raw(),
filtered_image.width() as _,
filtered_image.height() as _,
filtered_image.width(),
filtered_image.height(),
))
});

View file

@ -25,7 +25,7 @@ fn pdf(x: f64, y: f64) -> f64 {
fn render_plot(pitch: f32) -> sixtyfps::Image {
let mut pixel_buffer = SharedPixelBuffer::new(640, 480);
let size = (pixel_buffer.width() as u32, pixel_buffer.height() as u32);
let size = (pixel_buffer.width(), pixel_buffer.height());
let backend = BitMapBackend::with_buffer(pixel_buffer.make_mut_bytes(), size);

View file

@ -18,25 +18,25 @@ use super::{IntRect, IntSize};
#[derive(Debug, Clone)]
#[repr(C)]
pub struct SharedPixelBuffer<Pixel> {
width: usize,
height: usize,
stride: usize,
width: u32,
height: u32,
stride: u32,
data: SharedVector<Pixel>,
}
impl<Pixel> SharedPixelBuffer<Pixel> {
/// Returns the width of the image in pixels.
pub fn width(&self) -> usize {
pub fn width(&self) -> u32 {
self.width
}
/// Returns the height of the image in pixels.
pub fn height(&self) -> usize {
pub fn height(&self) -> u32 {
self.height
}
/// Returns the number of pixels per line.
pub fn stride(&self) -> usize {
pub fn stride(&self) -> u32 {
self.stride
}
}
@ -75,12 +75,14 @@ impl<Pixel> SharedPixelBuffer<Pixel> {
impl<Pixel: Clone + Default> SharedPixelBuffer<Pixel> {
/// Creates a new SharedPixelBuffer with the given width and height. Each pixel will be initialized with the value
/// that [`Default::default()`] returns for the Pixel type.
pub fn new(width: usize, height: usize) -> Self {
pub fn new(width: u32, height: u32) -> Self {
Self {
width,
height,
stride: width,
data: core::iter::repeat(Pixel::default()).take(width * height).collect(),
data: core::iter::repeat(Pixel::default())
.take(width as usize * height as usize)
.collect(),
}
}
}
@ -91,8 +93,8 @@ impl<Pixel: Clone> SharedPixelBuffer<Pixel> {
/// and you would like to convert it for use in SixtyFPS.
pub fn clone_from_slice<SourcePixelType>(
pixel_slice: &[SourcePixelType],
width: usize,
height: usize,
width: u32,
height: u32,
) -> Self
where
[SourcePixelType]: rgb::AsPixels<Pixel>,
@ -139,7 +141,7 @@ pub enum SharedImageBuffer {
impl SharedImageBuffer {
/// Returns the width of the image in pixels.
#[inline]
pub fn width(&self) -> usize {
pub fn width(&self) -> u32 {
match self {
Self::RGB8(buffer) => buffer.width(),
Self::RGBA8(buffer) => buffer.width(),
@ -149,7 +151,7 @@ impl SharedImageBuffer {
/// Returns the height of the image in pixels.
#[inline]
pub fn height(&self) -> usize {
pub fn height(&self) -> u32 {
match self {
Self::RGB8(buffer) => buffer.height(),
Self::RGBA8(buffer) => buffer.height(),
@ -271,7 +273,7 @@ pub struct LoadImageError(());
/// ```
/// # use sixtyfps_corelib::graphics::{SharedPixelBuffer, Image, Rgb8Pixel};
///
/// fn low_level_render(width: usize, height: usize, buffer: &mut [u8]) {
/// fn low_level_render(width: u32, height: u32, buffer: &mut [u8]) {
/// // render beautiful circle or other shapes here
/// }
///
@ -297,8 +299,8 @@ pub struct LoadImageError(());
///
/// let buffer = SharedPixelBuffer::<Rgba8Pixel>::clone_from_slice(
/// cat_image.as_raw(),
/// cat_image.width() as _,
/// cat_image.height() as _,
/// cat_image.width(),
/// cat_image.height(),
/// );
/// let image = Image::from_rgba8(buffer);
/// ```
@ -311,7 +313,7 @@ pub struct LoadImageError(());
/// let width = pixel_buffer.width();
/// let height = pixel_buffer.height();
/// let mut pixmap = tiny_skia::PixmapMut::from_bytes(
/// pixel_buffer.make_mut_bytes(), width as _, height as _
/// pixel_buffer.make_mut_bytes(), width, height
/// ).unwrap();
/// pixmap.fill(tiny_skia::Color::TRANSPARENT);
///

View file

@ -554,15 +554,24 @@ fn image_buffer_to_image_source(
) -> (femtovg::ImageSource<'_>, femtovg::ImageFlags) {
match buffer {
SharedImageBuffer::RGB8(buffer) => (
{ imgref::ImgRef::new(buffer.as_slice(), buffer.width(), buffer.height()).into() },
{
imgref::ImgRef::new(buffer.as_slice(), buffer.width() as _, buffer.height() as _)
.into()
},
femtovg::ImageFlags::empty(),
),
SharedImageBuffer::RGBA8(buffer) => (
{ imgref::ImgRef::new(buffer.as_slice(), buffer.width(), buffer.height()).into() },
{
imgref::ImgRef::new(buffer.as_slice(), buffer.width() as _, buffer.height() as _)
.into()
},
femtovg::ImageFlags::empty(),
),
SharedImageBuffer::RGBA8Premultiplied(buffer) => (
{ imgref::ImgRef::new(buffer.as_slice(), buffer.width(), buffer.height()).into() },
{
imgref::ImgRef::new(buffer.as_slice(), buffer.width() as _, buffer.height() as _)
.into()
},
femtovg::ImageFlags::PREMULTIPLIED,
),
}

View file

@ -882,7 +882,7 @@ pub(crate) fn load_image_from_resource(
};
let width: i32 = buffer.width() as _;
let height: i32 = buffer.height() as _;
let pixmap = cpp! { unsafe [format as "QImage::Format", width as "int", height as "int", bytes_per_line as "size_t", buffer_ptr as "const uchar *"] -> qttypes::QPixmap as "QPixmap" {
let pixmap = cpp! { unsafe [format as "QImage::Format", width as "int", height as "int", bytes_per_line as "uint32_t", buffer_ptr as "const uchar *"] -> qttypes::QPixmap as "QPixmap" {
QImage img(buffer_ptr, width, height, bytes_per_line, format);
return QPixmap::fromImage(img);
} };