2
0
mirror of https://github.com/koreader/koreader synced 2024-11-16 06:12:56 +00:00
koreader/frontend/ui/widget/imagewidget.lua

134 lines
3.7 KiB
Lua
Raw Normal View History

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")
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,
image = nil,
2014-03-13 13:52:43 +00:00
invert = nil,
dim = nil,
hide = nil,
-- 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,
-- 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
}
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 "")
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)
2016-01-03 09:18:54 +00:00
cache = ImageCacheItem:new{
bb = Mupdf.renderImageFile(self.file, self.width, self.height),
2014-08-22 09:22:41 +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
else
error("Image file type not supported.")
2014-03-13 13:52:43 +00:00
end
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)
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,
h = size.h
2014-03-13 13:52:43 +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
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