Skip to content

Commit

Permalink
root: added scaling factor validator
Browse files Browse the repository at this point in the history
Since scaling factor is an important input, we should add its
validator in. Hence, let's do this.

This patch adds scaling factor validator into root repository.

Co-authored-by: Shuralyov, Jean <jean.shuralyov@proton.me>
Co-authored-by: Galyna, Cory <cory.galyna@gmail.com>
Co-authored-by: (Holloway) Chew, Kean Ho <hollowaykeanho@gmail.com>
Signed-off-by: (Holloway) Chew, Kean Ho <hollowaykeanho@gmail.com>
  • Loading branch information
3 people committed Feb 9, 2024
1 parent 09a843b commit 6b2a9af
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 6 deletions.
64 changes: 64 additions & 0 deletions init/services/compilers/upscaler.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,67 @@ function UPSCALER-Model-Get {
# report status
return ""
}




function UPSCALER-Scale-Get {
param(
[string]$___limit,
[string]$___input
)


# validate input
if (
((STRINGS-Is-Empty "${___limit}") -eq 0) -or
((STRINGS-Is-Empty "${___input}") -eq 0)) {
return 0
}


# execute
switch ($___limit) {
"any" {
switch ($___input) {
"1" {
return 1
} "2" {
return 2
} "3" {
return 3
} "4" {
return 4
} default {
}}
} "1" {
switch ($___input) {
"1" {
return 1
} default {
}}
} "2" {
switch ($___input) {
"2" {
return 2
} default {
}}
} "3" {
switch ($___input) {
"3" {
return 3
} default {
}}
} "4" {
switch ($___input) {
"4" {
return 4
} default {
}}
} default {
}}


# report status
return 0
}
77 changes: 77 additions & 0 deletions init/services/compilers/upscaler.sh
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,80 @@ UPSCALER_Model_Get() {
printf -- ""
return 1
}




UPSCALER_Scale_Get() {
#___limit="$1"
#___input="$2"


# validate input
if [ -z "$1" ]; then
printf -- ""
return 1
fi


# execute
case "$1" in
any)
case "$2" in
1|2|3|4)
printf -- "%b" "$2"
return 0
;;
*)
;;
esac
;;
1)
case "$2" in
1)
printf -- "1"
return 0
;;
*)
;;
esac
;;
2)
case "$2" in
2)
printf -- "2"
return 0
;;
*)
;;
esac
;;
3)
case "$2" in
3)
printf -- "3"
return 0
;;
*)
;;
esac
;;
4)
case "$2" in
4)
printf -- "4"
return 0
;;
*)
;;
esac
;;
*)
;;
esac


# report status
printf -- ""
return 1
}
51 changes: 51 additions & 0 deletions init/services/i18n/error-scale-unknown.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# BSD 3-Clause License
#
# Copyright (c) 2024 (Holloway) Chew, Kean Ho
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
. "${env:LIBS_UPSCALER}\services\i18n\__printer.ps1"




function I18N-Status-Error-Scale-Unknown {
# execute
switch (${env:UPSCALER_LANG}) {
{ $_ -in "DE", "de" } {
# german
$null = I18N-Status-Print "error" `
"Unbekannte/fehlende Skala. Es kann nur sein: 1|2|3|4`n"
} default {
# fallback to default english
$null = I18N-Status-Print "error" `
"Unknown/Missing scale. It can only be: 1|2|3|4`n"
}}


# report status
return 0
}
52 changes: 52 additions & 0 deletions init/services/i18n/error-scale-unknown.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# BSD 3-Clause License
#
# Copyright (c) 2024 (Holloway) Chew, Kean Ho
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
. "${LIBS_UPSCALER}/services/i18n/__printer.sh"




