libpng is a Ruby binding for the official PNG reference library
(libpng). The native shared library
is pre-compiled for each target platform and shipped inside the gem, so
gem install libpng Just Works without a C compiler on the host.
Installation
Add to your Gemfile:
gem "libpng"
Then:
bundle install
Or install manually:
gem install libpng
The gem downloads a pre-compiled binary for your platform automatically. No C compiler, libpng development headers, or system packages are needed at install time.
Usage
require "libpng"
Supported pixel formats
-
"GRAY"/"GRAYSCALE"— 8-bit grayscale (1 byte/pixel) -
"GA"/"AG"— 8-bit grayscale + alpha (2 bytes/pixel) -
"RGB","BGR"— 8-bit truecolor (3 bytes/pixel) -
"RGBA","ARGB","BGRA","ABGR"— 8-bit truecolor + alpha (4 bytes/pixel)
Ractor safety
Libpng.encode and Libpng.decode are Ractor-safe. Every call
allocates and frees its own libpng png_image; no shared mutable state
exists on the Ruby side. Calls from multiple Ractors run concurrently
without locking.
Example:
Versioning
This gem follows a {LIBPNG_VERSION}.{ITERATION} scheme:
-
LIBPNG_VERSIONis the upstream libpng release the gem is built against (currently 1.6.48). -
ITERATIONis a counter that bumps on Ruby-side changes (recipe bug fixes, CI tweaks, doc updates). It resets to 0 each timeLIBPNG_VERSIONbumps.
For example, 1.6.48.0 is the first release against libpng 1.6.48,
1.6.48.1 is a Ruby-side fix, and 1.6.49.0 would be a release against
the next upstream libpng.
How it works
The gem has three layers:
-
lib/libpng.rb— Ruby FFI wrapper. Loadslibpng16.{so,dylib,dll}from the gem’slib/libpng/directory at runtime. -
lib/libpng/recipe.rb— a MiniPortileCMake recipe that downloads the libpng source tarball and runs CMake to produce the shared library. This is invoked byext/extconf.rbwhen the gem is installed from source. -
Rakefiledefinesgem:native:<platform>tasks that pre-compile the gem for each target platform. The pre-compiled gems bundle the.so/.dylib/.dlland disableextconf.rb, so installation requires no C compiler.
License
BSD-2-Clause; see LICENSE.txt. The gem bundles pre-compiled libpng binaries; libpng itself is distributed under the libpng license.