crossbind
GitHub
← Alle Bibliotheken

WebP

WebP-Bildcodec.

Installation

Läuft in Browsern; Node.js und Edge-Laufzeitumgebungen nutzen dasselbe Paket. Gebaut für wasm32, mit einem Thread und mit mehreren Threads.

shell
npm install @crossbind/port-webp-wasm@beta
crossbind.config.js
import webpWasm from '@crossbind/port-webp-wasm/crossbind.config.js';
 
export default {
dependencies: [webpWasm],
paths: { config: import.meta.url },
};
main.js
import { initNative, WebPGetEncoderVersion } from '@crossbind/port-webp/webp/encode.h';
 
await initNative();

crossbind selbst kommt mit deinem Bundler-Plugin; Bundler-Plugins behandelt Vite, Webpack, Rspack, Rsbuild, Rollup, esbuild, Bun und Next.js.

WebP-Kodierung mit den Einstellungen, die ein canvas nicht hat

Jeder aktuelle Browser zeigt WebP an, aber ein canvas kann es nur mit einer Qualitätsstufe kodieren, und das canvas von Safari kann es überhaupt nicht kodieren. Diese Apps führen libwebp 1.6.0 selbst aus, kompiliert von crossbind: Sie zeigen, was jede Qualitätsstufe kostet und preisgibt, bringen einen Sticker unter ein Byte-Budget und dekodieren eine Datei, während ihre Bytes ankommen. Der erste Lauf lädt einmalig 0,8 MB WebAssembly herunter; alle Apps auf dieser Seite teilen es, und nichts wird hochgeladen.

APP 01

Sehen, was jede WebP-Qualitätsstufe kostet und was dabei verloren geht

Kodiere das Testbild oder dein eigenes Bild mit libwebp 1.6.0 und vergleiche das Ergebnis dann mit dem Original, neben Größe, PSNR und SSIM. Einem canvas lassen sich nur ein Typ und eine Qualität mitgeben, und Safari kann WebP aus einem canvas überhaupt nicht kodieren (caniuse). Hier bekommst du außerdem verlustfrei, nahezu verlustfrei, Inhalts-Presets, Aufwand und Sharp YUV.

Das Testbild wird im Modul generiert, daher sind seine Zahlen in jedem Browser gleich. Dein eigenes Bild bleibt in diesem Tab; eines mit mehr als 1.600 px an einer Seite wird zuerst verkleinert. Nach dem ersten Lauf kodiert jede Änderung erneut.

Kodiere das Testbild oder öffne ein Bild, um es Seite an Seite mit seinem WebP zu vergleichen.
CODE ANZEIGEN
src/native/webp_studio.h
// src/native/webp_studio.h (excerpt): one encode, then measure what it decodes to
WebPConfig config;
WebPConfigPreset(&config, lossy ? presetNamed(preset) : WEBP_PRESET_DEFAULT, quality);
config.method = method;
if (lossy) {
config.use_sharp_yuv = sharpYuv ? 1 : 0;
} else {
config.lossless = 1;
config.near_lossless = mode == "near-lossless" ? nearLossless : 100;
}
const std::vector<uint8_t> webp = imaging::encode(config, rgba, width, height);
const imaging::Fidelity fidelity = imaging::measure(rgba, imaging::decode(webp).rgba, width, height);
main.js
const m = await initNative();
const pixels = canvas.getContext('2d').getImageData(0, 0, 512, 384).data;
// m.FS.writeFile appends to an existing file here: unlink it first when you replace one
await m.FS.writeFile('/memfs/webpapps/studio/source.rgba', new Uint8Array(pixels.buffer));
 
const report = JSON.parse(await m.WebpStudio.encode('/memfs/webpapps/studio/source.rgba', 512, 384,
'/memfs/webpapps/studio/out.webp', 'lossy', 75, 100, 'default', 4, false));
// the test card: report.bytes 14852, report.psnr 29.37, report.ssim 0.9561
const webp = await m.getFileBytes('/memfs/webpapps/studio/out.webp');
APP 02

Jedes Bild in einen Sticker verwandeln, der ins Limit passt

WhatsApp verlangt Sticker als WebP, genau 512 × 512 und höchstens 100 KB groß, und empfiehlt einen weißen Rand von 8 px (sein Sticker-Leitfaden). libwebp schneidet den leeren Rand ab, passt den Rest in das Quadrat ein, erhält die Transparenz exakt und findet die höchste Qualität, die unter dem Budget bleibt.

Das Beispiel ist ein Smiley, im Modul auf transparentem Hintergrund gezeichnet. Ein PNG mit Transparenz ergibt den besten Sticker; ein Foto wird zu einem gerahmten Quadrat. Nichts wird hochgeladen.

