png / jpeg: maxSide + yield docs (guide, API) and tested snippet; jpeg destuff calls the yield hook per 64 KB; F.model.loadTextures passes maxSide / yield / yieldRows / maxPixels to the image decoders; bench lines (4096^2 16-bit PNG full vs maxSide 1024, JPEG 1/4); build/ regenerated

init.luau (small, backward compatible): loadTextures forwards those four options to F.load.
This commit is contained in:
4DBug committed 2026-11-02 23:39:52 -06:00
1 parent 64a2553c0c
commit 8d7f3e4d34
17 files changed
+2695 -1270

No files matched your search

+5 -1
View File
@@ -49,7 +49,11 @@ one category. Extras:
It also opens animated images (GIF / APNG / WebP) as videos; `F.video.fromImage(img)`,
`F.video.fromFrames(images, delays?)` (an image sequence: `F.save(F.video.fromFrames(list, 1 / 25), "out.avi")`),
`F.video.codecs` (the codec registry `codecs/video/Video`).
- `F.model.loadTextures(scene, opts?)`: decodes embedded / referenced textures into `texture.image`.
- `F.image.load(src, opts?)`: `opts.maxSide` decodes PNG (power-of-two box reduction) and JPEG (DCT scaling) straight
to an image whose sides are at most n pixels, `opts.yield` is a cooperative yield hook (PNG every `opts.yieldRows`
rows; JPEG every MCU row); see the image guide.
- `F.model.loadTextures(scene, opts?)`: decodes embedded / referenced textures into `texture.image`; `opts.maxSide`,
`opts.yield`, `opts.yieldRows` and `opts.maxPixels` reach the image decoders.
- `F.cad.toBodies`, `F.cad.fromBodies`, `F.cad.tessellate`: STEP B-rep calls through the injected kernel
(`F.plugins.brep`).
- `F.data.null`: the JSON / YAML / TOML / msgpack / CBOR null.
+47
View File
@@ -104,11 +104,58 @@ local _ = img and same and err
| `format` | all | decode as this format (name or extension) instead of sniffing |
| `maxPixels` | all | refuse larger images with `Err.LIMIT` (default `2^26`) |
| `native` | JPEG, WebP, TIFF, PNM, QOI, TGA, HDR | keep the source layout: `gray8` / `gray16` / `rgb8` / `rgba16` / `rgbaf32` / `yuv420p` (lossy WebP) instead of `rgba8` |
| `maxSide` | PNG, JPEG | decode straight to a smaller image whose sides are at most this (below) |
| `reduce` | PNG | box reduction by this power of two |
| `yield`, `yieldRows` | PNG, JPEG | cooperative yield hook for long decodes (below) |
| `scale`, `orient` | JPEG | reduced-size decode; apply the EXIF orientation (`meta.orientation` reports it either way) |
| `coalesce`, `frames` | WebP | every frame as a full canvas; `frames = false` decodes only the first |
| `page`, `maxPages` | TIFF | decode one page (1-based); cap on pages in `frames` |
| `maxFrames` | GIF | cap on frames |
### Huge textures: `maxSide` and `yield`
A 4096 x 4096 16-bit PNG normal map is about 100 MB inflated and 64 MB as an `rgba8` Image. With `maxSide`, PNG and
JPEG shrink the picture while decoding, so the full-size image is never built:
- **PNG** reduces by the smallest power of two `k` that brings both sides to `maxSide` or less (`ceil(side / k)`), so
the result can be up to half of `maxSide`: 4096 with `maxSide = 1024` gives 1024, 3000 gives 750. Each output pixel
is the alpha-weighted mean of its k x k block (16-bit samples are cut to 8 bits first, as a full decode does;
smaller blocks at the right and bottom edges; halves round up; a fully transparent block is `0, 0, 0, 0`). Rows
stream through the inflater, so memory is the output plus a few rows. Interlaced (Adam7) PNGs still inflate and
decode at full size before the reduction. APNG frames are reduced one by one, their offsets divided by `k`.
`reduce = k` asks for a factor directly; `meta.source.reduce` reports the one used and `meta.source.width` /
`height` the original size.
- **JPEG** picks the smallest DCT scale (1/2, 1/4, 1/8) that fits, the same as `scale`: pixels are bit-exact with
libjpeg-turbo's `djpeg -scale 1/n`. Below 1/8 it stops at 1/8. The coefficients are still stored at full size
(2 bytes per pixel and component).
For an exact size (e.g. Roblox's 1024 EditableImage cap), follow with `F.ops.image.resize`.
`yield` is called through `pcall` every `yieldRows` rows (PNG; default: about 64 KB of row data) or every MCU row and
block row (JPEG), so a decode inside a coroutine can give the frame back. On Roblox, call `task.wait()` from it in a
`task.spawn`ed thread once a time budget is spent; the same with `coroutine.yield` everywhere:
```luau
local small = assert(F.image.load(pngBytes, { maxSide = 16 })) -- 64 x 48 -> 16 x 12 (k = 4)
assert(small.width == 16 and small.height == 12 and small.meta.source.reduce == 4)
local thumb = assert(F.image.load(jpegBytes, { maxSide = 16 })) -- JPEG at 1/4
assert(thumb.width == 16 and thumb.meta.source.scale == 4)
local budget, start = 0.004, os.clock()
local co = coroutine.wrap(function(): any
return F.image.load(pngBytes, { maxSide = 32, yieldRows = 4, yield = function()
if os.clock() - start > budget then
coroutine.yield() -- Roblox: task.wait()
start = os.clock()
end
end })
end)
local img = co()
while img == nil do -- once per frame
img = co()
end
assert(img.width == 32)
```
To read only the header, use the format module's `info` on a reader (no pixels are decoded):
```luau