merge ws/downscale

This commit is contained in:
4DBug committed 2026-11-02 23:41:47 -06:00
commit f1d98ec904
24 files changed
+3432 -1340

No files matched your search

+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