Skip to content

Commit

Permalink
create Image from raw buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
chrox committed Aug 22, 2014
1 parent 7909ef4 commit 77654eb
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions ffi/mupdfimg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 77654eb

Please sign in to comment.