/*--------------------------- Commande MegaWave -----------------------------*/
/* mwcommand
   name = {diffpn};
   author = {"Trung Nguyen"};
   version = {"1.0"};
   function = {"Subtract image A with image B to obtain a Positive and a Negative output image"};
   usage = {   
     A->A              "Input Fimage",
     B->B              "Output Fimage",
     O1<-O1            "Positive output image",
     O2<-O2            "Negative output image"
   };
*/

#include "mw.h"

void diffpn(A, B, O1, O2)
     Fimage A, B, O1, O2;
{
  int i;

  if (A->ncol != B->ncol || A->nrow != B->nrow) 
    mwerror(FATAL, 1, "Image A and Image B have not the same size!\n");
  
  if  ((O1 = mw_change_fimage(O1 ,A->nrow, A->ncol)) == NULL)
  mwerror(FATAL, 1, "Not enough memory !\n");
  
  if  ((O2 = mw_change_fimage(O2 ,A->nrow, A->ncol)) == NULL)
  mwerror(FATAL, 1, "Not enough memory !\n");

  for (i = 0; i < A->ncol * A->nrow; i++) 
    if (A->gray[i] > B->gray[i]) {
      O1->gray[i] = A->gray[i] - B->gray[i];
      O2->gray[i] = 0;
    }
    else {
      O1->gray[i] = 0;
      O2->gray[i] = B->gray[i] - A->gray[i];
    }
}

