grph-bmp.lua /size: 2434 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
grph-bmp
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to grph-inc.mkiv
"
,
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
local
random
=
math
.
random
10
local
context
=
context
11 12
local
report_bitmap
=
logs
.
reporter
(
"
graphics
"
,
"
bitmap
"
)
13 14
local
bitmaps
=
{
}
15
graphics
.
bitmaps
=
bitmaps
16 17
local
wrapimage
=
images
.
wrap
18 19
function
bitmaps
.
new
(
xsize
,
ysize
,
colorspace
,
colordepth
,
mask
,
index
)
20
if
not
xsize
or
not
ysize
or
xsize
=
=
0
or
ysize
=
=
0
then
21
report_bitmap
(
"
provide 'xsize' and 'ysize' larger than zero
"
)
22
return
23
end
24
if
not
colorspace
then
25
report_bitmap
(
"
provide 'colorspace' (1, 2, 3, 'gray', 'rgb', 'cmyk'
"
)
26
return
27
end
28
if
not
colordepth
then
29
report_bitmap
(
"
provide 'colordepth' (1, 2)
"
)
30
return
31
end
32
return
graphics
.
identifiers
.
bitmap
{
33
colorspace
=
colorspace
,
34
colordepth
=
colordepth
,
35
xsize
=
xsize
,
36
ysize
=
ysize
,
37
mask
=
mask
and
true
or
nil
,
38
index
=
index
and
true
or
nil
,
39
}
40
end
41 42
-- function backends.codeinjections.bitmap(bitmap)
43
-- return lpdf.injectors.bitmap(bitmap)
44
-- end
45 46
local
function
flush
(
bitmap
)
47
local
specification
=
backends
.
codeinjections
.
bitmap
(
bitmap
)
48
if
specification
then
49
return
wrapimage
(
specification
)
50
end
51
end
52 53
bitmaps
.
flush
=
flush
54 55
function
bitmaps
.
tocontext
(
bitmap
,
width
,
height
)
56
local
bmp
=
flush
(
bitmap
)
57
if
bmp
then
58
if
type
(
width
)
=
=
"
number
"
then
59
width
=
width
.
.
"
sp
"
60
end
61
if
type
(
height
)
=
=
"
number
"
then
62
height
=
height
.
.
"
sp
"
63
end
64
if
width
or
height
then
65
context
.
scale
(
66
{
67
width
=
width
,
68
height
=
height
,
69
}
,
70
bmp
71
)
72
else
73
context
(
bmp
)
74
end
75
end
76
end
77 78
local
function
placeholder
(
nx
,
ny
)
79 80
local
nx
=
nx
or
8
81
local
ny
=
ny
or
nx
82
local
bitmap
=
bitmaps
.
new
(
nx
,
ny
,
"
gray
"
,
1
)
83
local
data
=
bitmap
.
data
84 85
for
i
=
1
,
ny
do
86
local
d
=
data
[
i
]
87
for
j
=
1
,
nx
do
88
d
[
j
]
=
random
(
100
,
199
)
89
end
90
end
91 92
return
lpdf
.
injectors
.
bitmap
(
bitmap
)
93 94
end
95 96
bitmaps
.
placeholder
=
placeholder
97