source: palm/trunk/SCRIPTS/img2stereo @ 1046

Last change on this file since 1046 was 1046, checked in by maronga, 12 years ago

put scripts and utilities under GPL

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