From 9b2bc84b39e39276a12344d24c05568945d42220 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 08:53:29 -0500 Subject: [PATCH] imgA: JPEG-in-TIFF pixel check through F.load (image/jpeg via the decodeImage hook) against tifffile --- tests/image/tiff.luau | 13 +++++++++++++ tools/fixtures/tiff.py | 11 +++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/tests/image/tiff.luau b/tests/image/tiff.luau index 53b029b..f5c0f7a 100644 --- a/tests/image/tiff.luau +++ b/tests/image/tiff.luau @@ -89,6 +89,19 @@ do end, }) t:check(img and calls > 0, "jpeg through the hook") + -- the real thing through the api (image/jpeg decodes the stitched streams): within 2 levels of libjpeg-turbo + local F = require("@formats") + local ref = Fx.jpegRef + local full, ferr = F.load(Fx.jpeg, { format = "tiff" }) + t:check(full and full.width == ref.width and full.height == ref.height, "jpeg-in-tiff through F.load: " .. tostring(ferr)) + local worst = 0 + for i = 0, ref.width * ref.height - 1 do + for k = 0, 2 do + worst = math.max(worst, math.abs(buffer.readu8(full.pixels, i * 4 + k) - string.byte(ref.rgb, i * 3 + k + 1))) + end + end + t:check(worst <= 2, "jpeg-in-tiff pixels: max error " .. worst .. " against tifffile") + t:note("JPEG-in-TIFF through F.load: max error %d against tifffile / libjpeg-turbo", worst) end -- encoder round trips diff --git a/tools/fixtures/tiff.py b/tools/fixtures/tiff.py index 77c9256..d6ed3b3 100644 --- a/tools/fixtures/tiff.py +++ b/tools/fixtures/tiff.py @@ -1,7 +1,8 @@ # TIFF fixtures (tools/fixtures.py): files written by tifffile (+ imagecodecs) and ImageMagick, with references from # tifffile's decode (the native sample arrays) turned into what image/tiff must produce. # tests/gen/tiff.luau { cases = { { name, data, width, height, pages = { { crc, native, ncrc } } } }, -# jpeg = { name, data } (a JPEG-compressed TIFF for the decodeImage hook test) } +# jpeg = data (a JPEG-compressed TIFF for the decodeImage hook test), +# jpegRef = { width, height, rgb } (tifffile + imagecodecs' libjpeg-turbo decode of it) } # crc CRC-32 of the default rgba8 decode # native / ncrc: the opts.native pixel format and the CRC-32 of its little-endian bytes # Every case is decoded by tifffile; the expected pixels follow image/tiff's rules: unsigned samples v / (2^bits - 1), @@ -191,6 +192,10 @@ for name, path in cases: w, h = pages[0].imagewidth, pages[0].imagelength emit(name.encode()); emit(open(path, "rb").read()); emit(("%d %d " % (w, h) + " ".join(info)).encode()) emit(open(jp, "rb").read()) +with tifffile.TiffFile(jp) as t: + a = t.pages[0].asarray() + emit(("%d %d" % (a.shape[1], a.shape[0])).encode()) + emit(np.ascontiguousarray(a[:, :, 0:3]).astype(np.uint8).tobytes()) ''' @@ -216,4 +221,6 @@ def generate(ctx): crc, kind, ncrc = s.split(":") pages.append({"crc": int(crc), "native": kind, "ncrc": int(ncrc)}) cases.append({"name": name.decode(), "data": data, "width": int(info[0]), "height": int(info[1]), "pages": pages}) - ctx.emit("tiff", {"cases": cases, "jpeg": items[1 + count * 3]}) + jw, jh = map(int, items[2 + count * 3].decode().split()) + ctx.emit("tiff", {"cases": cases, "jpeg": items[1 + count * 3], + "jpegRef": {"width": jw, "height": jh, "rgb": items[3 + count * 3]}})