LibSTBImage
forked from calebuharrison/LibSTBImageLibSTBImage
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
- Fork it ( https://github.com/calebuharrison/lib_stb_image/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
Contributors
- calebuharrison Caleb Uriah Harrison - creator, maintainer
LibSTBImage
- 0
- 0
- 0
- 1
- 0
- 1 day ago
- September 13, 2020
MIT License
Wed, 02 Dec 2026 02:09:44 GMT