DVUI

size

Get the size of a raster image. If source is .imageFile, this only decodes enough info to get the size.

See dvui.image.

Only valid between Window.beginand Window.end.

Parameters

#
source:ImageSource
ImageSource

Source

Implementation

#
pub fn size(source: ImageSource) !Size {
        switch (source) {
            .imageFile => |file| {
                var w: c_int = undefined;
                var h: c_int = undefined;
                var n: c_int = undefined;
                const ok = dvui.c.stbi_info_from_memory(file.bytes.ptr, @as(c_int, @intCast(file.bytes.len)), &w, &h, &n);
                if (ok == 1) {
                    return .{ .w = @floatFromInt(w), .h = @floatFromInt(h) };
                } else {
                    dvui.log.warn("imageSize stbi_info error on image \"{s}\": {s}\n", .{ file.name, dvui.c.stbi_failure_reason() });
                    return StbImageError.stbImageError;
                }
            },
            .pixelsPMA => |a| return .{ .w = @floatFromInt(a.width), .h = @floatFromInt(a.height) },
            .pixels => |a| return .{ .w = @floatFromInt(a.width), .h = @floatFromInt(a.height) },
            .texture => |tex| return .{ .w = @floatFromInt(tex.width), .h = @floatFromInt(tex.height) },
        }
    }