Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Palette quantization library that powers pngquant and other PNG optimizers

NotificationsYou must be signed in to change notification settings

ImageOptim/libimagequant

Repository files navigation

libimagequant — Image Quantization Library

Imagequant library converts RGBA images to palette-based 8-bit indexed images,including alpha component. It's ideal for generating tiny PNG images andnice-looking GIFs.

Image encoding/decoding isn't handled by the library itself, bring your own encoder. If you're looking for a command-line tool, seepngquant.

Getting started in C

This library can be used in C programs viaimagequant-sysRust package.

rustup updategit clone https://github.com/ImageOptim/libimagequantcd libimagequant/imagequant-syscargo build --release --target-dir=target# makes target/release/libimagequant_sys.a

Seethe C library API documentation andthe readme for more info.

Getting started in Rust

Add toCargo.toml:

rustup updatecargo add imagequant

See docs.rs for the library API documentation.

License

Libimagequant is dual-licensed:

  • For Free/Libre Open Source Software it's available under GPL v3 or later with additionalcopyright notices for historical reasons.
  • For use in closed-source software, AppStore distribution, and other non-GPL uses, you canobtain a commercial license. Feel free to askkornel@pngquant.org for details and custom licensing terms if you need them.

Upgrading instructions

libimagequant v2 used to be a C library. libimagequant v4 is written entirely in Rust, but still exports the same C interface for C programs. You will need to install Rust 1.70+ to build it, and adjust your build commands. If you do not want to upgrade, you can keep usingthe C version of the library in the2.x branch of therepo.

C static library users

Files for C/C++ are now in theimagequant-sys/ subdirectory, not in the root of the repo. There is noconfigure && make any more.

To build the library, installRust via rustup, and run:

rustup updatecd imagequant-syscargo build --release

It producestarget/release/libimagequant_sys.a static library. The API, ABI, and header files remain the same, so everything else should work the same.If you're building for macOS or iOS, see included xcodeproj file (add it as asubproject to yours).

If you're building for Android, runrustup target add aarch64-linux-android; cargo build --release --target aarch64-linux-android and usetarget/aarch64-linux-android/release/libimagequant_sys.a. Same for cross-compiling to other platforms. Seerustup target list.

Seeimagequant-sys readme for instructions how to make smaller builds.

C dynamic library for package maintainers

If you're an application developer, please use the static linking option above — that option is much easier, and gives smaller executables.

The API and ABI of this library remains the same. It has the same sover, so it can be a drop-in replacement for the previous C version.

This library is now a typical Rust/Cargo library. If you want to set upoff-line builds oroverride dependencies, it works the same as for every other Rust project. SeeCargo docs for things likecargo fetch orcargo vendor (but I don't recommend vendoring).

If you want to build a dynamic library, but aren't bothered by soname and rpath being wrong, modifyimagequant-sys/Cargo.toml and add"cdylib" to the existingcrate-type property, and thencargo build --release will do its usual half-finished job and buildtarget/release/libimagequant.{so,dylib,dll}.

Building withmake

configure && make is gone. I hoped I could build a dynamic library just by wrapping the static library, but apparently that won't work, so I can't easily recreate the oldmake install. I wish there was a more standard and lightweight solution than using thecargo-c tool, so if you're good at wrangling linker flags and symbol visibility, please send pull requests.

Building withcargo-c

Thecargo-c tool knows how to build and link so/dylib properly, and generates an accurate pkg-config file, so it's de-facto required for a correct system-wide install of a dynamic library.

rustup updatecd imagequant-syscargo install cargo-ccargo cinstall --prefix=/usr/local --destdir=.

This makes Rust 1.70 andcargo-c package a build-time dependency. No runtime deps (apart from Cargo-internal ones). OpenMP has been dropped entirely.

Interaction with pngquant

pngquant v2 can use this library as a dynamic library. However, pngquant v4 does not support unbundling. It uses this library as a Cargo dependency via its Rust-native interface. The shared libimagequant library exports only a stable ABI for C programs, and this interface is not useful for Rust programs.

Upgrading for Rust users

If you've used theimagequant-sys crate, switch to the higher-levelimagequant crate. Theimagequant v4 is almost entirely backwards-compatible, with small changes that the Rust compiler will point out (e.g. changed use ofc_int tou32). Seedocs. Please fix any deprecation warnings you may get, because the deprecated functions will be removed.

Theopenmp Cargo feature has been renamed tothreads.

.new_image() can now take ownership of its argument to avoid copying. If you get an error thatFrom<&Vec<RGBA>> is not implemented, then either don't pass by reference (moves, avoids copying), or call.as_slice() on it (to copy the pixels), or use.new_image_borrowed() method instead.

Threads support and WASM

By default, when thethreads Cargo feature is enabled, this library uses multi-threading. Number of threads can be controlled viaRAYON_NUM_THREADS environment variable.

Threads in WASM are experimental, and requirespecial handling. If you're targeting WASM, you'll most likely want to disable threads.

To disable threads when using this library as a dependency, disable default features like this inCargo.toml:

[dependencies]imagequant = {version ="4.0",default-features =false }

When you compile the library directly, add--no-default-features flag instead.


[8]ページ先頭

©2009-2025 Movatter.jp