source: palm/trunk/SCRIPTS/img2stereo @ 2716

Last change on this file since 2716 was 2716, checked in by kanani, 6 years ago

Correction of "Former revisions" section

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