I18N_Status_Error_Scale_Unknown() {
# execute
case "$UPSCALER_LANG" in
DE|de)
# German
I18N_Status_Print "error" \
"Unbekannte/fehlende Skala. Es kann nur sein: 1|2|3|4\n"
;;
*)
# fallback to default english
I18N_Status_Print "error" "Unknown/Missing scale. It can only be: 1|2|3|4\n"
;;
esac


# report status
return 0
}
10 changes: 8 additions & 2 deletions init/start.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ ${env:LIBS_UPSCALER} = "${env:UPSCALER_PATH_ROOT}\${env:UPSCALER_PATH_SCRIPTS}"
. "${env:LIBS_UPSCALER}\services\compilers\upscaler.ps1"
. "${env:LIBS_UPSCALER}\services\i18n\error-unsupported.ps1"
. "${env:LIBS_UPSCALER}\services\i18n\error-model-unknown.ps1"
. "${env:LIBS_UPSCALER}\services\i18n\error-scale-unknown.ps1"
. "${env:LIBS_UPSCALER}\services\i18n\help.ps1"


Expand Down Expand Up @@ -185,14 +186,19 @@ if ((STRINGS-Is-Empty "${___process}") -eq 0) {
}
$___process = $___process -split ""
$__model = $___process[0]
$__scale_limit = $___process[1]
$__model_name = $___process[2]


$__scale = UPSCALER-Scale-Get $___process[1] $__scale
if (($__scale -le 0) -or ($__scale -gt 4)) {
$null = I18N-Status-Error-Scale-Unknown
return 1
}




# placeholder
Write-Host "DEBUG: |${__model}|${__scale_limit}|${__model_name}|"
Write-Host "DEBUG: Model='${__model}' Scale='${__scale}' Format='${__format}' Parallel='${__parallel}' Video='${__video}' Input='${__input}' Output='${__output}' GPU='${__gpu}'"

return 0
8 changes: 7 additions & 1 deletion init/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export LIBS_UPSCALER="${UPSCALER_PATH_ROOT}/${UPSCALER_PATH_SCRIPTS}"
. "${LIBS_UPSCALER}/services/compilers/upscaler.sh"
. "${LIBS_UPSCALER}/services/i18n/error-unsupported.sh"
. "${LIBS_UPSCALER}/services/i18n/error-model-unknown.sh"
. "${LIBS_UPSCALER}/services/i18n/error-scale-unknown.sh"
. "${LIBS_UPSCALER}/services/i18n/help.sh"


Expand Down Expand Up @@ -187,9 +188,14 @@ fi
__model="${___process%%*}"
__model_name="${___process##*│}"
___process="${___process#*│}"
___scale_limit="${___process%%*}"


__scale="$(UPSCALER_Scale_Get "${___process%%*}" "$__scale")"
if [ -z "$__scale" ]; then
I18N_Status_Error_Scale_Unknown
return 1
fi



# placeholder
Expand Down
8 changes: 5 additions & 3 deletions tests/01_test_upscale_image_single.sh.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ $__verdict = $true

$null = I18N-Status-Print "note" "test single image upscale...`n"
$__process = . "${env:LIBS_UPSCALER}\start.ps1" `
"--model" `
"upscayl-nmkd-4x-superscale-sd-178000-g"
"--model" "upscayl-nmkd-4x-superscale-sd-178000-g" `
"--scale" "4" `
if ($__process -ne 0) {
$null = I18N-Status-Print "error" "Failed.`n`n"
$__verdict = $false
Expand Down Expand Up @@ -166,7 +166,9 @@ __verdict=0
I18N_Status_Print "note" "test single image upscale...\n"
. "${LIBS_UPSCALER}"/start.sh --model upscayl-nmkd-4x-superscale-sd-178000-g
. "${LIBS_UPSCALER}"/start.sh \
--model upscayl-nmkd-4x-superscale-sd-178000-g \
--scale 4
if [ $? -ne 0 ]; then
I18N_Status_Print "error" "Failed.\n\n"
__verdict=1
Expand Down

0 comments on commit 6b2a9af

Please sign in to comment.