This repository provides BQN bindings for some of the libspng library, which enables you to decode and encode PNG images.
Libspng doesn't support doing anything fancy with the image once decoded, so these bindings are primarily useful for loading and saving pixel data in PNG format.
cd <your-libspng-directory>
mkdir cbuild
cd cbuild
cmake .. # Don't forget to set optimization level!
make
make install
- Clone this repository:
git clone https://github.com/tankorsmash/bqn-libspng<path to libspng.so> •Import "libspng.bqn"in your BQN code to use the bindings.
Consult the official C examples and this wrapper mirrors it (where it's implemented)
Consult the BQN example.bqn as well for loading an image, editing its pixels, and saving it back to a PNG file.
This is a decode example that loads a PNG file into RGBA8 format. The example.bqn file has this, plus the encoding logic, but I recommend reading the libspng docs and C examples as well.
libspng ← •Import "libspng.bqn"
ctx ← libspng.Spng_ctx_new 0 # init libspng context
buf ← •Fbytes "input.png" # read PNG file
setOut‿newBuf ← libspng.Spng_set_png_buffer ctx‿buf‿(≠buf) # set PNG buffer
ihdrRes‿⟨ihdr⟩ ← libspng.Spng_get_ihdr ctx‿(⋈7⥊0) # get image header
fmt ← libspng.sPNG_FMT_RGBA8
resSize‿⟨newSize⟩ ← libspng.Spng_decoded_image_size ctx‿fmt‿(⋈0) # get size of decoded image
decRes‿decImg ← libspng.Spng_decode_image ctx‿(out)‿(newSize)‿fmt‿libspng.sPNG_DECODE_TRNS # decode partially image
libspng.Spng_ctx_free ctx #free deletes the context
# libspng uses error codes, so we can check if everything went fine like this:
•Show "set error:"⋈ libspng.StrFromPtr libspng.Spng_strerror ⊑setOut
•Show "get_ihdr error:"⋈ libspng.StrFromPtr libspng.Spng_strerror ⊑ihdrRes
•Show "size error:"⋈ libspng.StrFromPtr libspng.Spng_strerror ⊑resSize
•Show "dec res error:"⋈ libspng.StrFromPtr libspng.Spng_strerror ⊑decRes
"error, see above"!0=+´⟨setOut, ihdrRes, resSize, decRes⟩
# see example.bqn for encoding