Ivanovskiy.com

Navigation

· Home Page
·      Our Photo Gallery

»

2009

· Photo Gallery Albums 2009
»

2008

· Photo Gallery Albums 2008
»

2007

· Russia - St Isaac's Cathedral
· Russia - The State Hermitage
· Russia - The Hermitage Theater
· Russia - Peterhof
· Russia - Peter and Paul Fortress
· Russia - Church On The Spilled Blood
· Russia - Pushkin (Outside of St. Petersburg)
· Russia - YusupovPalace
· Russia - Circuis (Shapito)
· Russia -Cemetery (Family Plot)
· Bahamas - 6th Wedding Anniversary
· Bahamas - Video Clips
»

2006

· Photo Gallery Albums 2006
»

2005

· Discovery Cove 2005 (4th Wedding Anniversary)
»

2004

· Second Chance Prom 2004
· Photo Gallery Albums 2004
»

2003

· Halloween 2003
· North Carolina 2003
· Cozumel Trip 2003
· Renaissance Festival 2003
· Sergei's Wedding Pics
· Photo Gallery Albums 2003
»

2002

· Photo Gallery Albums 2002
»

2001

· Our Wedding Pictures (Tarpon Springs 2001)
· Photo Gallery Albums 2001
»

2000

· Photo Gallery Albums 2000
»

Misc Pics

· Is hell exothermic or endothermic?
· Funny Commercials and more!
· Russian Cathedrals
· Russian Links
· Kodak Hockey Team

Angela

· Email Angela
· About Angela

Angela's Writing Portfolio

· Porcelain God: Friend or Foe?
· More Coming Soon!

Tim

· Email Tim
· About Tim

· Histogram Modification Technique ( Image Processing)
· Smoothing and Edge Detection (Image Processing)
· More Coming Soon!

Other Websites

· MandalayBeachClub.net
· Yahoo! - Astrology
· Netscape Radio

· University Of South Florida
· Computer Engineering Department
· Computer Science Department
· USF Football

In The News

· CNN
·USAToday
·St.Petersburg Times

Free Plug Ins

·
·
· Good FTP Program
· Email Webmaster

 



/*******************************************/
/* 					   */
/*  project3.c				   */ 
/*  Image Processing Application           */
/*	HISTOGRAM MODIFICATION TECHNIQUE   */
/*                                         */
/*					   */	
/*******************************************/
#include "header.h"
/**************************************************************************/
/*                      PROJECT 3 CALLBACK FUNCTION                       */
/**************************************************************************/
void col4_cb(widget, client_data, call_data)
Widget widget;
XtPointer client_data;
XtPointer call_data;
{
  

   int button = (int) client_data;
   if(main_id < 0)
   {
      printf("No image is open\n\n");
      return;
   }
   
   if(button == 0) /** GLOBAL HISTOGRAM EQUALIZATION **/
   {
    printf("\nGlobal Histogram Equalization ");
  
    col4row0_cb(widget, client_data, call_data);
	
   }
   else if(button == 1) /** LOCAL HISTOGRAM EQUALIZATION **/
   { 
    localFlag = 1; /* setting local flag */
    col4row1_cb(widget, client_data, call_data);
   }
   else if(button == 2) /* Color Histogram equalization */
   {
   col4row2_cb(widget, client_data, call_data);
   }
}

