-
Notifications
You must be signed in to change notification settings - Fork 1
/
mastertoaccess-kdu.sh
executable file
·142 lines (107 loc) · 3.53 KB
/
mastertoaccess-kdu.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
#!/bin/bash
# Convert directory with JP2 lossless master images to lossy access JP2s
# Requires:
# - Kakadu
# - ExifTool
# -sed
# - jprofile
if [ "$#" -ne 2 ] ; then
echo "Usage: mastertoaccess-kdu.sh dirIn dirOut" >&2
exit 1
fi
# Input and output directories
dirIn="$1"
dirOut="$2"
if ! [ -d "$dirIn" ] ; then
echo "input directory does not exist" >&2
exit 1
fi
if ! [ -d "$dirOut" ] ; then
mkdir "$dirOut"
fi
dirAccess="$dirOut"/access
if ! [ -d "$dirAccess" ] ; then
mkdir "$dirAccess"
fi
# Location of Kakadu binaries
kduPath=/Applications/kakadu
# Add Kakadu path to LD_LIBRARY_PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$kduPath
# Log file (used too store kakadu stdout, stderr)
logFile=$dirOut/mastertoaccess-kdu.log
# Kakadu status file (used to store Kakadu exit status)
kduStatusFile=$dirOut/kduStatus.csv
# Remove log and status files if they exist already (writing done in append mode!)
if [ -f $logFile ] ; then
rm $logFile
fi
if [ -f $kduStatusFile ] ; then
rm $kduStatusFile
fi
# Codestream comment string for access images
cCommentAccess="KB_ACCESS_LOSSY_01/01/2015"
# Iterate over all files in dirIn and convert JP2s
# to TIFF, then convert those TIFFs to lossy JP2
# according to KB access specs.
while IFS= read -d $'\0' file ; do
# File basename, extension removed
bName=$(basename "$file" | cut -f 1 -d '.')
# Output names
outNameTIF=$bName.tif
outNameJP2=$bName.jp2
# Input path
inPath=$(dirname "$file")
# Full paths to output files
tifOut="$dirAccess/$outNameTIF"
jp2Out="$dirAccess/$outNameJP2"
# First convert master JP2 to TIFF
cmdDecompress="$kduPath/kdu_expand -i "$file"
-o "$tifOut""
$cmdDecompress >>$logFile 2>&1
kduDecompressStatus=$?
echo $tifOut,$kduDecompressStatus >> $kduStatusFile
# Get SamplesPerPixel value from output TIFF
samplesPerPixel=$(exiftool -s -s -s -SamplesPerPixel "$tifOut")
# Determine bitrate values, depending on samplesPerPixel value
# Since bitrate = (BPP/CompressionRatio)
if [ $samplesPerPixel -eq 3 ] ; then
bitratesAccess="1.2,0.6,0.3,0.15,0.075,0.0375,0.01875,0.009375"
fi
if [ $samplesPerPixel -eq 1 ] ; then
bitratesAccess="0.4,0.2,0.1,0.05,0.025,0.0125,0.00625,0.003125"
fi
# Name for temporary XMP sidecar file
xmpName=$bName.xmp
# Extract metadata from TIFF with Exiftool and write to XMP sidecar
exiftool "$file" -o "$xmpName" >>$logFile 2>&1
# Insert string "xml "at start of sidecar file so Kakadu knows to use XML box
sed -i "1s/^/xml /" "$xmpName"
# Convert TIFF to lossy access JP2 according to KB specs
# Note: XMP metadata are written to UUID box, whereas KB
# specs prescribe XML box. Don't think this is a problem
# for access images.
cmdCompress="$kduPath/kdu_compress -i "$tifOut"
-o "$jp2Out"
Creversible=no
Clevels=5
Corder=RPCL
Stiles={1024,1024}
Cblk={64,64}
Cprecincts={256,256},{256,256},{128,128}
Clayers=8
-rate $bitratesAccess
Cuse_sop=yes
Cuse_eph=yes
Cmodes=SEGMARK
-jp2_box "$xmpName"
-com "$cCommentAccess""
$cmdCompress >>$logFile 2>&1
kduCompressStatus=$?
echo $jp2Out,$kduCompressStatus >> $kduStatusFile
# Remove TIFF file
rm $tifOut
# Remove XMP sidecar file
rm $xmpName
done < <(find $dirIn -type f -regex '.*\.\(jpf\|JPF\)' -print0)
# Run jprofile
jprofile -p kb_300Colour_2014.xml $dirAccess jprofile-kdu