summaryrefslogtreecommitdiff
path: root/scripts/muggle-image-convert
blob: 8d29f93ced0b31f6cdf38fb00ccd3fa4af2559f9 (plain)
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
#!/bin/bash
#
# requires: *topnm, pnmpad, pnmscale, ppmntsc, ppmtoy4m, mpeg2enc,
#           mktemp, awk, file
#

# video format. pal or ntsc
FORMAT=pal

# target image width/height (taking into account visible screen area)
if [ "$FORMAT" = "ntsc" ]; then
  TW=704
  TH=480
else
  TW=704
  TH=576
fi

TMP=$(mktemp ${TMPDIR:-/tmp}/image_convert.pnm.XXXXXX) || exit 2
IMG=$1
MPG=$2

left=$3
top=$4
width=$5
height=$6
right=`expr $TW - $left - $width`
bottom=`expr $TH - $top - $height`


DIR=$(dirname "$MPG")
if [ ! -d "$DIR" ]; then
  mkdir -p "$DIR" || exit 2
fi

trap cleanup EXIT
cleanup()
{
  [ -z "$TMP" ] || rm -f "$TMP"
}

#
# get the file type and set the according converter to PNM
#
FILE_TYPE=$(file -i -L -b "$IMG" 2>/dev/null | cut -f2 -d/)
case "$FILE_TYPE" in
  jpg | jpeg)
  TO_PNM=jpegtopnm
  ;;
  tiff)
  TO_PNM=tifftopnm
  ;;
  bmp | x-bmp)
  TO_PNM=bmptoppm
  ;;
  png | x-png)
  TO_PNM=pngtopnm
  ;;
  Netpbm | pnm | x-portable-pixmap)
  TO_PNM=cat
  ;;
  gif)
  TO_PNM=giftopnm
  ;;
  *)
  echo "filetype '$FILE_TYPE' is not supported"
  exit 1
  ;;
esac

$TO_PNM "$IMG" >"$TMP" 2>/dev/null

if [ "$FORMAT" = "ntsc" ]; then
# untested
    pnmscale -width $width -height $height "$TMP" |
    pnmpad -black -left $left -top $top -right $right -bottom $bottom |
    ppmntsc |
    ppmtoy4m -v 0 -n 1 -r -S 420mpeg2 -F 30000:1001 |
    mpeg2enc -f 7 -T 90 -F 4 -nn -a 2 -v 0 -o "$MPG"
else
    pnmscale -width $width -height $height "$TMP" |
    pnmpad -black -left $left -top $top -right $right -bottom $bottom |
    ppmntsc --pal |
    ppmtoy4m -v 0 -n 1 -r -S 420mpeg2 -F 25:1 |
    mpeg2enc -f 7 -T 90 -F 3 -np -a 2 -v 0 -o "$MPG"
# variant:     mpeg2enc -f 3 -b 12500 -a 2 -q 1 -n p -I 0 -v 0 -o "$MPG"
fi