if not modules then modules = { } end modules ['grph-downsample'] = { version = 1.103, comment = "companion to grph-inc.mkiv", author = "Peter Münster", -- adapted to LMTX by Hraban copyright = "PRAGMA ADE / ConTeXt Development Team", license = "see context related readme files" } 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 -- this is probably the original pixel size local width = request.width local height = request.height -- width/height are integer in sp or a boolean local TEXpt = 65536 local inch = 72.27 if resolution == "" then -- or (not width and not height) then report("Nothing to do (missing resolution): %s, %s, %s dpi, %s x %s px", oldname, newname, resolution, width, height) return else report("Requested: %s, %s, %s dpi, %s x %s px", oldname, newname, resolution, width, height) end -- MkIV: -- local image = img.scan{filename = oldname} -- LMTX: local image = figures.getinfo(oldname,1) image = image.status.private local xy = image.xsize / image.ysize -- size ratio if (not width and not height) then -- no size requested? use default width -- probably wrong… width = 300 * TEXpt end if not width then height = height / TEXpt width = height * xy end if not height then width = width / TEXpt height = width / xy end width = math.floor(width) height = math.floor(height) report("image size %dx%dpx. requested %dx%dpt at %ddpi.", image.xsize, image.ysize, width, height, resolution) local xsize = math.floor(resolution * width / inch) local ysize = math.floor(resolution * height / inch) report("resize %dx%dpx to %dx%dpx:", image.xsize, image.ysize, xsize, ysize) if xsize < image.xsize or ysize < image.ysize then -- maybe add a tolerance of 20% (don’t downsample if the difference is marginal) local s = format("gm convert -resize %dx%d -resample %dx%d \"%s\" \"%s\"", xsize, ysize, resolution, resolution, oldname, newname) report("calling: %s", s) os.execute(s) else report("Nothing to do (image smaller than requested): %s, %s, %s dpi, %d x %d px", oldname, newname, resolution, width, height) 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 {} -- use \setupexternalfigure[conversion=lowres.jpg]: figures.converters[s]["lowres." .. s] = sample_down -- use without setup: --figures.converters[s].default = sample_down --figures.converters[s].pdf = sample_down -- gets used end