/*************************************************************/
/*                  PROJECT 3 OPTION 0 CALLBACK FUNCTION     */
/*  			GLOBAL HISTOGRAM EQUALIZATION	     */
/*************************************************************/
void col4row0_cb(widget, client_data, call_data)
Widget widget;
XtPointer client_data;
XtPointer call_data;
{
  char *s, filename[] = "Global Histogram Equalization";
  int row, col, index,val, num, Val, maxVal, i, TotalCount;
  Image_data *oldImage, *newImage, *preH, *postH;
  int Histogram[Hmax]={0};
  double pdf[Hmax], cdf[Hmax], scaleFactor;
  int black, white;
  
  printf("\nhello from col4row0_cb\n");
 	
   /* Access Old Image */
   oldImage = &(images[main_id]);
  /* CREATE NEW IMAGE FOR ORIGINAL HISTOGRAM */
  preH = &(images[++last_id]);
  preH->n_cols = 256;
  preH->n_rows = 256;
  /* BW IMAGE */
  preH->image_R_ptr = NULL;
  preH->image_B_ptr = NULL;
  /*ALLOCATING MEMORY */
  if((preH->image_G_ptr = (unsigned char *)malloc(preH->n_rows * preH->n_cols *
          sizeof(unsigned char))) == NULL)
    {
      printf("Unable to allocate sufficient memory for image \n");
      free(preH->filename);
      return;
    }
  /* NEED FOR DISPLAYING THE FILE */
  if((preH->filename = (char *)malloc(strlen(filename) * sizeof(char))) == NULL)
    {
      printf("Unable to allocate sufficient memory for file name \n");
      return;
    }
   strcpy(preH->filename, "Original Histogram");
   strcpy(preH->pgm_type, oldImage->pgm_type);
  /*END OF PREPARATION FOR ORIGINAL HISTOGRAM*/
   /*COUNT FREQUENCIES */
   getCount(oldImage, Histogram);
   getPdfandCdf( pdf, cdf, Histogram, oldImage);
    /* MAKE PGM HISTOGRAM OF ORIGINAL */
    makeHistPGM( preH, pdf);
   /*MAKE CUMULITIVA HISTOGRAM PGM
   makeHistPGM( preH, cdf);*/
   
   /* Display Histogram image */ 
   display_cb(widget, (XtPointer) &last_id, call_data);       
/***************************************************************/
/***HISTOGRAM WAS DISPLAYED FOR ORIGINAL IMAGE *****************/
/***************************************************************/
/***************************************************************/
/*** PREPARE RESOURCES FOR EQUALIZED IMAGE     *****************/
/***************************************************************/  
 
 /* printf("\nPrepare resource for image");
  PrepImage( newImage, oldImage, "EQUALIZED IMAGE OUTPUT");
  printf("\nOut of PrepImage");**/
/***************************************************************/
  /* CREATE NEW IMAGE - EQUALIZED IMAGE ***************/
  newImage = &(images[++last_id]);
  newImage->n_cols = oldImage->n_cols;
  newImage->n_rows = oldImage->n_rows;
  /****************** BW IMAGE ***********************/
  newImage->image_R_ptr = NULL;
  newImage->image_B_ptr = NULL;
  /*****************ALLOCATING MEMORY ****************/
  if((newImage->image_G_ptr = (unsigned char *)malloc(newImage->n_rows * 
          newImage->n_cols *sizeof(unsigned char))) == NULL)
    {
      printf("Unable to allocate sufficient memory for image \n");
      free(newImage->filename);
      return;
    }
  /***** NEED FOR DISPLAYING THE FILE **************/
  if((newImage->filename = (char *)malloc(strlen(filename) * sizeof(char))) == NULL)
    {
      printf("Unable to allocate sufficient memory for file name \n");
      return;
    }
   strcpy(newImage->filename, "Equalized Output");
   strcpy(newImage->pgm_type, oldImage->pgm_type);
  /**** END OF PREPARATION FOR EQUALIZED IMAGE *****/
  /************** EQUALIZE IMAGE *******************/
  equalize(cdf, newImage, oldImage);
  
  /*********** DISLPLAY EQUALIZED IMAGE ************/
  display_cb(widget, (XtPointer) &last_id, call_data);
  printf("\nEqualized image was displayed.");
/***************************************************************/
/***  DISPLAY EQUALIZED HISTOGRAM              *****************/
/***************************************************************/
   /* Access Old Image */
   oldImage = newImage;
  /*** CREATE NEW IMAGE FOR EQUALIZED HISTOGRAM * */
  postH = &(images[++last_id]);
  postH->n_cols = 256;
  postH->n_rows = 256;
  /********** BW IMAGE ***************************/
  postH->image_R_ptr = NULL;
  postH->image_B_ptr = NULL;
  /****** ALLOCATING MEMORY **********************/
  if((postH->image_G_ptr = (unsigned char *)malloc(postH->n_rows * postH->n_cols *
          sizeof(unsigned char))) == NULL)
    {
      printf("Unable to allocate sufficient memory for image \n");
      free(postH->filename);
      return;
    }
  /********* NEED FOR DISPLAYING THE FILE*******/
  if((postH->filename = (char *)malloc(strlen(filename) * sizeof(char))) == NULL)
    {
      printf("Unable to allocate sufficient memory for file name \n");
      return;
    }
   strcpy(postH->filename, "Equalized Histogram");
   strcpy(postH->pgm_type, oldImage->pgm_type);
  /*END OF PREPARATION */
   /************* *COUNT FREQUENCIES *********/
   getCount(oldImage, Histogram);
	
 
    getPdfandCdf( pdf, cdf, Histogram, newImage);
  
   /******** MAKE PGM HISTOGRAM OF EQUALIZED IMAGE *******/
   makeHistPGM( postH, pdf);
   
   
   /****DISPLAY EQUALIZED HISTOGRAM **********************/ 
   display_cb(widget, (XtPointer) &last_id, call_data);       
}
/**************************************************************************/
/*                  PROJECT 3 OPTION 1 CALLBACK FUNCTION                  */
/**************************************************************************/
/*********          LOCAL HISTOGRAM EQUALIZATION         ******************/
/**************************************************************************/
void col4row1_cb(widget, client_data, call_data)
Widget widget;
XtPointer client_data;
XtPointer call_data;
{
  char *s, filename[] = "Local Histogram Equalization";
  int row, col, index,val, num, Val, maxVal, i, TotalCount;
  Image_data *oldImage, *newImage, *preH, *postH;
  int Histogram[Hmax]={0};
  double pdf[Hmax]={0};
  double cdf[Hmax]={0};
  double  scaleFactor;
  int black, white;
 	
   /* Access Old Image */
   oldImage = &(images[main_id]);
  /* CREATE NEW IMAGE FOR ORIGINAL HISTOGRAM */
  preH = &(images[++last_id]);
  preH->n_cols = 256;
  preH->n_rows = 256;
  /* BW IMAGE */
  preH->image_R_ptr = NULL;
  preH->image_B_ptr = NULL;
  /*ALLOCATING MEMORY */
  if((preH->image_G_ptr = (unsigned char *)malloc(preH->n_rows * 
          preH->n_cols *sizeof(unsigned char))) == NULL)
    {
      printf("Unable to allocate sufficient memory for image \n");
      free(preH->filename);
      return;
    }
  /* NEED FOR DISPLAYING THE FILE */
  if((preH->filename = (char *)malloc(strlen(filename) * sizeof(char))) == NULL)
    {
      printf("Unable to allocate sufficient memory for file name \n");
      return;
    }
   strcpy(preH->filename, "Original Local Histogram");
   strcpy(preH->pgm_type, oldImage->pgm_type);
  /*END OF PREPARATION FOR ORIGINAL HISTOGRAM*/
   /*COUNT FREQUENCIES for local */
printf("\nbefore get count");
   getCount(oldImage, Histogram);
printf("\nafter get count");
   getPdfandCdf( pdf, cdf, Histogram, oldImage);
    /* MAKE PGM HISTOGRAM OF ORIGINAL */
    makeHistPGM( preH, pdf);
   
   /* Display Histogram image */ 
   display_cb(widget, (XtPointer) &last_id, call_data);       
/***************************************************************/
/***HISTOGRAM WAS DISPLAYED FOR ORIGINAL IMAGE *****************/
/***************************************************************/
/***************************************************************/
/*** PREPARE RESOURCES FOR EQUALIZED IMAGE     *****************/
/***************************************************************/  
 
  /* CREATE NEW IMAGE - EQUALIZED IMAGE ***************/
  newImage = &(images[++last_id]);
  newImage->n_cols = oldImage->n_cols;
  newImage->n_rows = oldImage->n_rows;
  /****************** BW IMAGE ***********************/
  newImage->image_R_ptr = NULL;
  newImage->image_B_ptr = NULL;
  /*****************ALLOCATING MEMORY ****************/
  if((newImage->image_G_ptr = (unsigned char *)malloc(newImage->n_rows * 
          newImage->n_cols *sizeof(unsigned char))) == NULL)
    {
      printf("Unable to allocate sufficient memory for image \n");
      free(newImage->filename);
      return;
    }
  /***** NEED FOR DISPLAYING THE FILE **************/
  if((newImage->filename = (char *)malloc(strlen(filename) * sizeof(char))) == NULL)
    {
      printf("Unable to allocate sufficient memory for file name \n");
      return;
    }
   strcpy(newImage->filename, "Local Equalized Output");
   strcpy(newImage->pgm_type, oldImage->pgm_type);
  /**** END OF PREPARATION FOR EQUALIZED IMAGE *****/

  /**************  COPY IMAGE **********************/
  CopyImage( newImage, oldImage);
  /************** EQUALIZE IMAGE *******************/
  equalize(cdf, newImage, oldImage);
  
  /*********** DISLPLAY LOCAL EQUALIZED IMAGE ************/
  display_cb(widget, (XtPointer) &last_id, call_data);
  printf("\nLocal Equalized image was displayed.");
/***************************************************************/
/***  DISPLAY EQUALIZED HISTOGRAM              *****************/
/***************************************************************/
   /* Access Old Image */
   oldImage = newImage;
  /*** CREATE NEW IMAGE FOR EQUALIZED HISTOGRAM * */
  postH = &(images[++last_id]);
  postH->n_cols = 256;
  postH->n_rows = 256;
  /********** BW IMAGE ***************************/
  postH->image_R_ptr = NULL;
  postH->image_B_ptr = NULL;
  /****** ALLOCATING MEMORY **********************/
  if((postH->image_G_ptr = (unsigned char *)malloc(postH->n_rows * postH->n_cols *
          sizeof(unsigned char))) == NULL)
    {
      printf("Unable to allocate sufficient memory for image \n");
      free(postH->filename);
      return;
    }
  /********* NEED FOR DISPLAYING THE FILE*******/
  if((postH->filename = (char *)malloc(strlen(filename) * sizeof(char))) == NULL)
    {
      printf("Unable to allocate sufficient memory for file name \n");
      return;
    }
   strcpy(postH->filename, "Local Equalized Histogram");
   strcpy(postH->pgm_type, oldImage->pgm_type);
  /*END OF PREPARATION */
   /************* *COUNT FREQUENCIES *********/
   getCount(oldImage, Histogram);
	
 
    getPdfandCdf( pdf, cdf, Histogram, newImage);
  
   /******** MAKE PGM HISTOGRAM OF EQUALIZED IMAGE *******/
   makeHistPGM( postH, pdf);
   
   
   /****DISPLAY LOCAL  EQUALIZED HISTOGRAM **********************/ 
   display_cb(widget, (XtPointer) &last_id, call_data);       
   /**** RESET LOCAL FLAG ***/
   localFlag = 0;
}
/**************************************************************************/
/*                  PROJECT 3 OPTION 2 CALLBACK FUNCTION                  */
/*COLOR HISTOGRAM EQUALIZATION						  */
/**************************************************************************/
void col4row2_cb(widget, client_data, call_data)
Widget widget;
XtPointer client_data;
XtPointer call_data;
{
  char  filename[] = "Color Histogram Equalization";
  int row, col, index, val, count;
  Image_data *oldImage, *newImage,*preH, *postH;
  double r, g, b, h, s, i, temp, *H, *S, *I, *newI;
  double pdf[Hmax]={0};
  double cdf[Hmax]={0};
  int Histogram[Hmax]={0};
  /* Accsee Old Image */
  oldImage = &(images[main_id]);
  
 
  /*allocating memory for HSI*/
  H = (double *)malloc(oldImage->n_cols * oldImage->n_rows * sizeof(double));
  S = (double *)malloc(oldImage->n_cols * oldImage->n_rows * sizeof(double));
  I = (double *)malloc(oldImage->n_cols * oldImage->n_rows * sizeof(double));
  newI = (double *)malloc(oldImage->n_cols * oldImage->n_rows * sizeof(double));
  
printf("\nMemory allocation done for HSI\n");
  for(row = 0 ; row<oldImage->n_rows; row++)
   {
   for(col = 0; col<oldImage->n_cols; col ++)
    {
    	index = row* oldImage->n_cols + col;
	r = (double)oldImage->image_R_ptr[index]/255.0;
	g = (double)oldImage->image_G_ptr[index]/255.0;
	b = (double)oldImage->image_B_ptr[index]/255.0;
/***********************************************************/
/* 	CONVERTING TO  i, h, s 				   */
/***********************************************************/
	i = (double)(r + g + b)/3;
	temp =  (double)pow((r-g),2) + (r-b)*(g-b);
	if(temp <= 0) h = 0;
	else h =(double) acos( (.5 * (r-g) + (r-b) )/sqrt( temp));
	  
	s =(double)1 - ( 3/(r+g+b) ) * min(r,g,b);
	if( b > g )
	{
	  h = (2 * PI) - h;
	}
	else
	{
	  h=0;
	}
	/* NORMALIZE FOR H */
	H[index] = h/(2*PI);
	S[index] = s;
	I[index] = i;
     
        if( I[index]<0 || I[index]>1)
		printf("I= %.2f ",I[index]);
	if( H[index]<0 || H[index]>1)
		printf("H=%.2f", H[index]);
	if( S[index]<0 || S[index]>1)
		 printf("S= %f.2 ", S[index]);
    }/*2nd for*/
   }/*1st for*/
/***********************************************************/
/*    DONE 	CONVERTING TO  i, h, s 			   */
/***********************************************************/
/***********************************************************/
/*    DISPLAYING ORIGINAL HISTOGRAM 			   */
/***********************************************************/
/*****************************************************************/
   /*display Original  Histogram */
printf("\nCreating image for histogram");  
  /* CREATE NEW IMAGE */
  preH = &(images[++last_id]);
  preH->n_cols = 256;
  preH->n_rows = 256;
  preH->image_R_ptr = NULL;
  preH->image_B_ptr = NULL;
  CheckMemBW(preH);
 
  
   
  if((preH->filename = (char *) malloc(strlen(filename) * sizeof(char))) == NULL)
    {
      printf("Unable to allocate sufficient memory for file name.\n");
      return;
    }     
  strcpy(preH->pgm_type, "P2");
  strcpy(preH->filename, "Original I Histogram");
 
  printf("\nMemory for new HISTOGRAM was allocated");
  
  freqI( oldImage, I, Histogram);		
  getPdfandCdf( pdf, cdf, Histogram, preH);	/*pdf and cdf are set */
  makeHistPGM( preH, pdf);
  /****DISPLAY HISTOGRAM FILE **********************/
  printf("\ndisplaying histogram file"); 
  display_cb(widget, (XtPointer) &last_id, call_data);  

/***************************************************/
/*   PREPARE RESOURCE TO GO BACK TO RGB            */
/***************************************************/
  /*ALLOCATE RESOUCES TO CREATE NEW IMAGE */
  newImage = &(images[++last_id]);
  newImage->n_cols = oldImage->n_cols;
  newImage->n_rows = oldImage->n_rows;
  CheckMemColor(newImage);
  if((newImage->filename = (char *) malloc(strlen(filename) * sizeof(char))) == NULL)
    {
      printf("Unable to allocate sufficient memory for file name.\n");
      return;
    }     
  strcpy(newImage->pgm_type, oldImage->pgm_type);
  strcpy(newImage->filename, "Equalized Output");
 /* END OF ALLOCATION RESOUCES */
  printf("\nMemory for new image was allocated");
/***************************************************/
/*             DO EQUALIZATION                     */
/***************************************************/
/*do equalization on I here */
/************** EQUALIZE IMAGE *******************/
  
printf("\ndoing equalization on I"); 
   freqI( oldImage, I, Histogram);
   /*calculate pdf */
  for(count=0; count <Hmax; count++)
   {
    pdf[count] = (double)Histogram[count]/(oldImage->n_cols * oldImage->n_rows);
printf("%.2f ",pdf[count]);
   }
printf("\ndone calculated pdf "); 
  /*calculate cdf*/
  cdf[0]= pdf[0];
  for(count=1; count<Hmax; count++)
   {
    cdf[count]= (double)cdf[count-1]+pdf[count];
printf("%.2f ", cdf[count]);
   }
printf("\ndone calculating cdf \n");
/**********
   for( row = 0; row< oldImage->n_rows; row ++)
    {
     for(col = 0; col< oldImage->n_cols; col ++)
      {
       index = row * oldImage->n_cols + col;
       newI[index] = round(cdf[round(I[index])]*255.0);  
printf("<newI = %.2f>", newI[index]);
      }/*second for***********
    }/*first for**************
/* copy newI to I ************
   for( row = 0; row< oldImage->n_rows; row ++)
    {
     for(col = 0; col< oldImage->n_cols; col ++)
      {
       index = row * oldImage->n_cols + col;
       I[index] = newI[index];  
      }/*second for**********
    }/*first for************/
/***************************************************/
/*             DONE EQUALIZATION                   */
/***************************************************/
printf("\ngoing back to RGB");
/***************************************************/
/*             GOING BACK TO RGB                   */
/***************************************************/
  for(row = 0 ; row<oldImage->n_rows; row++)
   {
   for(col = 0; col<oldImage->n_cols; col ++)
    {
     index = row* oldImage->n_cols + col;
	/*convert back to RGB*/
	h = H[index] * 2 * PI;
	s = S[index];
	i = I[index]; 
	if( i != 0)
	{
	 if(h <= (2 * PI) /3)
	  {
	   r = i * (1+ ( (s*cos(h)) / cos((PI/3)-h) ));
	   b = i * (1 - s);
	   g = 3 *i * ( 1- ((r+b)/(3*i)) );
	  }
	 else if( h<= 2 * ((2*PI) / 3))
	  {
	   h = h - (2 * PI / 3);
	   g = i * (1+ ( (s*cos(h))/ cos( (PI*3)-h) ) );
	   r = i * (1 - s);
	   b = 3 * i* ( 1 - ((r+g)/(3*i)) );
	  } 
	 else if( h <= 2*PI)
	  {
	   h = h-( 4*PI /3 );
	   b = i * (1+ ( (s*cos(h)) / cos((PI/3)-h) ) );
	   g = i * (1-s);
	   r = 3 * i* (1- ( (g+b)/(3*i) ) );
	  }
	 }/*if (i !=0)*/
/*don't forget to scale while converting */
	if(r < 0 )
	 {
 	  r = 0;
	 }
	else
	 {
	  r = round(r * 255);
	  if(r> 255) r=255;
	 }
	if (g < 0)
	 {
	  g = 0;
	 }
	else
	 {
	  g = round( g * 255);
	  if(g> 255) g=255;
	 }
	if( b<0)
	 {
	  b=0;
	 }
	else
	 {
	  b = round( b * 255);
	  if(b>255) b=255;
	 }
	newImage->image_R_ptr[index] = (unsigned char)r;
	newImage->image_G_ptr[index] = (unsigned char)g;
	newImage->image_B_ptr[index] = (unsigned char)b;
	
    }/*2nd for*/
   }/*1st for*/
printf("\nReady to display hsi to rgb");
  
   /****DISPLAY FILE **********************/ 
   printf("\nDisplaying Equalized file"); 
   display_cb(widget, (XtPointer) &last_id, call_data);  
/*****************************************************************/
   /*display Equalized  Histogram */
printf("\nCreating image for equalize histogram");  
  /* CREATE NEW IMAGE */
  postH = &(images[++last_id]);
  postH->n_cols = 256;
  postH->n_rows = 256;
  postH->image_R_ptr = NULL;
  postH->image_B_ptr = NULL;
  CheckMemBW(postH);
 
  
   
  if((postH->filename = (char *) malloc(strlen(filename) * sizeof(char))) == NULL)
    {
      printf("Unable to allocate sufficient memory for file name.\n");
      return;
    }     
  strcpy(postH->pgm_type, "P2");
  strcpy(postH->filename, "Equalized I Histogram");
 
  printf("\nMemory for equalized HISTOGRAM was allocated\n");
  
  freqI( newImage, I, Histogram);
printf("\nfreq done");
  getPdfandCdf( pdf, cdf, Histogram, postH);
printf("\ngetpdf and cdf done\n");
  makeHistPGM( postH, pdf);
printf("\nmakeHistPGM done\n");
  /*makeHistPGM( postH, I); */
  /****DISPLAY HISTOGRAM FILE **********************/
  printf("\ndisplaying equalized histogram file"); 
  display_cb(widget, (XtPointer) &last_id, call_data);  
  
   
}
/**************************************************************************/
/*		FUNCTION COUNTS FREQUENCIES				  */
/**************************************************************************/
void freqI(struct image_data *OldImage, double I[],int Histogram[])
{
 int row, col, index, val;
/* count frequencies of pixels in the I */
    
    for( row = 0; row < OldImage->n_rows; row ++)
    {
     for ( col = 0 ; col < OldImage->n_cols; col ++)
     {
      index = row * OldImage->n_cols + col;
       val = (int)round(I[index]*255.0);
       Histogram[val] = Histogram[val]+1;
     }/*second for ****/
    }/*first for ***/
}
/**************************************************************************/
/*		FUNCTION FINDS MIN VALUE				  */
/**************************************************************************/
double min( double x, double y, double z)
{
  double min = x;
  if( y < min)
    min = y;
  if( z < min)
    min = z;
  return min;    
}

