/* pnmcmp.c - compare two portable anymaps
**
** Copyright (C) 1994 by Ingo Wilken (Ingo.Wilken@informatik.uni-oldenburg.de)
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation.  This software is provided "as is" without express or
** implied warranty.
**
** 11/Dec/94: first version
*/
#include "pnm.h"

/*#define DEBUG*/
static char *formatname ARGS((int format));


int
main(argc, argv)
    int argc;
    char *argv[];
{
    FILE *ifp1, *ifp2;
    xel *pnmrow1, *pnmrow2;
    int format1, format2, newformat;
    xelval maxval1, maxval2, newmaxval;
    short promote1, promote2;
    int cols1, rows1, cols2, rows2, col, row;
    int argn;
    char *usage = "<pnmfile1> <pnmfile2>";

    pnm_init(&argc, argv);

    argn = 1;
    while( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' ) {
#if 0
        if( pm_keymatch(argv[argn], "-verbose", 2) )
            verbose++;
        else
        if( pm_keymatch(argv[argn], "-noverbose", 4) )
            verbose = 0;
        else
#endif
            pm_usage(usage);
        ++argn;
    }
    if( argc - argn != 2 )
        pm_usage(usage);
    ifp1 = pm_openr(argv[argn]);
    ifp2 = pm_openr(argv[argn+1]);

    pnm_pbmmaxval = 1;
    pnm_readpnminit(ifp1, &cols1, &rows1, &maxval1, &format1);
    pnm_readpnminit(ifp2, &cols2, &rows2, &maxval2, &format2);

    if( cols1 != cols2 || rows1 != rows2 ) {
        pm_message("images differ in size: %dx%d != %dx%d", cols1, rows1, cols2, rows2);
        exit(1);
    }

    pnmrow1 = pnm_allocrow(cols1);
    pnmrow2 = pnm_allocrow(cols2);

    newmaxval = max(maxval1, maxval2);
    switch( PNM_FORMAT_TYPE(format1) ) {
        case PBM_TYPE:
            newformat = format2;
            break;
        case PGM_TYPE:
            if( PNM_FORMAT_TYPE(format2) == PPM_TYPE )
                newformat = format2;
            else
                newformat = format1;
            break;
        case PPM_TYPE:
            newformat = format1;
            break;
        default:
            pm_error("can\'t happen");
    }

    promote1 = (newformat != format1) || (newmaxval != maxval1);
    promote2 = (newformat != format2) || (newmaxval != maxval2);

    if( promote1 )
        pm_message("promoting first image to %s, maxval %d", formatname(newformat), newmaxval);
    if( promote2 )
        pm_message("promoting second image to %s, maxval %d", formatname(newformat), newmaxval);

    for( row = 0; row < rows1; row++ ) {
        pnm_readpnmrow(ifp1, pnmrow1, cols1, maxval1, format1);
        if( promote1 )
            pnm_promoteformatrow(pnmrow1, cols1, maxval1, format1, newmaxval, newformat);
        pnm_readpnmrow(ifp2, pnmrow2, cols2, maxval2, format2);
        if( promote2 )
            pnm_promoteformatrow(pnmrow2, cols2, maxval2, format2, newmaxval, newformat);
        for( col = 0; col < cols1; col++ ) {
            if( PNM_FORMAT_TYPE(newformat) == PPM_TYPE ) {
                if( !PPM_EQUAL(pnmrow1[col], pnmrow2[col]) ) {
                    pm_message("images differ at column %d row %d", col, row);
                    exit(1);
                }
            }
            else {
                if( PNM_GET1(pnmrow1[col]) != PNM_GET1(pnmrow2[col]) ) {
                    pm_message("images differ at column %d row %d", col, row);
                    exit(1);
                }
            }
        }
    }

    pm_message("images are equal");
    exit(0);
}


static char *
formatname(format)
    int format;
{
    switch( PNM_FORMAT_TYPE(format) ) {
        case PBM_TYPE:
            return "PBM";
        case PGM_TYPE:
            return "PGM";
        case PPM_TYPE:
            return "PPM";
        default:
            pm_error("unknown format type");
    }
}

