/*--------------------------- Commande MegaWave -----------------------------*/
/* mwcommand
   name = {addnoise};
   author = {"Trung Nguyen"};
   version = {"1.0"};
   function = {"Add impulsional noise to image"};
   usage = {   
     't':[t=0]->t       "Type of noise: 0=pepper & salt, 1=salt, 2=pepper, default 0",
     'p':[p=20]->p      "Probability of noise per cent, default 20",
     in->u              "Input Fimage",
     out<-v             "Output Fimage"
   };
*/

#include "mw.h"

void addnoise(u, v, t, p) 
     Fimage u, v;
     int *t, *p;
{
  int i;
  
  if  ((v = mw_change_fimage(v ,u->nrow, u->ncol)) == NULL)
  mwerror(FATAL, 1, "Not enough memory !\n");
  
  for (i = 0; i < u->ncol * u->nrow; i++)
    if (((float)rand() / RAND_MAX * 100) < *p)
      if ((*t == 0 && rand() < RAND_MAX / 2) || *t == 1) 
	v->gray[i] = 255;
      else
	v->gray[i] = 0;
    else
      v->gray[i] = u->gray[i];
}