Erstelle den Beispiel-Sticker oder bring ein PNG mit Transparenz mit, um ihn in hellen und dunklen Chats zu sehen.
CODE ANZEIGEN
src/native/sticker_maker.h
// src/native/sticker_maker.h (excerpt): trim, fit, then the best quality under the budget
WebPPictureCrop(&picture, box.left, box.top, box.width, box.height); // drop empty borders
WebPPictureRescale(&picture, fitWidth, fitHeight); // aspect kept
// ... centre it on a transparent 512x512 canvas, draw the 8 px white outline ...
if (!attempt(100)) {
int low = 0;
int high = 99;
while (low <= high) {
const int quality = (low + high) / 2;
if (attempt(quality)) low = quality + 1;
else high = quality - 1;
}
}
main.js
const m = await initNative();
// any size, with alpha; m.FS.writeFile appends to an existing file here, so unlink it first when you replace one
await m.FS.writeFile('/memfs/webpapps/sticker/source.rgba', rgba);
 
const report = JSON.parse(await m.StickerMaker.make('/memfs/webpapps/sticker/source.rgba', 600, 400,
true, 100000, '/memfs/webpapps/sticker/sticker.webp', '/memfs/webpapps/sticker/canvas.rgba'));
// the sample: 33,610 B at quality 100; under a 12,000 B budget, 11,952 B at quality 12
const sticker = await m.getFileBytes('/memfs/webpapps/sticker/sticker.webp');
APP 03

Zusehen, wie ein WebP Zeile für Zeile ankommt

Bei einer langsamen Verbindung baut sich ein Bild von oben auf. Der inkrementelle Decoder von libwebp tut das für WebP: Gib ihm die Bytes, die angekommen sind, und er liefert jede Zeile, die er schon fertigstellen kann. Spiele den Download ab oder ziehe Byte für Byte durch die Datei.

Das Beispiel ist das Testbild aus WebP Studio bei Qualität 75. Deine eigene Datei wird im Speicher eingehängt und bleibt in diesem Tab. Animierte Dateien werden nicht inkrementell dekodiert.

Streame das Testbild oder öffne eine eigene .webp, um das Dekodieren zu verfolgen, während die Bytes ankommen.
CODE ANZEIGEN
src/native/webp_stream.h
// src/native/webp_stream.h (excerpt): decode whatever has arrived
std::unique_ptr<WebPIDecoder, void (*)(WebPIDecoder*)> decoder(WebPINewRGB(MODE_RGBA, nullptr, 0, 0), WebPIDelete);
const VP8StatusCode status = WebPIUpdate(decoder.get(), data.data(), available); // SUSPENDED: wants more
int rows = 0, width = 0, height = 0, stride = 0;
const uint8_t* rgba = WebPIDecGetRGB(decoder.get(), &rows, &width, &height, &stride);
// rows 0 to rows - 1 are final: copy them into the frame, leave the rest transparent
main.js
const m = await initNative();
const report = JSON.parse(await m.WebpStream.decodePrefix('/memfs/webpapps/stream/card.webp', 7426,
'/memfs/webpapps/stream/frame.rgba'));
// the test card at quality 75 is 14,852 B: half of it gives 135 of its 384 rows
const frame = await m.getFileBytes('/memfs/webpapps/stream/frame.rgba');
context.putImageData(new ImageData(new Uint8ClampedArray(frame.buffer), report.width, report.height), 0, 0);

Beispiele

Jedes besteht aus einer kleinen C++-Header-Datei und dem JavaScript, das sie aufruft, mit dem, was es beim Bauen der Website ausgegeben hat. Der Tab Nur JavaScript tut dasselbe mit den eigenen Header-Dateien von WebP und ohne C++-Datei. Das letzte ist ein ganzes C++-Programm, gebaut für WASI.

Pixel als WebP kodieren und zurück dekodieren

Die einfache API, mit der der meiste Code beginnt: WebPEncodeRGBA mit einer Qualitätsstufe, WebPEncodeLosslessRGBA, WebPGetInfo für die Größe im Header und WebPDecodeRGBA für die Pixel. Die Eingabe ist eine erzeugte Landschaft mit 256×256 Pixeln und etwas Rauschen, stellvertretend für ein Foto.

src/native/webp_codec.h
#pragma once
 
#include <webp/decode.h>
#include <webp/encode.h>
 
#include <cstdint>
#include <stdexcept>
#include <string>
 
