diff --git a/ffi/mupdfimg.lua b/ffi/mupdfimg.lua index ceb30f614..59ed762fe 100644 --- a/ffi/mupdfimg.lua +++ b/ffi/mupdfimg.lua @@ -15,24 +15,36 @@ function Image:initContext(cache_size) self.context = mupdf.fz_new_context_imp(nil, nil, cache_size or bit.lshift(8, 20), "1.5") end -function Image:loadImage(filename, width, height) +function Image:_loadImage(data, size, width, height) + local image = mupdf.fz_new_image_from_data(self.context, + ffi.cast("unsigned char*", data), size) + if image ~= nil then + self.pixmap = mupdf.fz_new_pixmap_from_image(self.context, + image, width or -1, height or -1) + self.image = mupdf.fz_keep_image(self.context, image) + end +end + +function Image:loadImageFile(filename, width, height) local file = io.open(filename) if file then local data = file:read("*a") file:close() - local image = mupdf.fz_new_image_from_data(self.context, - ffi.cast("unsigned char*", data), #data) - if image ~= nil then - self.pixmap = mupdf.fz_new_pixmap_from_image(self.context, - image, width or -1, height or -1) - self.image = mupdf.fz_keep_image(self.context, image) + if data then + self:_loadImage(data, #data, width, height) end end end +function Image:loadImageData(data, size, width, height) + if data and size then + self:_loadImage(data, size, width, height) + end +end + function Image:toBlitBuffer() - local pixmap = ffi.new("fz_pixmap[1]") - pixmap = self.pixmap + if self.pixmap == nil then return end + local pixmap = ffi.new("fz_pixmap*[1]", self.pixmap)[0] if self.pixmap.n ~= 2 then self.pixmap = mupdf.fz_new_pixmap(self.context, mupdf.fz_device_gray(self.context), pixmap.w, pixmap.h); @@ -61,7 +73,15 @@ end function Image:fromFile(filename, width, height) self:initContext(0) - self:loadImage(filename, width, height) + self:loadImageFile(filename, width, height) + self:toBlitBuffer() + self:freeContext() + return self.bb +end + +function Image:fromData(data, size, width, height) + self:initContext(0) + self:loadImageData(data, size, width, height) self:toBlitBuffer() self:freeContext() return self.bb