/**************************************************************************/
/*		FUNCTION EQUALIZES THE HISTOGRAM			  */
/**************************************************************************/
void equalize(double cdf[], struct image_data *NewImage, 
          struct image_data *OldImage)
{
  int row, col, index;
  if( localFlag == 1) /** DO LOCAL OPERATION **/
   {
    for(row = y_1; row < y_2; row++)
     {
      for(col = x_1; col <=x_2; col++)
       {
	index = row * NewImage->n_cols + col;
        NewImage->image_G_ptr[index] = 
          round(cdf[OldImage->image_G_ptr[index]]*255.0);

       }/*second for*/
      }/*first for*/
   }/*if*/
   
  else /** DO GLOBAL OPERATION **/
   {
    for( row = 0; row< OldImage->n_rows; row ++)
    {
     for(col = 0; col< OldImage->n_cols; col ++)
      {
       index = row * OldImage->n_cols + col;

       NewImage->image_G_ptr[index] = 
          round(cdf[OldImage->image_G_ptr[index]]*255.0);  
      }/*second for*/
    }/*first for*/
   }/*else*/
}
/**************************************************************************/
/*		FUNCTION MAKES PGM HISTOGRAM     			  */
/**************************************************************************/
void makeHistPGM( struct image_data *pgmHist, double pdf[])
{
  int col, row, index, i;
  double black, white, maxVal, factor;
/*Scaling */
  /*find max value ***************************/
 
 maxVal = 0.0;
 for( i = 0; i < Hmax; i++)
  {
    if ( maxVal< pdf[i] )
     {
      maxVal = pdf[i];
     }
  }
 
 factor = 1.0 - maxVal;
  

printf("\n maxVal= %.2f", maxVal);
/*********************************************/
 /* MAKING PGM HISTOGRAM */

   if( localFlag == 2) /** DO LOCAL OPERATION **/
   { 
    for(col = x_1; col <=x_2; col++)
     {
      for(row = y_1; row < y_2; row++)
       {
	index = row * pgmHist->n_cols + col;
   	black = (double)pdf[col]*255;
	white = 255 - black;
	if( row<=white)
	 {
	   pgmHist->image_G_ptr[index]=255;
	 }
	else
	 {
	  pgmHist->image_G_ptr[index]=0;
	 }    
       }
     }
    }/*if*/
  else /*global */
  {
   for( col=0; col < pgmHist->n_cols; col ++)
    {
    for( row = 0; row < pgmHist->n_rows; row ++)
      {
 	index = row * pgmHist->n_cols + col;
	
	
	black = (double)(pdf[col])*255;
	white = 255 - black;
	if( row<=white)
	 {
	   pgmHist->image_G_ptr[index]=255;
	 }
	else
	 {
	  pgmHist->image_G_ptr[index]=0;
	 }
	
      }/*second*/
    }/*first*/
  }/*else*/
}
/**************************************************************************/
/*		FUNCTION PREPARES IMAGE FOR DISPLAY			  */
/**************************************************************************/
void PrepHist( struct image_data *newImage, 
          struct image_data *oldImage, char filename[])
{
 
