31 lines
669 B
Zig
31 lines
669 B
Zig
const std = @import("std");
|
|
|
|
const c = @cImport({
|
|
@cInclude("stb_image.h");
|
|
});
|
|
|
|
const STBImage = @This();
|
|
|
|
rgba8_pixels: [*c]u8,
|
|
width: u32,
|
|
height: u32,
|
|
|
|
pub fn load(png_data: []const u8) !STBImage {
|
|
var width: c_int = undefined;
|
|
var height: c_int = undefined;
|
|
const pixels = c.stbi_load_from_memory(png_data.ptr, @intCast(png_data.len), &width, &height, null, 4);
|
|
if (pixels == null) {
|
|
return error.InvalidPng;
|
|
}
|
|
|
|
return STBImage{
|
|
.rgba8_pixels = pixels,
|
|
.width = @intCast(width),
|
|
.height = @intCast(height)
|
|
};
|
|
}
|
|
|
|
pub fn deinit(self: *const STBImage) void {
|
|
c.stbi_image_free(self.rgba8_pixels);
|
|
}
|