// libwebp's simple API. Bytes cross the binding as a byte string: one UTF-16 code unit (0-255) per byte.
class WebpCodec {
public:
// WebPGetEncoderVersion() packs the version into one integer, 0xMMmmpp.
static std::string version() {
const int packed = WebPGetEncoderVersion();
return std::to_string(packed >> 16) + "." + std::to_string((packed >> 8) & 0xff) + "." + std::to_string(packed & 0xff);
}
 
// `rgba` is 4 bytes per pixel, row after row. Quality runs from 0 (smallest file) to 100 (best).
static std::u16string encode(const std::u16string& rgba, int width, int height, float quality) {
const std::string pixels = checkedPixels(rgba, width, height);
uint8_t* output = nullptr;
const size_t size = WebPEncodeRGBA(reinterpret_cast<const uint8_t*>(pixels.data()), width, height, width * 4, quality, &output);
return take(output, size);
}
 
static std::u16string encodeLossless(const std::u16string& rgba, int width, int height) {
const std::string pixels = checkedPixels(rgba, width, height);
uint8_t* output = nullptr;
const size_t size = WebPEncodeLosslessRGBA(reinterpret_cast<const uint8_t*>(pixels.data()), width, height, width * 4, &output);
return take(output, size);
}
 
// "<width>x<height>", read from the file header without decoding the image.
static std::string dimensions(const std::u16string& webp) {
const std::string data = fromUnits(webp);
int width = 0;
int height = 0;
if (!WebPGetInfo(reinterpret_cast<const uint8_t*>(data.data()), data.size(), &width, &height)) throw std::runtime_error("not a WebP image");
return std::to_string(width) + "x" + std::to_string(height);
}
 
static std::u16string decode(const std::u16string& webp) {
const std::string data = fromUnits(webp);
int width = 0;
int height = 0;
uint8_t* rgba = WebPDecodeRGBA(reinterpret_cast<const uint8_t*>(data.data()), data.size(), &width, &height);
if (!rgba) throw std::runtime_error("not a decodable WebP image");
return take(rgba, static_cast<size_t>(width) * height * 4);
}
 
private:
static std::string checkedPixels(const std::u16string& rgba, int width, int height) {
if (width <= 0 || height <= 0 || rgba.size() != static_cast<size_t>(width) * height * 4) {
throw std::invalid_argument("expected width * height * 4 bytes of RGBA");
}
return fromUnits(rgba);
}
 
// Copies memory libwebp allocated into a byte string, then releases it with WebPFree.
static std::u16string take(uint8_t* data, size_t size) {
if (!data || size == 0) {
WebPFree(data);
throw std::runtime_error("WebP encoding failed");
}
std::u16string units(data, data + size);
WebPFree(data);
return units;
}
 
static std::string fromUnits(const std::u16string& units) {
std::string data(units.size(), '\0');
for (size_t i = 0; i < units.size(); ++i) {
if (units[i] > 0xFF) throw std::invalid_argument("not a byte string");
data[i] = static_cast<char>(units[i]);
}
return data;
}
};
main.js
import { initNative, WebpCodec } from './native/webp_codec.h';
 
await initNative();
let seed = 1;
const random = (n) => (seed = (seed * 48271) % 2147483647) % n;
let rgba = '';
for (let y = 0; y < 256; y += 1) {
for (let x = 0; x < 256; x += 1) {
const sun = (x - 180) ** 2 + (y - 70) ** 2 < 900;
const hill = y > 170 + (((x - 128) ** 2) >> 8);
const [r, g, b] = sun ? [255, 214, 90] : hill ? [40 + (y >> 2), 120 + (x >> 3), 50] : [90 + (y >> 1), 150 + (y >> 2), 235];
rgba += String.fromCharCode(r ^ random(8), g ^ random(8), b ^ random(8), 255);
}
}
 
const lossy = await WebpCodec.encode(rgba, 256, 256, 80);
const lossless = await WebpCodec.encodeLossless(rgba, 256, 256);
console.log(await WebpCodec.version(), await WebpCodec.dimensions(lossy));
console.log(`RGBA ${rgba.length} B -> lossy q80 ${lossy.length} B, lossless ${lossless.length} B`);
console.log((await WebpCodec.decode(lossless)) === rgba);
GIBT AUSder erste Lauf lädt 0,8 MB herunter
1.6.0 256x256
RGBA 262144 B -> lossy q80 1966 B, lossless 87058 B
true

Den Encoder abstimmen und messen, was verloren ging

Die erweiterte API: WebPConfigPreset wählt Einstellungen für die Art des Bildes, dann passen Methode, scharfes YUV und eine Zielgröße sie vor WebPEncode an. WebPPlaneDistortion misst PSNR und SSIM gegenüber dem Original, sodass sich eine Qualitätsstufe nach Zahlen wählen lässt.

src/native/webp_encoder.h
#pragma once
 
#include <webp/decode.h>
#include <webp/encode.h>
 
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <stdexcept>
#include <string>
 