  newImage->n_cols = 255;
  newImage->n_rows = 255;
  /* BW IMAGE ************************/
  newImage->image_R_ptr = NULL;
  newImage->image_B_ptr = NULL;
 /* NEED FOR DISPLAYING THE FILE *************/
  if((newImage->filename = (char *)malloc(strlen(filename) * 
          sizeof(char))) == NULL)
    {
      printf("Unable to allocate sufficient memory for file name \n");
      return;
    }
  /*ALLOCATING MEMORY ****************/
  if((newImage->image_G_ptr = (unsigned char *)malloc(newImage->n_rows * 
          newImage->n_cols *sizeof(unsigned char))) == NULL)
    {
      printf("Unable to allocate sufficient memory for image \n");
      free(newImage->filename);
      return;
    }

   strcpy(newImage->filename, filename);
   strcpy(newImage->pgm_type, "P5");
  /*END OF PREPARATION */
}
/**************************************************************************/
/*		FUNCTION CALCULATES Pdf and Cdf FOR THE IMAGE		  */
/**************************************************************************/
void getPdfandCdf( double pdf[], double cdf[], int Hist[], 
          struct image_data *OldImage)
{
  int i, TotPixels;
  if(localFlag == 1){ /*adjust # of pixels for local area */
   TotPixels = (y_2 - y_1 +1) * (x_2 - x_1 +1);
   }
  else{
   TotPixels = (OldImage->n_cols * OldImage->n_rows);
   }
  /*calculate pdf */
  for(i=0; i<Hmax; i++)
   {
    pdf[i] = (double)Hist[i]/TotPixels;
   }
 
