Skip to content

Commit

Permalink
init: developed single image processing function
Browse files Browse the repository at this point in the history
Now that everything is in place, it's time to develop the single
image processing function. Hence, let's do this.

This patch develops single image processing function in init/
directory.

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 10, 2024
1 parent ac2d971 commit 5d0be0f
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 7 deletions.
60 changes: 59 additions & 1 deletion init/services/compilers/upscaler.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
. "${env:LIBS_UPSCALER}\services\io\os.ps1"
. "${env:LIBS_UPSCALER}\services\io\fs.ps1"
. "${env:LIBS_UPSCALER}\services\io\strings.ps1"
. "${env:LIBS_UPSCALER}\services\i18n\report-simulation.ps1"



Expand Down Expand Up @@ -303,8 +304,65 @@ function UPSCALER-Program-Get {


function UPSCALER-Run-Image {
param(
[string]$___model,
[string]$___scale,
[string]$___format,
[string]$___gpu,
[string]$___input,
[string]$___output
)


# validate input
if (((STRINGS-Is-Empty "${___model}") -eq 0) -or
((STRINGS-Is-Empty "${___scale}") -eq 0) -or
((STRINGS-Is-Empty "${___format}") -eq 0) -or
((STRINGS-Is-Empty "${___gpu}") -eq 0) -or
((STRINGS-Is-Empty "${___input}") -eq 0) -or
((STRINGS-Is-Empty "${___output}") -eq 0)) {
return 1
}


if ((STRINGS-Is-Empty "$(UPSCALER-Program-Get)") -eq 0) {
return 1
}


# construct arugment
$___cmd = "-i '${___input}' -o '${___output}'"
$___cmd = "${___cmd} -m '${env:UPSCALER_PATH_ROOT}/models/' -n '${___model}'"
$___cmd = "${___cmd} -s '${___scale}' -g '${___gpu}'"
switch ($___format) {
"jpg" {
$___cmd = "${___cmd} -f jpg"
} "webp" {
$___cmd = "${___cmd} -f webp"
} default {
# do nothing - maintain png
}}


# execute
$null = FS-Make-Housing-Directory "${___output}"
$null = FS-Remove-Silently "${___output}"
if ((STRINGS-Is-Empty "${env:UPSCALER_TEST_MODE}") -ne 0) {
$null = I18N-Report-Simulation "$(UPSCALER-Program-Get) ${___cmd}"
$___process = FS-Copy-File "${___input}" "${___output}"
if ($___process -eq 0) {
return 0
}
} else {
$___process = OS-Exec "$(UPSCALER-Program-Get)" "${___cmd}"
if ($___process -eq 0) {
return 0
}
}


# report status
return 0
return 1
}


Expand Down
58 changes: 57 additions & 1 deletion init/services/compilers/upscaler.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
# 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/report-simulation.sh"
. "${LIBS_UPSCALER}/services/io/os.sh"
. "${LIBS_UPSCALER}/services/io/fs.sh"
. "${LIBS_UPSCALER}/services/io/strings.sh"
Expand Down Expand Up @@ -319,8 +320,63 @@ UPSCALER_Program_Get() {


UPSCALER_Run_Image() {
#___model="$1"
#___scale="$2"
#___format="$3"
#___gpu="$4"
#___input="$5"
#___output="$6"


# validate input
if [ "$(STRINGS_Is_Empty "$1")" = "0" ] ||
[ "$(STRINGS_Is_Empty "$2")" = "0" ] ||
[ "$(STRINGS_Is_Empty "$3")" = "0" ] ||
[ "$(STRINGS_Is_Empty "$4")" = "0" ] ||
[ "$(STRINGS_Is_Empty "$5")" = "0" ] ||
[ "$(STRINGS_Is_Empty "$6")" = "0" ]; then
return 1
fi

if [ "$(STRINGS_Is_Empty "$(UPSCALER_Program_Get)")" = "0" ]; then
return 1
fi


# construct argument
___cmd="-i '${5}' -o '${6}'"
___cmd="${___cmd} -m '${UPSCALER_PATH_ROOT}/models/' -n '${1}' -s ${2} -g ${4}"
case "${3}" in
jpg)
___args="${___args} -f jpg"
;;
webp)
___args="${___args} -f webp"
;;
*)
;;
esac