// libwebp's advanced API: a WebPConfig started from a content preset and adjusted field by field,
// then WebPEncode on a WebPPicture. Bytes cross the binding as one UTF-16 code unit (0-255) per byte.
class WebpEncoder {
public:
// preset: "default", "photo", "picture" (indoor and portrait shots), "drawing", "icon" or "text".
WebpEncoder(const std::string& preset, float quality) {
if (!WebPConfigPreset(&config, presetNamed(preset), quality)) throw std::runtime_error("libwebp version mismatch");
}
 
// 0 is the fastest; 6 is the slowest and usually the smallest. The default is 4.
void setMethod(int method) { config.method = method; }
 
// A slower RGB to YUV conversion that keeps colour edges sharper.
void setSharpYuv(bool enabled) { config.use_sharp_yuv = enabled ? 1 : 0; }
 
// The encoder searches for the quality that lands closest to this many bytes, over up to 6 passes.
void setTargetSize(int bytes) {
config.target_size = bytes;
config.pass = 6;
}
 
std::u16string encode(const std::u16string& rgba, int width, int height) const {
if (!WebPValidateConfig(&config)) throw std::invalid_argument("invalid encoder settings");
if (width <= 0 || height <= 0 || rgba.size() != static_cast<size_t>(width) * height * 4) {
throw std::invalid_argument("expected width * height * 4 bytes of RGBA");
}
const std::string pixels = fromUnits(rgba);
WebPPicture picture;
if (!WebPPictureInit(&picture)) throw std::runtime_error("libwebp version mismatch");
picture.width = width;
picture.height = height;
// ARGB input lets WebPEncode do the YUV conversion itself, which is where sharp YUV applies.
picture.use_argb = 1;
WebPMemoryWriter writer;
WebPMemoryWriterInit(&writer);
picture.writer = WebPMemoryWrite;
picture.custom_ptr = &writer;
const bool ok = WebPPictureImportRGBA(&picture, reinterpret_cast<const uint8_t*>(pixels.data()), width * 4) && WebPEncode(&config, &picture);
const WebPEncodingError error = picture.error_code;
WebPPictureFree(&picture);
if (!ok) {
WebPMemoryWriterClear(&writer);
throw std::runtime_error("WebP encoding failed with error " + std::to_string(error));
}
std::u16string units(writer.mem, writer.mem + writer.size);
WebPMemoryWriterClear(&writer);
return units;
}
 
// What the encoder gave away: PSNR over red, green and blue, and libwebp's SSIM over the same
// channels (1 means identical), between `rgba` and the pixels `webp` decodes to.
static std::string compare(const std::u16string& rgba, const std::u16string& webp) {
const std::string original = fromUnits(rgba);
const std::string data = fromUnits(webp);
int width = 0;
int height = 0;
uint8_t* decoded = WebPDecodeRGBA(reinterpret_cast<const uint8_t*>(data.data()), data.size(), &width, &height);
if (!decoded) throw std::runtime_error("not a decodable WebP image");
if (original.size() != static_cast<size_t>(width) * height * 4) {
WebPFree(decoded);
throw std::invalid_argument("the RGBA and the WebP image differ in size");
}
const uint8_t* source = reinterpret_cast<const uint8_t*>(original.data());
double squaredError = 0;
double similarity = 0;
for (int channel = 0; channel < 3; ++channel) {
float sum = 0;
float perChannel = 0;
WebPPlaneDistortion(source + channel, width * 4, decoded + channel, width * 4, width, height, 4, 0, &sum, &perChannel);
squaredError += sum;
WebPPlaneDistortion(source + channel, width * 4, decoded + channel, width * 4, width, height, 4, 1, &sum, &perChannel);
similarity += sum;
}
WebPFree(decoded);
const double samples = 3.0 * width * height;
const double psnr = squaredError > 0 ? 10 * std::log10(255.0 * 255.0 * samples / squaredError) : 99.0;
char text[64];
std::snprintf(text, sizeof text, "PSNR %.2f dB, SSIM %.4f", psnr, similarity / samples);
return text;
}
 
private:
static WebPPreset presetNamed(const std::string& name) {
if (name == "default") return WEBP_PRESET_DEFAULT;
if (name == "photo") return WEBP_PRESET_PHOTO;
if (name == "picture") return WEBP_PRESET_PICTURE;
if (name == "drawing") return WEBP_PRESET_DRAWING;
if (name == "icon") return WEBP_PRESET_ICON;
if (name == "text") return WEBP_PRESET_TEXT;
throw std::invalid_argument("unknown preset " + name);
}
 
static std::string fromUnits(const std::u16string& units) {
std::string data(units.size(), '\0');
for (size_t i = 0; i < units.size(); ++i) {
if (units[i] > 0xFF) throw std::invalid_argument("not a byte string");
data[i] = static_cast<char>(units[i]);
}
return data;
}
 
WebPConfig config;
};
main.js
import { initNative, WebpEncoder } from './native/webp_encoder.h';
 
