ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Henning Hraban Ramm <texml@fiee.net>
To: ntg-context@ntg.nl
Subject: [NTG-context] Re: Downsampling images (again)
Date: Wed, 16 Oct 2024 19:59:55 +0200	[thread overview]
Message-ID: <19239da4-a9c1-4f2a-93e7-d6d61d57433b@fiee.net> (raw)
In-Reply-To: <87y13kqi9a.fsf@a16n.net>

[-- Attachment #1: Type: text/plain, Size: 1192 bytes --]

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

[-- Attachment #2: grph-downsample.lua --]
[-- Type: text/plain, Size: 2811 bytes --]

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

[-- Attachment #3: Type: text/plain, Size: 511 bytes --]

___________________________________________________________________________________
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
___________________________________________________________________________________

  parent reply	other threads:[~2024-10-16 18:04 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-21 11:21 [NTG-context] " Henning Hraban Ramm
2024-09-21 14:30 ` [NTG-context] " Alan Braslau via ntg-context
2024-09-21 19:44   ` Henning Hraban Ramm
2024-09-23  1:48     ` Alan Braslau via ntg-context
2024-09-22  7:06 ` Peter Münster
2024-10-09 16:52   ` Henning Hraban Ramm
2024-10-16 17:59   ` Henning Hraban Ramm [this message]
2024-10-17 11:11     ` Peter Münster

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=19239da4-a9c1-4f2a-93e7-d6d61d57433b@fiee.net \
    --to=texml@fiee.net \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).