[NTG-context] setupexternalfigures and resoĺution

Henning Hraban Ramm texml at fiee.net
Fri Sep 2 11:26:23 CEST 2022

Am 02.09.22 um 10:39 schrieb Keith McKay via ntg-context:
> Thanks for this code Hraban. I tried this last night and it worked 
> really well. I did have one stumbling block when I noticed that those 
> image files with a space in their names were not found, however 
> renaming  without a space solved the problem. Is this a bug or a feature?

The call to gm convert was lacking quoting of the file names. Fixed in 

While the code does some downsampling, the calculation is not always 
right, because it doesn’t consider scaling (or something like that).

-------------- next part --------------
if not modules then modules = { } end modules ['grph-downsample'] = {
  version   = 1.101,
  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"

assert(not figures.getinfo2)

figures.getinfo2 = function(name, page) -- [ [NTG-context] Pdf info with Lua/Ctx API ]
 if type(name) == "string" then
   name = { name = name, page = page }

 if name.name then
   local data = figures.push(name)
   local info = figures.identify()

   if info.status.status ~= 0 then
     figures.check() -- !Counts pages here!


   return --data

local format = string.format
-- figures.cachepaths.path = "cache" -- should be setup-option
local function sample_down(oldname, newname, resolution)
  print("DOWNSAMPLE sample_down " .. oldname .. " to " .. newname)
  local request = figures.current().request
  local width = request.width
  local height = request.height
  if resolution == "" then -- or (not width and not height) then
    print(format("DOWNSAMPLE Nothing to do: %s, %s, %s dpi, %s x %s px", oldname, newname, resolution, width, height))
  local TEXpt = 65536
  local inch = 72.27

  -- MkIV:
  -- local image = img.scan{filename = oldname}

  -- LMTX:
  local image = figures.getinfo(oldname,1)
  image = image.status.private

  local xy = image.xsize / image.ysize
  if (not width and not height) then
    -- no size requested? use default width
    width = 300 * TEXpt
  if not width then
    height = height / TEXpt
    width = height * xy
  if not height then
    width = width / TEXpt
    height = width / xy
  width = math.floor(width)
  height = math.floor(height)
  print(format("DOWNSAMPLE image size %dx%dpx. requested %dx%d?", image.xsize, image.ysize, width, height))
  local xsize = math.floor(resolution * width / inch)
  local ysize = math.floor(resolution * height / inch)
  print(format("DOWNSAMPLE size %d x %d to %d x %d", image.xsize, image.ysize, xsize, ysize))
  if xsize < image.xsize or ysize < image.ysize then
    local s = format("gm convert -resize %dx%d -resample %dx%d \"%s\" \"%s\"",
                     xsize, ysize, resolution, resolution, oldname, newname)
    print("DOWNSAMPLE Conversion: " .. s)
    print(format("DOWNSAMPLE Nothing to do: %s, %s, %s dpi, %d x %d px", oldname, newname, resolution, width, height))
    print(format("DOWNSAMPLE xsize = %d, ysize = %d", xsize, ysize))

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

More information about the ntg-context mailing list