await initNative();
let seed = 1;
const random = (n) => (seed = (seed * 48271) % 2147483647) % n;
let rgba = '';
for (let y = 0; y < 256; y += 1) {
for (let x = 0; x < 256; x += 1) {
const sun = (x - 180) ** 2 + (y - 70) ** 2 < 900;
const hill = y > 170 + (((x - 128) ** 2) >> 8);
const [r, g, b] = sun ? [255, 214, 90] : hill ? [40 + (y >> 2), 120 + (x >> 3), 50] : [90 + (y >> 1), 150 + (y >> 2), 235];
rgba += String.fromCharCode(r ^ random(8), g ^ random(8), b ^ random(8), 255);
}
}
 
for (const quality of [50, 75, 90]) {
const encoder = await new WebpEncoder('photo', quality);
const webp = await encoder.encode(rgba, 256, 256);
console.log(`photo q${quality}: ${webp.length} B, ${await WebpEncoder.compare(rgba, webp)}`);
}
 
const slow = await new WebpEncoder('photo', 75);
await slow.setMethod(6);
await slow.setSharpYuv(true);
const tuned = await slow.encode(rgba, 256, 256);
console.log(`photo q75, method 6, sharp YUV: ${tuned.length} B, ${await WebpEncoder.compare(rgba, tuned)}`);
 
const budget = await new WebpEncoder('photo', 75);
await budget.setTargetSize(3000);
const fitted = await budget.encode(rgba, 256, 256);
console.log(`photo, 3000 B target: ${fitted.length} B, ${await WebpEncoder.compare(rgba, fitted)}`);
GIBT AUSder erste Lauf lädt 0,8 MB herunter
photo q50: 1300 B, PSNR 33.64 dB, SSIM 0.8993
photo q75: 1658 B, PSNR 34.26 dB, SSIM 0.9047
photo q90: 4388 B, PSNR 35.02 dB, SSIM 0.9133
photo q75, method 6, sharp YUV: 1708 B, PSNR 34.58 dB, SSIM 0.9050
photo, 3000 B target: 3000 B, PSNR 34.83 dB, SSIM 0.9097

Transparenz erhalten und entscheiden, was darunter passiert

Ein verlustbehaftetes WebP speichert Alpha in einer eigenen Ebene: alpha_quality 100 erhält es exakt, niedrigere Werte tauschen es gegen Bytes. Verlustfrei bleibt jedes sichtbare Pixel erhalten, die Farbe unter vollständig transparenten Pixeln kann sich aber ändern, damit besser komprimiert wird, sofern exact nicht gesetzt ist.

src/native/webp_alpha.h
#pragma once
 
#include <webp/decode.h>
#include <webp/encode.h>
 
#include <cstdint>
#include <stdexcept>
#include <string>
 
// Transparency in WebP. Lossy files keep alpha in a plane of its own with its own quality; lossless
// files keep it exactly. Bytes cross the binding as one UTF-16 code unit (0-255) per byte.
class WebpAlpha {
public:
// Lossy colour at `quality`, alpha at `alphaQuality` (100 keeps it lossless, lower values shrink it).
static std::u16string encodeLossy(const std::u16string& rgba, int width, int height, float quality, int alphaQuality) {
WebPConfig config;
if (!WebPConfigInit(&config)) throw std::runtime_error("libwebp version mismatch");
config.quality = quality;
config.alpha_quality = alphaQuality;
return encode(config, rgba, width, height);
}
 
// Lossless. Without `exact`, libwebp may change the colour under fully transparent pixels,
// which nobody sees, to compress better.
static std::u16string encodeLossless(const std::u16string& rgba, int width, int height, bool exact) {
WebPConfig config;
if (!WebPConfigInit(&config)) throw std::runtime_error("libwebp version mismatch");
config.lossless = 1;
config.exact = exact ? 1 : 0;
return encode(config, rgba, width, height);
}
 
static std::u16string decode(const std::u16string& webp) {
const std::string data = fromUnits(webp);
int width = 0;
int height = 0;
uint8_t* rgba = WebPDecodeRGBA(reinterpret_cast<const uint8_t*>(data.data()), data.size(), &width, &height);
if (!rgba) throw std::runtime_error("not a decodable WebP image");
std::u16string units(rgba, rgba + static_cast<size_t>(width) * height * 4);
WebPFree(rgba);
return units;
}
 
private:
static std::u16string encode(const WebPConfig& config, const std::u16string& rgba, int width, int height) {
if (!WebPValidateConfig(&config)) throw std::invalid_argument("invalid encoder settings");
if (width <= 0 || height <= 0 || rgba.size() != static_cast<size_t>(width) * height * 4) {
throw std::invalid_argument("expected width * height * 4 bytes of RGBA");
}
const std::string pixels = fromUnits(rgba);
WebPPicture picture;
if (!WebPPictureInit(&picture)) throw std::runtime_error("libwebp version mismatch");
picture.width = width;
picture.height = height;
picture.use_argb = 1;
WebPMemoryWriter writer;
WebPMemoryWriterInit(&writer);
picture.writer = WebPMemoryWrite;
picture.custom_ptr = &writer;
const bool ok = WebPPictureImportRGBA(&picture, reinterpret_cast<const uint8_t*>(pixels.data()), width * 4) && WebPEncode(&config, &picture);
WebPPictureFree(&picture);
if (!ok) {
WebPMemoryWriterClear(&writer);
throw std::runtime_error("WebP encoding failed");
}
std::u16string units(writer.mem, writer.mem + writer.size);
WebPMemoryWriterClear(&writer);
return units;
}
 
static std::string fromUnits(const std::u16string& units) {
std::string data(units.size(), '\0');
for (size_t i = 0; i < units.size(); ++i) {
if (units[i] > 0xFF) throw std::invalid_argument("not a byte string");
data[i] = static_cast<char>(units[i]);
}
return data;
}
};
main.js
import { initNative, WebpAlpha } from './native/webp_alpha.h';
 
