#!/usr/local/bin/python
# -*- Mode: Python; tab-width: 4 -*-

import tempfile
import sys
import os

if len(sys.argv) < 2:
    print 'usage: %s <left_image> <right_image> <width>' % sys.argv[0]
else:
    rot = ''
    size = '-xsize'
    if '-ccw' in sys.argv:
        rot = '| pnmflip -ccw '
        size = '-ysize'
        sys.argv.remove ('-ccw')
    if '-cw' in sys.argv:
        rot = '| pnmflip -cw '
        size = '-ysize'
        sys.argv.remove ('-cw')
    ifnl = sys.argv[1]
    ifnr = sys.argv[2]
    width = int(sys.argv[3])
    ofnl = tempfile.mktemp()
    ofnr = tempfile.mktemp()
    os.system ('djpeg %s | pnmscale %s %d %s > %s' % (ifnl, size, width/2, rot, ofnl))
    os.system ('djpeg %s | pnmscale %s %d %s > %s' % (ifnr, size, width/2, rot, ofnr))
    # this is cross-eyed; reverse inputs for wide-eyed.
    os.system ('pnmcat -lr %s %s | cjpeg' % (ofnr, ofnl))
    os.unlink (ofnl)
    os.unlink (ofnr)
