[luatex] Prevent stop when tiff file loads

Hans Hagen pragma at wxs.nl
Fri Jan 22 11:18:57 CET 2016


On 1/21/2016 10:10 PM, Patrick Gundlach wrote:
>>>
>>> Is there a way to prevent LuaTeX from stopping the whole process? Is there a function like
>>>
>>> can_you_handle_this_imagetype(filename="foo.tiff")
>>>
>>> ?
>>
>> local imgscan   = img.scan
>> local supported = { pdf = true, jpg = true, png = true }
>>
>> function img.scan(t)
>>     if t and t.filename then
>>         local e = string.match(t.filename,".-%.(.-)$")
>>         if supported[e] then
>>             return imgscan(t)
>>         end
>>     end
>> end
>>
>> local foo = img.scan({filename = "foo.tiff"})
>> local foo = img.scan({filename = "foo.pdf"})
>
>
> in my (real) case I don't have a file extension. I need to ask LuaTeX: do you understand that file format? I guess there is no way except for a fatal error, right?

just open the file look at the magic bytes

local imgscan = img.scan

local valid = {
     ["png"] = string.char(0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A),
     ["jpg"] = string.char(0xFF,0xD8,0xFF),
     ["jp2"] = 
string.char(0x00,0x00,0x00,0x0C,0x6A,0x50,0x20,0x20,0x0D,0x0A),
     ["gif"] = "GIF",
     ["pdf"] = ".-%%PDF",
}

function img.scan(t)
     if t and t.filename then
         local f = io.open(t.filename,"rb")
         if f then
             local d = f:read(5000)
             for k, v in pairs(valid) do
                 if string.find(d,"^"..v) then
                     return imgscan(t)
                 end
             end
         end
     end
end

print(img.scan({filename = "hacker1b.tif"}))
print(img.scan({filename = "cow.pdf"}))
print(img.scan({filename = "mill.png"}))



-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
       tel: 038 477 53 69 | www.pragma-ade.com | www.pragma-pod.nl
-----------------------------------------------------------------


More information about the luatex mailing list