Skip to content

Instantly share code, notes, and snippets.

@tai
Created April 4, 2019 13:17
Show Gist options
  • Select an option

  • Save tai/6f1a498e2bf1b490817b10eef41ab3f9 to your computer and use it in GitHub Desktop.

Select an option

Save tai/6f1a498e2bf1b490817b10eef41ab3f9 to your computer and use it in GitHub Desktop.
#!/bin/sh
usage() {
local p=$(basename $0)
cat <<EOF >&2
$p - Convert between BMP and RGB565/RGB8888(RGB32)
Usage: $p input.bmp output.rgb32 [WxH]
Example:
$ $p foo.bmp foo.rgb32
$ $p foo.bmp foo.rgb565
$ $p foo.rgb565 foo.bmp 1280x720
$ $p foo.rgb32 foo.bmp 1280x720
NOTE:
- Suffix MUST be in either bmp/rgb32/rgb8888/rgb565.
EOF
exit 0
}
rgb32_to_bmp() {
local ifile=$1 ofile=$2 geom=$3
# rgb8888 -> bmp
ffmpeg \
-f rawvideo -vcodec rawvideo -pix_fmt rgb32 -s $geom -i $ifile \
-f image2 -vcodec bmp $ofile
}
rgb565_to_bmp() {
local ifile=$1 ofile=$2 geom=$3
# rgb565 -> bmp
ffmpeg \
-f rawvideo -vcodec rawvideo -pix_fmt rgb565 -s $geom -i $ifile \
-f image2 -vcodec bmp $ofile
}
bmp_to_rgb32() {
local ifile=$1 ofile=$2
# bmp -> rgb8888
ffmpeg \
-vcodec bmp -i $ifile \
-vcodec rawvideo -f rawvideo -pix_fmt rgb32 $ofile
}
bmp_to_rgb565() {
local ifile=$1 ofile=$2
# bmp -> rgb565
ffmpeg \
-vcodec bmp -i $ifile \
-vcodec rawvideo -f rawvideo -pix_fmt rgb565 $ofile
}
run() {
local ifile=$1 ofile=$2 geom=$3
local itype=${ifile##*.}
local otype=${ofile##*.}
case "$itype" in
bmp)
case "$otype" in
rgb32|rgb8888) bmp_to_rgb32 $ifile $ofile;;
rgb565) bmp_to_rgb565 $ifile $ofile;;
*) usage;;
esac
;;
rgb32|rgb8888) rgb32_to_bmp $ifile $ofile $geom;;
rgb565) rgb565_to_bmp $ifile $ofile $geom;;
*) usage;;
esac
}
test $# -gt 0 || usage
run "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment