1if not modules then modules = { } end modules [ ' grph-inc ' ] = {
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 xpcall , pcall = xpcall , pcall
10
11local bpfactor = number . dimenfactors . bp
12
13local report = logs . reporter ( " graphics " )
14local report_inclusion = logs . reporter ( " graphics " , " inclusion " )
15local report_bitmap = logs . reporter ( " graphics " , " bitmap " )
16local report_pdf = logs . reporter ( " graphics " , " pdf " )
17
18local trace_pdf = false trackers . register ( " graphics.pdf " , function ( v ) trace_pdf = v end )
19
20local checkers = figures . checkers
21local genericchecker = checkers . generic
22
23local placeholder = graphics . bitmaps . placeholder
24
25
26
27
28
29
30
31
32
33
34function checkers . pdf ( data )
35 local request = data . request
36 local used = data . used
37 if request and used and not request . scanimage then
38 local image = lpdf . epdf . image
39 local openpdf = image . open
40 local closepdf = image . close
41 local querypdf = image . query
42 local copypage = image . copy
43 local pdfdoc = nil
44 local filename = nil
45 request . scanimage = function ( t )
46 if pdfdoc then
47 if trace_pdf then
48 report_pdf ( " scan image %a " , filename )
49 end
50 if not filename then
51 filename = pdfdoc . filename
52 end
53 else
54 filename = t . filename
55 if trace_pdf then
56 report_pdf ( " open and scan image %a " , filename )
57 end
58 pdfdoc = openpdf ( filename , request . userpassword , request . ownerpassword )
59 end
60 if pdfdoc then
61
62 local info = querypdf ( pdfdoc , request . page , request . size )
63 if info then
64 local bbox = info and info . boundingbox or { 0 , 0 , 0 , 0 }
65 local height = bbox [ 4 ] - bbox [ 2 ]
66 local width = bbox [ 3 ] - bbox [ 1 ]
67 local rotation = info . rotation or 0
68 if rotation = = 90 then
69 rotation , height , width = 3 , width , height
70 elseif rotation = = 180 then
71 rotation = 2
72 elseif rotation = = 270 then
73 rotation , height , width = 1 , width , height
74 elseif rotation = = 1 or rotation = = 3 then
75 height , width = width , height
76 else
77 rotation = 0
78 end
79 return {
80 filename = filename ,
81
82 pages = pdfdoc . nofpages ,
83 width = width ,
84 height = height ,
85 depth = 0 ,
86 colordepth = 0 ,
87 xres = 0 ,
88 yres = 0 ,
89 xsize = width ,
90 ysize = height ,
91 rotation = rotation ,
92 pdfdoc = pdfdoc ,
93 }
94 end
95 end
96 end
97 request . copyimage = function ( t )
98 if not pdfdoc then
99 pdfdoc = t . pdfdoc
100 end
101 if pdfdoc then
102 local page = request . page
103 local copied = pdfdoc . nofcopied or 0
104 if not pdfdoc . copied [ page ] then
105 pdfdoc . copied [ page ] = true
106 copied = copied + 1
107 end
108 pdfdoc . nofcopied = copied
109 if trace_pdf then
110 report_pdf ( " copy page %i from image %a, %i pages copied " , page , filename , copied )
111 end
112 local result = copypage ( pdfdoc , page , nil , request . compact , request . width , request . height , request . attr )
113 if pdfdoc . nofcopied > = pdfdoc . nofpages then
114 if trace_pdf then
115 report_pdf ( " closing image %a, %i pages copied " , filename , copied )
116 end
117 closepdf ( pdfdoc )
118 pdfdoc = nil
119 t . pdfdoc = nil
120 end
121 return result
122 else
123
124 end
125 end
126 end
127 return genericchecker ( data )
128end
129
130local function wrappedidentify ( identify , filename , filetype )
131 local wrapup = function ( ) report_inclusion ( " fatal error reading %a " , filename ) end
132 local _ , result = xpcall ( identify , wrapup , filename , filetype )
133 if result then
134 local xsize = result . xsize or 0
135 local ysize = result . ysize or 0
136 local xres = result . xres or 0
137 local yres = result . yres or 0
138 if xres = = 0 or yres = = 0 then
139 xres = 300
140 yres = 300
141 end
142 result . xsize = xsize
143 result . ysize = ysize
144 result . xres = xres
145 result . yres = yres
146 result . width = result . width or ( ( 72 / xres ) * xsize / bpfactor )
147 result . height = result . height or ( ( 72 / yres ) * ysize / bpfactor )
148 result . depth = result . depth or 0
149 result . filename = filename
150 result . colordepth = result . colordepth or 0
151 result . colorspace = result . colorspace or 0
152 result . rotation = result . rotation or 0
153 result . orientation = result . orientation or 0
154 result . transform = result . transform or 0
155 return result
156 else
157 return { error = " fatal error " }
158 end
159end
160
161function checkers . jpg ( data )
162 local request = data . request
163 local used = data . used
164 if request and used and not request . scanimage then
165 local identify = graphics . identify
166 local inject = lpdf . injectors . jpg
167 local found = false
168 request . scanimage = function ( t )
169 local result = wrappedidentify ( identify , t . filename , " jpg " )
170 found = not result . error
171 return {
172 filename = result . filename ,
173 width = result . width ,
174 height = result . height ,
175 depth = result . depth ,
176 colordepth = result . colordepth ,
177 xres = result . xres ,
178 yres = result . yres ,
179 xsize = result . xsize ,
180 ysize = result . ysize ,
181 colorspace = result . colorspace ,
182 rotation = result . rotation ,
183 orientation = result . orientation ,
184 transform = result . transform ,
185 }
186 end
187 request . copyimage = function ( t )
188 if found then
189 found = false
190 return inject ( t )
191 end
192 end
193 end
194 return genericchecker ( data )
195end
196
197function checkers . jp2 ( data )
198 local request = data . request
199 local used = data . used
200 if request and used and not request . scanimage then
201 local identify = graphics . identify
202 local inject = lpdf . injectors . jp2
203 local found = false
204 request . scanimage = function ( t )
205 local result = wrappedidentify ( identify , t . filename , " jp2 " )
206 found = not result . error
207 return {
208 filename = result . filename ,
209 width = result . width ,
210 height = result . height ,
211 depth = result . depth ,
212 colordepth = result . colordepth ,
213 xres = result . xres ,
214 yres = result . yres ,
215 xsize = result . xsize ,
216 ysize = result . ysize ,
217 rotation = result . rotation ,
218 colorspace = result . colorspace ,
219 orientation = result . orientation ,
220 transform = result . transform ,
221 }
222 end
223 request . copyimage = function ( t )
224 if found then
225 found = false
226 return inject ( t )
227 end
228 end
229 end
230 return genericchecker ( data )
231end
232
233function checkers . png ( data )
234 local request = data . request
235 local used = data . used
236 if request and used and not request . scanimage then
237 local identify = graphics . identify
238 local inject = lpdf . injectors . png
239 local found = false
240 request . scanimage = function ( t )
241 local result = wrappedidentify ( identify , t . filename , " png " )
242 found = not result . error
243 return {
244 filename = result . filename ,
245 width = result . width ,
246 height = result . height ,
247 depth = result . depth ,
248 colordepth = result . colordepth ,
249 xres = result . xres ,
250 yres = result . yres ,
251 xsize = result . xsize ,
252 ysize = result . ysize ,
253 rotation = result . rotation ,
254 colorspace = result . colorspace ,
255 tables = result . tables ,
256 interlace = result . interlace ,
257 filter = result . filter ,
258 orientation = result . orientation ,
259 transform = result . transform ,
260 }
261 end
262 request . copyimage = function ( t )
263 t . colorref = used . colorref
264 if found then
265 found = false
266 local ok , result = pcall ( inject , t )
267 if ok then
268 return result
269 else
270 report_inclusion ( " bad bitmap image " )
271 return placeholder ( )
272 end
273 end
274 end
275 end
276 return genericchecker ( data )
277end
278 |