From: "Peter Münster" <pm@a16n.net>
To: ntg-context@ntg.nl
Subject: t-downsample.lua
Date: Sun, 17 Feb 2019 11:32:46 +0100 [thread overview]
Message-ID: <87k1hyvh69.fsf@a16n.net> (raw)
[-- Attachment #1: Type: text/plain, Size: 1509 bytes --]
Hi,
Please find attached a new version of the module, with some ideas
from Marco. Later, I'll upload it to modules.contextgarden.net.
Here a usage example:
\usemodule[downsample]
\setupexternalfigures[
location=default, % find hacker.jpg and mill.png
cache=/tmp, % where to put the low resolution images
resolution=50, % resolution in DPI
conversion=downsample.pdf] % use this module
\starttext
\externalfigure[hacker][width=0.4\textwidth]
\setupexternalfigures[resolution=20]
\externalfigure[mill][width=4in, height=3in]
\setupexternalfigures[resolution=200]
\externalfigure[mill][width=4in, height=3in]
\stoptext
Hans, it would be nice, to apply this patch, to trigger new conversion,
when figure dimensions change:
--- grph-inc.lua~ 2019-02-14 17:04:12.000000000 +0100
+++ grph-inc.lua 2019-02-17 08:45:38.129971581 +0100
@@ -864,6 +864,14 @@
newbase = prefix .. newbase
end
local hash = ""
+ local width = figures.current().request.width
+ local height = figures.current().request.height
+ if width then
+ hash = hash .. "[w:" .. width .. "]"
+ end
+ if height then
+ hash = hash .. "[h:" .. height .. "]"
+ end
if resolution then
hash = hash .. "[r:" .. resolution .. "]"
end
TIA,
--
Peter
[-- Attachment #2: t-downsample.lua --]
[-- Type: application/octet-stream, Size: 1874 bytes --]
if not modules then modules = { } end modules ['t-downsample'] = {
version = 1.002,
comment = "companion to grph-inc.mkiv",
author = "Peter Münster",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
local myself = "downsample"
local format = string.format
local round = math.round
local report = logs.report
local function sample_down(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(myself, format("nothing to do: %s, %s, %s",
oldname, newname, resolution))
file.copy(oldname, newname)
return
end
local inch = 72.27 * 65536
local image = img.scan{filename = oldname}
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)
if xsize < image.xsize or ysize < image.ysize then
local s = format("gm convert -strip -resize %dx%d %s %s",
xsize, ysize, oldname, newname)
report("downsample", s)
os.execute(s)
else
report(myself, format("nothing to do: %s, %s, %s",
oldname, newname, resolution))
report(myself, format("xsize = %d, ysize = %d", xsize, ysize))
file.copy(oldname, newname)
end
end
local formats = {"png", "jpg", "gif"}
for _, s in ipairs(formats) do
figures.converters[s] = figures.converters[s] or {}
figures.converters[s]["downsample.pdf"] = sample_down
-- without the ".pdf", ConTeXt won't find the converted image...
end
[-- Attachment #3: Type: text/plain, Size: 493 bytes --]
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
next reply other threads:[~2019-02-17 10:32 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-02-17 10:32 Peter Münster [this message]
2019-02-17 11:07 ` t-downsample.lua Peter Münster
2019-02-17 12:52 ` t-downsample.lua Hans Hagen
2019-02-24 10:55 ` t-downsample.lua Peter Münster
2019-02-26 8:09 ` t-downsample.lua Hans Hagen
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=87k1hyvh69.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).