-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvtrimscan
executable file
·531 lines (480 loc) · 17.5 KB
/
vtrimscan
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#!/bin/bash
#
# Licensing:
#
# Copyright © Laurence Lumi 2017
# Licensed GNU General Public License v3
# which can be found here https://www.gnu.org/licenses/gpl-3.0.en.html
#
progname=`basename $0`
usage()
{
echo >&2 ""
echo >&2 "$progname:" "$@"
cat << MESSAGE1
USAGE: $progname gap | vstrip | ends [-d] [-dd] [-m] [-flip] [-r rotate] [-z] [-e extend] [-fb fbfuzz] [-f fuzz] [-n number] [ -t ] [ -type 35mm|6x6 ] infile [outfile_prefix]
USAGE: $progname [-h ] [ -help ]
vstrip split vstrip the script will seperate verticals strims of film
ends trim ends the script will trim the ends of the film
gap remove gap the script will remove the film frame gaps
OPTIONS:
-d debug provided usefull debuging and leaves intermediate files behind
-dd debug more extensive debugging, outputs the black strip/frames as well
-m mirror mirrors left to right the scan before it is processed, usefull if the negative was scanned emulsion down
-flip flip mirrors the scan vertically before it is processed, usefull if the negative was scanned emulsion down
-r rotate rotates the scan before it is processed, positive integer <= 360
-e extend extend crop on each side in pixels; integer; default=0
-fb filmbase threshold value for removing any white areas from the scan default is 5% which means the film base should be dimmer than 95%;
-f fuzz fuzz value used for a threshold to find area outside the frame, either the black film holder or the gaps between frames
expressed as (integer) percent 0 to 100; default=3 for gap and 10 for vstrip
-n frame number the starting frame number to use in file name; defaults to #1
-z zip zip compress the output (the default does not compress the output)
-t temporary output temporary mpc files instead of tif (used by scantool not a normal user option)
MESSAGE1
exit 1
}
help()
{
echo >&2 ""
echo >&2 "$progname:" "$@"
cat >&2 << MESSAGE2
NAME: $progname
PURPOSE: to trim the film holder and separate frames of a trimmed film strip
Arguments:
TODO
MESSAGE2
exit 1
}
function DEBUG()
{
[ ! "$debug" -eq 0 ] && $@
}
# quicksorts positional arguments
# return is in box qsort_ret
# Note: iterative, NOT recursive! :)
# First argument is a function name that takes two arguments and compares them
qsort() {
(($#<=1)) && return 0
local compare_fun=$1
shift
local stack=( 0 $(($#-1)) ) beg end i pivot smaller larger
qsort_ret=("$@")
while ((${#stack[@]})); do
beg=${stack[0]}
end=${stack[1]}
stack=( "${stack[@]:2}" )
smaller=() larger=()
pivot=${qsort_ret[beg]}
for ((i=beg+1;i<=end;++i)); do
if "$compare_fun" "${qsort_ret[i]}" "$pivot"; then
smaller+=( "${qsort_ret[i]}" )
else
larger+=( "${qsort_ret[i]}" )
fi
done
qsort_ret=( "${qsort_ret[@]:0:beg}" "${smaller[@]}" "$pivot" "${larger[@]}" "${qsort_ret[@]:end+1}" )
if ((${#smaller[@]}>=2)); then stack+=( "$beg" "$((beg+${#smaller[@]}-1))" ); fi
if ((${#larger[@]}>=2)); then stack+=( "$((end-${#larger[@]}+1))" "$end" ); fi
done
}
function cleanup {
DEBUG set -x
#rm -f "$tmpfileprefix".mpc "$tmpfileprefix".cache
#dont explicitly delete the mpc and cache files when in debug mode, allows the debug cache to be resused in subsequent test runs
if [ ! -z "$tmpfileprefix" ] && [ "$debug" -eq 0 ]; then
rm -f "$tmpfileprefix"*
fi
exit 0
}
trap cleanup 0 1 2 3 6
tmpdir="./"
debug=0
mpc=0
tmpprefix=$tmpdir"_vt_"
filmbase=5 #can reduce this value if increase the radius of the blur
framenumber=1
extend=0
zip="+compress"
if [ "$1" = "vstrip" ]; then
mode="vstrip"
fuzz=8 #this is actually 4 as the values are scalled by 2
suffix="strip"
shift
elif [ "$1" = "ends" ]; then
mode="ends"
fuzz=3 #this is actually 1.5 as the values are scalled by 2
suffix="trimmed"
shift
elif [ "$1" = "gap" ]; then
mode="gap"
fuzz=7 #note this is scalled
#fuzz=5 #note could bring this down to this as this seems to work pretty reliably need to work out
#logic to sprint frames
suffix="frame"
shift
else
echo "must specify either vstrip, ends or gap"
help
fi
while [ $# -gt 0 ]
do
# get parameters
case "$1" in
-h|-help) # help information
echo ""
help
;;
-d)
debug=1
r_options+=" "$1
;;
-dd)
debug=2
r_options+=" "$1
;;
-t)
mpc=1
;;
-m)
mirror="-flop"
;;
-flip)
flip="-flip"
;;
-r) # rotate
shift
rotate="$1"
if [[ ! "$rotate" =~ ^[0-9]+$ ]] || [ "$rotate" -lt 0 -o "$rotate" -gt 360 ]; then
echo "--- rotate=$rotate must be a positive integer value between 0 and 360 ---"
usage
fi
rotate="-rotate $rotate +repage"
;;
-e) # extend
r_options+=" "$1
shift
extend="$1"
r_options+=" "$1
if [[ ! "$extend" =~ ^[0-9]+$ ]]; then
echo "--- extend=$extend must be a positive integer value ---"
usage
fi
;;
-fb) # filmbase
r_options+=" "$1
shift
filmbase="$1"
r_options+=" "$1
if [[ ! "$filmbase" =~ ^[0-9]+$ ]] || [ "$filmbase" -lt 0 -o "$filmbase" -gt 100 ]; then
echo "--- filmbase=$filmbase must be a positive integer value between 0 and 100 ---"
usage
fi
;;
-f) # fuzz
r_options+=" "$1
shift
fuzz="$1"
r_options+=" "$1
if [[ ! "$fuzz" =~ ^[0-9]+$ ]] || [ "$fuzz" -lt 0 -o "$fuzz" -gt 100 ]; then
echo "--- fuzz=$fuzz must be a positive integer value between 0 and 100 ---"
usage
fi
;;
-n) # number
shift
framenumber="$1"
if [[ ! "$framenumber" =~ ^[0-9]+$ ]]; then
echo "--- framenumber=$framenumber must be a integer value ---"
usage
fi
;;
-type) # type
shift
type="$1"
if [ "$1" != "35mm" ] && [ "$1" != "6x6" ]; then
echo "type must be 35mm or 6x6 currently"
usage
fi
;;
-z)
zip="-compress zip"
r_options+=" "$1
;;
-) # STDIN and end of arguments
break
;;
-*) # any other - argument
echo "--- UNKNOWN OPTION ---" $1
usage
;;
*) # end of arguments
break
;;
esac
shift # next option
done
infile=$1
outprefix=$2
if [ -z $infile ]
then
echo "must provide a filename"
usage
fi
if [ ! -f "$infile" -o ! -r "$infile" ]; then
echo "Cannot open: $infile"
exit 1
fi
if [ -z $outprefix ]; then
outprefix=${infile}
fi
outprefix=${outprefix%".tif"}
outprefix=${outprefix%".mpc"}_
if [ "$debug" -eq 0 ]; then
tmpfileprefix="$tmpprefix"`date +%N`_`basename "$outprefix"` #in debug don't use the clock
else
tmpfileprefix="$tmpprefix"debug_`basename "$outprefix"` #in debug don't use the clock
fi
DEBUG echo tmpfileprefix: $tmpfileprefix
DEBUG echo outprefix: $outprefix
#first orientate the image if necessary
DEBUG set -x;
if [ ! -z "$mirror" ] || [ ! -z "$flip" ] || [ ! -z "$rotate" ]; then
echo "re orientating scan before processing"
#reorientatedfile="$tmpfileprefix"reoreintated.tif
#convert $infile $mirror $rotate $flip +compress "$reorientatedfile"
convert $infile $mirror $rotate $flip +compress "$tmpfileprefix".mpc
infile="$tmpfileprefix".mpc
elif [[ ! "$infile" == *.mpc ]]; then
DEBUG echo "converting input to mpc file"
convert $infile "$tmpfileprefix".mpc
infile="$tmpfileprefix".mpc
fi
Y=`identify -format "%h" $infile `
shave=0 #shave is not used for vstrip but initialise it anyway
tmpfile0="$tmpfileprefix"0work.tif
if [ "$mode" = "gap" ]; then
let shave=$Y/25
#remove any sprocket holes or missing bits of film use a default of 5% this mean the film base must be darker than 5%
convert $infile -shave 0x$shave -fuzz "$filmbase"% -fill black -opaque white +compress +repage "$tmpfile0"
elif [ "$mode" = "ends" ]; then
let shave=$Y/12 #better tuned for when the film holder is slightly offset
convert $infile -shave 0x$shave -fuzz "$filmbase"% -fill black -opaque white +compress "$tmpfile0"
else
tmpfile0="$infile"
fi
convert "$tmpfile0" -scale x1! +compress "$tmpfileprefix"1work.tif
if [ $mode = "gap" ]; then
#note radius of 25 means at least one gap needs be at least 50pix wide
stripcolor=(`convert -shave "$shave"x0 "$tmpfileprefix"1work.tif -strip -blur 25x65355 -format \
"%[fx: maxima.r * 100 ] %[fx: maxima.g * 100 ] %[fx: maxima.b * 100 ] " info:`)
exposure_increase=(`convert -size 1x1 xc: -format \
"%[fx: ${stripcolor[0]}^-1*100 ] %[fx: ${stripcolor[0]}^-1 * 100 ] %[fx: ${stripcolor[0]}^-1 * 100 ] " info:`)
convert "$tmpfileprefix"1work.tif\
-channel R -evaluate multiply "${exposure_increase[0]}"\
-channel G -evaluate multiply "${exposure_increase[1]}"\
-channel B -evaluate multiply "${exposure_increase[2]}"\
"$tmpfileprefix"2work.tif
stripcolor=(`convert -shave "$shave"x0 "$tmpfileprefix"2work.tif -strip -blur 25x65355 -format \
"%[fx: maxima.r * 100 ] %[fx: maxima.g * 100 ] %[fx: maxima.b * 100 ] " info:`)
opaque="rgb("${stripcolor[0]}"%,"${stripcolor[1]}"%,"${stripcolor[2]}"%)"
else
convert "$tmpfileprefix"1work.tif -evaluate multiply 2 \
"$tmpfileprefix"2work.tif
opaque="black"
fi
convert "$tmpfileprefix"2work.tif -blur 10x65355 -fuzz "$fuzz"% -fill black -opaque "$opaque" +fuzz -fill white +opaque black +compress "$tmpfileprefix"3work.tif
let pixel=$Y/10
DEBUG echo pixel $pixel
#morphology to remove small white strips
convert "$tmpfileprefix"3work.tif -morphology open "Rectangle:"$pixel"x1" +compress "$tmpfileprefix"4work.tif
#the following line should remove false film gaps that are X pixel wide but after testing it did not work see git a5a92bcf6438587c6d23a903c5d4a7514f09fc1f
#convert "$tmpfileprefix"3work.tif -morphology close "Rectangle:"$pixel"x1" +compress "$tmpfileprefix"4work.tif
arr=()
OLD_IFS=$IFS
IFS=$'\n'
arr=(`convert "$tmpfileprefix"4work.tif -type bilevel \
-define connected-components:verbose=true \
-define connected-components:mean-color=true \
-connected-components 4 \
null: | sed 's/^[ ]*//' | tail -n +2`)
set +x
DEBUG echo box size is: ${#arr[*]}
compare() { [[ ${1%:*} -lt ${2%%:*} ]]; }
qsort compare ${arr[*]}
arr=("${qsort_ret[@]}")
#declare -p qsort_ret
DEBUG declare -p arr
IFS=$OLD_IFS
num=${#arr[*]}
boxes=()
for ((i=0;i<num;i++)); do
color=`echo "${arr[$i]}" | cut -d\ -f5`
bbox=`echo "${arr[$i]}" | cut -d\ -f2`
if [ "$color" = "gray(0)" ] || [ "$color" = "srgb(0,0,0)" ] || [ "$color" = "gray(0%)" ]; then
color="B"
else
color="W"
fi
boxes[i]="$color":`echo $bbox | sed 's/[+x]/:/g'`
done
DEBUG declare -p boxes
if [ $mode = "gap" ]; then
if [ -z $type ]; then
#work out the ratio of strip
X=`identify -format "%w" $infile `
stripratio=`convert -size 1x1 xc: -format "%[fx: $X"/"$Y ]" info:`
DEBUG echo stripratio $stripratio
if (( $(echo "$stripratio > 5" | bc -l) )); then #scope for tuning these parameters
type="35mm"
DEBUG echo guessing 35mm
else
type="6x6"
DEBUG echo guessing 6x6 in 3x3 frames
fi
fi
if [ $type == "35mm" ]; then #scope for tuning these parameters
gapratio=0.1
frameratio=1.35
elif [ $type == "6x6" ]; then
gapratio=0.1
frameratio=0.8
else
exit #this should not happen
fi
for ((i=0;i<num;i++)); do
ibox=(${boxes[i]//:/ })
icolor="${ibox[0]}"
iX="${ibox[1]}"
ix="${ibox[3]}"
ratio=`convert -size 1x1 xc: -format "%[fx: $iX"/"$Y]" info:`
#maybe need flags for 35mm and 120 ratios
if ( [ $icolor = "B" ] && (( $(echo "$ratio > $gapratio" | bc -l) )) ) || ( [ $icolor = "W" ] && (( $(echo "$ratio < $frameratio" | bc -l) )) ) ; then
DEBUG echo found a suspect ratio: $ratio box: ${boxes[i]}
for ((j=$i+1;j<num;j++)); do
jbox=(${boxes[j]//:/ })
jcolor="${jbox[0]}"
jX="${jbox[1]}"
jx="${jbox[3]}"
ratio=`convert -size 1x1 xc: -format "%[fx: $jX"/"$Y]" info:`
if (( $(echo "$ratio < $frameratio" | bc -l) )) ; then
DEBUG echo found the following suspect ratio: $ratio box: ${boxes[j]}
#merge box and nextbox
DEBUG echo merging
DEBUG echo before box i: ${boxes[i]}
DEBUG echo before box j: ${boxes[j]}
boxes[i]="B:0:0:0:0"
DEBUG echo after box i: ${boxes[i]}
iX=$((iX+$jX))
boxes[j]="W:""$iX":1:"$ix":0
i=$j
DEBUG echo after box j: ${boxes[j]}
#if we are now on black box and total ratio is greater frameratio break out
ratio=`convert -size 1x1 xc: -format "%[fx: $iX"/"$Y]" info:`
if ( [ $jcolor = "B" ] && (( $(echo "$ratio > $frameratio" | bc -l) )) ) ; then
DEBUG echo the last merged frame was black and total merged ratio is now $ratio so ending this merge iteration
break;
fi
else
DEBUG echo following non-suspect ratio: $ratio box: ${boxes[j]}
break;
fi
done
else
DEBUG echo non-suspect ratio: $ratio box: ${boxes[i]}
fi
done
DEBUG declare -p boxes
fi
if [ $mode = "ends" ] && [ $num -gt 3 ]; then
DEBUG set -x;
for ((i=0;i<num;i++)); do
ibox=(${boxes[i]//:/ })
icolor="${ibox[0]}"
iX="${ibox[1]}"
ix="${ibox[3]}"
if [ $icolor = "B" ] && [ $i = 0 ]; then
continue;
fi
for ((j=$i+1;j<num;j++)); do
jbox=(${boxes[j]//:/ })
jcolor="${jbox[0]}"
jX="${jbox[1]}"
jx="${jbox[3]}"
if [ $jcolor = "B" ] && [ $((num - 1)) = $j ]; then
break 2;
fi
#merge box and nextbox
DEBUG echo merging
DEBUG echo before box i: ${boxes[i]}
DEBUG echo before box j: ${boxes[j]}
boxes[i]="B:0:0:0:0"
DEBUG echo after box i: ${boxes[i]}
iX=$((iX+$jX))
boxes[j]="W:""$iX":1:"$ix":0
i=$j
DEBUG echo after box j: ${boxes[j]}
done
done
DEBUG declare -p boxes
set +x;
fi
DEBUG echo cropping frames
for ((i=0;i<num;i++)); do
box=(${boxes[i]//:/ })
color="${box[0]}"
X="${box[1]}"
#you no longer have the correct height
# Y="${box[2]}"
x="${box[3]}"
#y="${box[4]}"
y=0
if [ "$debug" -lt 2 ] && [ "$color" = "B" ]; then
DEBUG echo skipping $i XYxy $X $Y $x $y
continue;
fi
#check to see a large frame and time to have another go
checkratio=`convert -size 1x1 xc: -format "%[fx: $X"/"$Y]" info:`
doubleratio=`convert -size 1x1 xc: -format "%[fx: $frameratio * 2 + $gapratio ]" info:`
DEBUG echo checkratio $checkratio
if [ "$mode" = "gap" ] && [ $num -gt 1 ] && (( $(echo "$checkratio > $doubleratio" | bc -l) )) ; then #check for $num > 1 is stop infinite recursion
DEBUG echo found one $checkratio
DEBUG set -x;
outfile="$outprefix""sub"-`printf "%03d" "$framenumber"`.mpc
convert $infile +repage -crop "$X"x"$Y"+"$x"+"$y" +repage $zip $outfile
vtrimscan gap -type $type $r_options $outfile
wait #important to wait here
else
if [ $extend -ne 0 ]; then
X=$((X-($extend*2)))
x=$((x+$extend))
fi
if [ "$mpc" -gt 0 ] ; then
outfile="$outprefix""$suffix"-`printf "%03d" "$framenumber"`.mpc
else
outfile="$outprefix""$suffix"-`printf "%03d" "$framenumber"`.tif
fi
echo CROPPING $outfile #CROPPING is keyword used to filter output
DEBUG set -x;
convert $infile +repage -crop "$X"x"$Y"+"$x"+"$y" +repage $zip $outfile &
fi
framenumber=$((framenumber+1))
set +x
done
if [ ! -z "$tmpfileprefix" ] && [ "$debug" -gt 0 ]; then
#touch "$tmpfileprefix"0work.tif
DEBUG set -x
if [ "$mode" = "gap" ] || [ "$mode" = "ends" ]; then #restore the correct size works better with rawtherepee
convert "$tmpfileprefix"0work.tif -bordercolor Tomato -border 0x"$shave" -compress zip "$tmpfileprefix"0work.tif &
fi
mapfile files < <(ls "$tmpfileprefix"1work.tif "$tmpfileprefix"2work.tif "$tmpfileprefix"3work.tif "$tmpfileprefix"4work.tif)
#do the conversion in both X an Y easier to visualise
set +x;
for ((i=0;i<${#files[*]};i++)); do
DEBUG set -x
convert ${files[i]} -scale x"$Y"! -compress zip ${files[i]} &
set +x;
done
fi
DEBUG echo "waiting to complete"
DEBUG set -x
wait