await initNative();
let rgba = '';
for (let y = 0; y < 128; y += 1) {
for (let x = 0; x < 128; x += 1) {
const d = (x - 64) ** 2 + (y - 64) ** 2;
const alpha = d < 1600 ? 255 : d < 2304 ? Math.floor(((2304 - d) * 255) / 704) : 0;
rgba += alpha ? String.fromCharCode(255 - x, 2 * y, 40, alpha) : String.fromCharCode(2 * x, 2 * y, 77, 0);
}
}
const alphaOf = (pixels) => Array.from({ length: pixels.length / 4 }, (_, i) => pixels[i * 4 + 3]).join('');
 
for (const alphaQuality of [100, 50]) {
const webp = await WebpAlpha.encodeLossy(rgba, 128, 128, 80, alphaQuality);
const alphaKept = alphaOf(await WebpAlpha.decode(webp)) === alphaOf(rgba);
console.log(`lossy q80, alpha quality ${alphaQuality}: ${webp.length} B, alpha unchanged: ${alphaKept}`);
}
for (const exact of [false, true]) {
const webp = await WebpAlpha.encodeLossless(rgba, 128, 128, exact);
console.log(`lossless${exact ? ', exact' : ''}: ${webp.length} B, identical: ${(await WebpAlpha.decode(webp)) === rgba}`);
}
GIBT AUSder erste Lauf lädt 0,8 MB herunter
lossy q80, alpha quality 100: 1890 B, alpha unchanged: true
lossy q80, alpha quality 50: 1472 B, alpha unchanged: false
lossless: 1710 B, identical: false
lossless, exact: 1982 B, identical: true

Ein WebP untersuchen und dann nur das Nötige dekodieren

WebPGetFeatures liest Größe, Alpha, Animation und verlustbehaftet oder verlustfrei aus dem Header. WebPDecode mit einer WebPDecoderConfig skaliert beim Dekodieren auf ein Vorschaubild oder schneidet einen Bereich aus. Die Datei kommt vom Encoder des ersten Beispiels.

src/native/webp_reader.h
#pragma once
 
#include <webp/decode.h>
 
#include <cstdint>
#include <stdexcept>
#include <string>
 
