Skip to content

tankorsmash/bqn-libspng

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

BQN bindings for libspng

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.

Installation

  1. Build libspng by following the instructions there:
cd <your-libspng-directory>
mkdir cbuild
cd cbuild
cmake .. # Don't forget to set optimization level!
make
make install
  1. 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.

Usage

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"
ctxlibspng.Spng_ctx_new 0 # init libspng context
buf•Fbytes "input.png" # read PNG file
setOutnewBuflibspng.Spng_set_png_buffer ctxbuf‿(buf) # set PNG buffer
ihdrRes‿⟨ihdr⟩ ← libspng.Spng_get_ihdr ctx‿(70) # get image header
fmtlibspng.sPNG_FMT_RGBA8
resSize‿⟨newSize⟩ ← libspng.Spng_decoded_image_size ctxfmt‿(0) # get size of decoded image
decResdecImglibspng.Spng_decode_image ctx‿(out)‿(newSize)‿fmtlibspng.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

About

BQN bindings for some of libspng

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages