2013-10-18 20:38:07 +00:00
|
|
|
local Widget = require("ui/widget/widget")
|
2014-11-20 07:52:05 +00:00
|
|
|
local Screen = require("device").screen
|
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-11-20 07:52:05 +00:00
|
|
|
-- if autoscale is true image will be rescaled according to screen dpi
|
|
|
|
autoscale = false,
|
2014-11-28 15:31:54 +00:00
|
|
|
-- when alpha is set to true, alpha values from the image will be honored
|
|
|
|
alpha = false,
|
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-11-20 07:52:05 +00:00
|
|
|
local native_w, native_h = self._bb:getWidth(), self._bb:getHeight()
|
|
|
|
local scaled_w, scaled_h = self.width, self.height
|
|
|
|
if self.autoscale then
|
|
|
|
local dpi_scale = Screen:getDPI() / 167
|
|
|
|
-- rounding off to power of 2 to avoid alias with pow(2, floor(log(x)/log(2))
|
|
|
|
local scale = math.pow(2, math.max(0, math.floor(math.log(dpi_scale)/0.69)))
|
|
|
|
scaled_w, scaled_h = scale * native_w, scale * native_h
|
|
|
|
end
|
|
|
|
if (scaled_w and scaled_w ~= native_w) or (scaled_h and scaled_h ~= native_h) then
|
|
|
|
self._bb = self._bb:scale(scaled_w or native_w, scaled_h or native_h)
|
2014-08-14 10:08:52 +00:00
|
|
|
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
|
|
|
}
|
2014-11-28 15:31:54 +00:00
|
|
|
if self.alpha == true then
|
|
|
|
bb:alphablitFrom(self._bb, x, y, 0, 0, size.w, size.h)
|
|
|
|
else
|
|
|
|
bb:blitFrom(self._bb, x, y, 0, 0, size.w, size.h)
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
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
|