// Reading WebP files: what the header says, and decoding with WebPDecoderConfig, which can scale or
// crop while it decodes. Bytes cross the binding as one UTF-16 code unit (0-255) per byte.
class WebpReader {
public:
// {"width","height","hasAlpha","hasAnimation","format"} from the header; format is "lossy", "lossless" or "mixed".
static std::string features(const std::u16string& webp) {
const std::string data = fromUnits(webp);
WebPBitstreamFeatures features;
if (WebPGetFeatures(reinterpret_cast<const uint8_t*>(data.data()), data.size(), &features) != VP8_STATUS_OK) {
throw std::runtime_error("not a WebP image");
}
const char* format = features.format == 1 ? "lossy" : features.format == 2 ? "lossless" : "mixed";
return "{\"width\":" + std::to_string(features.width) + ",\"height\":" + std::to_string(features.height) +
",\"hasAlpha\":" + (features.has_alpha ? "true" : "false") + ",\"hasAnimation\":" + (features.has_animation ? "true" : "false") +
",\"format\":\"" + format + "\"}";
}
 
// The whole image, resized to width x height while it decodes.
static std::u16string decodeScaled(const std::u16string& webp, int width, int height) {
WebPDecoderConfig config;
if (!WebPInitDecoderConfig(&config)) throw std::runtime_error("libwebp version mismatch");
config.options.use_scaling = 1;
config.options.scaled_width = width;
config.options.scaled_height = height;
return decode(webp, config);
}
 
// Only the width x height rectangle at (left, top) comes out as RGBA.
static std::u16string decodeRegion(const std::u16string& webp, int left, int top, int width, int height) {
WebPDecoderConfig config;
if (!WebPInitDecoderConfig(&config)) throw std::runtime_error("libwebp version mismatch");
config.options.use_cropping = 1;
config.options.crop_left = left;
config.options.crop_top = top;
config.options.crop_width = width;
config.options.crop_height = height;
return decode(webp, config);
}
 
private:
static std::u16string decode(const std::u16string& webp, WebPDecoderConfig& config) {
const std::string data = fromUnits(webp);
config.output.colorspace = MODE_RGBA;
const VP8StatusCode status = WebPDecode(reinterpret_cast<const uint8_t*>(data.data()), data.size(), &config);
if (status != VP8_STATUS_OK) throw std::runtime_error("WebP decoding failed with status " + std::to_string(status));
const WebPRGBABuffer& buffer = config.output.u.RGBA;
std::u16string units(buffer.rgba, buffer.rgba + buffer.size);
WebPFreeDecBuffer(&config.output);
return units;
}
 
static std::string fromUnits(const std::u16string& units) {
std::string data(units.size(), '\0');
for (size_t i = 0; i < units.size(); ++i) {
if (units[i] > 0xFF) throw std::invalid_argument("not a byte string");
data[i] = static_cast<char>(units[i]);
}
return data;
}
};
main.js
import { initNative, WebpReader } from './native/webp_reader.h';
import { WebpCodec } from './native/webp_codec.h';
 
await initNative();
let seed = 1;
const random = (n) => (seed = (seed * 48271) % 2147483647) % n;
let rgba = '';
for (let y = 0; y < 256; y += 1) {
for (let x = 0; x < 256; x += 1) {
const sun = (x - 180) ** 2 + (y - 70) ** 2 < 900;
const hill = y > 170 + (((x - 128) ** 2) >> 8);
const [r, g, b] = sun ? [255, 214, 90] : hill ? [40 + (y >> 2), 120 + (x >> 3), 50] : [90 + (y >> 1), 150 + (y >> 2), 235];
rgba += String.fromCharCode(r ^ random(8), g ^ random(8), b ^ random(8), 255);
}
}
const lossy = await WebpCodec.encode(rgba, 256, 256, 80);
const lossless = await WebpCodec.encodeLossless(rgba, 256, 256);
 
for (const webp of [lossy, lossless]) {
const info = JSON.parse(await WebpReader.features(webp));
console.log(`${info.width}x${info.height} ${info.format}, alpha: ${info.hasAlpha}, animated: ${info.hasAnimation}`);
}
const pixel = (pixels, width, x, y) => [...pixels.slice((y * width + x) * 4, (y * width + x) * 4 + 4)].map((c) => c.charCodeAt(0)).join(', ');
const thumbnail = await WebpReader.decodeScaled(lossy, 64, 64);
console.log(`thumbnail 64x64: ${thumbnail.length} B, the sun at (45, 17) is rgba(${pixel(thumbnail, 64, 45, 17)})`);
const region = await WebpReader.decodeRegion(lossy, 150, 40, 60, 60);
console.log(`region 60x60 from (150, 40): ${region.length} B, its centre is rgba(${pixel(region, 60, 30, 30)})`);
GIBT AUSder erste Lauf lädt 0,8 MB herunter
256x256 lossy, alpha: false, animated: false
256x256 lossless, alpha: false, animated: false
thumbnail 64x64: 16384 B, the sun at (45, 17) is rgba(251, 211, 93, 255)
region 60x60 from (150, 40): 14400 B, its centre is rgba(251, 211, 93, 255)

Ein Kommandozeilen-Tool für WebP

WASI hat keine JavaScript-Bindings: Das Programm ist ein main(), das libwebp linkt, zu einer einzigen .wasm gebaut und mit wasmtime ausgeführt. Dieses hier kodiert ein PPM mit einer Qualitätsstufe als WebP und liest einen WebP-Header zurück.

crossbind.config.js
import webpWasi from '@crossbind/port-webp-wasi/crossbind.config.js';
 
export default {
general: { name: 'webp-tool' },
dependencies: [webpWasi],
paths: { config: import.meta.url },
};
src/native/main.cpp
// A command-line WebP tool for WASI:
// webp-tool encode <input.ppm> <output.webp> [quality]
// webp-tool info <input.webp>
#include <webp/decode.h>
#include <webp/encode.h>
 
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <string>
#include <vector>
 
