meta-imp-magick.mkxl /size: 3250 b    last modification: 2021-10-28 13:51
1%D \module
2%D   [       file=meta-imp-magick,
3%D        version=2021.08.03,
4%D          title=\METAPOST\ Graphics,
5%D       subtitle=Magick Manipulations,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14\registerctxluafile{libs-imp-graphicsmagick}{autosuffix}
15\registerctxluafile{libs-imp-imagemagick}{autosuffix}
16
17\startluacode
18    local converters = {
19        im          = "imagemagick",
20        imagemagick = "imagemagick",
21        magick      = "imagemagick",
22    }
23
24    local frozen    = false
25    local convert = false
26
27    -- for some reason we cannot load both libraries
28
29    function mp.lmt_magick_convert()
30        local specification = metapost.getparameterset("magick")
31        local inputname     = specification.filename
32        if inputname then
33            -- we don't want to trigger reuse when we have the same input file
34            local hash = md5.HEX(table.sequenced(specification))
35            local outputname = file.addsuffix("m_k_i_v_mp_fuzzy_" .. hash,file.suffix(inputname))
36            -- make the table a bit unique and don't polute it
37            local whattodo = table.setmetatableindex( {
38                inputname  = inputname,
39                outputname = outputname,
40            }, specification)
41            luatex.registertempfile(outputname)
42            -- now do the magick
43            if not convert then
44                convert = utilities[converters[specification.converter or "gm"] or "graphicsmagick"].convert
45            end
46            convert(whattodo)
47            -- and return the result
48            return [[figure("]] .. outputname .. [[")]]
49        else
50            -- bad luck
51            return [[textext("missing filename")]]
52        end
53    end
54\stopluacode
55
56\startMPdefinitions
57
58    presetparameters "magick" [
59        filename = "unset",
60      % blur     = [ radius = 10, sigma = 5 ],
61      % noise    = [ type = 4 ],
62    ] ;
63
64    def lmt_magick = applyparameters "magick" "lmt_do_magick" enddef ;
65
66    vardef lmt_do_magick = lua.mp.lmt_magick_convert() enddef ;
67
68\stopMPdefinitions
69
70\continueifinputfile{meta-imp-magick.mkxl}
71
72\enabletrackers[*lib*]
73
74\startMPpage
75    presetparameters "magick" [
76        converter = "magick", % comment / uncomment
77    ] ;
78    draw lmt_magick [
79        filename  = "hacker.jpg",
80        % for im
81        options   = { "-rotate", 180 },
82    ] ysized 4cm ;
83
84    draw lmt_magick [
85        filename = "hacker.jpg",
86        % for gm
87        blur     = [ radius = 10, sigma = 5 ],
88        noise    = [ type = 2 ],
89        % for gm
90        options  = { "-noise", 2 },
91    ] ysized 4cm shifted (8cm, -4cm) ;
92
93    draw lmt_magick [
94        filename = "hacker.jpg",
95        % for gm
96        blur     = [ radius = 5, sigma = 3 ],
97        noise    = [ type = 4 ],
98        % for gm
99        options  = { "-noise", 4 },
100    ] ysized 4cm shifted (0, -4cm) ;
101
102    draw lmt_magick [
103        filename = "hacker.jpg",
104        blur     = [ radius = 10, sigma = 5 ],
105        % for gm
106        noise    = [ type = 4 ],
107    ] ysized 4cm shifted (8cm, 0cm) ;
108\stopMPpage
109