if not modules then modules = { } end modules ['grph-downsample'] = { version = 1.104, comment = "companion to grph-inc.mkiv", author = "Peter Münster and Henning Hraban Ramm", copyright = "PRAGMA ADE / ConTeXt Development Team", license = "see context related readme files" } -- todo: -- figures.cachepaths.path = "cache" -- should be a setup-option -- threshold should be a setup-option local function round(x) return math.floor(x + 0.5) end local format = string.format local report = logs.reporter("DOWNSAMPLE") local function sample_down(oldname, newname, resolution) report("sample_down %s to %s (%s)", oldname, newname, resolution) local request = figures.current().request local width = request.width local height = request.height if resolution == "" or (not width and not height) then report("Nothing to do: %s, %s, %s", oldname, newname, resolution) return else report("Requested: %s, %s, %s dpi, %s sp x %s sp", oldname, newname, resolution, width or "???", height or "???") end -- requested size must be smaller than original size -- multiplied by this threshold: local threshold = 0.8 local inch = 72.27 * 65536 -- an inch in scaled points local image = figures.getinfo(oldname, 1) image = image.status.private local xy = image.xsize / image.ysize if not width then width = height * xy end if not height then height = width / xy end local xsize = round(resolution * width / inch) local ysize = round(resolution * height / inch) report("resize %d x %d to %d x %d:", image.xsize, image.ysize, xsize, ysize) if xsize < image.xsize * threshold or ysize < image.ysize * threshold then local s = format("gm convert -resize %dx%d %s %s", xsize, ysize, oldname, newname) report("calling: %s", s) os.execute(s) else report("Nothing to do: %s, %s, %s", oldname, newname, resolution) report("xsize = %d, ysize = %d", xsize, ysize) end end local formats = {"png", "jpg", "gif"} for _, s in ipairs(formats) do figures.converters[s] = figures.converters[s] or {} figures.converters[s]["lowres." .. s] = sample_down end