Browse Source

Add support for TTA mode on web.lua

nagadomi 9 years ago
parent
commit
472215d7ee
6 changed files with 167 additions and 14 deletions
  1. 34 9
      web.lua
  2. 4 0
      webgen/assets/style.css
  3. 44 4
      webgen/assets/ui.js
  4. 4 0
      webgen/locales/en.yml
  5. 3 0
      webgen/locales/ja.yml
  6. 78 1
      webgen/templates/index.html.erb

+ 34 - 9
web.lua

@@ -63,16 +63,39 @@ local CURL_OPTIONS = {
    max_redirects = 2
    max_redirects = 2
 }
 }
 local CURL_MAX_SIZE = 3 * 1024 * 1024
 local CURL_MAX_SIZE = 3 * 1024 * 1024
-local TTA_SUPPORT = false
+local TTA_SUPPORT = true
 
 
-local function valid_size(x, scale)
+local function valid_size(x, scale, tta_level)
    if scale == 0 then
    if scale == 0 then
-      return x:size(2) * x:size(3) <= MAX_NOISE_IMAGE
+      local limit = math.pow(math.floor(math.pow(MAX_NOISE_IMAGE / tta_level, 0.5)), 2)
+      return x:size(2) * x:size(3) <= limit
    else
    else
-      return x:size(2) * x:size(3) <= MAX_SCALE_IMAGE
+      local limit = math.pow(math.floor(math.pow(MAX_SCALE_IMAGE / tta_level, 0.5)), 2)
+      return x:size(2) * x:size(3) <= limit
+   end
+end
+local function auto_tta_level(x, scale)
+   local limit2, limit4, limit8
+   if scale == 0 then
+      limit2 = math.pow(math.floor(math.pow(MAX_NOISE_IMAGE / 2, 0.5)), 2)
+      limit4 = math.pow(math.floor(math.pow(MAX_NOISE_IMAGE / 4, 0.5)), 2)
+      limit8 = math.pow(math.floor(math.pow(MAX_NOISE_IMAGE / 8, 0.5)), 2)
+   else
+      limit2 = math.pow(math.floor(math.pow(MAX_SCALE_IMAGE / 2, 0.5)), 2)
+      limit4 = math.pow(math.floor(math.pow(MAX_SCALE_IMAGE / 4, 0.5)), 2)
+      limit8 = math.pow(math.floor(math.pow(MAX_SCALE_IMAGE / 8, 0.5)), 2)
+   end
+   local px = x:size(2) * x:size(3)
+   if px <= limit8 then
+      return 8
+   elseif px <= limit4 then
+      return 4
+   elseif px <= limit2 then
+      return 2
+   else
+      return 1
    end
    end
 end
 end
-
 local function cache_url(url)
 local function cache_url(url)
    local hash = md5.sumhexa(url)
    local hash = md5.sumhexa(url)
    local cache_file = path.join(CACHE_DIR, "url_" .. hash)
    local cache_file = path.join(CACHE_DIR, "url_" .. hash)
@@ -237,22 +260,24 @@ function APIHandler:post()
    local x, meta, filename = get_image(self)
    local x, meta, filename = get_image(self)
    local scale = tonumber(self:get_argument("scale", "0"))
    local scale = tonumber(self:get_argument("scale", "0"))
    local noise = tonumber(self:get_argument("noise", "0"))
    local noise = tonumber(self:get_argument("noise", "0"))
-   local tta_level = tonumber(self:get_argument("noise", "1"))
+   local tta_level = tonumber(self:get_argument("tta_level", "1"))
    local style = self:get_argument("style", "art")
    local style = self:get_argument("style", "art")
    local download = (self:get_argument("download", "")):len()
    local download = (self:get_argument("download", "")):len()
 
 
    if not TTA_SUPPORT then
    if not TTA_SUPPORT then
       tta_level = 1 -- disable TTA mode
       tta_level = 1 -- disable TTA mode
    else
    else
-      if not (tta_level == 1 or tta_level == 2 or tta_level == 4 or tta_level == 8) then
+      if tta_level == 0 then
+	 tta_level = auto_tta_level(x, scale)
+      end
+      if not (tta_level == 0 or tta_level == 1 or tta_level == 2 or tta_level == 4 or tta_level == 8) then
 	 tta_level = 1
 	 tta_level = 1
       end
       end
    end
    end
-
    if style ~= "art" then
    if style ~= "art" then
       style = "photo" -- style must be art or photo
       style = "photo" -- style must be art or photo
    end
    end
