1
0
Fork 0
mirror of synced 2024-06-02 02:54:31 +12:00

Merge branch 'dev' of private repo into dev

This commit is contained in:
nagadomi 2016-04-15 13:32:02 +09:00
commit ac9b6f1149
3 changed files with 27 additions and 17 deletions

View file

@ -38,11 +38,17 @@ function image_loader.encode_png(rgb, options)
local im
if rgb:size(1) == 4 then -- RGBA
im = gm.Image(rgb, "RGBA", "DHW")
if options.grayscale then
im:type("GrayscaleMatte")
end
elseif rgb:size(1) == 3 then -- RGB
im = gm.Image(rgb, "RGB", "DHW")
if options.grayscale then
im:type("Grayscale")
end
elseif rgb:size(1) == 1 then -- Y
im = gm.Image(rgb, "I", "DHW")
-- im:colorspace("GRAY") -- it does not work
im:type("Grayscale")
end
if options.gamma then
im:gamma(options.gamma)
@ -73,19 +79,19 @@ function image_loader.decode_float(blob)
if gamma ~= 0 and math.floor(im:gamma() * 1000000) / 1000000 ~= gamma_lcd then
meta.gamma = im:gamma()
end
-- FIXME: How to detect that a image has an alpha channel?
if blob:sub(1, 4) == "\x89PNG" or blob:sub(1, 3) == "GIF" then
local image_type = im:type()
if image_type == "Grayscale" or image_type == "GrayscaleMatte" then
meta.grayscale = true
end
if image_type == "TrueColorMatte" or image_type == "GrayscaleMatte" then
-- split alpha channel
im = im:toTensor('float', 'RGBA', 'DHW')
local sum_alpha = (im[4] - 1.0):sum()
if sum_alpha < 0 then
meta.alpha = im[4]:reshape(1, im:size(2), im:size(3))
-- drop full transparent background
local mask = torch.le(meta.alpha, 0.0)
im[1][mask] = background_color
im[2][mask] = background_color
im[3][mask] = background_color
end
meta.alpha = im[4]:reshape(1, im:size(2), im:size(3))
-- drop full transparent background
local mask = torch.le(meta.alpha, 0.0)
im[1][mask] = background_color
im[2][mask] = background_color
im[3][mask] = background_color
local new_im = torch.FloatTensor(3, im:size(2), im:size(3))
new_im[1]:copy(im[1])
new_im[2]:copy(im[2])

View file

@ -66,7 +66,7 @@ local function convert_image(opt)
else
error("undefined method:" .. opt.method)
end
image_loader.save_png(opt.o, new_x, {depth = opt.depth, inplace = true, gamma = meta.gamma})
image_loader.save_png(opt.o, new_x, tablex.update({depth = opt.depth, inplace = true}, meta))
print(opt.o .. ": " .. (sys.clock() - t) .. " sec")
end
local function convert_frames(opt)
@ -144,7 +144,7 @@ local function convert_frames(opt)
output = string.format(opt.o, i)
end
image_loader.save_png(output, new_x,
{depth = opt.depth, inplace = true, gamma = meta.gamma})
tablex.update({depth = opt.depth, inplace = true}, meta))
xlua.progress(i, #lines)
if i % 10 == 0 then
collectgarbage()

10
web.lua
View file

@ -138,7 +138,9 @@ local function convert(x, meta, options)
end
if path.exists(cache_file) then
x = image_loader.load_float(cache_file)
return x, {alpha = alpha, gamma = meta.gamma, blob = meta.blob}
meta = tablex.copy(meta)
meta.alpha = alpha
return x, meta
else
if options.style == "art" then
if options.border then
@ -192,8 +194,10 @@ local function convert(x, meta, options)
end
end
image_loader.save_png(cache_file, x)
meta = tablex.copy(meta)
meta.alpha = alpha
return x, {alpha = alpha, gamma = meta.gamma, blob = meta.blob}
return x, meta
end
end
local function client_disconnected(handler)
@ -283,7 +287,7 @@ function APIHandler:post()
name = uuid() .. ".png"
end
local blob = image_loader.encode_png(alpha_util.composite(x, meta.alpha),
{ depth = 8, inplace = true, gamma = meta.gamma})
tablex.update({depth = 8, inplace = true}, meta))
self:set_header("Content-Length", string.format("%d", #blob))
if download > 0 then