ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* t-downsample.lua
@ 2019-02-17 10:32 Peter Münster
  2019-02-17 11:07 ` t-downsample.lua Peter Münster
  2019-02-17 12:52 ` t-downsample.lua Hans Hagen
  0 siblings, 2 replies; 5+ messages in thread
From: Peter Münster @ 2019-02-17 10:32 UTC (permalink / raw)
  To: ntg-context

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-02-26  8:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-17 10:32 t-downsample.lua Peter Münster
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

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).