libs-imp-graphicsmagick.lmt /size: 2598 b    last modification: 2021-10-28 13:51
1if not modules then modules = { } end modules ['libs-imp-graphicsmagick'] = {
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
9local libname = "graphicsmagick"
10local libfile = { "CORE_RL_magick_", "CORE_RL_wand_" }
11
12local gmlib = resolvers.libraries.validoptional(libname)
13
14if not gmlib then return end
15
16local function okay()
17    if resolvers.libraries.optionalloaded(libname,libfile) then
18        okay = function() return true end
19    else
20        okay = function() return false end
21    end
22    return okay()
23end
24
25local graphicsmagick     = utilities.graphicsmagick or { }
26utilities.graphicsmagick = graphicsmagick
27utilities.graphicmagick  = graphicsmagick
28
29local gm_execute = gmlib.execute
30local nofruns    = 0
31local report     = logs.reporter(libname)
32
33function graphicsmagick.convert(specification)
34    if okay() then
35        --
36        nofruns = nofruns + 1
37        statistics.starttiming(graphicsmagick)
38        --
39        local inputname  = specification.inputname
40        if not inputname or inputname == "" then
41            report("invalid run %s, no inputname specified",nofruns)
42            statistics.stoptiming(graphicsmagick)
43            return false
44        end
45        local outputname = specification.outputname
46        if not outputname or outputname == "" then
47            outputname = file.replacesuffix(inputname,"pdf")
48        end
49        --
50        if not lfs.isfile(inputname) then
51            report("invalid run %s, input file %a is not found",nofruns,inputname)
52            statistics.stoptiming(graphicsmagick)
53            return false
54        end
55        --
56        report("run %s, input file %a, outputfile %a",nofruns,inputname,outputname)
57        --
58        specification.inputfile  = inputname
59        specification.outputfile = outputname
60        --
61        local okay, detail = gm_execute(specification)
62        if not okay then
63            report("error %a",detail)
64        end
65        --
66        statistics.stoptiming(graphicsmagick)
67    end
68end
69
70function graphicsmagick.statistics(feedback)
71    local runtime = statistics.elapsedtime(graphicsmagick)
72    if feedback then
73        report("nofruns %s, runtime %s",nofruns,runtime)
74    else
75        return {
76            runtime = runtime,
77            nofruns = nofruns,
78        }
79    end
80end
81
82-- graphicsmagick.convert { inputname = "t:/sources/hacker.jpg", outputname = "e:/tmp/hacker.png" }
83-- graphicsmagick.statistics(true)
84