#png #gif #jpeg #image #bmp #load-image #format

stb_image_rust

stb_image 的 Rust 版本

3 个稳定版本

使用旧的 Rust 2015

2.27.2 2022年1月6日
2.27.1 2022年1月3日
2.27.0 2022年1月1日

#488 in 图像

每月49次下载
3 crate 中使用

Unlicense 许可

305KB
8K SLoC

关于

stb_image_rust 是 stb_image.h 的 Rust 版本,它是一个用于加载 JPG、PNG、BMP、TGA、PSD 和 GIF 格式图像的 C 库。

需要注意的是,这个项目是一个 端口,而不是 包装器。原始的 C 代码已经被移植到了 Rust。因此,stb_image_rust 不需要任何本地二进制文件。

移植工作不是手动完成的,而是使用 Hebron 完成的。

crate

https://crates.io/crates/stb_image_rust

示例代码

        // Load file into memory
        let mut f = File::open(path).expect("file not found");
        let mut contents = vec![];
        f.read_to_end(&mut contents);

	// Load the image
        let mut x: i32 = 0;
        let mut y: i32 = 0;
        let mut comp: i32 = 0;
        let img: *mut u8;
		
        unsafe {
            img = stb_image_rust::stbi_load_from_memory(
                contents.as_mut_ptr(),
                contents.len() as i32,
                &mut x,
                &mut y,
                &mut comp,
                stb_image_rust::STBI_rgb_alpha,
            );
        }
		
	// Do something with it
	...
		
	// Free the allocated memory
        unsafe {
            stb_image_rust::c_runtime::free(img);
        }		

许可

公共领域

无运行时依赖