LibSTBImage

forked from calebuharrison/LibSTBImage
Crystal bindings for stb_image

LibSTBImage

Crystal bindings for stb_image.

Installation

Add this to your application's shard.yml:

dependencies:
  lib_stb_image:
    github: calebuharrison/LibSTBImage

Install your dependencies:

shards install

Usage

Garnet callers can use Garnet::Graphics::STBDecoder from garnet-graphics-stb to get a managed RGBA8 image and enforce encoded and decoded size limits. Direct FFI callers own the allocation returned by LibSTBImage.load. Check for a null pointer and release successful results with LibSTBImage.image_free after the last consumer has finished reading them. A Crystal slice of these bytes borrows the allocation and is valid only until it is freed.

require "lib_stb_image"

image_data = LibSTBImage.load("path/to/image/file", out width, out height, out source_channels, LibSTBImage::Channels::Default)
raise IO::Error.new("Could not decode image") if image_data.null?

begin
  puts "#{width} × #{height} pixels with #{source_channels} source channels"
ensure
  LibSTBImage.image_free(image_data.as(Void*))
end

Specify a non-default output channel count to request conversion. The output parameter source_channels describes the encoded image's channels. See the stb_image API comments for the native function contracts.

Contributing

  1. Fork it ( https://github.com/calebuharrison/lib_stb_image/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

Repository

LibSTBImage

Owner
Statistic
  • 0
  • 0
  • 0
  • 1
  • 0
  • 1 day ago
  • September 13, 2020
License

MIT License

Links
Synced at

Wed, 02 Dec 2026 02:09:44 GMT

Languages