-
Notifications
You must be signed in to change notification settings - Fork 1
/
stackfuser
executable file
·4580 lines (4102 loc) · 171 KB
/
stackfuser
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /bin/bash
#
# stackfuser - generate sharp aligned images from stackshots
#
Version="0.2"
#### Messages, Logf
error() {
echo "stackfuser ERROR: $*" >&2
showmessage "$(shortpath "$*")"
showpercent error
}
failure() {
echo "stackfuser Failure: $*" >&2
showmessage "$(shortpath "$*")"
}
note() {
echo "stackfuser: $*" >&2
showmessage "$(shortpath "$*")"
}
notify() {
note "$*"
notify-send "stackfuser: $*" >/dev/null 2>&1 &
}
verbose() {
echo "stackfuser: $*" >&2
}
warning() {
echo "stackfuser WARNING: $*" >&2
showmessage "$(shortpath "$*")"
}
historyentry() {
local Historyfile
Historyfile="$(storeinfo dump history)"
[ "$Historyfile" ] && echo "$*" >> "$Historyfile"
}
showmessage() {
sendkaptain "wizardlog='$(cut -c1-100 <<< "${1:-}")'"
}
shortpath() {
LC_ALL=C sed "s!$Projecthome!.!g" <<< "${1:-}"
# echo "${1:-}"
}
#### misc
checkvar() {
local Arg
Arg="${1:-}"
[ "$(cut -c1 <<< "$Arg")" = "-" ] && shift || Arg=""
[ -z "${3:-}" ] && {
failure "${1:-}: Variable ${2:-} is empty."
return 1
}
case "$Arg" in
-d)
[ -d "${3:-}" ] || {
failure "${1:-}: ${2:-} is not a directory: ${3:-}"
return 1
}
;;
-e)
[ -e "${3:-}" ] || {
failure "${1:-}: ${2:-} does not exist: ${3:-}"
return 1
}
;;
esac
case "$Arg" in
-D)
check_fileglob "${3:-}/*" || {
failure "${1:-}: ${2:-}: directory is empty: ${3:-}"
return 1
}
;;
-E)
[ -s "${3:-}" ] || {
failure "${1:-}: ${2:-}: file is empty: ${3:-}"
return 1
}
;;
esac
return 0
}
freememory() {
local Freemem Line Zram
Freemem="$(LC_ALL=C free | grep "Mem:" | awk '{print $7}')"
while read Line; do
Zram="$(awk '{print ($3 - $4)}' <<< "$Line")"
Zram="$((Zram/1000))"
Freemem="$((Freemem + Zram))"
done < <(/sbin/swapon --bytes | grep zram)
Freemem="$((Freemem*100/95))"
echo $Freemem
}
milisleep() {
local Time
Time="$(awk "BEGIN { print ${1:-1000} / 1000 }")"
sleep $Time
}
printnum(){
# print number $1 with leading zeroes.
# $1 number
# $2 digits. Default: 3
printf %0${2:-3}d "${1:-0}"
}
storeinfo() {
# store and provide pieces of information
# replace entry if codeword is already present
# Store as codeword=string:
# $1 codeword=string
# Dump stored string:
# $1 dump
# #2 codeword
# Drop stored string:
# $1 drop
# #2 codeword
# Test for codeword: (return 1 if not found)
# $1 test
# $2 codeword
#
local i Return
[ -e "$Storeinfofile" ] || return 1
[ -e "$Storeinfofile.lock" ] && {
for i in $(seq 20); do
sleep 0.1
[ -e "$Storeinfofile.lock" ] || break
done
[ "$i" = "20" ] && verbose "storeinfo overwrite"
}
:> "$Storeinfofile.lock"
case "${1:-}" in
dump) grep "^${2:-}=" $Storeinfofile | sed "s/^${2:-}=//" ;; # dump entry
drop) sed -i "/^${2:-}=/d" $Storeinfofile ;; # drop entry
test) grep -q "^${2:-}=" $Storeinfofile ; Return=$? ;; # test for entry
*) # store entry
sed -i "/^$(echo "${1:-}" | cut -d= -f1)=/d" $Storeinfofile # drop possible old entry
echo "${1:-}" >> $Storeinfofile
;;
esac
rm "$Storeinfofile.lock"
return ${Return:-0}
}
storepid() {
# store pid and command name. Use in finish() to kill background processes
echo "$(ps -p ${1:-} -h -o pid -o comm)" >> $Storepidfile
}
unspecialstring() { # replace special chars of $1 with -
# Replace all characters except those described in "a-zA-Z0-9_" with a '-'.
# Replace newlines, too.
# Remove leading and trailing '-'
# Avoid double '--'
# Return empty string if only special chars are given.
printf %s "${1:-}" | LC_ALL=C tr -cs "a-zA-Z0-9._" "-" | sed -e 's/^-// ; s/-$//'
}
zeronumber() {
printf "%0${2:-4}d" "${1:-}"
}
#### file operations
cut_extension() {
local Extension
Extension="$(get_extension "${1:-}")"
sed "s%\.${Extension}\$%%" <<< "${1:-}"
}
check_dirglob() {
# check whether directories exist for quoted glob $1
[ -f "${1:-}" ] && return 1
[ -n "$(eval find ${1:-} -maxdepth 0 -type d -print -quit 2>/dev/null)" ]
}
check_fileglob() {
# check whether files exist for quoted glob $1
[ -n "$(eval find ${1:-} -maxdepth 0 -type f -print -quit 2>/dev/null)" ]
}
check_stackdir() {
# Check $1 if it is a valid directory containing a subfolder stackshot
local Stackdir Newstackdir Stackdirvalid Projectname Projecthome
Stackdir="$(storeinfo dump stackshotdir)"
Newstackdir="$(realpath "${1:-}")"
Newstackdir="${Newstackdir:-$Stackdir}"
Newstackdir="${Newstackdir%/}"
Projecthome="$(storeinfo dump projecthome)"
[ -z "$Newstackdir" ] && failure "No stackshot directory given." && return 1
[ ! -d "$Newstackdir" ] && failure "Not a directory: $Newstackdir" && return 1
[ -d "$Newstackdir/stackshot" ] && Stackdirvalid="yes" || Stackdirvalid="no"
[ "$Stackdirvalid" = "no" ] && [ "${Newstackdir%/stackshot}" != "$Newstackdir" ] && {
Newstackdir="${Newstackdir%/stackshot}"
Stackdirvalid="yes"
}
storeinfo "stackshotdir=$Newstackdir"
case "$Stackdirvalid" in
yes)
storeinfo "stackshotdir_valid=yes"
storeinfo "history=$Newstackdir/wizard.history"
grep -q "$Projecthome" <<< "$Newstackdir" && {
Projectname="${Newstackdir#$Projecthome}"
Projectname="$(dirname "$Projectname" | tr "/" ".")"
Projectname="${Projectname#.}"
[ "$Projectname" = "$(basename "$Newstackdir")" ] && Projectname=""
[ "$Projectname" = "." ] && Projectname=""
}
[ "$Projectname" ] && storeinfo "projectdir=$Projecthome/$Projectname" || storeinfo drop projectdir
[ -z "$Projecthome" ] && Projectname="np"
[ -z "$Projectname" ] && Projectname="np"
storeinfo "projectname=$Projectname"
return 0
;;
no)
storeinfo drop stackshotdir_valid
storeinfo drop history
storeinfo "projectname=np"
[ "$(storeinfo dump allow_dir_invalid)" = "yes" ] && return 0 || return 1
;;
esac
}
dirglob() {
# echos files only for glob $1
#local Path Glob
local Filelist Whitespace
#[ -d "${1:-}" ] && {
# Path="${1:-}"
# Glob="*"
#} || {
# Path="$(dirname "${1:-}")"
# Glob="$(basename "${1:-}")"
#}
#Path="$(realpath "$Path")"
[ "${1:-}" = "-w" ] && Whitespace="yes" && shift
Filelist="$(eval find ${1:-} -maxdepth 0 -type d 2>/dev/null)"
Filelist="$(sort -V <<< "$Filelist")"
[ "$Whitespace" ] && Filelist="$(echo $Filelist)"
[ "$Filelist" ] && echo "$Filelist" || return 1
}
exiftransfer() {
# Transfer exif data from image $1 to image $2
# Does not transfer image size information.
# Sets orientation tag to horizontal / no rotation.
# Several warnings are supressed.
local Sourceimage Destinationimage Exifargs
Sourceimage="${1:-}"
Destinationimage="${2:-}"
Exifargs="$(exiftool -a -u -g1 -args "$Sourceimage")"
Exifargs="$(LC_ALL=C grep -v -E -- \
'-ExifTool|-System:|-File:|ImageWidth|ImageHeight|ImageSize|Compression|Orientation|Resolution' <<< "$Exifargs" \
| sed "s/'/'\"'\"'/g ; s/=/='/ ; s/\$/'/" )"
eval exiftool -ignoreMinorErrors -overwrite_original_in_place $Exifargs -Orientation=Horizontal "$Destinationimage" 2>&1 | grep -v -E 'Warning|files updated'
}
fileglob() {
# echos files only for glob $1
#local Path Glob
local Filelist Whitespace
#[ -d "${1:-}" ] && {
# Path="${1:-}"
# Glob="*"
#} || {
# Path="$(dirname "${1:-}")"
# Glob="$(basename "${1:-}")"
#}
#Path="$(realpath "$Path")"
[ "${1:-}" = "-w" ] && Whitespace="yes" && shift
Filelist="$(eval find ${1:-} -maxdepth 0 -type f 2>/dev/null)"
Filelist="$(sort -V <<< "$Filelist")"
[ "$Whitespace" ] && Filelist="$(echo $Filelist)"
[ "$Filelist" ] && echo "$Filelist" || return 1
}
get_extension() {
local Filename Extension
Filename="$(head -n1 <<< "${1:-}")"
Extension="$(rev <<< "$Filename" | cut -d/ -f1 | cut -d. -s -f1 | rev)"
echo "$Extension"
}
mycp() {
# copy quoted glob $1 to directory $2
check_fileglob "${1:-}" || {
verbose "cp: Source is empty: ${1:-}"
return 1
}
[ -d "${2:-}" ] || {
failure "cp: Target is not a directory: ${2:-}"
return 1
}
#verbose "cp: copy ${1:-} to $(basename "${2:-}")"
cp $(fileglob "${1:-}") "${2:-}"
}
mymv() {
# move quoted glob $1 to directory $2
check_fileglob "${1:-}" || {
verbose "mv: Source is empty: ${1:-}"
return 1
}
[ -d "${2:-}" ] || {
failure "mv: Target is not a directory: ${2:-}"
return 1
}
#verbose "mv: moving ${1:-} to $(basename "${2:-}")"
mv -f $(fileglob "${1:-}") "${2:-}"
}
myrm() {
# remove files
# $@ quoted globs
local Line
for Line in "$@" ; do
check_fileglob "$Line" && {
verbose "rm: Deleting $Line"
rm $(fileglob "$Line")
}
done
}
myrmdir() {
# remove directories and their content
# $@ quoted globs
local Line
for Line in "$@" ; do
check_dirglob "$Line" && {
verbose "rmdir: Deleting $Line"
rm -rf $Line
}
done
}
rmrecursive() {
local Dir
Dir="${2:-INVALIDDIR}"
case "${1:-}" in
bak)
find "$Dir" -type d -name "*.bak" -exec rm -r {} +
;;
cache)
find "$Dir" -type d -name "cache" -exec rm -r {} +
find "$Dir" -type d -name "cache.imfuse" -exec rm -r {} +
;;
prepared)
find "$Dir" -type d -name "prepared" -exec rm -r {} +
find "$Dir" -type d -name "prepared.bak" -exec rm -r {} +
;;
esac
}
setup_targetdirnames() {
local Targetdir
Targetdir="${1:-}"
Targetdir="$(realpath "$Targetdir")"
Projectname="$(storeinfo dump projectname)"
[ -d "$Targetdir" ] || {
failure "No target directory found. Please specify a target directory or make a stackshot.
Wrong Target: $Targetdir"
return 1
}
[ -d "$Targetdir/stackshot" ] && Targetstackshotdir="$Targetdir/stackshot" || Targetstackshotdir="$Targetdir"
[ -d "$Targetdir/stackshot-median" ] && Targetstackshotdir="$Targetdir/stackshot-median"
Targetbasename="$Projectname.$(basename "$Targetdir")"
Targetprepareddir="$Targetdir/prepared"
Targetfuseddir="$Targetdir/fused"
Targetcomposeddir="$Targetdir/composed"
Targetevaluateddir="$Targetdir/evaluated"
Targetvideodir="$Targetdir/video"
Targetcachedir="$Targetdir/cache"
}
setup_workdir() {
# Detect possible source directories in $2 for function $1
# Set up variables that can be used to call function $1
# Usage:
# setup_workdir Function Targetdir
# Args:
# Function One of align, sharpen, fuse, median, min, max, mean, set
# Targetdir Target directory [+glob] to check.
# Can already be in an ardustack-structure or an arbitrary one.
# Location of $Destinationdir depends on detected directory structure.
# Opt2 Mode of Function
# Sets variables:
# Sourcedir Source directory within $Targetdir
# Sourcefileglob Glob to get files. Mostly just "*"
# Destinationdir Recommended directory to store resulting images.
# Often serves as source dir for following functions.
# Destinationfile Possible prefix of destination file(s). Can be "", though.
# Targetcachedir Cache folder
#
# Run in a loop to check for next possible source dir in Targetdir.
# Returns 0 on success and 1 if nothing more is to do.
#
# Stores already checked source dirs of $Targetdir in $Sourcedirchecklist.
# -> Set Sourcedirchecklist="" before running a loop with setup_workdir().
# Use local in calling function:
# local Sourcedir Sourcefileglob Destinationdir Destinationfile Targetptofile Targetcachedir Sourcedirchecklist
local Targetnewdir Targetprevdir
#local Targetstackshotdir
#local Targetprepareddir Targetfuseddir Targetevaluateddir Targetcomposeddir Targetcachedir
local Projectname
local Imageformat
Function="${1:-}"
Targetdir="${2:-}"
Sourcefileglob="${3:-*}"
Function="${Function%prepared}"
Destinationdir=""
Destinationfile=""
Projectname="$(storeinfo dump projectname)"
setup_targetdirnames "$Targetdir"
storeinfo test rmcache_before && myrmdir "$Targetcachedir"
mkdir -p "$Targetcachedir"
Cachedir="$Targetcachedir"
grep -q -- "fuse" <<< "$Function" && Targetnewdir="$Targetfuseddir"
grep -q -- "compose" <<< "$Function" && Targetnewdir="$Targetcomposeddir"
grep -q -- "evaluate" <<< "$Function" && Targetnewdir="$Targetevaluateddir"
grep -q -- "animation" <<< "$Function" && Targetnewdir="$Targetvideodir"
grep -q -- "video" <<< "$Function" && Targetnewdir="$Targetvideodir"
[ "$Targetnewdir" ] || Targetnewdir="$Targetprepareddir"
[ "$Targetnewdir" = "$Targetprepareddir" ] && {
mkdir -p $Targetnewdir
check_fileglob "$Targetprepareddir/$Sourcefileglob" || {
Imageformat="$(get_extension "$(fileglob "$Targetstackshotdir/$Sourcefileglob")")"
grep -q -i -x "$Imageformat" <<< "$Imageformatlist" && {
mycp "$Targetstackshotdir/$Sourcefileglob" "$Targetprepareddir" || return 1
:
} || {
imageformat -2 "tif" -s "$Targetstackshotdir" -g "$Sourcefileglob" -d "$Targetprepareddir" || return 1
}
}
}
Targetprevdir="$Targetnewdir.bak"
# backup for undo, serving also as source for batch operations
case $Function in
undo*) ;;
evaluate*)
mkdir -p $Targetprevdir
check_fileglob "$Targetnewdir/*" && mycp "$Targetnewdir/*" "$Targetprevdir/"
mkdir -p $Targetcomposeddir.bak
check_fileglob "$Targetcomposeddir/*" && mycp "$Targetcomposeddir/*" "$Targetcomposeddir.bak/"
;;
animation*|video*|fuse*)
mkdir -p $Targetprevdir
check_fileglob "$Targetnewdir/*" && mycp "$Targetnewdir/*" "$Targetprevdir/"
;;
*)
mkdir -p $Targetprevdir
myrm "$Targetprevdir/*"
check_fileglob "$Targetnewdir/*" && mymv "$Targetnewdir/*" "$Targetprevdir/"
;;
esac
case $Function in
fuse*)
Sourcedir="$Targetprepareddir"
Destinationdir="$Targetnewdir"
Destinationfile="$Projectname.$(basename "$Targetdir")"
;;
evaluate)
Sourcedir="$Targetprepareddir"
Destinationdir="$Targetnewdir"
Destinationfile="$Projectname.$(basename "$Targetdir").$(basename "$Sourcedir")"
mkdir -p $Targetcomposeddir
;;
evaluatefused)
Sourcedir="$Targetfuseddir"
Destinationdir="$Targetnewdir"
Destinationfile="$Projectname.$(basename "$Targetdir").$(basename "$Sourcedir")"
mkdir -p $Targetcomposeddir
Sourcefileglob="$(fuse_glob "$Opt1")"
Sourcefilelist="$(fuse_glob --list "$Opt1")"
check_filelist $Sourcefilelist || return 1
;;
compose)
Sourcedir="$Targetevaluateddir"
Destinationdir="$Targetnewdir"
#Destinationfile="$Projectname.$(basename "$Targetdir").$(basename "$Sourcedir")"
;;
align*|brightness*|contrast*|crop*|cuberotate*|imageformat*|fakehdr*|gamma*|level*|orient*|resize*|rotate*|ruler*|sharpen*|statistic*|evalstep*|stretch*|wizard*)
Sourcedir="$Targetprevdir"
Destinationdir="$Targetnewdir"
;;
whitebalance*)
Sourcedir="$Targetprevdir"
Destinationdir="$Targetnewdir"
;;
animation|video)
Sourcedir="$Targetprepareddir"
Destinationdir="$Targetnewdir"
Destinationfile="$Projectname.$(basename "$Targetdir")"
;;
undo*)
Sourcedir="$Targetprevdir"
Destinationdir="$Targetnewdir"
;;
*)
failure "setup_workdir(): Function not defined: $Function"
return 1
;;
esac
[ "$Sourcedir" = "$Targetprepareddir" ] && {
mkdir -p $Targetprepareddir
check_fileglob "$Targetprepareddir/$Sourcefileglob" || mycp "$Targetstackshotdir/$Sourcefileglob" "$Targetprepareddir"
}
check_fileglob "$Sourcedir/$Sourcefileglob" || {
failure "setup_workdir(): No source found: $Sourcedir/$Sourcefileglob"
return 1
}
[ "$Destinationdir" ] || {
failure "setup_workdir(): Destinationdir not specified"
return 1
}
mkdir -p "$Destinationdir"
return 0
}
#### show images
showimage() {
local Imagelist Image Shrinkimage
case $# in
0) ;;
1)
Imagelist="$(fileglob "${1:-}")" >&2
;;
*)
while [ "$#" -gt "0" ] ; do
Imagelist="$Imagelist
${1:-}"
shift
done
Imagelist="$(grep . <<< "$Imagelist")"
;;
esac
case $(grep -c . <<< "$Imagelist") in
0)
failure "showimage: Not found: ${1:-}"
Image="$(storeinfo dump notfoundimage)"
[ -e "$Image" ] || $Magickbin -size 300x200 xc:red -fill black -pointsize 20 -gravity center -draw "text 0,0 'image not found'" "$Image"
geeqie -t -r File:"$Image" 1>&2
storeinfo drop showimage
return 1
;;
1)
Image="$Imagelist"
case "$(get_extension "$Image")" in
gif)
case $(identify "$Image" | grep -c GIF) in
1)
geeqie -t -r File:"$Image" 1>&2
#imgview "$Image"
storeinfo "showimage=$Image"
;;
*) showvideo "$Image" ;;
esac
;;
mp4|webm)
showvideo "$Image"
;;
*)
geeqie -t -r File:"$Image" 1>&2
#imgview "$Image"
storeinfo "showimage=$Image"
;;
esac
;;
*)
viewnior $Imagelist 1>&2 &
;;
esac
return 0
}
showvideo() {
ffplay -window_title "$(basename "${1:-}")" -loglevel error -loop 0 "${1:-}" & storepid $!
}
#### batch functions
batch_function() {
# Apply image processing function $1 on all possible source dirs in target directory $2, options in $3
local $Batchlocal
local Return=0
local Gallerydir Image
local Startzeit
local Sourcelist Targetlist Line Count
parse_batchoptions "$@" || return 1
checkvar -d batch Targetdir "$Targetdir" || return 1
checkvar batch Function "$Function" || return 1
check_stackdir "$Targetdir" || {
failure "batch_function(): Invalid directory: $Targetdir"
return 1
}
ifcmdbreak && return 1
setup_workdir "$Function" "$Targetdir" "$Sourcefileglob" || {
failure "batch $Function: Source not found in $Targetdir
$@"
return 1
}
cd "$Targetdir"
case $Function in
compose|fuse|evaluate) ;;
*)
Function="${Function%prepared}"
Function="${Function%composed}"
Function="${Function%evaluated}"
Function="${Function%fused}"
;;
esac
Function="${Function%_}"
case "$Function" in
brightness|contrast|gamma|level|orient|prepare|resize|rotate|sharpen|statistic|stretch|wizard)
Opt2="$Opt1 $Opt2 $Opt3"
Opt3=""
Opt2="${Opt2# }"
Opt2="${Opt2# }"
Opt2="${Opt2% }"
Opt2="${Opt2% }"
Opt1="$Function"
Function="wizard"
;;
esac
### FIXME
checkvar -D "$Function" Sourcedir "$Sourcedir" || return 1
checkvar -d "$Function" Destinationdir "$Destinationdir" || return 1
case $Function in
undo|compose) ;;
*) checkvar $Function Sourcefileglob "$Sourcefileglob" || return 1 ;;
esac
case $Function in
wizard) checkvar $Function Opt1 "$Opt1" || return 1 ;;
esac
case $Function in
align*|crop|undo|whitebalance)
checkvar -d $Function Cachedir "$Cachedir" || return 1 ;;
esac
[ -z "$Opt1" ] && case "$Function" in
compose|evaluate) Opt1="all" ;;
esac
note "$Function $Opt1 $Opt2"
storeinfo "cmd=$@"
storeinfo "cmd_source=$Sourcedir"
storeinfo "cmd_destination=$Destinationdir"
storeinfo "cmd_cache=$Cachedir"
cd "$Cachedir"
verbose "batch_function(): $Function -1 '$Opt1' -2 '$Opt2' -3 '$Opt3' -4 '$Opt4' -5 '$Opt5' -6 '$Opt6' -7 '$Opt7' -8 '$Opt8' -9 '$Opt9' -s '$Sourcedir' -g '$Sourcefileglob' -d '$Destinationdir' -b '$Destinationfile' -c '$Cachedir'"
Startzeit="$(date +%s)"
$Function -1 "$Opt1" -2 "$Opt2" -3 "$Opt3" -4 "$Opt4" -5 "$Opt5" -6 "$Opt6" -7 "$Opt7" -8 "$Opt8" -9 "$Opt9" -s "$Sourcedir" -g "$Sourcefileglob" -d "$Destinationdir" -b "$Destinationfile" -c "$Cachedir"
Return=$?
cd "$Targetdir"
storeinfo drop cmd
storeinfo drop cmd_source
storeinfo drop cmd_destination
storeinfo drop cmd_cache
[ "$Return" = "0" ] || {
failure "ERROR in $Function $Opt1 $Opt2 $Opt3: $(basename "$Sourcedir") -> $(basename "$Destinationdir")"
case $Function in
animation|video) ;;
evaluate)
undo -s "$Destinationdir.bak" -d "$Destinationdir" -c "$Cachedir"
undo -s "$Targetcomposeddir.bak" -d "$Targetcomposeddir" -c "$Cachedir"
;;
compose|fuse*)
undo -s "$Destinationdir.bak" -d "$Destinationdir" -c "$Cachedir"
;;
*)
undo -s "$Sourcedir" -d "$Destinationdir" -c "$Cachedir"
;;
esac
}
storeinfo test rmcache_after && rm -R "$Cachedir"
# transfer exif data
false && [ "$Return" = "0" ] && {
note "Transferring exif meta data"
Sourcelist="$(fileglob "$Targetstackshotdir/*")"
Targetlist="$(fileglob "$Destinationdir/*")"
case "$Destinationdir" in
$Targetprepareddir)
Count=0
for Line in $Sourcelist; do
Count="$((Count+1))"
#exiftool -ignoreMinorErrors -overwrite_original -tagsfromfile "$(sed "${Count}q;d" <<< "$Sourcelist")" -Orientation= -all:all "$(sed "${Count}q;d" <<< "$Targetlist")" >/dev/null || failure "Failed to transfer exif data"
exiftransfer "$(sed "${Count}q;d" <<< "$Sourcelist")" "$(sed "${Count}q;d" <<< "$Targetlist")"
done
;;
$Targetfuseddir|$Targetevaluateddir|$Targetcomposeddir)
Image="$(head -n1 <<< "$Sourcelist")"
Count=0
for Line in $Targetlist; do
Count="$((Count+1))"
#exiftool -ignoreMinorErrors -overwrite_original -tagsfromfile "$Image" -Orientation= -all:all "$(sed "${Count}q;d" <<< "$Targetlist")" >/dev/null || failure "Failed to transfer exif data"
exiftransfer "$Image" "$(sed "${Count}q;d" <<< "$Targetlist")"
done
;;
esac
}
[ "$Return" = "0" ] && [ "$(basename "$Destinationdir")" = "evaluated" ] && {
storeinfo test stackshotdir_valid && {
# link prepared.max
ls $Destinationdir/*prepared.max* >/dev/null 2>&1 && {
ln -f $(fileglob "$Destinationdir/*.prepared.max.*") "$Targetdir"
}
# link fused.mean results
ls $Destinationdir/*fused.*.mean* >/dev/null 2>&1 && {
Gallerydir="$(storeinfo dump gallerydir)"
# link fused.median result into parent directory
while read Line; do
ln -f "$Line" "$Targetdir"
[ -d "$Gallerydir" ] && ln -f "$Line" "$Gallerydir"
[ "$(storeinfo dump projectname)" != "np" ] && ln -f "$Line" "$Targetdir/../"
done < <(fileglob "$Targetevaluateddir/*fused.*.mean.*")
}
}
}
note "Ready: $Function $Opt1 $Opt2 $Opt3 $Opt4; time: $(date -u -d @$(($(date +%s)-Startzeit)) +"%T")"
verbose "batch_function(): $Function: return $Return"
return $Return
}
parse_batchoptions() {
# To be called by batch functions to parse options.
# Include this local line in the calling function:
# local Targetdir Sourcedir Sourcefileglob Destinationdir Destinationfile Cachedir Function Opt1 Opt2
local Shortoptions Longoptions Parsedoptions
local Error
Shortoptions="b:c:d:f:g:m:o:s:t:1:2:3:4:5:6:7:8:9:"
Longoptions=""
Parsedoptions="$(getopt --options "$Shortoptions" --longoptions "$Longoptions" --name "$0" -- "$@" )"
eval set -- "$Parsedoptions"
while [ $# -gt 0 ] ; do
case "${1:-}" in
-t) Targetdir="${2:-}" ;;
-s) Sourcedir="${2:-}" ;;
-g) Sourcefileglob="${2:-}" ;;
-d) Destinationdir="${2:-}" ;;
-b) Destinationfile="${2:-}" ;;
-c) Cachedir="${2:-}" ;;
-f) Function="${2:-}" ;;
-1) Opt1="${2:-}" ;;
-2) Opt2="${2:-}" ;;
-3) Opt3="${2:-}" ;;
-4) Opt4="${2:-}" ;;
-5) Opt5="${2:-}" ;;
-6) Opt6="${2:-}" ;;
-7) Opt7="${2:-}" ;;
-8) Opt8="${2:-}" ;;
-9) Opt9="${2:-}" ;;
--) ;;
*)
failure "Unbekannte Option ${1:-} ($Function)"
Error=1
;;
esac
shift
shift
done
[ "$Sourcefileglob" ] || Sourcefileglob="*"
#[ ! -d "$Sourcedir" ] && failure "$Function: source dir not found: $Sourcedir" && return 1
#[ ! -d "$Destinationdir" ] && failure "$Function: destination dir not found: $Destinationdir"&& return 1
#! check_fileglob "$Sourcedir/$Sourcefileglob" && failure "$Function: No source files found: $Sourcedir/$Sourcefileglob" && return 1
return ${Error:-0}
}
getbatchtwo() {
local Command= Line Argument
for Line in rotate gamma level contrast sharpen brightness stretch statistic wizard; do
Argument="$(askkaptain "$Line")"
[ "$Argument" ] && case $Line in
brightness) Line="-brightness-contrast" ;;
contrast) Line="-sigmoidal-contrast" ;;
rotate) Line="-rotate" ; Argument="$(askkaptain rotateopt1combo)" ;;
sharpen) Line="-adaptive-sharpen" ;;
statistic) Line="-statistic" ; Argument="$(askkaptain statisticopt1combo) $(askkaptain statisticopt2combo)" ;;
stretch) Line="-contrast-stretch" ;;
wizard) Line="" ;;
*) Line="-$Line" ;;
esac
case $Argument in
"") ;;
auto) Command="$Command -auto$Line" ;;
*) Command="$Command $Line $Argument" ;;
esac
done
echo "$Command"
}
getfunctionsettings() {
local Function
Function="${1:-none}"
Opt1="$(askkaptain -c ${Function}opt1combo)"
Opt2="$(askkaptain -c ${Function}opt2combo)"
Opt3="$(askkaptain -c ${Function}opt3combo)"
Opt4="$(askkaptain -c ${Function}opt4combo)"
Opt5="$(askkaptain -c ${Function}opt5combo)"
Opt6="$(askkaptain -c ${Function}opt6combo)"
Opt7="$(askkaptain -c ${Function}opt7combo)"
Opt8="$(askkaptain -c ${Function}opt8combo)"
Opt9="$(askkaptain -c ${Function}opt9combo)"
}
runbatchone() {
local $Batchlocal
for Function in imageformat orient resize crop; do
askkaptain -c $Function > /dev/null && {
getfunctionsettings $Function
cmd -f "$Function" -1 "$Opt1" -2 "$Opt2" -3 "$Opt3" -4 "$Opt4" -5 "$Opt5" -6 "$Opt6" -7 "$Opt7" -8 "$Opt8" -9 "$Opt9" -t "$Stackdir"
}
done
return 0
}
runbatchtwo() {
local $Batchlocal
Stackdir="${1:-}"
cmd -f wizard -1 "$(getbatchtwo)" -t "$Stackdir"
}
runbatchthree() {
local $Batchlocal
for Function in align_vidstab align_hugin align_ais align_focusstack; do
askkaptain -c $Function > /dev/null && {
getfunctionsettings $Function
cmd -f "$Function" -1 "$Opt1" -2 "$Opt2" -3 "$Opt3" -4 "$Opt4" -5 "$Opt5" -6 "$Opt6" -7 "$Opt7" -8 "$Opt8" -9 "$Opt9" -t "$Stackdir"
}
done
return 0
}
runbatchfour() {
askkaptain evaluate1 >/dev/null && cmd -f evaluateprepared -t "$Stackdir"
askkaptain fuse >/dev/null && {
Opt1="$(askkaptain fuseopt1combo)"
Opt2="$(askkaptain fuseopt2combo)"
cmd -f fuse -t "$Stackdir" -1 "$Opt1" -2 "$Opt2"
[ "$Opt2" = "all" ] && cmd -f evaluatefused -t "$Stackdir" -1 "$Opt1"
}
askkaptain evaluate2 >/dev/null && cmd -f evaluatefused -t "$Stackdir" -1 "$(askkaptain evaluateopt1combo)"
askkaptain deletebackups2 >/dev/null && cmd -f deletebackups -t "$Stackdir"
askkaptain deleteprepared2 >/dev/null && cmd -f deleteprepared -t "$Stackdir"
#cmd -f showimage -s "$Stackdir/evaluated" -g "*fused.*.mean*"
}
setup_imagelist() {
# Set up often needed image variables
Imagelist="$(fileglob "$Sourcedir/$Sourcefileglob")"
Imagelist2="$(echo $Imagelist)"
Imagenumber="$(grep -c . <<< "$Imagelist")"
Image="$(head -n1 <<< "$Imagelist")"
[ -z "$Image" ] && {
failure "No images found"
return 1
}
Imagewidth=$($Magickbin -format '%w' "$Image" info:)
Imageheight=$($Magickbin -format '%h' "$Image" info:)
Imageformat="$(get_extension "$Image")"
Imagememsize="$((Imagewidth*Imageheight*Magickpixelmemory/1000))"
Imagememsize="$((Imagememsize*125/100))" # by observation. Alpha channel? Meta data?
Imagelistmemsize="$((Imagememsize*Imagenumber))"
LC_ALL=C file $Image | grep -q "raw image" && Imageraw="yes" || Imageraw="no"
Image=""
Imagecount=0
Cropgeometry=""
verbose "setup_imagelist(): $Sourcedir
Image number: $Imagenumber
Image width : $Imagewidth
Image height: $Imageheight
Image format: $Imageformat
Image mem size: $Imagememsize
List mem size: $Imagelistmemsize
Raw image: $Imageraw"
}
#### image sequence operations
align_focusstack() {
local $Batchlocal
local $Imagelocal
local Pid Logfile
local Part Full
parse_batchoptions "$@" || return 1
setup_imagelist || return 1
Logfile=$Cachedir/focus-stack.log
focus-stack --verbose --output="$Destinationdir/" --align-only $(fileglob "$Sourcedir/$Sourcefileglob") >$Logfile 2>&1 || storeinfo "cmdbreak=yes" & Pid=$!
# tail -F -n+1 --pid $Pid $Logfile &
while ps -p $Pid >/dev/null; do
sleep 0.1
ifcmdbreak && {
kill $Pid
break
}
Line="$(tail $Logfile | head -n9 | tac)"
grep -q "Starting task: Save" <<< "$Line" && {
Line="$(grep -m1 "Starting task: Save" <<< "$Line")"
echo "$Line"
Image="$(sed -n -e 's/^.*Save //p' <<< "$Line")"
showimage "$Image"
Line="$(cut -d "[" -f2 <<< "$Line")"
Line="$(cut -d "]" -f1 <<< "$Line")"
Part="$(cut -d "/" -f1 <<< "$Line")"
Full="$(cut -d "/" -f2 <<< "$Line")"
showpercent $Part $Full
}
done
ifcmdbreak && return 1 || return 0
}
align_ais() {
local $Batchlocal
local $Imagelocal
local Alignlist
local Aligndir Distortdir
local Cropminimage Rotate
local Stackcount Stacklist Firstimage Step Stepsize
local Startzeit Dauer Restzeit
local Error
parse_batchoptions "$@" || return 1
setup_imagelist || return 1
[ "$Opt1" ] && Opt1="-m"
Stepsize=10
note "align_ais: Running align_image_stack with $Stepsize images on each iteration.
"
Stackcount=0
Startzeit="$(date +%s)"
for Step in 0 $(seq $((Imagenumber/$Stepsize)) ); do
showpercent $((Step*Stepsize)) $Imagenumber
ifcmdbreak && break
[ "$Error" ] && break
Stackcount="$((Stackcount+1))"
Stacklist="$(tail -n+$((Stepsize*Step+1)) <<< "$Imagelist" | head)"
[ -z "$Stacklist" ] && break
Aligndir=$Cachedir/align.$Stackcount
mkdir $Aligndir
nice align_image_stack $Opt1 -v --use-given-order -p $Cachedir/ais.$Stackcount.pto -a $Aligndir/align $Firstimage $Stacklist || Error=1
Firstimage="$(fileglob "$Aligndir/align*" | tail -n1)"
showimage $Firstimage
Dauer="$(( $(date +%s)-Startzeit))"
Restzeit="$(( Dauer*(Imagenumber-((Step+1)*Stepsize)) / ((Step+1)*Stepsize) ))"
[ "$Restzeit" -lt "0" ] && Restzeit="$((Restzeit*-1))"
note "align_ais: Duration: $(date -u -d @$Dauer +"%T"); ETA: $(date -u -d @$Restzeit +"%T") ($Restzeit)"
done
ifcmdbreak && return 1
[ "$Error" ] && return 1
Distortdir=$Cachedir/result
mkdir $Distortdir
Count=0
for Aligndir in $(dirglob "$Cachedir/align*"); do
[ "$Count" = "0" ] || rm $Aligndir/align0000.tif
for Image in $(fileglob "$Aligndir/*"); do
ifcmdbreak && break
Count=$((Count+1))