Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zig-gamedev/zstbi
Zig bindings and build package for stb_image, stb_image_resize and stb_image_write
https://github.com/zig-gamedev/zstbi
bindings gamedev stb-image zig
Last synced: 3 months ago
JSON representation
Zig bindings and build package for stb_image, stb_image_resize and stb_image_write
- Host: GitHub
- URL: https://github.com/zig-gamedev/zstbi
- Owner: zig-gamedev
- License: mit
- Created: 2024-08-07T22:58:40.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-08-07T23:30:22.000Z (5 months ago)
- Last Synced: 2024-09-30T03:20:57.068Z (3 months ago)
- Topics: bindings, gamedev, stb-image, zig
- Language: C++
- Homepage:
- Size: 120 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# [zstbi](https://github.com/zig-gamedev/zstbi)
Zig bindings and build package for stb_image, stb_image_resize and stb_image_write from [Sean Barrett's stb single-file C libraries](https://github.com/nothings/stb)
## Features
* Supports Zig memory allocators
* Supports decoding most popular formats
* Supports HDR images
* Supports 8-bits and 16-bits per channel
* Supports image resizing
* Supports image writing (.png, .jpg)## Getting started
Add `zstbi` to your `build.zig.zon` .dependencies and in your `build.zig` add:
```zig
pub fn build(b: *std.Build) void {
const exe = b.addExecutable(.{ ... });const zstbi = b.dependency("zstbi", .{});
exe.root_module.addImport("zstbi", zstbi.module("root"));
exe.linkLibrary(zstbi.artifact("zstbi"));
}
```
Now in your code you may import and use `zstbi`.Init the lib. `zstbi.init()` is cheap and you may call it whenever you need to change memory allocator. Must be called from the main thread.
```zig
const zstbi = @import("zstbi");zstbi.init(allocator);
defer zstbi.deinit();
```
```zig
pub const Image = struct {
data: []u8,
width: u32,
height: u32,
num_components: u32,
bytes_per_component: u32,
bytes_per_row: u32,
is_hdr: bool,
...
```
```zig
pub fn loadFromFile(pathname: [:0]const u8, forced_num_components: u32) !Imagepub fn loadFromMemory(data: []const u8, forced_num_components: u32) !Image
pub fn createEmpty(width: u32, height: u32, num_components: u32, args: struct {
bytes_per_component: u32 = 0,
bytes_per_row: u32 = 0,
}) !Imagepub fn info(pathname: [:0]const u8) struct {
is_supported: bool,
width: u32,
height: u32,
num_components: u32,
}pub fn resize(image: *const Image, new_width: u32, new_height: u32) Image
pub fn writeToFile(
image: *const Image,
filename: [:0]const u8,
image_format: ImageWriteFormat,
) ImageWriteError!voidpub fn writeToFn(
image: *const Image,
write_fn: *const fn (ctx: ?*anyopaque, data: ?*anyopaque, size: c_int) callconv(.C) void,
context: ?*anyopaque,
image_format: ImageWriteFormat,
) ImageWriteError!void
```
```zig
var image = try zstbi.Image.loadFromFile("data/image.png", forced_num_components);
defer image.deinit();const new_resized_image = image.resize(1024, 1024);
```
Misc functions:
```zig
pub fn isHdr(filename: [:0]const u8) bool
pub fn is16bit(filename: [:0]const u8) boolpub fn setFlipVerticallyOnLoad(should_flip: bool) void
```