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