/* pnmarith.c - perform arithmetic on two portable anymaps
**
** slightly modified by Marcel Wijkstra <wijkstra@fwi.uva.nl>
**
**
** Copyright (C) 1989, 1991 by Jef Poskanzer.
**
** 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.
*/

#include "pam.h"
#include "shhopt.h"

enum function {FN_ADD, FN_SUBTRACT, FN_MULTIPLY, FN_DIFFERENCE,
               FN_MINIMUM, FN_MAXIMUM, FN_MEAN};

struct cmdlineInfo {
    /* All the information the user supplied in the command line,
       in a form easy for the program to use.
    */
    char *input1Filespec;  
    char *input2Filespec;  
    enum function function;
};



static void
parseCommandLine(int argc, char ** const argv,
                 struct cmdlineInfo * const cmdlineP) {
/*----------------------------------------------------------------------------
   Note that the file spec array we return is stored in the storage that
   was passed to us as the argv array.
-----------------------------------------------------------------------------*/
    optEntry *option_def = malloc(100*sizeof(optStruct));
        /* Instructions to OptParseOptions2 on how to parse our options.
         */
    optStruct3 opt;

    unsigned int option_def_index;
    
    unsigned int addSpec, subtractSpec, multiplySpec, differenceSpec,
        minimumSpec, maximumSpec, meanSpec;

    option_def_index = 0;   /* incremented by OPTENTRY */
    OPTENT3(0, "add",         OPT_FLAG,   NULL, &addSpec,        0);
    OPTENT3(0, "subtract",    OPT_FLAG,   NULL, &subtractSpec,   0);
    OPTENT3(0, "multiply",    OPT_FLAG,   NULL, &multiplySpec,   0);
    OPTENT3(0, "difference",  OPT_FLAG,   NULL, &differenceSpec, 0);
    OPTENT3(0, "minimum",     OPT_FLAG,   NULL, &minimumSpec,    0);
    OPTENT3(0, "maximum",     OPT_FLAG,   NULL, &maximumSpec,    0);
    OPTENT3(0, "mean",        OPT_FLAG,   NULL, &meanSpec,       0);

    opt.opt_table = option_def;
    opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
    opt.allowNegNum = TRUE;  /* We may have parms that are negative numbers */

    optParseOptions3(&argc, argv, opt, sizeof(opt), 0);
        /* Uses and sets argc, argv, and some of *cmdlineP and others. */

    if (addSpec + subtractSpec + multiplySpec + differenceSpec +
        minimumSpec + maximumSpec + meanSpec > 1)
        pm_error("You may specify only one function");

    if (argc-1 != 2)
        pm_error("You must specify two arguments:  the files which are "
                 "the operands of the "
                 "dyadic function.  You specified %d", argc-1);
    else {
        cmdlineP->input1Filespec = argv[1];
        cmdlineP->input2Filespec = argv[2];
    }

    if (addSpec)
        cmdlineP->function = FN_ADD;
    else if (subtractSpec)
        cmdlineP->function = FN_SUBTRACT;
    else if (multiplySpec)
        cmdlineP->function = FN_MULTIPLY;
    else if (differenceSpec)
        cmdlineP->function = FN_DIFFERENCE;
    else if (minimumSpec)
        cmdlineP->function = FN_MINIMUM;
    else if (maximumSpec)
        cmdlineP->function = FN_MAXIMUM;
    else if (meanSpec)
        cmdlineP->function = FN_MEAN;
    else
        pm_error("You must specify a function (e.g. '-add')");
}        



int
main(int argc, char *argv[]) {

    struct cmdlineInfo cmdline;
    struct pam inpam1;
    struct pam inpam2;
    struct pam outpam;
    FILE* if1P;
    FILE* if2P;
    tuple* tuplerow1;
    tuple* tuplerow2;
    tuple* tuplerowOut;
    unsigned int row;
    
    pnm_init( &argc, argv );

    parseCommandLine(argc, argv, &cmdline);

    if1P = pm_openr(cmdline.input1Filespec);
    if2P = pm_openr(cmdline.input2Filespec);

    pnm_readpaminit(if1P, &inpam1, sizeof(inpam1));
    pnm_readpaminit(if2P, &inpam2, sizeof(inpam2));

    if (inpam1.width != inpam2.width || inpam1.height != inpam1.height ||
        inpam1.depth != inpam2.depth)
        pm_error("the two images must be the same dimensions.  "
                 "The first is %dx%dx%d, but the second is %dx%dx%d",
                 inpam1.width, inpam1.height, inpam1.depth,
                 inpam2.width, inpam2.height, inpam2.depth);

    outpam = inpam1;
    outpam.file = stdout;
    outpam.maxval = MAX(inpam1.maxval, inpam2.maxval);

    tuplerow1 = pnm_allocpamrow(&inpam1);
    tuplerow2 = pnm_allocpamrow(&inpam2);
    tuplerowOut = pnm_allocpamrow(&outpam);

    pnm_writepaminit(&outpam);

    for (row = 0; row < outpam.height; ++row) {
        unsigned int col;
        pnm_readpamrow(&inpam1, tuplerow1);
        pnm_readpamrow(&inpam2, tuplerow2);
        
        if (outpam.maxval != inpam1.maxval)
            pnm_scaletuplerow(&inpam1, tuplerow1, tuplerow1, outpam.maxval);
        if (outpam.maxval != inpam2.maxval)
            pnm_scaletuplerow(&inpam2, tuplerow2, tuplerow2, outpam.maxval);

        for (col = 0; col < outpam.width; ++col) {
            unsigned int plane;
            for (plane = 0; plane < outpam.depth; ++plane) {
                double result;
                switch (cmdline.function)
                {
                case FN_ADD:
                    result = tuplerow1[col][plane] + tuplerow2[col][plane];
                    break;
                case FN_SUBTRACT:
                    result = tuplerow1[col][plane] - tuplerow2[col][plane];
                    break;
                case FN_MULTIPLY:
                    result = tuplerow1[col][plane] * tuplerow2[col][plane] /
                        outpam.maxval;
                    break;
                case FN_DIFFERENCE:
                    result =
                        tuplerow1[col][plane] > tuplerow2[col][plane] ?
                        tuplerow1[col][plane] - tuplerow2[col][plane] :
                            tuplerow2[col][plane] - tuplerow1[col][plane];
                    break;
                case FN_MINIMUM:
                    result = MIN(tuplerow1[col][plane], tuplerow2[col][plane]);
                    break;
                case FN_MAXIMUM:
                    result = MAX(tuplerow1[col][plane], tuplerow2[col][plane]);
                    break;
                case FN_MEAN:
                    result = 
                        (tuplerow1[col][plane] + tuplerow2[col][plane])/2.0;
                    break;
                }
                result = MAX(0, result);
                result = MIN(outpam.maxval, result);
                tuplerowOut[col][plane] = (unsigned int) (result + 0.5);
            }
        }
        pnm_writepamrow(&outpam, tuplerowOut);
    }

    pm_close(if1P);
    pm_close(if2P);
    
    exit(0);
}
