summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/grph-bmp.lua
blob: e10b01aa4b675c6be1241055b6a970d1b25a87e7 (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
if not modules then modules = { } end modules ['grph-bmp'] = {
    version   = 1.001,
    comment   = "companion to grph-inc.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local random        = math.random
local context       = context

local report_bitmap = logs.reporter("graphics","bitmap")

local bitmaps       = { }
graphics.bitmaps    = bitmaps

local wrapimage     = images.wrap

function bitmaps.new(xsize,ysize,colorspace,colordepth,mask,index)
    if not xsize or not ysize or xsize == 0 or ysize == 0 then
        report_bitmap("provide 'xsize' and 'ysize' larger than zero")
        return
    end
    if not colorspace then
        report_bitmap("provide 'colorspace' (1, 2, 3, 'gray', 'rgb', 'cmyk'")
        return
    end
    if not colordepth then
        report_bitmap("provide 'colordepth' (1, 2)")
        return
    end
    return graphics.identifiers.bitmap {
        colorspace = colorspace,
        colordepth = colordepth,
        xsize      = xsize,
        ysize      = ysize,
        mask       = mask and true or nil,
        index      = index and true or nil,
    }
end

-- function backends.codeinjections.bitmap(bitmap)
--     return lpdf.injectors.bitmap(bitmap)
-- end

local function flush(bitmap)
    local specification = backends.codeinjections.bitmap(bitmap)
    if specification then
        return wrapimage(specification)
    end
end

bitmaps.flush = flush

function bitmaps.tocontext(bitmap,width,height)
    local bmp = flush(bitmap)
    if bmp then
        if type(width) == "number" then
            width = width .. "sp"
        end
        if type(height) == "number" then
            height = height .. "sp"
        end
        if width or height then
            context.scale (
                {
                    width  = width,
                    height = height,
                },
                bmp
            )
        else
            context(bmp)
        end
    end
end

local function placeholder(nx,ny)

    local nx     = nx or 8
    local ny     = ny or nx
    local bitmap = bitmaps.new(nx,ny,"gray",1)
    local data   = bitmap.data

    for i=1,ny do
        local d = data[i]
        for j=1,nx do
            d[j] = random(100,199)
        end
    end

    return lpdf.injectors.bitmap(bitmap)

end

bitmaps.placeholder = placeholder