From 11ad899fa3553c8b400c2e02c65272a20bcd78b3 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 08:44:49 -0500 Subject: [PATCH] imgA: jpeg opts.native (grey files -> gray8); Dct local type annotations --- src/Formats/codecs/Dct/init.luau | 2 +- src/Formats/image/jpeg/init.luau | 8 ++++++++ tests/image/jpeg.luau | 7 +++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Formats/codecs/Dct/init.luau b/src/Formats/codecs/Dct/init.luau index 8adc3fe..c46923c 100644 --- a/src/Formats/codecs/Dct/init.luau +++ b/src/Formats/codecs/Dct/init.luau @@ -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) diff --git a/src/Formats/image/jpeg/init.luau b/src/Formats/image/jpeg/init.luau index 210a273..f6c3077 100644 --- a/src/Formats/image/jpeg/init.luau +++ b/src/Formats/image/jpeg/init.luau @@ -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 diff --git a/tests/image/jpeg.luau b/tests/image/jpeg.luau index 964dc51..c0af085 100644 --- a/tests/image/jpeg.luau +++ b/tests/image/jpeg.luau @@ -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")