https://github.com/braheezy/zpix
Image decoding
https://github.com/braheezy/zpix
jpeg png zig
Last synced: 8 months ago
JSON representation
Image decoding
- Host: GitHub
- URL: https://github.com/braheezy/zpix
- Owner: braheezy
- Created: 2024-11-02T17:25:41.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-08T16:30:01.000Z (about 1 year ago)
- Last Synced: 2025-05-09T02:15:15.917Z (about 1 year ago)
- Topics: jpeg, png, zig
- Language: Zig
- Homepage:
- Size: 5.18 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# zpix
Image decoding library in pure Zig. It supports:
**JPEG**
- Baseline and Progressive formats
- Gray, YCbCr, RGBA, and YCMK color formats.
**PNG**
- Gray 1, 2, 4, 8, and 16 bit
- Gray + alpha 8 bit and 16 bit
- Truecolor 8 bit and 16 bit
- Truecolor + alpha, 8 bit and 16 bit
- Paletted 1, 2, 4, and 8 bit
- Interlaced
**QOI**
- Encoding and decoding Quite OK Image (QOI) file format.
Here's proof! The Mac image viewer on the left, and a SDL image viewer in Zig using `zpix` to view a JPEG file:

## Usage
Add to project:
zig fetch --save git+https://github.com/braheezy/zpix
In your `build.zig`:
const zpix = b.dependency("zpix", .{});
root_module.addImport("zjpeg", zpix.module("jpeg"));
root_module.addImport("png", zpix.module("png"));
// Or the whole module that has everything
exe.root_module.addImport("zpix", zpix.module("zpix"));
In your program, load an image file
```zig
const jpeg = @import("jpeg");
const png = @import("png");
const qoi = @import("qoi");
// or const jpeg = @import("zpix").jpeg;
// or const png = @import("zpix").png;
// or const qoi = @import("zpix").qoi;
const img = if (std.mem.eql(u8, file_ext, ".jpg") or std.mem.eql(u8, file_ext, ".jpeg"))
try jpeg.load(allocator, arg)
else if (std.mem.eql(u8, file_ext, ".png")) png: {
const img = png.load(allocator, arg) catch {
std.process.exit(0);
};
break :png img;
} else if (std.mem.eql(u8, file_ext, ".qoi")) qoi: {
const img = qoi.load(allocator, arg) catch {
std.process.exit(0);
};
break :qoi img;
} else return error.UnsupportedFileExtension;
defer {
img.free(allocator);
}
// Do something with pixels
```
See [`example/zpixview.zig`](./example/zpixview.zig) for an example with SDL.
## Development
Run using `zig`:
zig build run --
Or build and run:
zig build
./zig-out/bin/zpixview