  /*calculate cdf*/
  cdf[0]= pdf[0];
  for(i=1; i<Hmax; i++)
   {
    cdf[i]= (double)cdf[i-1]+pdf[i];
   }
  
}
/**************************************************************************/
/*                  Get Histogram    			                  */
/**************************************************************************/
void getCount( struct image_data *OldImage, int Histogram[] )
{
  int col, row, val, index;
  
  val = 0;
  if(localFlag == 1) /* disabled only when == 1 */
  {
    /* count frequencies of pixels in the image */
    
    for ( col = x_1 ; col <= x_2; col ++)
    {
     for ( row = y_1 ; row <= x_2; row ++)
     {
      index = row * OldImage->n_cols + col;
       val = OldImage->image_G_ptr[index];
       Histogram[val] = Histogram[val]+1;
     }/*second for ****/
    }/*first for ***/   
  }/*if*/
  
  else
  {
    /* count frequencies of pixels in the image */
    
    for( row = 0; row < OldImage->n_rows; row ++)
    {
     for ( col = 0 ; col < OldImage->n_cols; col ++)
     {
      index = row * OldImage->n_cols + col;
       val = OldImage->image_G_ptr[index];
       Histogram[val] = Histogram[val]+1;
     }/*second for ****/
    }/*first for ***/
  }/*else*/
    printf("\nEnd of get histogram \n");
}
/***********************************************************************/
void printHist( int Hist[])
{
 int i;
/*printing the contents of histogram */ 
for (i=0; i<Hmax; i++)
 { 
  printf("\nHist[%d] = %d", i, Hist[i]);
 }
 printf("\n");
}
/**************************************************************************/
/*  FUNCTION ROUNDS OFF NUMBER. RECEIVES DOUBLE - RETURNS INT             */
/**************************************************************************/
int round( double x)
{
  int q;
  double temp;
  
  temp = (int)x;
  temp = x - temp;
  if( temp<0.5){
    q = (int)x;
    }
  
  else {
    q = (int)x + 1;
    }
  
  return q;
}
/***********************************************************************/
/*****************************************************************************/
/* Function allocates memory for B&W image		     		     */
/* 				     					     */
/* 									     */
/* 									     */
/*****************************************************************************/
void GetMemBW( struct image_data *our_newImage )
{
  our_newImage->image_R_ptr == NULL;
  our_newImage->image_B_ptr == NULL;
 
 if((our_newImage->image_G_ptr = (unsigned char *) malloc(our_newImage->n_rows * 
                   our_newImage->n_cols * sizeof(unsigned char))) == NULL )
	{
  	  printf("Unable to allocate sufficient memory for image.\n");
  	  free(our_newImage->filename);
  	  return;
	}
}

 

Thank You For Visiting