diff --git a/lib/iproc.lua b/lib/iproc.lua
index 6e68063..d37ddcc 100644
--- a/lib/iproc.lua
+++ b/lib/iproc.lua
@@ -84,6 +84,30 @@ function iproc.padding(img, w1, w2, h1, h2)
flow[2]:add(-w1)
return image.warp(img, flow, "simple", false, "clamp")
end
+function iproc.white_noise(src, std, rgb_weights, gamma)
+ gamma = gamma or 0.454545
+ local conversion
+ src, conversion = iproc.byte2float(src)
+ std = std or 0.01
+
+ local noise = torch.Tensor():resizeAs(src):normal(0, std)
+ if rgb_weights then
+ noise[1]:mul(rgb_weights[1])
+ noise[2]:mul(rgb_weights[2])
+ noise[3]:mul(rgb_weights[3])
+ end
+
+ local dest
+ if gamma ~= 0 then
+ dest = src:clone():pow(gamma):add(noise):pow(1.0 / gamma)
+ else
+ dest = src + noise
+ end
+ if conversion then
+ dest = iproc.float2byte(dest)
+ end
+ return dest
+end
local function test_conversion()
local a = torch.linspace(0, 255, 256):float():div(255.0)
diff --git a/waifu2x.lua b/waifu2x.lua
index e07209d..a1faf7f 100644
--- a/waifu2x.lua
+++ b/waifu2x.lua
@@ -58,6 +58,9 @@ local function convert_image(opt)
else
error("undefined method:" .. opt.method)
end
+ if opt.white_noise == 1 then
+ new_x = iproc.white_noise(new_x, opt.white_noise_std, {1.0, 0.8, 1.0})
+ end
image_loader.save_png(opt.o, new_x, alpha, opt.depth)
print(opt.o .. ": " .. (sys.clock() - t) .. " sec")
end
@@ -138,6 +141,10 @@ local function convert_frames(opt)
else
error("undefined method:" .. opt.method)
end
+ if opt.white_noise == 1 then
+ new_x = iproc.white_noise(new_x, opt.white_noise_std, {1.0, 0.8, 1.0})
+ end
+
local output = nil
if opt.o == "(auto)" then
local name = path.basename(lines[i])
@@ -175,6 +182,8 @@ local function waifu2x()
cmd:option("-resume", 0, "skip existing files (0|1)")
cmd:option("-thread", -1, "number of CPU threads")
cmd:option("-tta", 0, '8x slower and slightly high quality (0|1)')
+ cmd:option("-white_noise", 0, 'adding white noise to output image (0|1)')
+ cmd:option("-white_noise_std", 0.0055, 'standard division of white noise')
local opt = cmd:parse(arg)
if opt.thread > 0 then
diff --git a/web.lua b/web.lua
index 4ebe492..8ac4dce 100644
--- a/web.lua
+++ b/web.lua
@@ -142,6 +142,7 @@ function APIHandler:post()
local x, alpha, blob = get_image(self)
local scale = tonumber(self:get_argument("scale", "0"))
local noise = tonumber(self:get_argument("noise", "0"))
+ local white_noise = tonumber(self:get_argument("white_noise", "0"))
local style = self:get_argument("style", "art")
if style ~= "art" then
style = "photo" -- style must be art or photo
@@ -169,6 +170,9 @@ function APIHandler:post()
"Jinc")
end
end
+ if white_noise == 1 then
+ x = iproc.white_noise(x, 0.005, {1.0, 0.8, 1.0})
+ end
end
local name = uuid() .. ".png"
local blob, len = image_loader.encode_png(x, alpha)
@@ -179,7 +183,7 @@ function APIHandler:post()
else
if not x then
self:set_status(400)
- self:write("ERROR: unsupported image format.")
+ self:write("ERROR: An error occurred. (unsupported image format/connection timeout/file is too large)")
else
self:set_status(400)
self:write("ERROR: image size exceeds maximum allowable size.")