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