imgcal.c

来自「JPEG Image compression using IJG standar」· C语言 代码 · 共 328 行

C
328
字号
/*  ********************************************************************** * * Course: 		520.643 Digital Multimedia Coding and Processing  *  * Homework Assignment: 1 * * File Name:		imgcal.c *  * Designed by:		Jie Liang * * Created Date:	Sept. 23, 1999 * * Contents: *			1: Calculate the entropies of two grayscale images, *			2: Calculate the MSE, *			3: Calculate the PSNR, *			4: Calculate the Mean Absolute Difference, *			5: Calculate the maximum Pixel Error. * * Limitations: *			Accept only the following formats: *			1: QCIF *			2: pgm *			3: raw grayscale data file without any eader. *	 * Usage: *			imgcal colorimg imgfile1 frame1 imgfile2 frame2 HSize VSize *			where: *				imgfile1: input file 1, *				file1type:  *					  1 for QCIF, *					  0 for raw data(grayscale only), *				frame1:   desired frame in the first file, *				imgfile2: input file 2, *				file2type:1 for QCIF 0 for raw or pgm, *				frame2:	  desired frame in the second file, *				HSize: 	  Horizontal size, *				VSize:    Vertical size. *				 *			 * Copyright (C) 1999 Department of Electrical and Computer Engineering  * The Johns Hopkins University. All right reserved.  * ********************************************************************* *//* ********************************************************************* * *   Modification History: * *   Date	Programmer	Description *------------------------------------------------------------------- * * 09/24/99	Jie Liang	Created. *  * $Log: imgcal.c,v $ * Revision 1.11  2000/02/12 21:52:13  jliang * clarified that it cannot accept pgm. * * Revision 1.10  1999/10/09 02:03:03  jliang * *** empty log message *** * * Revision 1.9  1999/09/30 02:14:43  jliang * Added help info. * * Revision 1.8  1999/09/28 19:44:01  jliang * Tided up parameters parsing. * * Revision 1.7  1999/09/26 18:37:24  jliang * *** empty log message *** * * Revision 1.6  1999/09/26 18:12:33  jliang * Modified to accept pgm file. * * Revision 1.5  1999/09/26 15:39:56  jliang * Frame starts from 0, not 1. * * Revision 1.4  1999/09/26 15:12:57  jliang * Moved functions to imgpara.h. * * Revision 1.3  1999/09/26 14:58:46  jliang * *** empty log message *** * * Revision 1.2  1999/09/25 21:51:52  jliang * Created functions. * * Revision 1.1  1999/09/25 03:38:32  jliang * Initial revision * * ********************************************************************* */static const char rcsid[] = 	"$Id: imgcal.c,v 1.11 2000/02/12 21:52:13 jliang Exp jliang $";/* ******************************************************************** * *	Include Files * ******************************************************************** */#include <stdio.h>#include <stdlib.h>#include <math.h>#include <string.h>#include <X11/Xlib.h>#include <X11/Xutil.h>#include <X11/extensions/XShm.h>#include <sys/ipc.h>#include <sys/shm.h>#include "imgpara.h"#include "utility.h"/* ******************************************************************* * * 	Local Constants * ******************************************************************* *//* ******************************************************************* * *	Local Macros * ******************************************************************* *//* ******************************************************************* * *	Local Enumerated Types * ******************************************************************* *//* ******************************************************************* * *	Local Data Types * ******************************************************************* *//* ******************************************************************* * *	Variables * ******************************************************************* *//* ******************************************************************* * *	Local Function Prototypes * ******************************************************************* *//* *********************************************************************** * * Function Name:		main * * Descriptions: *				open files, get frames, and call various procedures. * * Input Parameters: *				argc: argument count; *				argv: array of input arguments. * Output Parameters:		0:    successful, *				-1:   error. * Limitations: * ************************************************************************ */ int main(int argc, char *argv[]){  FILE 	*imgfile[2];  int	colorimg, frame[2], hsize, vsize, framesize[2];  unsigned int	tmp, i;  unsigned char *imgdata[2] = {0, 0};  double entropy, mse, psnr, mad, mpe;  int tmp1, tmp2, tmp3;  printf("\nWelcome to Image Calculation Program $Revision: 1.11 $ by Jie Liang @ JHU.\n");  if (argc != 9) {	printf(		"\nUsage:\n  imgcal imgfile1 file1_type frame1 imgfile2 file2_type frame2 HSize VSize\n"		"Arguments:\n"		"	imgfile1: 	input file 1,\n" 		"	file1_type:	0 for raw, 1 for qcif, 2 for pgm,\n"		"	frame1:		desired frame in the first file,\n"		"	imgfile2: 	input file 2,\n"		"	file2_type:	1 for QCIF, 0 for raw,\n"		"	frame2: 	desired frame in the second file\n"		" 	HSize:		Horizontal size in pixels,\n"		"	VSize:		Vertical size in pixels\n"		"Examples:\n"		"Input files can be QICF, PGM or raw data:\n"		"	./imgcal foreman.qcif 1 10 suzie.qcif 1 100 176 144\n"		"	./imgcal foreman.qcif 1 10 fore1.raw 0 0 176 144\n"		"	./imgcal lena.raw 0 0 lena.raw 0 0 512 512\n\n"	);		return(-1);  }   hsize = atoi(argv[7]);  vsize = atoi(argv[8]);  if ((hsize < 0) || (vsize < 0)) {	printf("Image size error.\n");	return(-1);  }  for (i = 0; i < 2; i++) {		  imgdata[i] = (unsigned char *) malloc(hsize * vsize);	  if (imgdata[i] == NULL) {		printf("Memory allocation error.\n");		return(-1);	  }	/* Open Input files */  	imgfile[i] = fopen(argv[3 * i + 1], "r"); /* argv[1], argv[4] */  	if (imgfile[i] == NULL)  {		printf("Input file not found.\n");		return(-1);  	}	/* read frame number */  	frame[i] = atoi(argv[3 * i + 3]); /* argv[3], arg[6] */	  if (frame[i] < 0)  {		printf("Frame Number error.\n");		return(-1);  	}	/* read file type */ 	colorimg = atoi(argv[3 * i + 2]);	/* argv[2], argv[5] */	if (colorimg == 1) {		  framesize[i] = hsize * vsize * 3 / 2; /* valid for qcif only. */	} else {		framesize[i] = hsize * vsize;  	}  } /* end for */  /* read in imgae frame */   for (i = 0; i < 2; i++) {    		/*skip the pgm file header.*/	/* The subsequent fseek moves from the current position. */	/* Do nothing for other types. */		rewind(imgfile[i]);	get_image_info(imgfile[i], (int *) &tmp1, (int *) &tmp2, (int *) &tmp3);   	tmp = framesize[i] * frame[i];	if (fseek(imgfile[i], tmp, SEEK_CUR) != 0) {		printf(" fseek() error.\n");		return(-1);	}	fread(imgdata[i], sizeof(unsigned char), hsize * vsize, imgfile[i]);  }  fclose(imgfile[0]);  fclose(imgfile[1]);  /*   ***********************************   * task 1: Entropies    ***********************************   */  for(i = 0; i < 2; i++) {	  entropy = getentropy(imgdata[i], hsize, vsize);	  printf("The entropy of Frame %s of File %s is: %9.4f bpp.\n",		argv[ 3 * i + 3], argv[ 3 * i + 1], entropy);  }  /*   **********************************   *   * MSE   *   **********************************   */      mse = getmse(imgdata[0], imgdata[1], hsize, vsize);   printf("Mean Square Error:\t%f\n", mse);   psnr = getpsnr(imgdata[0], imgdata[1], hsize, vsize, 255);   printf("PSNR:\t\t\t%f\n", psnr);     mad = getmad(imgdata[0], imgdata[1], hsize, vsize);   printf("Mean Absolute Diff:\t%f\n", mad);   mpe = getmpe(imgdata[0], imgdata[1], hsize, vsize);   printf("Maximum Pixel Error:\t%f\n", mpe);  free(imgdata[0]);  free(imgdata[1]);    return(0);   }/* ************************************************************************* * * End of $Source: /home/jliang/courses/520.643/hw1/RCS/imgcal.c,v $ * ************************************************************************** */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?