#!/bin/sh # # mkanaglyph # Create a red-blue stereo anaglyph image from left & right images in PPM format. # Requires utilities distributed with netpbm. # # Copyright 2004 Ken Hardy # # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 2 of the License, or (at your option) any later # version. # # This program is distributed in the hope that it will be useful, but WITHOUT ANY # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A # PARTICULAR PURPOSE. See the GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along with # this program; if not, write to the Free Software Foundation, Inc., 675 Mass # Ave, Cambridge, MA 02139, USA. # # Never work with the only copy of your images files -- make a copy! # More error and sanity checking is needed in the code below. # set - `getopt r:l:o:x: $@` while [ $# -gt 0 ] ; do case "$1" in "-x") let shiftby="$2"/2 ; shift; shift; ;; "-r") rightjpg="$2" ; shift;shift; ;; "-l") leftjpg="$2" ; shift;shift; ;; "-o") output="$2" ; shift;shift; ;; "--") shift ; break 2 ; ;; default) echo "bad options." >&2 ; exit 1 ; ;; esac done [ "$output"x != x -a "$rightjpg"x != x -a "$leftjpg"x != x ] || { echo "Usage: $0 [-x offset] -l left.jpg -r right.jpg -o output.jpg" >&2 ; echo >&2 ; #........................................................................ echo " Offset, if required, is number of pixels to shift the images" >&2 ; echo " hoziontally for comfortable viewing." >&2 ; echo >&2 ; echo " Always work with a copy of your image files!" >&2 ; echo >&2 ; exit 1 ; } left=`basename \`basename $leftjpg .jpg\` .JPG` right=`basename \`basename $right .jpg\` .JPG` [ -f ${left}.ppm ] && { echo ${left}.ppm already exists >&2 ; exit 1 ; } [ -f ${right}.ppm ] && { echo ${left}.ppm already exists >&2 ; exit 1 ; } tmpppm=_tmpppm_$$ trap "/bin/rm -f ${left}.ppm ${left}.red ${left}.grn ${left}.blu ${right}.ppm ${right}.red ${right}.grn ${right}.blu ${tmpppm}; exit" 0 jpegtopnm ${leftjpg} > ${left}.ppm 2>/dev/null jpegtopnm ${rightjpg} > ${right}.ppm 2>/dev/null # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [ "$shiftby"x = x ] || { unset width height ; eval `pnmfile ${left}.ppm | sed -n '/[0-9] by [0-9]/s/.*\ \([1-9][0-9]*\) by \([1-9][0-9]*\) .*/width=\1 height=\2/p'` ; [ "$width" ] || exit 1 ; let width=$width-$shiftby ; pnmcut ${right}.ppm -top 0 -left 0 -width $width -height $height > $tmpppm /bin/mv $tmpppm ${right}.ppm pnmcut ${left}.ppm -top 0 -left $shiftby -width $width -height $height > $tmpppm /bin/mv $tmpppm ${left}.ppm } # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ppmtorgb3 ${left}.ppm && \ ppmtorgb3 ${right}.ppm && \ rgb3toppm ${left}.red ${right}.grn ${right}.blu | ppmtojpeg >$output res=$? rm -f ${left}.red ${left}.grn ${left}.blu rm -f ${right}.red ${right}.grn ${right}.blu rm -f ${tmpppm} exit $res