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

* Re: t-downsample.lua
  2019-02-17 10:32 t-downsample.lua Peter Münster
@ 2019-02-17 11:07 ` Peter Münster
  2019-02-17 12:52 ` t-downsample.lua Hans Hagen
  1 sibling, 0 replies; 5+ messages in thread
From: Peter Münster @ 2019-02-17 11:07 UTC (permalink / raw)
  To: ntg-context

On Sun, Feb 17 2019, Peter Münster wrote:

> Later, I'll upload it to modules.contextgarden.net.

Ok, done.

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

* Re: t-downsample.lua
  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 ` Hans Hagen
  2019-02-24 10:55   ` t-downsample.lua Peter Münster
  1 sibling, 1 reply; 5+ messages in thread
From: Hans Hagen @ 2019-02-17 12:52 UTC (permalink / raw)
  To: ntg-context

On 2/17/2019 11:32 AM, Peter Münster wrote:
> 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
hm that needs a bit of thinking because here we have cases where we 
downsample from say 600 to 150 dpi but then use that same one for all 
kind of sizes (and then a different wd/ht should not trigger an extra 
downsample)

Hans

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
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

* Re: t-downsample.lua
  2019-02-17 12:52 ` t-downsample.lua Hans Hagen
@ 2019-02-24 10:55   ` Peter Münster
  2019-02-26  8:09     ` t-downsample.lua Hans Hagen
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Münster @ 2019-02-24 10:55 UTC (permalink / raw)
  To: ntg-context

On Sun, Feb 17 2019, Hans Hagen wrote:

>> +                if width then
>> +                    hash = hash .. "[w:" .. width .. "]"
>> +                end
>> +                if height then
>> +                    hash = hash .. "[h:" .. height .. "]"
>> +                end
>>
> hm that needs a bit of thinking because here we have cases where we downsample
> from say 600 to 150 dpi but then use that same one for all kind of sizes (and
> then a different wd/ht should not trigger an extra downsample)

Ok. Perhaps with a configuration option? Like "conversion-if-new-dimens=yes/no"?

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

* Re: t-downsample.lua
  2019-02-24 10:55   ` t-downsample.lua Peter Münster
@ 2019-02-26  8:09     ` Hans Hagen
  0 siblings, 0 replies; 5+ messages in thread
From: Hans Hagen @ 2019-02-26  8:09 UTC (permalink / raw)
  To: ntg-context

On 2/24/2019 11:55 AM, Peter Münster wrote:
> On Sun, Feb 17 2019, Hans Hagen wrote:
> 
>>> +                if width then
>>> +                    hash = hash .. "[w:" .. width .. "]"
>>> +                end
>>> +                if height then
>>> +                    hash = hash .. "[h:" .. height .. "]"
>>> +                end
>>>
>> hm that needs a bit of thinking because here we have cases where we downsample
>> from say 600 to 150 dpi but then use that same one for all kind of sizes (and
>> then a different wd/ht should not trigger an extra downsample)
> 
> Ok. Perhaps with a configuration option? Like "conversion-if-new-dimens=yes/no"?
maybe later this year, when images are on my agenda

Hans

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
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).