Files
kryon/tests/image_decode_linux_test.zi
T
waozi e31434063c Decode WebP images on every host
Lossy, lossless and transparent WebP files, and the first frame of
animated ones, load through the existing image paths: raylib and Android
link Kryon's kryonwebp archive built from the locked libwebp source, the
Cairo desktop host opens the system libwebpdemux, and web hosts use the
browser's decoder. Files are recognized by content, not extension.

Fixtures cover each kind on the raylib, Cairo and browser paths, the
renderer artifact ships libkryonwebp.a, and android-webp-link-test links
the decoder for every Android ABI.
2026-11-01 23:03:37 -03:00

40 lines
2.0 KiB
Ziran

Decode :: #import "image_decode_linux";
c :: #system_library "libc";
Allocate :: (size: usize) -> *void #foreign c "malloc";
Free :: (memory: *void) #foreign c "free";
#import "std/byte_text_linux"
#import "text_tools"
// The WebP fixture's ARGB32 pixel at index, or 0 when it does not decode.
WebPPixel :: (directory: string, name: string, index: s32, width: s32, height: s32) -> u32 {
parts: [4]string = .[directory, "/", name, ".webp"]
image := Decode.OpenWebP(TextJoin(parts[:], ""))
if image.pixels == null { return 0 }; defer { Decode.CloseWebP(image) }
if image.width != width || image.height != height { return 1 }
pixels: [256]u32
if !Decode.CopyWebP(image, cast(*u8)*pixels[0], width * 4) { return 2 }
return pixels[index]
}
main :: (argc: s32, argv: **u8) -> s32 {
if argc != 3 { return 2 }
webp := TextFromCString(argv[2])
// Straight RGBA becomes premultiplied ARGB32; animations keep frame one.
if WebPPixel(webp, "lossless", 0, 3, 2) != cast(u32)0xff0a141e ||
WebPPixel(webp, "lossless", 3, 3, 2) != cast(u32)0x8032373c ||
WebPPixel(webp, "animated", 9, 8, 8) != cast(u32)0xfffac800 ||
WebPPixel(webp, "lossy-alpha", 8 * 16 + 13, 16, 16) >> 24 != cast(u32)64 ||
WebPPixel(webp, "lossy", 8 * 16 + 2, 16, 16) >> 24 != cast(u32)255 {
print("WebP through the system libwebp failed\n"); return 8
}
image := Decode.OpenImage(argv[1])
if image == null { return 3 }; defer { Decode.CloseImage(image) }
if Decode.Width(image) != 5000 || Decode.Height(image) != 1000 { print("Large original was reduced\n"); return 4 }
data := Allocate(20000000); if data == null { return 5 }; defer { Free(data) }
if !Decode.CopyPixels(image, cast(*u8)data, 20000) { return 6 }
pixels := cast(*u32)data
if pixels[0] != cast(u32)0xff00c800 || pixels[300 * 5000 + 4500] != cast(u32)0x80640000 { print("Original pixels or alpha changed\n"); return 7 }
print("Full-resolution 5000-pixel original, premultiplied alpha and WebP passed without a display\n")
return 0
}