2
0
mirror of https://github.com/koreader/koreader synced 2024-11-16 06:12:56 +00:00
koreader/frontend/ui/widget/imagewidget.lua
2014-03-13 21:52:43 +08:00

57 lines
1.2 KiB
Lua

local Widget = require("ui/widget/widget")
local Geom = require("ui/geometry")
-- Image
--[[
ImageWidget shows an image from a file
--]]
local ImageWidget = Widget:new{
file = nil,
invert = nil,
dim = nil,
hide = nil,
_bb = nil
}
function ImageWidget:_render()
local itype = string.lower(string.match(self.file, ".+%.([^.]+)") or "")
if itype == "jpeg" or itype == "jpg" then
self._bb = Image:fromJPEG(self.file)
elseif itype == "png" then
self._bb = Image:fromPNG(self.file)
end
end
function ImageWidget:getSize()
if not self._bb then
self:_render()
end
return Geom:new{ w = self._bb:getWidth(), h = self._bb:getHeight() }
end
function ImageWidget:paintTo(bb, x, y)
local size = self:getSize()
self.dimen = Geom:new{
x = x, y = y,
w = size.w,
h = size.h
}
if self.hide then return end
bb:blitFrom(self._bb, x, y, 0, 0, size.w, size.h)
if self.invert then
bb:invertRect(x, y, size.w, size.h)
end
if self.dim then
bb:dimRect(x, y, size.w, size.h)
end
end
function ImageWidget:free()
if self._bb then
self._bb:free()
self._bb = nil
end
end
return ImageWidget