-   if x and valid_size(x, scale) then
+   if x and valid_size(x, scale, tta_level) then
       local prefix = nil
       local prefix = nil
       if (noise ~= 0 or scale ~= 0) then
       if (noise ~= 0 or scale ~= 0) then
 	 local hash = md5.sumhexa(meta.blob)
 	 local hash = md5.sumhexa(meta.blob)

+ 4 - 0
webgen/assets/style.css

@@ -196,3 +196,7 @@ label {
 .address {
 .address {
     text-align: center;
     text-align: center;
 }
 }
+.tta_rule_text td {
+    padding: 4px;
+    border: 1px solid #ccc;
+}

+ 44 - 4
webgen/assets/ui.js

@@ -1,5 +1,8 @@
 $(function (){
 $(function (){
-    var expires = 365;
+    var g_expires = 365;
+    var g_max_noise_image = 2560 * 2560;
+    var g_max_scale_image = 1280 * 1280;
+
     function clear_file() {
     function clear_file() {
 	var new_file = $("#file").clone();
 	var new_file = $("#file").clone();
 	new_file.change(clear_url);
 	new_file.change(clear_url);
@@ -15,17 +18,48 @@ $(function (){
 	} else {
 	} else {
 	    $(".main-title").html("w<s>/a/</s>ifu2x");
 	    $(".main-title").html("w<s>/a/</s>ifu2x");
 	}
 	}
-	$.cookie("style", checked.val(), {expires: expires});
+	$.cookie("style", checked.val(), {expires: g_expires});
+    }
+    function on_click_tta_rule(e) {
+	e.preventDefault();
+	e.stopPropagation();
+	$(".tta_rule_text").toggle();
+    }
+    function on_change_tta_level(e) {
+	var checked = $("input[name=tta_level]:checked");
+	$.cookie("tta_level", checked.val(), {expires: g_expires});
+	var level = checked.val();
+	if (level == 0) {
+	    level = 1;
+	}
+	var max_noise_w = Math.floor(Math.pow(g_max_noise_image / level, 0.5));
+	var max_scale_w = Math.floor(Math.pow(g_max_scale_image / level, 0.5));
+	var limit_text = $(".file_limits").text();
+	var hits = 0;
+	limit_text = limit_text.replace(/\d+x\d+/g, function() {
+	    hits += 1;
+	    if (hits == 1) {
+		return "" + max_noise_w + "x" + max_noise_w;
+	    } else {
+		return "" + max_scale_w + "x" + max_scale_w;
+	    }
+	});
+	$(".file_limits").text(limit_text);
+	if (level == 1) {
+	    $(".file_limits").css("color", "");
+	} else {
+	    $(".file_limits").css("color", "blue");
+	}
     }
     }
     function on_change_noise_level(e)
     function on_change_noise_level(e)
     {
     {
 	var checked = $("input[name=noise]:checked");
 	var checked = $("input[name=noise]:checked");
-	$.cookie("noise", checked.val(), {expires: expires});
+	$.cookie("noise", checked.val(), {expires: g_expires});
     }
     }
     function on_change_scale_factor(e)
     function on_change_scale_factor(e)
     {
     {
 	var checked = $("input[name=scale]:checked");
 	var checked = $("input[name=scale]:checked");
-	$.cookie("scale", checked.val(), {expires: expires});
+	$.cookie("scale", checked.val(), {expires: g_expires});
     }
     }
     function restore_from_cookie()
     function restore_from_cookie()
     {
     {
@@ -38,6 +72,9 @@ $(function (){
 	if ($.cookie("scale")) {
 	if ($.cookie("scale")) {
 	    $("input[name=scale]").filter("[value=" + $.cookie("scale") + "]").prop("checked", true)
 	    $("input[name=scale]").filter("[value=" + $.cookie("scale") + "]").prop("checked", true)
 	}
 	}
+	if ($.cookie("tta_level")) {
+	    $("input[name=tta_level]").filter("[value=" + $.cookie("tta_level") + "]").prop("checked", true)
+	}
     }
     }
     function uuid() 
     function uuid() 
     {
     {
@@ -85,10 +122,13 @@ $(function (){
     $("input[name=style]").change(on_change_style);
     $("input[name=style]").change(on_change_style);
     $("input[name=noise]").change(on_change_noise_level);
     $("input[name=noise]").change(on_change_noise_level);
     $("input[name=scale]").change(on_change_scale_factor);
     $("input[name=scale]").change(on_change_scale_factor);
+    $("input[name=tta_level]").change(on_change_tta_level);
     $("input[name=download]").click(download_with_xhr);
     $("input[name=download]").click(download_with_xhr);
+    $("a.tta_rule").click(on_click_tta_rule);
 
 
     restore_from_cookie();
     restore_from_cookie();
     on_change_style();
     on_change_style();
     on_change_scale_factor();
     on_change_scale_factor();
     on_change_noise_level();
     on_change_noise_level();
+    on_change_tta_level();
 })
 })

+ 4 - 0
webgen/locales/en.yml

@@ -18,6 +18,10 @@ nr_highest: Highest
 nr_hint: "You need use noise reduction if image actually has noise or it may cause opposite effect."
 nr_hint: "You need use noise reduction if image actually has noise or it may cause opposite effect."
 upscaling: Upscaling
 upscaling: Upscaling
 up_none: None
 up_none: None
+tta: TTA
+tta_best_effort: "n-pass auto"
+tta_rule: "Show the rule of `n-pass auto`."
+tta_hint: "TTA makes stable quality but it limits the source image size."
 button_convert: Convert
 button_convert: Convert
 button_download: Download
 button_download: Download
 hints:
 hints:

+ 3 - 0
webgen/locales/ja.yml

@@ -18,6 +18,9 @@ nr_highest: 最高
 nr_hint: "ノイズ除去は細部が消えることがあります。JPEGノイズがある場合に使用します。"
 nr_hint: "ノイズ除去は細部が消えることがあります。JPEGノイズがある場合に使用します。"
 upscaling: 拡大
 upscaling: 拡大
 up_none: なし
 up_none: なし
+tta: TTA
+tta_hint: "TTAは安定した品質を作ります。ただし画像サイズに制限があります。"
+tta_rule: "ルール"
 button_convert: 実行
 button_convert: 実行
 button_download: 実行結果を保存
 button_download: 実行結果を保存
 hints:
 hints:

+ 78 - 1
webgen/templates/index.html.erb

@@ -73,7 +73,7 @@
 	      <%= t[:choose_file] %>: 
 	      <%= t[:choose_file] %>: 
 	      <input type="file" id="file" name="file"></div>
 	      <input type="file" id="file" name="file"></div>
 	  </div>
 	  </div>
-	  <div class="option-hint">
+	  <div class="option-hint file_limits">
 	    <%= t[:file_limits] %>
 	    <%= t[:file_limits] %>
 	  </div>
 	  </div>
 	</div>
 	</div>
@@ -152,6 +152,83 @@
 	    </label>
 	    </label>
 	  </div>
 	  </div>
 	</div>
 	</div>
+	<div class="option-box">
+	  <div class="option-left">
+	    <%= t[:tta] %>:
+	    <div class="option-left-small"></div>
+	  </div>
+	  <div class="option-right">
+	    <label><input type="radio" name="tta_level" class="radio" value="1" checked>
+	      <span class="r-text">
+		1 pass
+	      </span>
+	    </label>
+	    <!--
+	    <label><input type="radio" name="tta_level" class="radio" value="2" checked>
+	      <span class="r-text">
+		2 pass
+	      </span>
+	    </label>
+	    <label><input type="radio" name="tta_level" class="radio" value="4">
+	      <span class="r-text">
+		4 pass
+	      </span>
+	    </label>
+	    <label><input type="radio" name="tta_level" class="radio" value="8">
+	      <span class="r-text">
+		8 pass
+	      </span>
+	    </label>
+	    -->
+	    <label><input type="radio" name="tta_level" class="radio" value="0" checked>
+	      <span class="r-text">
+		<%= t[:tta_best_effort] %>
+	      </span>
+	    </label>
+	  </div>
+	  <div class="option-hint">
+	    <%= t[:tta_hint] %>
+	    <a href="#" class="tta_rule">
+	      <%= t[:tta_rule] %>
+	    </a>
+	    <div style="display:none" class="tta_rule_text">
+	      <table>
+		<tr>
+		  <td>
+		    &lt;= 452x452
+		  </td>
+		  <td>
+		    8 pass
+		  </td>
+		</tr>
+		<tr>
+		  <td>
+		    &lt;= 640x640 
+		  </td>
+		  <td>
+		    4 pass
+		  </td>
+		</tr>
+		<tr>
+		  <td>
+		    &lt;= 905x905
+		  </td>
+		  <td>
+		    2 pass
+		  </td>
+		</tr>
+		<tr>
+		  <td>
+		    else
+		  </td>
+		  <td>
+		    1 pass
+		  </td>
+		</tr>
+	      </table>
+	    </div>
+	  </div>
+	</div>
 	<% if t[:button_convert] && !t[:button_convert].empty? %>
 	<% if t[:button_convert] && !t[:button_convert].empty? %>
 	  <input type="submit" class="button" value="<%= t[:button_convert] %>">
 	  <input type="submit" class="button" value="<%= t[:button_convert] %>">
 	<% else %>
 	<% else %>