[981] | 1 | #!/bin/ksh |
---|
[1046] | 2 | |
---|
| 3 | #--------------------------------------------------------------------------------# |
---|
[2696] | 4 | # This file is part of the PALM model system. |
---|
[1046] | 5 | # |
---|
| 6 | # PALM is free software: you can redistribute it and/or modify it under the terms |
---|
| 7 | # of the GNU General Public License as published by the Free Software Foundation, |
---|
| 8 | # either version 3 of the License, or (at your option) any later version. |
---|
| 9 | # |
---|
| 10 | # PALM is distributed in the hope that it will be useful, but WITHOUT ANY |
---|
| 11 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
---|
| 12 | # A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
---|
| 13 | # |
---|
| 14 | # You should have received a copy of the GNU General Public License along with |
---|
| 15 | # PALM. If not, see <http://www.gnu.org/licenses/>. |
---|
| 16 | # |
---|
[2696] | 17 | # Copyright 1997-2017 Leibniz Universitaet Hannover |
---|
[1046] | 18 | #--------------------------------------------------------------------------------# |
---|
| 19 | # |
---|
[981] | 20 | # Current revisions: |
---|
| 21 | # ----------------- |
---|
[1047] | 22 | # |
---|
[981] | 23 | # |
---|
| 24 | # Former revisions: |
---|
| 25 | # ----------------- |
---|
| 26 | # $Id: img2stereo 2696 2017-12-14 17:12:51Z maronga $ |
---|
| 27 | # |
---|
[1047] | 28 | # 1046 2012-11-09 14:38:45Z maronga |
---|
| 29 | # code put under GPL (PALM 3.9) |
---|
| 30 | # |
---|
[982] | 31 | # 981 2012-08-09 14:57:44Z maronga |
---|
| 32 | # Initial version |
---|
| 33 | # |
---|
[981] | 34 | # Description: |
---|
| 35 | # ------------ |
---|
| 36 | # img2stereo converts single or series of images into anaglyphic stereo images |
---|
| 37 | #------------------------------------------------------------------------------! |
---|
| 38 | |
---|
| 39 | # |
---|
| 40 | #-- Define Variables |
---|
| 41 | input_left="." |
---|
| 42 | input_right="." |
---|
| 43 | output_dir="." |
---|
| 44 | grayscale=false |
---|
| 45 | mode="single" |
---|
| 46 | output_file="" |
---|
| 47 | file_ident="" |
---|
| 48 | |
---|
| 49 | # |
---|
| 50 | #-- Define strings |
---|
| 51 | typeset -L20 col1 |
---|
| 52 | typeset -L40 col2 |
---|
| 53 | typeset -L30 version="img2stereo Rev: $Rev$" |
---|
| 54 | typeset -R30 calltime |
---|
| 55 | typeset -L60 text1 |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | # |
---|
| 59 | #-- Get parameters |
---|
| 60 | while getopts :gl:n:o:r: option |
---|
| 61 | do |
---|
| 62 | case $option in |
---|
| 63 | (g) grayscale=true;; |
---|
| 64 | (l) input_left=$OPTARG;; |
---|
| 65 | (r) input_right=$OPTARG;; |
---|
| 66 | (n) file_ident=$OPTARG;; |
---|
| 67 | (\?) printf "\n +++ unknown option $OPTARG \n" |
---|
| 68 | printf "\n --> type \"$0 ?\" for available options \n" |
---|
| 69 | locat=parameter;exit;; |
---|
| 70 | esac |
---|
| 71 | done |
---|
| 72 | |
---|
| 73 | shift OPTIND-1 |
---|
| 74 | |
---|
| 75 | # |
---|
| 76 | #-- Print help |
---|
| 77 | if [[ "$1" = "?" ]] |
---|
| 78 | then |
---|
| 79 | (printf "\n *** img2stereo can be called as follows: \n" |
---|
| 80 | printf "\n Single mode: img2stereo #1 #2 \n" |
---|
| 81 | printf "\n with left eye image #1 and right eye image #2. \n" |
---|
| 82 | printf "\n Series mode: img2stereo -g -i.. -l.. -n.. -r.. \n" |
---|
| 83 | printf "\n Description of available options:\n" |
---|
| 84 | printf "\n Option Description Default-Value" |
---|
| 85 | printf "\n -g convert images to grayscale (recommended) false" |
---|
| 86 | printf "\n -l input directory (left images) ./" |
---|
| 87 | printf "\n -n file identifier (for series mode) " |
---|
| 88 | printf "\n -r input directory (right images) ./" |
---|
| 89 | printf "\n ? this outline \n\n") | more |
---|
| 90 | exit |
---|
| 91 | elif [[ "$1" == "" ]] |
---|
| 92 | then |
---|
| 93 | if [[ "$input_left" == "." || "$input_right" == "." ]] |
---|
| 94 | then |
---|
| 95 | printf "\n +++ Missing files to be converted \n" |
---|
| 96 | locat=parameter |
---|
| 97 | exit |
---|
| 98 | else |
---|
| 99 | mode="series" |
---|
| 100 | output_dir="${input_left%/*}/stereo" |
---|
| 101 | if [[ "$file_ident" == "" ]] |
---|
| 102 | then |
---|
| 103 | printf "\n +++ Parameter -n needed in series mode. \n" |
---|
| 104 | locat=parameter; exit |
---|
| 105 | fi |
---|
| 106 | fi |
---|
| 107 | elif [[ "$2" != "" ]] |
---|
| 108 | then |
---|
| 109 | mode="single" |
---|
| 110 | tmp=`readlink -f "$1"` |
---|
| 111 | output_dir="${tmp%/*}/stereo" |
---|
| 112 | else |
---|
| 113 | printf "\n +++ A file is missing \n" |
---|
| 114 | locat=parameter |
---|
| 115 | exit |
---|
| 116 | fi |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | # |
---|
| 122 | #-- Print header |
---|
| 123 | calltime=$(date) |
---|
| 124 | |
---|
| 125 | printf "\n#--------------------------------------------------------------#" |
---|
| 126 | printf "\n| $version$calltime |" |
---|
| 127 | |
---|
| 128 | col1="" |
---|
| 129 | col2="" |
---|
| 130 | printf "\n| $col1$col2 |" |
---|
| 131 | |
---|
| 132 | text1="Initialization parameters" |
---|
| 133 | printf "\n| $text1 |" |
---|
| 134 | |
---|
| 135 | if [[ "$mode" == "series" ]] |
---|
| 136 | then |
---|
| 137 | col1="Input dir (left):" |
---|
| 138 | col2=$input_left |
---|
| 139 | printf "\n| $col1$col2 |" |
---|
| 140 | row=$(echo "$input_left" | cut -c41-) |
---|
| 141 | while [[ "$row" != "" ]] |
---|
| 142 | do |
---|
| 143 | col1="" |
---|
| 144 | col2=$row |
---|
| 145 | printf "\n| $col1$col2 |" |
---|
| 146 | row=$(echo "$row" | cut -c41-) |
---|
| 147 | done |
---|
| 148 | |
---|
| 149 | col1="Input dir (right):" |
---|
| 150 | col2=$input_right |
---|
| 151 | printf "\n| $col1$col2 |" |
---|
| 152 | row=$(echo "$input_left" | cut -c41-) |
---|
| 153 | while [[ "$row" != "" ]] |
---|
| 154 | do |
---|
| 155 | col1="" |
---|
| 156 | col2=$row |
---|
| 157 | printf "\n| $col1$col2 |" |
---|
| 158 | row=$(echo "$row" | cut -c41-) |
---|
| 159 | done |
---|
| 160 | else |
---|
| 161 | col1="Left image:" |
---|
| 162 | col2=$1 |
---|
| 163 | printf "\n| $col1$col2 |" |
---|
| 164 | col1="Right image:" |
---|
| 165 | col2=$2 |
---|
| 166 | printf "\n| $col1$col2 |" |
---|
| 167 | fi |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | col1="Output directory:" |
---|
| 171 | col2=$output_dir |
---|
| 172 | printf "\n| $col1$col2 |" |
---|
| 173 | row=$(echo "$output_dir" | cut -c41-) |
---|
| 174 | while [[ "$row" != "" ]] |
---|
| 175 | do |
---|
| 176 | col1="" |
---|
| 177 | col2=$row |
---|
| 178 | printf "\n| $col1$col2 |" |
---|
| 179 | row=$(echo "$row" | cut -c41-) |
---|
| 180 | done |
---|
| 181 | |
---|
| 182 | col1="Mode:" |
---|
| 183 | col2=$mode |
---|
| 184 | printf "\n| $col1$col2 |" |
---|
| 185 | |
---|
| 186 | if [[ $grayscale == true ]] |
---|
| 187 | then |
---|
| 188 | text1="Colors will be converted to grayscale" |
---|
| 189 | else |
---|
| 190 | text1="Colors enabled" |
---|
| 191 | fi |
---|
| 192 | printf "\n| $text1 |" |
---|
| 193 | |
---|
| 194 | col1="" |
---|
| 195 | col2="" |
---|
| 196 | printf "\n| $col1$col2 |" |
---|
| 197 | |
---|
| 198 | |
---|
| 199 | if [[ "$mode" == "single" ]] |
---|
| 200 | then |
---|
| 201 | tmp=`basename ${1%}` |
---|
| 202 | filename=${tmp%.*} |
---|
| 203 | fileext=".${1##*.}" |
---|
| 204 | output_file="${filename}_stereo${fileext}" |
---|
| 205 | |
---|
| 206 | # |
---|
| 207 | #-- Check for cycle number |
---|
| 208 | looping=true |
---|
| 209 | output_file_tmp=$output_file |
---|
| 210 | while [[ $looping = true ]] |
---|
| 211 | do |
---|
| 212 | if [[ -f "$output_dir/$output_file_tmp" ]] |
---|
| 213 | then |
---|
| 214 | ((run_string +=1)) |
---|
| 215 | output_file_tmp="${output_file}_$run_string" |
---|
| 216 | else |
---|
| 217 | looping=false |
---|
| 218 | if (( run_string >= 1 )) |
---|
| 219 | then |
---|
| 220 | output_file="${output_file}_$run_string" |
---|
| 221 | fi |
---|
| 222 | break; |
---|
| 223 | fi |
---|
| 224 | done |
---|
| 225 | |
---|
| 226 | else |
---|
| 227 | output_file="$file_ident*..." |
---|
| 228 | fi |
---|
| 229 | |
---|
| 230 | text1="Image(s) will be saved under $output_file" |
---|
| 231 | printf "\n| $text1 |" |
---|
| 232 | |
---|
| 233 | printf "\n| |" |
---|
| 234 | printf "\n#--------------------------------------------------------------#\n" |
---|
| 235 | |
---|
| 236 | ############################################################################ |
---|
| 237 | |
---|
| 238 | |
---|
| 239 | #exit |
---|
| 240 | |
---|
| 241 | # |
---|
| 242 | #-- Create output directory if necessary |
---|
| 243 | if [[ ! -e $output_dir ]]; then |
---|
| 244 | mkdir $output_dir |
---|
| 245 | fi |
---|
| 246 | |
---|
| 247 | # |
---|
| 248 | #-- start converting images |
---|
| 249 | if [[ "$mode" == "single" ]] |
---|
| 250 | then |
---|
| 251 | |
---|
| 252 | # |
---|
| 253 | #-- SINGLE IMAGE MODE |
---|
| 254 | left_image=`basename ${1%}` |
---|
| 255 | right_image=`basename ${2%}` |
---|
| 256 | left_full=`readlink -f "$1"` |
---|
| 257 | right_full=`readlink -f "$2"` |
---|
| 258 | if [[ $grayscale == true ]] |
---|
| 259 | then |
---|
| 260 | opt1="-colorspace Gray" |
---|
| 261 | left_image_intern=$output_dir/"gray_$left_image" |
---|
| 262 | right_image_intern=$output_dir/"gray_$right_image" |
---|
| 263 | |
---|
| 264 | convert $opt1 "$left_full" "$left_image_intern" |
---|
| 265 | convert $opt1 "$right_full" "$right_image_intern" |
---|
| 266 | composite -stereo "0x0" "$right_image_intern" "$left_image_intern" "$output_dir/$output_file" |
---|
| 267 | rm "$right_image_intern" |
---|
| 268 | rm "$left_image_intern" |
---|
| 269 | else |
---|
| 270 | composite -stereo "0x0" "$right_full" "$left_full" "$output_dir/$output_file" |
---|
| 271 | fi |
---|
| 272 | printf "\n --> All actions finished. Stereo image saved as $output_dir/$output_file\n" |
---|
| 273 | exit |
---|
| 274 | |
---|
| 275 | else |
---|
| 276 | |
---|
| 277 | # |
---|
| 278 | #-- SERIES MODE |
---|
| 279 | |
---|
| 280 | printf "*** Generating stereo files...\n" |
---|
| 281 | |
---|
| 282 | # |
---|
| 283 | #-- get total number of output files |
---|
| 284 | nofl=`find $input_left/$ident* -type f -name $file_ident* -print -o -type d -prune |wc -l` |
---|
| 285 | nofr=`find $input_right/$ident* -type f -name $file_ident* -print -o -type d -prune |wc -l` |
---|
| 286 | |
---|
| 287 | if [[ $nofl != $nofr ]] |
---|
| 288 | then |
---|
| 289 | printf "+++ Different number of files for left and right eye!\n" |
---|
| 290 | locat=parameter |
---|
| 291 | exit |
---|
| 292 | fi |
---|
| 293 | typeset -Z${#nofl} counter |
---|
| 294 | # |
---|
| 295 | #-- get i-st left imgae |
---|
| 296 | for i in $input_left/$ident*; |
---|
| 297 | do |
---|
| 298 | (( img_counter_l+=1 )) |
---|
| 299 | # |
---|
| 300 | #-- get the i-st right file |
---|
| 301 | (( img_counter_r=0 )) |
---|
| 302 | for j in $input_right/$ident*; |
---|
| 303 | do |
---|
| 304 | (( img_counter_r+=1 )) |
---|
| 305 | if [[ $img_counter_l == $img_counter_r ]] then |
---|
| 306 | break; |
---|
| 307 | fi |
---|
| 308 | done |
---|
| 309 | # |
---|
| 310 | #-- combine the two images |
---|
| 311 | left_image=`basename ${i%}` |
---|
| 312 | right_image=`basename ${j%}` |
---|
| 313 | left_full=`readlink -f "$i"` |
---|
| 314 | right_full=`readlink -f "$j"` |
---|
| 315 | counter=$img_counter_r |
---|
| 316 | output_file="${file_ident}_stereo$counter" |
---|
| 317 | if [[ $grayscale == true ]] |
---|
| 318 | then |
---|
| 319 | opt1="-colorspace Gray" |
---|
| 320 | left_image_intern=$output_dir/"gray_$left_image" |
---|
| 321 | right_image_intern=$output_dir/"gray_$right_image" |
---|
| 322 | |
---|
| 323 | convert $opt1 "$left_full" "$left_image_intern" |
---|
| 324 | convert $opt1 "$right_full" "$right_image_intern" |
---|
| 325 | composite -stereo "0x0" "$right_image_intern" "$left_image_intern" "$output_dir/$output_file" |
---|
| 326 | printf " created $output_file\n" |
---|
| 327 | rm "$right_image_intern" |
---|
| 328 | rm "$left_image_intern" |
---|
| 329 | else |
---|
| 330 | composite -stereo "0x0" "$right_full" "$left_full" "$output_dir/$output_file" |
---|
| 331 | printf " created $output_file\n" |
---|
| 332 | fi |
---|
| 333 | done |
---|
| 334 | |
---|
| 335 | printf "\n --> All actions finished. Stereo images saved under $output_dir.\n" |
---|
| 336 | |
---|
| 337 | fi |
---|
| 338 | exit |
---|