www.pudn.com > psnr.rar > main.c, change:2008-09-05,size:3426b


 
#include "math.h" 
#include "stdio.h" 
#include "malloc.h" 
 
////////////////////////////   注意:每次计算PSNR时,需要修改的参数     //////////////////////////// 
#define ImageRows 512   
#define ImageWidth 512 
#define PixelBitDepth 8   // 像素比特深度 
#define SignedPixels 0     // 0:无符号; 1:有符号 
//////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void ImageRead(int **Image, FILE *inputfile) 
{ 
	int i,j; 
	unsigned char *temp; 
	char *temp1; 
	unsigned short *temp2; 
	short *temp3; 
 
	if( (PixelBitDepth<=8) && (PixelBitDepth!=0) ) 
	{ 
		if(SignedPixels == 0) // unsigned 
		{ 
			temp = (unsigned char *)calloc(ImageWidth, sizeof(unsigned char)); 
			for(i=0; i<ImageRows; i++) 
			{ 
				fread(temp,sizeof(char),ImageWidth,inputfile);  
				for(j=0; j<ImageWidth; j++) 
					Image[i][j] = temp[j]; 
			} 
			free(temp); 
		} 
		else if(SignedPixels == 1) // signed 
		{ 
			temp1=(char *)calloc(ImageWidth, sizeof(char)); 
			for(i=0; i<ImageRows; i++) 
			{ 
				fread(temp1,sizeof(char),ImageWidth,inputfile);  
				for(j=0; j<ImageWidth; j++) 
					Image[i][j] = temp1[j]; 
			} 
			free(temp1); 
		} 
	} 
 
	else if( (PixelBitDepth<=15) || (PixelBitDepth==0) ) 
	{ 
		if(SignedPixels == 0) // unsigned 
		{ 
			temp2=(unsigned short *)calloc(ImageWidth, sizeof(unsigned short)); 
			for(i=0; i<ImageRows; i++) 
			{ 
				fread(temp2,sizeof(short),ImageWidth,inputfile);  
				for(j=0; j<ImageWidth; j++) 
					Image[i][j] = ((temp2[j] & 0x00FF) << 8) + ((temp2[j] & 0xFF00) >> 8);  //读入的数据是低位在前,高位在后 
			} 
			free(temp2); 
		} 
		else if(SignedPixels == 1) // signed 
		{ 
			temp3=(short *)calloc(ImageWidth, sizeof(short)); 
			for(i=0; i<ImageRows; i++) 
			{ 
				fread(temp3,sizeof(short),ImageWidth,inputfile);  
				for(j=0; j<ImageWidth; j++) 
					Image[i][j] = temp3[j]; 
			} 
			free(temp3); 
		} 
	} 
} 
 
 
void main() 
{ 
 
	int **OriginalImage; 
    int **TransformedImage; 
	FILE *InputFile=NULL; 
 
	int i,j; 
	double Mse=0; 
	double c; 
	double psnr=0; 
   
	/*allocate the image's memory space*/ 
	OriginalImage=(int **)calloc(sizeof(int *),ImageRows); 
	for(i=0;i<ImageRows;i++) 
	{ 
	    OriginalImage[i]=(int *)calloc(sizeof(int),ImageWidth); 
	} 
               
	TransformedImage=(int **)calloc(sizeof(int *),ImageRows); 
	for(i=0;i<ImageRows;i++) 
	{ 
	    TransformedImage[i]=(int *)calloc(sizeof(int),ImageWidth); 
	} 
 
	////////////////////////////   注意:每次计算PSNR时,需要修改的图象文件     //////////////////////// 
    /*Read in the original image*/ 
    InputFile=fopen("f:\\jiang\\d_image\\marstest.raw","rb");   // 
	ImageRead(OriginalImage,InputFile); 
 
	/*Read in the transformed image*/ 
	InputFile=fopen("f:\\jiang\\d_image\\mar.raw","rb"); 
    ImageRead(TransformedImage,InputFile); 
     
	//////////////////////////////////////////////////////////////////////////////////////////////////// 
 
	/*caculate the psnr*/ 
 
    for(i=0;i<ImageRows;i++) 
		for(j=0;j<ImageWidth;j++) 
        { 
            Mse+=pow(OriginalImage[i][j]-TransformedImage[i][j],2); 
		} 
	Mse=Mse/(ImageRows*ImageWidth); 
 
    c = pow(2,PixelBitDepth) - 1; 
	psnr=10*log10(pow(c,2)/Mse);    
 
	printf("the value of psnr is:%f\n",psnr);  
 
    for(i=0;i<ImageRows;i++) 
		free(OriginalImage[i]); 
	free(OriginalImage); 
 
	for(i=0;i<ImageRows;i++) 
		free(TransformedImage[i]); 
	free(TransformedImage); 
 
}