summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/meta-imp-magick.mkxl
blob: 1adc7be83acb7b5d8ca6d7cdcec3699d2772b611 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
%D \module
%D   [       file=meta-imp-magick,
%D        version=2021.08.03,
%D          title=\METAPOST\ Graphics,
%D       subtitle=Magick Manipulations,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\registerctxluafile{libs-imp-graphicsmagick}{autosuffix}
\registerctxluafile{libs-imp-imagemagick}{autosuffix}

\startluacode
    local converters = {
        im          = "imagemagick",
        imagemagick = "imagemagick",
        magick      = "imagemagick",
    }

    local frozen    = false
    local convert = false

    -- for some reason we cannot load both libraries

    function mp.lmt_magick_convert()
        local specification = metapost.getparameterset("magick")
        local inputname     = specification.filename
        if inputname then
            -- we don't want to trigger reuse when we have the same input file
            local hash = md5.HEX(table.sequenced(specification))
            local outputname = file.addsuffix("m_k_i_v_mp_fuzzy_" .. hash,file.suffix(inputname))
            -- make the table a bit unique and don't polute it
            local whattodo = table.setmetatableindex( {
                inputname  = inputname,
                outputname = outputname,
            }, specification)
            luatex.registertempfile(outputname)
            -- now do the magick
            if not convert then
                convert = utilities[converters[specification.converter or "gm"] or "graphicsmagick"].convert
            end
            convert(whattodo)
            -- and return the result
            return [[figure("]] .. outputname .. [[")]]
        else
            -- bad luck
            return [[textext("missing filename")]]
        end
    end
\stopluacode

\startMPdefinitions

    presetparameters "magick" [
        filename = "unset",
      % blur     = [ radius = 10, sigma = 5 ],
      % noise    = [ type = 4 ],
    ] ;

    def lmt_magick = applyparameters "magick" "lmt_do_magick" enddef ;

    vardef lmt_do_magick = lua.mp.lmt_magick_convert() enddef ;

\stopMPdefinitions

\continueifinputfile{meta-imp-magick.mkxl}

\enabletrackers[*lib*]

\startMPpage
    presetparameters "magick" [
        converter = "magick", % comment / uncomment
    ] ;
    draw lmt_magick [
        filename  = "hacker.jpg",
        % for im
        options   = { "-rotate", 180 },
    ] ysized 4cm ;

    draw lmt_magick [
        filename = "hacker.jpg",
        % for gm
        blur     = [ radius = 10, sigma = 5 ],
        noise    = [ type = 2 ],
        % for gm
        options  = { "-noise", 2 },
    ] ysized 4cm shifted (8cm, -4cm) ;

    draw lmt_magick [
        filename = "hacker.jpg",
        % for gm
        blur     = [ radius = 5, sigma = 3 ],
        noise    = [ type = 4 ],
        % for gm
        options  = { "-noise", 4 },
    ] ysized 4cm shifted (0, -4cm) ;

    draw lmt_magick [
        filename = "hacker.jpg",
        blur     = [ radius = 10, sigma = 5 ],
        % for gm
        noise    = [ type = 4 ],
    ] ysized 4cm shifted (8cm, 0cm) ;
\stopMPpage