DVUI

fromImageFile

the returned []PMA inside PMAImage is allocated with alloc

Parameters

#
dbg_name:[]const u8
[]const u8
alloc:std.mem.Allocator
std.mem.Allocator
image_bytes:[]const u8
[]const u8

Source

Implementation

#
pub fn fromImageFile(dbg_name: []const u8, alloc: std.mem.Allocator, image_bytes: []const u8) !PMAImage {
        var w: c_int = undefined;
        var h: c_int = undefined;
        var channels_in_file: c_int = undefined;
        const data = dvui.c.stbi_load_from_memory(image_bytes.ptr, @as(c_int, @intCast(image_bytes.len)), &w, &h, &channels_in_file, 4);
        if (data == null) {
            dvui.log.warn("imageTexture stbi_load error on image \"{s}\": {s}\n", .{ dbg_name, dvui.c.stbi_failure_reason() });
            return dvui.StbImageError.stbImageError;
        }
        defer dvui.c.stbi_image_free(data);

        const size: usize = @intCast(w * h * 4);
        const pixels = try alloc.alloc(u8, size);
        @memcpy(pixels, data[0..size]);

        return PMAImage{
            .pma = PMA.sliceFromRGBA(pixels),
            .width = @intCast(w),
            .height = @intCast(h),
        };
    }