|
| 1 | +# zstbi v0.10.0 - stb image bindings |
| 2 | + |
| 3 | +## Features |
| 4 | + |
| 5 | +* Supports Zig memory allocators |
| 6 | +* Supports decoding most popular formats |
| 7 | +* Supports HDR images |
| 8 | +* Supports 8-bits and 16-bits per channel |
| 9 | +* Supports image resizing |
| 10 | +* Supports image writing (.png, .jpg) |
| 11 | + |
| 12 | +## Getting started |
| 13 | + |
| 14 | +Copy `zstbi` to a subdirectory of your project and add the following to your `build.zig.zon` .dependencies: |
| 15 | +```zig |
| 16 | + .zstbi = .{ .path = "libs/zstbi" }, |
| 17 | +``` |
| 18 | + |
| 19 | +Then in your `build.zig` add: |
| 20 | +```zig |
| 21 | +pub fn build(b: *std.Build) void { |
| 22 | + const exe = b.addExecutable(.{ ... }); |
| 23 | +
|
| 24 | + const zstbi = b.dependency("zstbi", .{}); |
| 25 | + exe.root_module.addImport("zstbi", zstbi.module("root")); |
| 26 | + exe.linkLibrary(zstbi.artifact("zstbi")); |
| 27 | +} |
| 28 | +``` |
| 29 | +Now in your code you may import and use `zstbi`. |
| 30 | + |
| 31 | +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. |
| 32 | +```zig |
| 33 | +const zstbi = @import("zstbi"); |
| 34 | +
|
| 35 | +zstbi.init(allocator); |
| 36 | +defer zstbi.deinit(); |
| 37 | +``` |
| 38 | +```zig |
| 39 | +pub const Image = struct { |
| 40 | + data: []u8, |
| 41 | + width: u32, |
| 42 | + height: u32, |
| 43 | + num_components: u32, |
| 44 | + bytes_per_component: u32, |
| 45 | + bytes_per_row: u32, |
| 46 | + is_hdr: bool, |
| 47 | + ... |
| 48 | +``` |
| 49 | +```zig |
| 50 | +pub fn loadFromFile(pathname: [:0]const u8, forced_num_components: u32) !Image |
| 51 | +
|
| 52 | +pub fn loadFromMemory(data: []const u8, forced_num_components: u32) !Image |
| 53 | +
|
| 54 | +pub fn createEmpty(width: u32, height: u32, num_components: u32, args: struct { |
| 55 | + bytes_per_component: u32 = 0, |
| 56 | + bytes_per_row: u32 = 0, |
| 57 | +}) !Image |
| 58 | +
|
| 59 | +pub fn info(pathname: [:0]const u8) struct { |
| 60 | + is_supported: bool, |
| 61 | + width: u32, |
| 62 | + height: u32, |
| 63 | + num_components: u32, |
| 64 | +} |
| 65 | +
|
| 66 | +pub fn resize(image: *const Image, new_width: u32, new_height: u32) Image |
| 67 | +
|
| 68 | +pub fn writeToFile( |
| 69 | + image: *const Image, |
| 70 | + filename: [:0]const u8, |
| 71 | + image_format: ImageWriteFormat, |
| 72 | +) ImageWriteError!void |
| 73 | +
|
| 74 | +pub fn writeToFn( |
| 75 | + image: *const Image, |
| 76 | + write_fn: *const fn (ctx: ?*anyopaque, data: ?*anyopaque, size: c_int) callconv(.C) void, |
| 77 | + context: ?*anyopaque, |
| 78 | + image_format: ImageWriteFormat, |
| 79 | +) ImageWriteError!void |
| 80 | +``` |
| 81 | +```zig |
| 82 | +var image = try zstbi.Image.loadFromFile("data/image.png", forced_num_components); |
| 83 | +defer image.deinit(); |
| 84 | +
|
| 85 | +const new_resized_image = image.resize(1024, 1024); |
| 86 | +``` |
| 87 | +Misc functions: |
| 88 | +```zig |
| 89 | +pub fn isHdr(filename: [:0]const u8) bool |
| 90 | +pub fn is16bit(filename: [:0]const u8) bool |
| 91 | +
|
| 92 | +pub fn setFlipVerticallyOnLoad(should_flip: bool) void |
| 93 | +``` |
0 commit comments