1if 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
9local random = math . random
10local context = context
11
12local report_bitmap = logs . reporter ( " graphics " , " bitmap " )
13
14local bitmaps = { }
15graphics . bitmaps = bitmaps
16
17local wrapimage = images . wrap
18
19function 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 }
40end
41
42
43
44
45
46local function flush ( bitmap )
47 local specification = backends . codeinjections . bitmap ( bitmap )
48 if specification then
49 return wrapimage ( specification )
50 end
51end
52
53bitmaps . flush = flush
54
55function 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
76end
77
78local 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
94end
95
96bitmaps . placeholder = placeholder
97 |