]> git.plutz.net Git - stereofy/blob - stereofy.sh
support for vertical and horizontal justification
[stereofy] / stereofy.sh
1 #!/bin/sh
2 # Copyright 2011 Paul Hänsch
3 # This file is part of Stereofy
4 #
5 # Stereofy is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Stereofy is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Stereofy.  If not, see <http://www.gnu.org/licenses/>.
17
18 algo="d"   #algorythm, Dubois by default
19 model="rc" #color model, red-cyan by default
20 param=""   #leave blank, will be -$algo$model
21 left=''    #left image filename
22 right=''   #right image file name
23 stereo=''  #output image file name
24 vert=0     #vertical justification
25 hor=0      #horizontal justification
26 lvoff=0    #left image vertical crop offset
27 rvoff=0    #right image vertical crop offset
28 lhoff=0    #left image horizontal crop offset
29 rhoff=0    #right image horizontal crop offset
30
31 while [ -n "$1" ]; do
32   case "$1" in
33     -d|--dubois) algo="d";;
34     -f|--full|--full-color) algo="f";;
35     -h|--half|--half-color) algo="d";;
36     -rc|--rc|--red-cyan) model="rc";;
37     -mc|--mc|--magenta-cyan) model="mc";;
38     -gm|--gm|--green-magenta) model="gm";;
39     -ab|--ab|--amber-blue|--yellow-blue) model="ab";;
40     -[dfh]rc|-[dfh]gm|-[fe]mc|-dab) param="$1";;
41     --vjust|-ver|--ver|--vertical) shift 1; vert="$1";;
42     --hjust|-hor|--hor|--horizontal) shift 1; hor="$1";;
43     -*) echo "no such color model" >/dev/stderr; exit 1;;
44     *)  if [ -z "$left" ]; then
45           left="$1"
46         elif [ -z "$right" ]; then
47           right="$1"
48         elif [ -z "$stereo" ]; then
49           stereo="$1"
50         fi
51         ;;
52   esac
53   shift 1;
54 done
55 if (echo $vert |egrep -qx '[+-]*[0-9]*[02468]'); then true
56 else
57   echo 'vertical alignment must be an even number'
58   exit 1
59 fi
60 if (echo $hor  |egrep -qx '[+-]*[0-9]*[02468]'); then true
61 else
62   echo 'horizontal alignment must be an even number'
63   exit 1
64 fi
65
66 [ -z "$param" ] && param="-$algo$model"
67 [ -z "$stereo" ] && stereo="${left}_stereo.jpg"
68
69 echo -n calculating geometry...
70 geom=$(identify "$left" |sed -rn 's:^.* ([0-9]+x[0-9]+) .*$:\1:gp')
71 width=$(echo $geom |cut -dx -f1)
72 height=$(echo $geom |cut -dx -f2)
73
74 if [ "$vert" -gt 0 ]; then
75   height=$(($height - $vert / 2))
76   lvoff=$(($vert / 3))
77 elif [ "$vert" -lt 0 ]; then
78   height=$(($height - $vert / -2))
79   rvoff=$(($vert / -2))
80 fi
81 if [ "$hor" -gt 0 ]; then
82   width=$(($width - $hor / 2))
83   lhoff=$(($hor / 2))
84 elif [ "$hor" -lt 0 ]; then
85   width=$(($width - $hor / -2))
86   rhoff=$(($hor / -2))
87 fi
88 geom="${width}x${height}"
89 lcrop="${geom}+${lhoff}+${lvoff}"
90 rcrop="${geom}+${rhoff}+${rvoff}"
91
92 echo -n setup streaming...
93 tmp=$(tempfile -d /tmp -p strfy)
94 rm "$tmp"
95 mkfifo "$tmp"
96
97 convert "$left" -crop "$lcrop" rgb:- >"$tmp" &
98 echo -n anaglyphing...
99 convert "$right" -crop "$rcrop" rgb:- |anaglyph "$param" "$tmp" /dev/stdin /dev/stdout |convert -depth 8 -size "$geom" rgb:- "$stereo"
100
101 echo cleaning...
102 rm "$tmp"