# execute
FS_Make_Housing_Directory "$6"
FS_Remove_Silently "$6"
if [ ! "$(STRINGS_Is_Empty "$UPSCALER_TEST_MODE")" = "0" ]; then
I18N_Report_Simulation "$(UPSCALER_Program_Get) ${___cmd}"
FS_Copy_File "$5" "$6"
if [ $? -eq 0 ]; then
return 0
fi
else
eval "$(UPSCALER_Program_Get) $___cmd"
if [ $? -eq 0 ]; then
return 0
fi
fi


# report status
return 0
return 1
}


Expand Down
53 changes: 53 additions & 0 deletions init/services/i18n/report-simulation.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# 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-Report-Simulation {
param(
[string]$___message
)


# execute
switch (${env:UPSCALER_LANG}) {
{ $_ -in "DE", "de" } {
# german
$null = I18N-Status-Print "warning" "Simulationsübung: ${___message}`n"
} default {
$null = I18N-Status-Print "warning" "Simulation: ${___message}`n"
}}


# report status
return 0
}
54 changes: 54 additions & 0 deletions init/services/i18n/report-simulation.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 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_Report_Simulation() {
#___message="$1"


# execute
case "$UPSCALER_LANG" in
DE|de)
# German
I18N_Status_Print "warning" "Simulationsübung: ${1}\n"
;;
*)
# fallback to default english
I18N_Status_Print "warning" "Simulation: ${1}\n"
;;
esac


# report status
return 0
}
1 change: 0 additions & 1 deletion init/start.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ if ((${__video} -eq 0) -and (${__batch} -eq 0)) {
"${__model}" `
"${__scale}" `
"${__format}" `
"${__parallel}" `
"${__gpu}" `
"${__input}" `
"${__output}"
Expand Down
1 change: 0 additions & 1 deletion init/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ if [ "$__video" = "0" ] && [ "$__batch" = "0" ]; then
"${__model}" \
"${__scale}" \
"${__format}" \
"${__parallel}" \
"${__gpu}" \
"${__input}" \
"${__output}"
Expand Down
13 changes: 10 additions & 3 deletions tests/01_test_upscale_image_single.sh.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,14 @@ $__process = . "${env:LIBS_UPSCALER}\start.ps1" `
"--input" "${env:UPSCALER_PATH_ROOT}\tests\image\sample-01.jpeg" `
"--output" "${env:UPSCALER_PATH_ROOT}\tmp\test-01\sample-01-upscaled.png"
if ($__process -ne 0) {
$null = I18N-Status-Print "error" "Failed.`n`n"
$null = I18N-Status-Print "error" "Failed - bad processing.`n`n"
$__verdict = $false
}

if (-not (Test-Path -Path "${env:UPSCALER_PATH_ROOT}\tmp\test-01\sample-01-upscaled.png")) {
$null = I18N-Status-Print "error" "Failed - missing output.`n`n"
$__verdict = $false
}



Expand Down Expand Up @@ -176,11 +180,14 @@ I18N_Status_Print "note" "test single image upscale...\n"
--input "${UPSCALER_PATH_ROOT}/tests/image/sample-01.jpeg" \
"--output" "${UPSCALER_PATH_ROOT}/tmp/test-01/sample-01-upscaled.png"
if [ $? -ne 0 ]; then
I18N_Status_Print "error" "Failed.\n\n"
I18N_Status_Print "error" "Failed - bad processing.\n\n"
__verdict=1
fi
I18N_Status_Print "note" "Passed.\n\n"
if [ ! -f "${UPSCALER_PATH_ROOT}/tmp/test-01/sample-01-upscaled.png" ]; then
I18N_Status_Print "error" "Failed - missing output.\n\n"
__verdict=1
fi
Expand Down

0 comments on commit 5d0be0f

Please sign in to comment.