ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: "Peter Münster" <pm@a16n.net>
To: ntg-context@ntg.nl
Subject: [NTG-context] Re: Downsampling images (again)
Date: Thu, 17 Oct 2024 13:11:27 +0200	[thread overview]
Message-ID: <87plnzoug0.fsf@a16n.net> (raw)
In-Reply-To: <19239da4-a9c1-4f2a-93e7-d6d61d57433b@fiee.net> (Henning Hraban Ramm's message of "Wed, 16 Oct 2024 19:59:55 +0200")

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

On Wed, Oct 16 2024, Henning Hraban Ramm wrote:

>> I use img.scan() for that.
>
> are you sure that your code works with LMTX?

Hi Hraban,

No, sorry, I didn’t test it...


> Find attached my version.

Thanks, I have integrated your ideas in a new version here attached.


> It seems to work, but only for the first instance of an image,

Yes, you need to patch the file grph-inc.lmt:
https://lists.contextgarden.net/archives/list/ntg-context@ntg.nl/message/ES7Q57MU5UIEV2B2AWEAPR66MMXI6ZHY/

Cheers,
-- 
           Peter

[-- Attachment #2: grph-downsample.lua --]
[-- Type: application/octet-stream, Size: 2299 bytes --]

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

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

      reply	other threads:[~2024-10-17 11:21 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
2024-10-17 11:11     ` Peter Münster [this message]

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=87plnzoug0.fsf@a16n.net \
    --to=pm@a16n.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).