namespace {
 
using File = std::unique_ptr<FILE, int (*)(FILE*)>;
 
std::string version() {
const int packed = WebPGetEncoderVersion();
return std::to_string(packed >> 16) + "." + std::to_string((packed >> 8) & 0xff) + "." + std::to_string(packed & 0xff);
}
 
bool readFile(const char* path, std::vector<uint8_t>& data) {
File file(std::fopen(path, "rb"), std::fclose);
if (!file) {
std::fprintf(stderr, "cannot open %s\n", path);
return false;
}
std::vector<uint8_t> chunk(1 << 16);
size_t got = 0;
while ((got = std::fread(chunk.data(), 1, chunk.size(), file.get())) > 0) data.insert(data.end(), chunk.begin(), chunk.begin() + got);
return true;
}
 
// Binary PPM: "P6", width, height and 255, separated by whitespace or # comments, one whitespace, then RGB.
bool parsePpm(const std::vector<uint8_t>& data, int& width, int& height, size_t& pixels) {
size_t at = 0;
const auto token = [&]() {
for (;;) {
while (at < data.size() && std::isspace(data[at])) ++at;
if (at >= data.size() || data[at] != '#') break;
while (at < data.size() && data[at] != '\n') ++at;
}
std::string text;
while (at < data.size() && !std::isspace(data[at])) text += static_cast<char>(data[at++]);
return text;
};
if (token() != "P6") return false;
width = std::atoi(token().c_str());
height = std::atoi(token().c_str());
const int maxval = std::atoi(token().c_str());
pixels = at + 1;
return width > 0 && height > 0 && maxval == 255 && pixels <= data.size() && data.size() - pixels >= static_cast<size_t>(width) * height * 3;
}
 
int encode(const char* input, const char* output, float quality) {
std::vector<uint8_t> data;
int width = 0;
int height = 0;
size_t pixels = 0;
if (!readFile(input, data)) return 1;
if (!parsePpm(data, width, height, pixels)) {
std::fprintf(stderr, "%s is not a binary PPM (P6, maxval 255)\n", input);
return 1;
}
uint8_t* webp = nullptr;
const size_t size = WebPEncodeRGB(data.data() + pixels, width, height, width * 3, quality, &webp);
File out(std::fopen(output, "wb"), std::fclose);
const bool written = size && out && std::fwrite(webp, 1, size, out.get()) == size;
WebPFree(webp);
if (!written) {
std::fprintf(stderr, "cannot encode %s to %s\n", input, output);
return 1;
}
std::printf("webp %s encode: %s %dx%d -> %s, %zu B at quality %g\n", version().c_str(), input, width, height, output, size, quality);
return 0;
}
 
int info(const char* input) {
std::vector<uint8_t> data;
if (!readFile(input, data)) return 1;
WebPBitstreamFeatures features;
if (WebPGetFeatures(data.data(), data.size(), &features) != VP8_STATUS_OK) {
std::fprintf(stderr, "%s is not a WebP file\n", input);
return 1;
}
const char* format = features.format == 1 ? "lossy" : features.format == 2 ? "lossless" : "mixed";
std::printf("webp %s info: %s %dx%d %s, %zu B, alpha: %s, animated: %s\n", version().c_str(), input, features.width, features.height, format, data.size(),
features.has_alpha ? "yes" : "no", features.has_animation ? "yes" : "no");
return 0;
}
 
} // namespace
 
int main(int argc, char** argv) {
if (argc >= 4 && std::strcmp(argv[1], "encode") == 0) return encode(argv[2], argv[3], argc >= 5 ? std::strtof(argv[4], nullptr) : 75.0f);
if (argc == 3 && std::strcmp(argv[1], "info") == 0) return info(argv[2]);
std::fprintf(stderr, "usage: webp-tool encode <input.ppm> <output.webp> [quality]\n webp-tool info <input.webp>\n");
return 2;
}
shell
npx crossbind build -p wasi -e wasi -b release
wasmtime run --dir=. .crossbind/build/webp-tool-wasi-wasm32-st-release.wasm encode landscape.ppm landscape.webp 80
wasmtime run --dir=. .crossbind/build/webp-tool-wasi-wasm32-st-release.wasm info landscape.webp
output
webp 1.6.0 encode: landscape.ppm 256x256 -> landscape.webp, 1966 B at quality 80
webp 1.6.0 info: landscape.webp 256x256 lossy, 1966 B, alpha: no, animated: no
Tippe, um alle Seiten und Abschnitte des Leitfadens zu durchsuchen.
↑↓ navigieren↵ öffnenesc schließen