File size: 2,797 Bytes
9c12830
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash
set -euo pipefail

fail() {
  printf 'android-sips-linux: %s\n' "$1" >&2
  exit "${2:-1}"
}

if [[ $# -ne 9 ||
  "$1" != "-s" || "$2" != "format" || "$3" != "jpeg" ||
  "$4" != "-s" || "$5" != "formatOptions" || "$6" != "best" ||
  "$8" != "--out" ]]; then
  fail "unsupported arguments" 2
fi

input_path="$7"
output_path="$9"
convert_bin="/usr/bin/convert"
identify_bin="/usr/bin/identify"

[[ "$input_path" == /* && "$output_path" == /* ]] ||
  fail "input and output paths must be absolute" 2
[[ "$input_path" != "$output_path" ]] || fail "input and output paths must differ" 2
[[ -f "$input_path" && ! -L "$input_path" ]] || fail "input is not a regular file"
[[ ! -e "$output_path" && ! -L "$output_path" ]] || fail "output already exists"
[[ -d "${output_path%/*}" ]] || fail "output directory does not exist"

[[ -x "$convert_bin" && -x "$identify_bin" ]] ||
  fail "fixed ImageMagick executables are unavailable" 2

if ! input_dimensions="$("$identify_bin" -ping -format '%w %h' "$input_path" 2>/dev/null)"; then
  fail "input is not a readable image"
fi
read -r input_width input_height extra <<<"$input_dimensions"
[[ "$input_width" =~ ^[1-9][0-9]*$ && "$input_height" =~ ^[1-9][0-9]*$ && -z "${extra:-}" ]] ||
  fail "input dimensions are invalid"

umask 077
temporary_dir="$(mktemp -d "${output_path}.tmp.XXXXXX")"
temporary_output="$temporary_dir/output.jpg"
cleanup() {
  rm -rf "$temporary_dir"
}
trap cleanup EXIT

if ! "$convert_bin" "$input_path" \
  -colorspace sRGB \
  -background white \
  -alpha remove \
  -alpha off \
  -type TrueColor \
  -quality 95 \
  "jpeg:$temporary_output" >/dev/null 2>&1; then
  fail "ImageMagick conversion failed"
fi

if ! output_description="$(
  "$identify_bin" +ping -format '%m|%w|%h|%[colorspace]|%[type]|%[channels]|%Q' \
    "$temporary_output" 2>/dev/null
)"; then
  fail "converted output is not a readable image"
fi
IFS='|' read -r output_format output_width output_height output_colorspace \
  output_type output_channels output_quality <<<"$output_description"
output_colorspace="$(printf '%s' "$output_colorspace" | tr '[:upper:]' '[:lower:]')"
output_channels="$(printf '%s' "$output_channels" | tr '[:upper:]' '[:lower:]')"

[[ "$output_format" == "JPEG" ]] || fail "converted output is not JPEG"
[[ "$output_width" == "$input_width" && "$output_height" == "$input_height" ]] ||
  fail "converted output dimensions changed"
[[ "$output_colorspace" == "srgb" ]] || fail "converted output is not sRGB"
[[ "$output_type" == "TrueColor" ]] || fail "converted output is not true color"
[[ "$output_channels" != *a* ]] || fail "converted output retained an alpha channel"
[[ "$output_quality" =~ ^[0-9]+$ && "$output_quality" -ge 90 ]] ||
  fail "converted output quality is too low"

mv "$temporary_output" "$output_path"