-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharw_compress.sh
executable file
·64 lines (52 loc) · 1.67 KB
/
arw_compress.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
#! /bin/bash
set -e
SRC="$1"
DST="$2"
if [ "x$SRC" == "x" ]
then
echo "Need a source file!"
exit 1
fi
if [ "x$DST" == "x" ]
then
DST="$SRC.flifraw"
fi
if [ -e "$DST" ]
then
echo "Destination $DST already exists, refusing to overwrite"
exit 1
fi
DIR=$(mktemp -d "$DST.XXXXXX")
cat <<EOL > "$DIR"/file.list
$DIR/alarms.bin
$DIR/begin
$DIR/orig.sha256
EOL
sha256sum - < "$SRC" > "$DIR"/orig.sha256
dcraw_hack "$SRC" > "$DIR"/temp.rgba 2> "$DIR"/err
HALF_SIZE=$(head -n1 "$DIR"/err | sed -e 's/^.*, half=\([0-9]*x[0-9]*\)$/\1/')
convert -depth 16 -size $HALF_SIZE rgba:"$DIR"/temp.rgba png:"$DIR"/temp.png
grep ALARM "$DIR"/err | cut -c 8- | xxd -r -ps > "$DIR"/alarms.bin
LAST_LINE=$(tail -n1 "$DIR"/err)
PADDING_BYTE=00
if (echo "$LAST_LINE" | grep -q PADDING "$DIR"/err)
then
PADDING_BYTE=$(echo "$LAST_LINE" | cut -c 10-)
echo $PADDING_BYTE | xxd -r -ps > "$DIR"/padding
echo "$DIR"/padding >> "$DIR"/file.list
fi
START=$(head -n1 "$DIR"/err | sed -e 's/^.*data_at=//' -e 's/, .*$//')
HALF_WIDTH=$(echo $HALF_SIZE | sed -e 's/x.*$//')
flif -e --keep-invisible-rgb "$DIR"/temp.png "$DIR"/result.flif
flif -d "$DIR"/result.flif "$DIR"/result.flif.png
head -c $START "$SRC" > "$DIR"/begin
if (cat "$DIR"/begin; convert "$DIR"/result.flif.png -depth 16 rgba:- | arw_encode $HALF_WIDTH $START "$DIR"/alarms.bin $PADDING_BYTE) | cmp - "$SRC"
then
cat "$DIR"/file.list | xargs tar --transform 's=^.*/==' -Jcf "$DIR"/extra.tar.xz
tar --transform 's=^.*/==' -cf "$DST" "$DIR"/extra.tar.xz "$DIR"/result.flif
cat "$DIR"/file.list | xargs rm "$DIR"/{extra.tar.xz,result.flif,err,result.flif.png,temp.png,temp.rgba,file.list}
rmdir "$DIR"
else
echo data mismatch, temp stuff left in $DIR
exit 1
fi