7.8.1. STBIMAGE-01 — Loading and Inspecting Images
This tutorial covers loading images from files, querying image properties,
and format detection using the stbimage_boost module.
7.8.1.2. Loading with Error Handling
Image.load() returns a tuple<bool; string>. On success the first
element is true and the error string is empty:
var inscope img : Image
let (ok, error) = img.load("photo.png")
if (!ok) {
print("Failed: {error}\n")
return
}
print("{img.width}x{img.height}, {img.channels} channels\n")
7.8.1.3. Image Properties
Once loaded, an Image exposes several query methods:
Method |
Description |
|---|---|
|
|
|
Bytes per row (width * channels * bpc) |
|
|
|
Bytes per pixel (channels * bpc) |
|
|
|
Total bytes of pixel data |
7.8.1.4. HDR and 16-bit Loading
load_hdr() loads float data (bpc=4). load_16() loads 16-bit data
(bpc=2). Both follow the same tuple<bool; string> error pattern:
var inscope img16 : Image
let (ok, err) = img16.load_16("photo.png")
7.8.1.5. Image Info Without Loading
image_info() reads just the header — much faster than loading pixel data:
let info = image_info("photo.png")
print("{info.w}x{info.h}, {info.comp} channels\n")
7.8.1.6. Convenience Loading
load_image() is a one-liner that panics on failure — good for scripts:
var inscope img <- load_image("photo.png")
A variant with an error out-parameter is also available:
var error : string
var inscope img <- load_image("photo.png", error)
7.8.1.7. Format Detection
is_hdr() and is_16_bit() check file format without loading pixels.
Memory variants (is_hdr_from_memory, is_16_bit_from_memory) also
exist:
print("HDR: {is_hdr(path)}, 16-bit: {is_16_bit(path)}\n")
See also
Full source: tutorials/dasStbImage/01_loading_images.das
Next tutorial: STBIMAGE-02 — Saving and Encoding Images