#!/usr/bin/env python
# -*- Mode: python -*-

#    Copyright (C) 2001-2006 Artifex Software Inc.
# 
# This file is part of GNU ghostscript
#
# GNU ghostscript is free software; you can redistribute it and/or
# modify it under the terms of the version 2 of the GNU General Public
# License as published by the Free Software Foundation.
#
# This software is provided AS-IS with no warranty, either express or
# implied. That is, 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA, 02110-1301.


# $Id: make_two_versions,v 1.7 2007/08/01 14:26:58 jemarch Exp $

#
# make_two_versions <filename> [<device> [<res> [<banding>]]]
#
# this script creates two versions of the file specified
# one from baseline, and one from head
# these are appropriate for a visual diff

import gstestgs
import gsconf
import rasterdb
import os, sys
import string

if len(sys.argv) < 2:
    print "\n    usage: make_two_versions filename [ device:ppmraw [ res:72 [ banding:0 ] ] ]\n"
    sys.exit()

file = os.path.basename(sys.argv[1])
if len(sys.argv) > 2:
    device = sys.argv[2]
else:
    device = 'ppmraw'
if len(sys.argv) > 3:
    res = int(sys.argv[3])
else:
    res = 72
if len(sys.argv) > 4:
    banding = int(sys.argv[4])
else:
    banding = 0

dbfile = "%s.%s.%d.%d" % (file, device, res, 0)
ofile = "%s.%s.baseline.pnm" % (file, device)

# check if raster for this baseline is in the database, and if so, use
# it instead of generating approximate raster via baselinegs
if rasterdb.exists(dbfile):
    rasterdb.get_file(dbfile, output=ofile)
else:
    gs = gstestgs.Ghostscript()
    gs.command = gsconf.baselinegs
    gs.infile = gsconf.comparefiledir + file
    gs.outfile = ofile
    gs.device = device
    gs.dpi = res
    gs.band = banding
    gs.log_stdout = gsconf.log_stdout
    gs.log_stderr = gsconf.log_stderr

    if not gs.process():
        print "Error occurred running baseline Ghostscript."
        sys.exit(1)

ofile = "%s.%s.compare.pnm" % (file, device)

gs = gstestgs.Ghostscript()
gs.command = gsconf.comparegs
gs.infile = gsconf.comparefiledir + file
gs.outfile = ofile
gs.device = device
gs.dpi = res
gs.band = banding
gs.log_stdout = gsconf.log_stdout
gs.log_stderr = gsconf.log_stderr

if not gs.process():
    print "Error occurred running compare Ghostscript."
    sys.exit(1)
