Skip to content

Commit d81a9d4

Browse files
committed
Add files from monorepo
@ commit 695b5833314ce0f73c7ff77877fb4b1c4fafbabc
0 parents  commit d81a9d4

File tree

11 files changed

+13185
-0
lines changed

11 files changed

+13185
-0
lines changed

.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.cpp text
2+
*.h text
3+
*.zig text
4+
*.txt text
5+
* text eol=lf

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Ignore some special directories
2+
*zig-cache
3+
*zig-out
4+
5+
# Ignore some special OS files
6+
*.DS_Store

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Michal Ziulek
4+
Copyright (c) 2024 zig-gamedev contributors
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
```

build.zig

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
const optimize = b.standardOptimizeOption(.{});
5+
const target = b.standardTargetOptions(.{});
6+
7+
_ = b.addModule("root", .{
8+
.root_source_file = b.path("src/zstbi.zig"),
9+
});
10+
11+
const zstbi_lib = b.addStaticLibrary(.{
12+
.name = "zstbi",
13+
.target = target,
14+
.optimize = optimize,
15+
});
16+
zstbi_lib.addIncludePath(b.path("libs/stbi"));
17+
if (optimize == .Debug) {
18+
// TODO: Workaround for Zig bug.
19+
zstbi_lib.addCSourceFile(.{
20+
.file = b.path("src/zstbi.c"),
21+
.flags = &.{
22+
"-std=c99",
23+
"-fno-sanitize=undefined",
24+
"-g",
25+
"-O0",
26+
},
27+
});
28+
} else {
29+
zstbi_lib.addCSourceFile(.{
30+
.file = b.path("src/zstbi.c"),
31+
.flags = &.{
32+
"-std=c99",
33+
"-fno-sanitize=undefined",
34+
},
35+
});
36+
}
37+
zstbi_lib.linkLibC();
38+
b.installArtifact(zstbi_lib);
39+
40+
const test_step = b.step("test", "Run zstbi tests");
41+
42+
const tests = b.addTest(.{
43+
.name = "zstbi-tests",
44+
.root_source_file = b.path("src/zstbi.zig"),
45+
.target = target,
46+
.optimize = optimize,
47+
});
48+
tests.linkLibrary(zstbi_lib);
49+
b.installArtifact(tests);
50+
51+
test_step.dependOn(&b.addRunArtifact(tests).step);
52+
}

build.zig.zon

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.{
2+
.name = "zstbi",
3+
.version = "0.10.0",
4+
.paths = .{
5+
"build.zig",
6+
"build.zig.zon",
7+
"libs",
8+
"src",
9+
"README.md",
10+
},
11+
}

0 commit comments

Comments
 (0)