Files
kryon/tests/cairo_image_sampling_test.zi
T
waoziandClaude Opus 5.5 15c3a22e43 Bring pictures, text input and scroll devices only with the widgets that use them
The desktop rasterizer loads, decodes and smooth-copies file pictures once
a program first asks a picture's size, which every widget that draws one
does first, so vector icons and managed pixels draw without decoders,
threads or asset paths. LanczosSnap is geometry's RectangleSnap, so a
texture draw no longer reaches the Lanczos cache.

A button reaches file pictures and atlas icons through the new
image_lookup. kryon/image is now image_files, which re-exports the Image
widget (image) and fills the pictures; kryon/icon fills the atlas. Library
code imports image itself, so importing a picture-capable widget registers
nothing. Navigation, copy and confirm buttons and the widget host fill the
lookup where they draw, as they reached pictures before. Props modules that
only name Image.Props import image_props, now also exported; unused image
imports are gone.

The frame and the accessibility host reach text requests and editor focus
through text_editing, which text_request and text_input_platform fill, and
the frame samples scrolls through scroll_sampling, which scroll_device
fills.

Desktop examples (C files, C bytes, executable), before -> after:
button 96, 1 411 582, 448 264 -> 70, 980 794, 342 016
text 95, 1 445 077, 462 504 -> 71, 1 020 207, 356 472
textfield 107, 1 580 157, 514 496 -> 84, 1 166 840, 409 840
make pay-per-import-test checks button and textfield; tests whose programs
no longer reach pictures lose their picture bindings and stubs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-15 23:10:23 -03:00

75 lines
3.0 KiB
Ziran

Raster :: #import "cairo_raster";
Texture :: #import "paint_texture";
#import "geometry"
#import "drawing_props"
#import "std/byte_text_linux"
#import "std/format"
#import "std/vec"
// The surface color at a pixel as 0xRRGGBB (Cairo keeps ARGB32 words).
Pixel :: (x: s32, y: s32) -> u32 {
data := Raster.RasterSurfaceData()
stride := Raster.RasterSurfaceStride()
word := cast(*u32)*data[y * stride + x * 4]
return word.* & cast(u32)0xffffff
}
Is :: (x: s32, y: s32, color: u32) -> bool {
if Pixel(x, y) == color { return true }
print("pixel % % is % expected %\n", x, y, Pixel(x, y), color)
return false
}
// A 2 by 2 image drawn over the whole 64 by 64 surface.
Draw :: (path: string, smooth: bool, tint: Color) -> bool {
// A widget asks a picture's size before drawing it, which brings files.
if Raster.ImageWidth(path) <= 0 { return false }
Raster.BeginRasterFrame()
Texture.emitting_smooth = smooth
full: Rectangle = .{0.0, 0.0, 64.0, 64.0}
Raster.RasterImage(path, 0, .{0.0, 0.0, 2.0, 2.0}, full, full, .{0.0, 0.0}, 0.0, 0.0, tint)
Texture.emitting_smooth = false
return Raster.FinishRasterFrame()
}
// Independent source-over reference against the raster host's clear colour.
Faded :: (color: Color, alpha: u8) -> u32 {
a := cast(u32)alpha
inverse := cast(u32)255 - a
red := (cast(u32)color.r * a + 248 * inverse + 127) / 255
green := (cast(u32)color.g * a + 250 * inverse + 127) / 255
blue := (cast(u32)color.b * a + 252 * inverse + 127) / 255
return (red << 16) | (green << 8) | blue
}
main :: (argc: s32, argv: **u8) -> s32 {
if argc != 2 { return 1 }
root := TextFromCString(argv[1])
text: Vec(u8)
BuilderPrint(*text, "%/pixels.png", root)
path := BuilderFinish(text)
if !Raster.OpenRaster(64, 64) { return 2 }
defer Raster.CloseRaster()
white: Color = .{255, 255, 255, 255}
red: u32 = cast(u32)0xff0000
green: u32 = cast(u32)0x00ff00
// Without smooth, each image pixel stays one solid square up to its edge.
if !Draw(path, false, white) { return 3 }
if !Is(1, 1, red) || !Is(31, 16, red) || !Is(32, 16, green) || !Is(62, 1, green) { return 4 }
// A tinted image keeps the same sharp edge.
if !Draw(path, false, .{0, 0, 255, 255}) { return 5 }
if !Is(31, 16, cast(u32)0x0000ff) { return 6 }
// Smooth blends neighbouring pixels across the middle.
if !Draw(path, true, white) { return 7 }
if Pixel(31, 16) == red || Pixel(32, 16) == green { print("smooth image kept a hard edge\n"); return 8 }
// Fading a multicoloured image changes its opacity, never its colours.
alphas: [5]u8 = .[0, 64, 128, 254, 255]
for alpha: alphas {
if !Draw(path, false, .{255, 255, 255, alpha}) { return 9 }
if !Is(16, 16, Faded(.{255, 0, 0, 255}, alpha)) ||
!Is(48, 16, Faded(.{0, 255, 0, 255}, alpha)) { return 10 }
}
print("Cairo preserves image colours while fading and scales sharply unless smooth\n")
return 0
}