libs-imp-imagemagick.lmt /size: 2944 b    last modification: 2021-10-28 13:51
1if not modules then modules = { } end modules ['libs-imp-imagemagick'] = {
2    version   = 1.001,
3    comment   = "companion to luat-lib.mkxl",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files",
7}
8
9-- \registerctxluafile{libs-imp-imagemagick}{autosuffix}
10-- \starttext
11--     \startluacode
12--         utilities.imagemagick.convert {
13--             inputname  = "hacker.jpg",
14--             outputname = "m_k_v_i_hacker.png",
15--             options    = { "-rotate", 90, "-noise", 2 },
16--         }
17--         context.externalfigure { "hacker.png" }
18--     \stopluacode
19-- \stoptext
20
21local libname = "imagemagick"
22local libfile = { "CORE_RL_MagickCore_", "CORE_RL_MagickWand_" }
23
24local imlib = resolvers.libraries.validoptional(libname)
25
26if not imlib then return end
27
28local function okay()
29    if resolvers.libraries.optionalloaded(libname,libfile) then
30        okay = function() return true end
31    else
32        okay = function() return false end
33    end
34    return okay()
35end
36
37local imagemagick     = utilities.imagemagick or { }
38utilities.imagemagick = imagemagick
39
40local im_execute = imlib.execute
41local nofruns    = 0
42local report     = logs.reporter(libname)
43
44function imagemagick.convert(specification)
45    if okay() then
46        --
47        nofruns = nofruns + 1
48        statistics.starttiming(imagemagick)
49        --
50        local inputname  = specification.inputname
51        if not inputname or inputname == "" then
52            report("invalid run %s, no inputname specified",nofruns)
53            statistics.stoptiming(imagemagick)
54            return false
55        end
56        local outputname = specification.outputname
57        if not outputname or outputname == "" then
58            outputname = file.replacesuffix(inputname,"pdf")
59        end
60        --
61        if not lfs.isfile(inputname) then
62            report("invalid run %s, input file %a is not found",nofruns,inputname)
63            statistics.stoptiming(imagemagick)
64            return false
65        end
66        --
67        report("run %s, input file %a, outputfile %a",nofruns,inputname,outputname)
68        --
69        specification.inputfile  = inputname
70        specification.outputfile = outputname
71        --
72        local okay, detail = im_execute(specification)
73        if not okay then
74            report("error %a (make sure options start with one -) ",detail)
75        end
76        --
77        statistics.stoptiming(imagemagick)
78    end
79end
80
81function imagemagick.statistics(feedback)
82    local runtime = statistics.elapsedtime(imagemagick)
83    if feedback then
84        report("nofruns %s, runtime %s",nofruns,runtime)
85    else
86        return {
87            runtime = runtime,
88            nofruns = nofruns,
89        }
90    end
91end
92
93-- imagemagick.convert { inputname = "t:/sources/hacker.jpg", outputname = "e:/tmp/hacker.png" }
94-- imagemagick.statistics(true)
95