local Util = require "imagesize.util"
local MIME_TYPE = "image/jpeg"
local MARKER = 0xFF
local SIZE_FIRST = 0xC0 local SIZE_LAST = 0xC3
local function size (stream, options)
stream:read(2)
while true do
local length = 4
local segheader = stream:read(length)
if not segheader or segheader:len() ~= length then
return nil, nil, "end of file before JPEG size found"
end
local marker, code = segheader:byte(1, 2)
length = Util.get_uint16_be(segheader, 3)
if marker ~= MARKER then
return nil, nil, "JPEG marker not found"
elseif code >= SIZE_FIRST and code <= SIZE_LAST then
length = 5
local sizeinfo = stream:read(length)
if not sizeinfo or sizeinfo:len() ~= length then
return nil, nil, "JPEG file's size info incomplete"
end
return Util.get_uint16_be(sizeinfo, 4),
Util.get_uint16_be(sizeinfo, 2), MIME_TYPE
else
stream:read(length - 2)
end
end
end
return size