2013-10-18 20:38:07 +00:00
|
|
|
local Widget = require("ui/widget/widget")
|
2014-08-22 09:22:41 +00:00
|
|
|
local CacheItem = require("cacheitem")
|
2014-10-18 22:22:35 +00:00
|
|
|
local Mupdf = require("ffi/mupdf")
|
2013-10-18 20:38:07 +00:00
|
|
|
local Geom = require("ui/geometry")
|
2014-08-22 09:22:41 +00:00
|
|
|
local Cache = require("cache")
|
|
|
|
local DEBUG = require("dbg")
|
|
|
|
|
|
|
|
local ImageCache = Cache:new{
|
|
|
|
max_memsize = 2*1024*1024, -- 2M of image cache
|
|
|
|
current_memsize = 0,
|
|
|
|
cache = {},
|
|
|
|
-- this will hold the LRU order of the cache
|
|
|
|
cache_order = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
local ImageCacheItem = CacheItem:new{}
|
|
|
|
|
|
|
|
function ImageCacheItem:onFree()
|
|
|
|
if self.bb.free then
|
|
|
|
DEBUG("free image blitbuffer", self.bb)
|
|
|
|
self.bb:free()
|
|
|
|
end
|
|
|
|
end
|
2013-03-12 17:18:53 +00:00
|
|
|
|
|
|
|
--[[
|
|
|
|
ImageWidget shows an image from a file
|
|
|
|
--]]
|
2013-10-18 20:38:07 +00:00
|
|
|
local ImageWidget = Widget:new{
|
2014-03-13 13:52:43 +00:00
|
|
|
file = nil,
|
2014-08-27 03:07:25 +00:00
|
|
|
image = nil,
|
2014-03-13 13:52:43 +00:00
|
|
|
invert = nil,
|
|
|
|
dim = nil,
|
|
|
|
hide = nil,
|
2014-08-14 10:08:52 +00:00
|
|
|
-- if width or height is given, image will rescale to the given size
|
|
|
|
width = nil,
|
|
|
|
height = nil,
|
2014-03-13 13:52:43 +00:00
|
|
|
_bb = nil
|
2013-03-12 17:18:53 +00:00
|
|
|
}
|
|
|
|
|
2014-08-27 03:07:25 +00:00
|
|
|
function ImageWidget:_loadimage()
|
|
|
|
self._bb = self.image
|
|
|
|
end
|
|
|
|
|
|
|
|
function ImageWidget:_loadfile()
|
2014-03-13 13:52:43 +00:00
|
|
|
local itype = string.lower(string.match(self.file, ".+%.([^.]+)") or "")
|
2014-08-17 09:39:03 +00:00
|
|
|
if itype == "png" or itype == "jpg" or itype == "jpeg"
|
|
|
|
or itype == "tiff" then
|
2014-08-22 09:22:41 +00:00
|
|
|
local hash = "image|"..self.file.."|"..(self.width or "").."|"..(self.height or "")
|
|
|
|
local cache = ImageCache:check(hash)
|
|
|
|
if cache then
|
|
|
|
-- hit cache
|
|
|
|
self._bb = cache.bb
|
|
|
|
else
|
|
|
|
-- cache this image
|
|
|
|
DEBUG("cache", hash)
|
|
|
|
local cache = ImageCacheItem:new{
|
2014-10-18 22:22:35 +00:00
|
|
|
bb = Mupdf.renderImageFile(self.file, self.width, self.height),
|
2014-08-22 09:22:41 +00:00
|
|
|
}
|
2014-10-18 22:22:35 +00:00
|
|
|
cache.size = cache.bb.pitch * cache.bb.h * cache.bb:getBpp() / 8
|
2014-08-22 09:22:41 +00:00
|
|
|
ImageCache:insert(hash, cache)
|
|
|
|
self._bb = cache.bb
|
|
|
|
end
|
2014-08-17 09:39:03 +00:00
|
|
|
else
|
|
|
|
error("Image file type not supported.")
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2014-08-27 03:07:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ImageWidget:_render()
|
|
|
|
if self.image then
|
|
|
|
self:_loadimage()
|
|
|
|
elseif self.file then
|
|
|
|
self:_loadfile()
|
|
|
|
else
|
|
|
|
error("cannot render image")
|
|
|
|
end
|
2014-08-14 10:08:52 +00:00
|
|
|
local w, h = self._bb:getWidth(), self._bb:getHeight()
|
|
|
|
if (self.width and self.width ~= w) or (self.height and self.height ~= h) then
|
|
|
|
self._bb = self._bb:scale(self.width or w, self.height or h)
|
|
|
|
end
|
2013-03-12 17:18:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ImageWidget:getSize()
|
2014-08-22 09:22:41 +00:00
|
|
|
self:_render()
|
2014-03-13 13:52:43 +00:00
|
|
|
return Geom:new{ w = self._bb:getWidth(), h = self._bb:getHeight() }
|
2013-03-12 17:18:53 +00:00
|
|
|
end
|
|
|
|
|
2014-08-14 12:11:21 +00:00
|
|
|
function ImageWidget:rotate(degree)
|
2014-08-22 09:22:41 +00:00
|
|
|
self:_render()
|
2014-08-14 12:11:21 +00:00
|
|
|
self._bb:rotate(degree)
|
|
|
|
end
|
|
|
|
|
2013-03-12 17:18:53 +00:00
|
|
|
function ImageWidget:paintTo(bb, x, y)
|
2014-08-22 09:22:41 +00:00
|
|
|
if self.hide then return end
|
|
|
|
-- self:_reader is called in getSize method
|
2014-03-13 13:52:43 +00:00
|
|
|
local size = self:getSize()
|
|
|
|
self.dimen = Geom:new{
|
|
|
|
x = x, y = y,
|
|
|
|
w = size.w,
|
2014-08-14 10:08:52 +00:00
|
|
|
h = size.h
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
|
|
|
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
|
2013-03-12 17:18:53 +00:00
|
|
|
end
|
|
|
|
|
2014-08-27 03:07:25 +00:00
|
|
|
function ImageWidget:free()
|
|
|
|
if self.image then
|
|
|
|
self.image:free()
|
|
|
|
self.image = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return ImageWidget
|