tycho_image

    Tycho Image — typed RGBA/HDR image IO (PNG/JPEG/PPM/BMP decode, PNG encode) for the Tycho stack

    image
    png
    hdr
    decoding
    tycho
    Download zip
    Version
    0.9.0
    License
    Apache-2.0
    Last updated
    3 days ago
    Downloads
    20

    #laplaceliu/tycho_image

    Typed image IO and pure MoonBit stb_image v2.34 decoder for the Tycho stack.

    #What it provides

    • Pure MoonBit stb_image v2.34 decoder (no C FFI for decoding):
      • Formats: PNG (1/2/4/8/16-bit, all color types, PLTE, tRNS, all 5 filters, Adam7 interlacing, iPhone CgBI), JPEG (baseline & progressive, 1/3/4 components, YCbCr, Adobe RGB, CMYK, YCCK, restart markers, arbitrary integer subsampling), BMP (1/4/8/16/24/32-bit, OS/2 & Windows v1/v3/v4/v5 headers, BI_RGB & BI_BITFIELDS), TGA (uncompressed & RLE, grayscale/truecolor/color-mapped, 8/15/16/24/32-bit), GIF (87a/89a, LZW, transparency, interlacing, single-frame & multi-frame animation with disposal modes), PSD (8/16-bit raw & 8-bit PackBits RLE), HDR (Radiance RGBE raw & RLE), PNM (P2/P3 ASCII and P5/P6 binary PGM/PPM, 8/16-bit), plus zlib/DEFLATE.
      • stbi_* entry points: stbi_load_from_memory, stbi_load_16_from_memory, stbi_loadf_from_memory, stbi_load_gif_from_memory, stbi_info_from_memory, stbi_is_16_bit_from_memory, stbi_is_hdr_from_memory, stbi_zlib_decode, stbi_zlib_decode_noheader.
      • Behavior is locked down against upstream C stb_image.h v2.34 via binary golden data (testdata/golden.bin).
    • High-level Tycho image API:
      • Image { width, height, format, data : Bytes } with ImageFormat (RGBA8 / RGBA8Srgb / R32Float) — row-major, tightly packed pixels.
      • HdrImage { width, height, pixels : FixedArray[Float] } for linear Radiance .hdr content.
      • ImageError (DecodeFailed / IoFailed / EncodeFailed).
      • Decoding: decode_rgba(bytes), load_rgba(path), load_image(path), decode_hdr(bytes), load_hdr(path).
      • Encoding: save_png(path, image, bgra?) / save_png_rgba(path, w, h, data) (via laplaceliu/tycho_shader).
      • File access: read_file_bytes(path) and read_file_bytes_checked(path) raise ImageError.

    #Example

    let img = @image.load_rgba("assets/logo.png")
    let tex = dev.create_texture_format(img.width, img.height, @tycho.TextureFormat::RGBA8, img.data)

    ImageError

    pub suberror ImageError {
    DecodeFailed(String)
    IoFailed(String)
    EncodeFailed(String)
    }

    HdrImage

    pub(all) struct HdrImage {
    width : Int
    height : Int
    pixels : FixedArray[Float]
    }

    Radiance HDR image: linear RGBA float pixels, row-major.

    HdrImage::is_empty

    fn HdrImage::is_empty(self : HdrImage) -> Bool

    Image

    pub(all) struct Image {
    width : Int
    height : Int
    format : ImageFormat
    data : Bytes
    }

    A decoded image: row-major, tightly packed pixels in format.

    Image::byte_size

    fn Image::byte_size(self : Image) -> Int

    Expected pixel byte count for the pitched layout (RGBA8 / R32Float).

    Image::is_empty

    fn Image::is_empty(self : Image) -> Bool

    Image::new

    fn Image::new(width : Int, height : Int, format : ImageFormat, data : Bytes) -> Image

    Image::rgba8

    fn Image::rgba8(width : Int, height : Int, data : Bytes) -> Image

    ImageFormat

    pub(all) enum ImageFormat {
    RGBA8
    RGBA8Srgb
    R32Float
    } derive(Eq,
    Debug
    )

    StbiGif

    pub(all) struct StbiGif {
    width : Int
    height : Int
    frames : Int
    channels_in_file : Int
    channels : Int
    delays : FixedArray[Int]
    data : Bytes
    }

    Decoded multi-frame animated GIF from stbi_load_gif_from_memory.

    StbiImage16

    pub(all) struct StbiImage16 {
    width : Int
    height : Int
    channels_in_file : Int
    channels : Int
    data : FixedArray[UInt16]
    }

    Decoded 16-bit image from stbi_load_16_from_memory.

    StbiImage8

    pub(all) struct StbiImage8 {
    width : Int
    height : Int
    channels_in_file : Int
    channels : Int
    data : Bytes
    }

    Decoded 8-bit image from stbi_load_from_memory.

    StbiImageF

    pub(all) struct StbiImageF {
    width : Int
    height : Int
    channels_in_file : Int
    channels : Int
    data : FixedArray[Float]
    }

    Decoded 32-bit float image from stbi_loadf_from_memory.

    StbiImageType

    pub(all) enum StbiImageType {
    Unsupported
    Png
    Jpeg
    Bmp
    Tga
    Gif
    Psd
    Hdr
    Pnm
    } derive(Eq,
    Debug
    )

    Image type identified by stbi_info_from_memory.

    StbiInfo

    pub(all) struct StbiInfo {
    width : Int
    height : Int
    channels : Int
    image_type : StbiImageType
    is_16_bit : Bool
    is_hdr : Bool
    is_iphone_png : Bool
    } derive(Eq,
    Debug
    )

    Metadata returned by stbi_info_from_memory.

    StbiOptions

    pub(all) struct StbiOptions {
    flip_vertically : Bool
    unpremultiply_iphone_png : Bool
    convert_iphone_png_to_rgb : Bool
    ldr_to_hdr_gamma : Float
    ldr_to_hdr_scale : Float
    hdr_to_ldr_gamma : Float
    hdr_to_ldr_scale : Float
    }

    Per-call decode options (replaces stb_image's global mutable flags cleanly).

    StbiOptions::default

    fn StbiOptions::default() -> StbiOptions

    decode_hdr

    fn decode_hdr(bytes : Bytes) -> HdrImage raise ImageError

    Decode a Radiance .hdr (or LDR converted to linear float) image from memory to linear RGBA float pixels in pure MoonBit.

    decode_rgba

    fn decode_rgba(bytes : Bytes) -> Image raise ImageError

    Decode any stb_image-supported format (PNG/JPEG/BMP/TGA/GIF/PSD/HDR/PNM) from memory to straight-alpha RGBA8 in pure MoonBit.

    load_hdr

    fn load_hdr(path : String) -> HdrImage raise ImageError

    Read a Radiance .hdr file as linear RGBA float pixels in pure MoonBit.

    load_image

    fn load_image(path : String) -> Image raise ImageError

    Load an image file (PNG/JPEG/BMP/TGA/GIF/PSD/HDR/PNM) to RGBA8 in pure MoonBit.

    load_rgba

    fn load_rgba(path : String) -> Image raise ImageError

    Read and decode an image file to straight-alpha RGBA8.

    read_file_bytes

    fn read_file_bytes(path : String) -> Bytes

    Read raw bytes, returning an empty Bytes when the file cannot be read. The _checked variant is preferred when the caller must distinguish a missing file from an empty one.

    read_file_bytes_checked

    fn read_file_bytes_checked(path : String) -> Bytes raise ImageError

    Read raw bytes, raising IoFailed when the file is missing or empty.

    save_png

    fn save_png(path : String, image : Image, bgra? : Bool) -> Unit raise ImageError

    Encode image as PNG. bgra=true swaps red/blue first, which Vulkan swapchain readbacks on some drivers need.

    save_png_rgba

    fn save_png_rgba(path : String, width : Int, height : Int, data : Bytes, bgra? : Bool) -> Unit raise ImageError

    Convenience overload of save_png for raw readback buffers.

    stbi_info_from_memory

    fn stbi_info_from_memory(buffer : Bytes) -> StbiInfo?

    Probe image dimensions, channels, and format without decoding pixels.

    stbi_is_16_bit_from_memory

    fn stbi_is_16_bit_from_memory(buffer : Bytes) -> Bool

    Return true if the image in buffer has 16 bits per channel.

    stbi_is_hdr_from_memory

    fn stbi_is_hdr_from_memory(buffer : Bytes) -> Bool

    Return true if buffer starts with a Radiance HDR signature.

    stbi_load_16_from_memory

    fn stbi_load_16_from_memory(buffer : Bytes, desired_channels? : Int, options? : StbiOptions) -> StbiImage16?

    Decode a 16-bit image from memory (pure MoonBit stbi_load_16_from_memory).

    stbi_load_from_memory

    fn stbi_load_from_memory(buffer : Bytes, desired_channels? : Int, options? : StbiOptions) -> StbiImage8?

    Decode an 8-bit image from memory (pure MoonBit stbi_load_from_memory).

    stbi_load_gif_from_memory

    fn stbi_load_gif_from_memory(buffer : Bytes, desired_channels? : Int, options? : StbiOptions) -> StbiGif?

    Decode all frames of a GIF from memory (pure MoonBit stbi_load_gif_from_memory).

    stbi_loadf_from_memory

    fn stbi_loadf_from_memory(buffer : Bytes, desired_channels? : Int, options? : StbiOptions) -> StbiImageF?

    Decode a 32-bit float image from memory (pure MoonBit stbi_loadf_from_memory).

    stbi_zlib_decode

    fn stbi_zlib_decode(data : Bytes) -> Bytes?

    Decompress a zlib-framed DEFLATE stream (matches stbi_zlib_decode_malloc).

    stbi_zlib_decode_noheader

    fn stbi_zlib_decode_noheader(data : Bytes) -> Bytes?

    Decompress a raw DEFLATE stream without zlib header (matches stbi_zlib_decode_noheader_malloc).