-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_masks.sh
executable file
·196 lines (170 loc) · 5.74 KB
/
add_masks.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env bash
#
# Given a grid of images as source for the panorama, this scripts adds
# exclusion masks to the bottom or to the right of every image, except
# for those at the lowest row or rightmost column respectively.
#
###############################################################################
# CONFIG
###############################################################################
if [[ -s "pano.conf" ]]; then
source "pano.conf" # Local overrides
fi
pushd ${BASH_SOURCE%/*} > /dev/null
if [[ -s "pano.conf" ]]; then
source "pano.conf" # General overrides
fi
: ${PTO:="$1"}
: ${GRID:="$2"}
: ${DIRECTION:="$3"}
: ${MASK1:="$4"}
: ${MASK2:="$5"}
: ${REMOVE_EXISTING_MASKS:="false"}
popd > /dev/null
function usage() {
cat <<EOF
Usage: ./add_masks.sh <pto_file> <grid> <direction> <mask> [mask]
Sample 1: ./add_masks.sh large_overlap.pto 8x3 td r200 > less_overlap.pto
Sample 2: ./add_masks.sh another.pto 12x5 lr r200 b200 > lesser_overlap.pto
grid: The layout of the panorama as WidthxHeight where width and height
are measured in images. A panorama of 6 images can be 1x6, 2x3,
3x2 or 6x1.
direction: How the grid of images was taken: td (top-down) first or
lr (left-right) first.
mask: rXXXX or bXXXX, where r=right, b=bottom and XXXX is the amount of
pixels.
If no grid, direction or mask is given, the amount of images in the panorams is
printed.
Given a grid of images as source for the panorama, this scripts adds
exclusion masks to the bottom or to the right of every image, except
for those at the lowest row or rightmost column respectively.
EOF
exit $1
}
check_parameters() {
if [[ "-h" == "$PTO" ]]; then
usage
fi
if [[ -z "$PTO" ]]; then
>&2 echo "Error: No pto_file specified"
usage 2
fi
if [[ ! -s "$PTO" ]]; then
>&2 echo "Error: Unable to locate $PTO"
usage 3
fi
}
trim_check_parameters() {
if [[ -z "$GRID" ]]; then
>&2 echo "Error: No grid specified"
usage 4
fi
if [[ -z "$DIRECTION" ]]; then
>&2 echo "Error: No direction specified"
usage 4
elif [[ "td" != "$DIRECTION" && "lt" != "$DIRECTION" ]]; then
>&2 echo "Error: The direction must be either 'td' or 'lr' but was '$DIRECTION'"
usage 5
fi
if [[ -z "$MASK1" ]]; then
>&2 echo "Error: No mask specified"
usage 7
fi
}
################################################################################
# FUNCTIONS
################################################################################
calc_stats() {
IMAGE_COUNT=$(grep "^i " "$PTO" | wc -l)
}
image_count() {
calc_stats
echo "Panorama $PTO"
echo "Images: $IMAGE_COUNT"
}
resolve_grid() {
calc_stats
GRID_WIDTH=$(cut -dx -f1 <<< "$GRID")
GRID_HEIGHT=$(cut -dx -f2 <<< "$GRID")
if [[ -z "$GRID_WIDTH" ]]; then
GRID_WIDTH=$(( IMAGE_COUNT / GRID_HEIGHT ))
fi
if [[ -z "$GRID_HEIGHT" ]]; then
GRID_HEIGHT=$(( IMAGE_COUNT / GRID_WIDTH ))
fi
local GRID_IMAGES=$(( GRID_WIDTH * GRID_HEIGHT ))
if [[ "$GRID_IMAGES" -ne "$IMAGE_COUNT" ]]; then
>&2 echo "Error: The defined grid ${GRID_WIDTH}x${GRID_HEIGHT} requires ${GRID_IMAGES} images, while the panorama contains $IMAGE_COUNT"
usage 11
fi
}
resolve_masks() {
if [[ -z "$MASK1" ]]; then
>&2 echo "Error: No masks defined"
usage 10
fi
MASK_RIGHT=0
MASK_BOTTOM=0
for MASK in $MASK1 $MASK2; do
if [[ "." == ".$(grep "\(r\|b\)[0-9]*" <<< "$MASK")" ]]; then
>&2 echo "Error: The mask command '$MASK' does not parse"
usage 12
fi
if [[ "$MASK" == r* ]]; then
MASK_RIGHT=$(grep -o "[0-9]*" <<< "$MASK")
else
MASK_BOTTOM=$(grep -o "[0-9]*" <<< "$MASK")
fi
done
}
trim() {
resolve_grid
resolve_masks
#echo "Adding mask_right=$MASK_RIGHT and mask_bottom=$MASK_BOTTOM to the ${GRID_WIDTH}x${GRID_HEIGHT} grid panorama $PTO"
if [[ "true" == "$REMOVE_EXISTING_MASKS" ]]; then
cat "$PTO" | grep -v "^#masks" | grep -v "^k"
else
cat "$PTO"
fi
echo "#masks"
local COL=1
local ROW=1
local IMAGE_ID=0
while read -r IMAGE; do
local IMAGE_WIDTH=$(cut -d\ -f2 <<< "$IMAGE" | tr -d w)
local IMAGE_HEIGHT=$(cut -d\ -f3 <<< "$IMAGE" | tr -d h)
if [[ "$MASK_RIGHT" -gt 0 && "$COL" -ne "$GRID_WIDTH" ]]; then
#echo "At ${COL}x${ROW} make right mask $MASK_RIGHT"
echo "k i${IMAGE_ID} t0 p\"$((IMAGE_WIDTH-MASK_RIGHT)) 0 $IMAGE_WIDTH 0 $IMAGE_WIDTH $IMAGE_HEIGHT $((IMAGE_WIDTH-MASK_RIGHT)) $IMAGE_HEIGHT\""
fi
if [[ "$MASK_BOTTOM" -gt 0 && "$ROW" -ne "$GRID_HEIGHT" ]]; then
#echo "At ${COL}x${ROW} make bottom mask $MASK_BOTTOM"
echo "k i${IMAGE_ID} t0 p\"0 $((IMAGE_HEIGHT-MASK_BOTTOM)) $IMAGE_WIDTH $((IMAGE_HEIGHT-MASK_BOTTOM)) $IMAGE_WIDTH $IMAGE_HEIGHT 0 $IMAGE_HEIGHT\""
fi
# cut is not on edge
if [[ "td" == "$DIRECTION" ]]; then
ROW=$(( ROW + 1 ))
if [[ "$ROW" -gt "$GRID_HEIGHT" ]]; then
ROW=1
COL=$(( COL + 1 ))
fi
else # lr
COL=$(( COL + 1 ))
if [[ "$COL" -gt "$GRID_WIDTH" ]]; then
COL=1
ROW=$(( ROW + 1 ))
fi
fi
local IMAGE_ID=$(( IMAGE_ID + 1 ))
done <<< $(grep "^i" < "$PTO")
}
###############################################################################
# CODE
###############################################################################
check_parameters "$@"
if [[ -z "$GRID" ]]; then
image_count
else
trim_check_parameters "$@"
trim
fi