imgA: jpeg opts.native (grey files -> gray8); Dct local type annotations

This commit is contained in:
4DBug committed 2026-10-31 08:44:49 -05:00
1 parent d741722b52
commit 11ad899fa3
3 files changed
+16 -1

No files matched your search

+1 -1
View File
@@ -293,7 +293,7 @@ function Dct.fdctQuantize(src: buffer, stride: number, x: number, y: number, w:
local lastX, lastY = w - 1, h - 1
for r = 0, 7 do
local row = math.min(y + r, lastY) * stride
local d0, d1, d2, d3, d4, d5, d6, d7
local d0: number, d1: number, d2: number, d3: number, d4: number, d5: number, d6: number, d7: number
if x + 7 <= lastX then
local p = row + x
d0, d1, d2, d3 = readu8(src, p), readu8(src, p + 1), readu8(src, p + 2), readu8(src, p + 3)
+8
View File
@@ -34,6 +34,7 @@ local Jpeg = {
export type DecodeOptions = {
scale: number?, -- 1 (default), 2, 4 or 8: decode at ceil(size / scale) (8 reads the DC coefficients only)
orient: boolean?, -- apply the EXIF orientation (default false: reported in meta.orientation only)
native: boolean?, -- grey files decode to gray8 (default false: rgba8)
maxPixels: number?, -- refuse larger images (default 2^26)
}
export type EncodeOptions = {
@@ -160,6 +161,13 @@ function Jpeg.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.
end
local img = Types.Image.new(1, 1)
img.width, img.height, img.pixels = w, h, px
if opts and opts.native and space == "gray" then
local g = buffer.create(w * h)
for i = 0, w * h - 1 do
buffer.writeu8(g, i, buffer.readu8(px, i * 4))
end
img.pixels, img.format = g, "gray8"
end
local f = st.frame
local sampling = {}
for _, c in f.comps do
+7
View File
@@ -80,6 +80,13 @@ do
t:check(i.colorSpace == "ycbcr" and i.precision == 8 and i.jfif and not i.arithmetic, "info: colour space")
t:check(Jpeg.info(Bytes.reader(file("photo_arith_420_prog"))).arithmetic, "info: arithmetic")
t:eq(Jpeg.info(Bytes.reader(file("photo_gray"))).colorSpace, "gray", "info: grey")
do
local g = decode(file("photo_gray"), { native = true })
local rgba = decode(file("photo_gray"))
t:check(g.format == "gray8" and buffer.len(g.pixels) == g.width * g.height, "native grey decodes to gray8")
t:eq(buffer.readu8(g.pixels, 77), buffer.readu8(rgba.pixels, 77 * 4), "gray8 samples")
t:eq(decode(file("photo_q90_420"), { native = true }).format, "rgba8", "native colour stays rgba8")
end
t:eq(Jpeg.info(Bytes.reader(file("photo_rgb"))).colorSpace, "rgb", "info: RGB")
t:eq(Jpeg.info(Bytes.reader(file("photo_cmyk"))).colorSpace, "cmyk", "info: CMYK")
t:eq(Jpeg.info(Bytes.reader(file("photo_ycck"))).colorSpace, "ycck", "info: YCCK")