Am 22.09.24 um 09:06 schrieb Peter Münster:
On Sat, Sep 21 2024, Henning Hraban Ramm wrote:

How can a converter function access …
– the final (scaled) size of a placed image

Hi,

I use "figures.current().request" in my module (here attached).


– the original pixel size of an image ?

I use img.scan() for that.

Hi Peter,
are you sure that your code works with LMTX?

Find attached my version.
It seems to work, but only for the first instance of an image, not for different sizes of the same image – makes sense WRT efficiency, but might give wrong results for some uses.

Here’s my test code:

"""
\loadluafile[grph-downsample.lua]

\setupexternalfigure[
  location={local,global,default},
  conversion=lowres.jpg,
  resolution=20,
]

\starttext

\useexternalfigure[zapf][https://upload.wikimedia.org/wikipedia/commons/d/dd/Hermann_Zapf_signing.jpg]
\useexternalfigure[lino][https://upload.wikimedia.org/wikipedia/commons/b/bd/Linotype-vorne-deutsches-museum.jpg]

\externalfigure[zapf][width=.5tw]

\externalfigure[lino][height=.3th]

\externalfigure[zapf][height=.5th]

\stoptext
"""

Hraban
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
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / 
https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

Reply via email to