www.pudn.com > edgedetect_canny.rar > main.C, change:2006-09-20,size:5904b


	#include <stdio.h> 
	#include <stdlib.h> 
	#include <math.h> 
	#include <malloc.h> 
	#include "global.h" 
 
 
/*-----global variables-----*/ 
FILE		*bmpInput, *bmpOutput; 
char		InputFileName; 
char		OutputFileName; 
sImage		originalImage; 
sImage		edgeImage; 
InputParameters ConfigInput; 
Mapping Map[]= { 
	{"InputFile",				&ConfigInput.InputFile,				1}, 
	{"OutputFile",				&ConfigInput.OutputFile,			1}, 
	{"Sigmal",					&ConfigInput.sigmal,				0}, 
	{"Threshold_H",				&ConfigInput.threshold_h,			0}, 
	{"Threshold_L",				&ConfigInput.threshold_l,			0}, 
 
	{NULL,                       NULL,                              -1} 
}; 
 
 
 
/*-------PROTOTYPES---------*/ 
long getImageInfo(FILE*, long, int); 
void copyImageInfo(FILE* inputFile, FILE* outputFile); 
void copyColorTable(FILE* inputFile, FILE* outputFile, int nColors); 
extern void Canny(LPBYTE pGray, SIZE sz, double sigma, double dRatLow, 
		   double dRatHigh, LPBYTE pResult); 
extern void Configure (int ac, char *av[]); 
 
extern SIZE             img; 
int main(int argc, char* argv[]) 
{ 
   int				nColors; 
   unsigned long	vectorSize; 
   unsigned long	fileSize; 
   
   unsigned char 	*pChar, someChar; 
   unsigned int		row, col; 
   unsigned char    *pResult; 
 
   Configure(argc, argv); 
 
   someChar = '0'; pChar = &someChar; 
 
 //   if(argc < 2) { 
 //    printf("Usage: %s bmpInput.bmp\n", argv[0]); 
 //    exit(0); 
 //  }; 
 //  printf("Reading filename %s\n", argv[1]); 
 
   /*-------DECLARE INPUT & OUTPUT FILES-------*/ 
//   bmpInput = fopen(argv[1], "rb"); 
//   bmpOutput = fopen("canny.bmp", "wb"); 
 
   /*---SET POINTER TO BEGINNING OF FILE----*/ 
   fseek(bmpInput, 0L, SEEK_END); 
 
   /*-------GET INPUT BMP DATA--------*/ 
   fileSize = getImageInfo(bmpInput, 2, 4); 
   originalImage.cols = (int)getImageInfo(bmpInput, 18, 4); 
   originalImage.rows = (int)getImageInfo(bmpInput, 22, 4); 
   edgeImage.rows = originalImage.rows; 
   edgeImage.cols = originalImage.cols; 
   img.cy = originalImage.rows; 
   img.cx = originalImage.cols; 
 
   /*--------PRINT DATA TO SCREEN----------*/ 
   printf("Width: %d\n", originalImage.cols); 
   printf("Height: %d\n", originalImage.rows); 
   printf("File size: %lu\n", fileSize); 
 
   nColors = (int)getImageInfo(bmpInput, 46, 4); 
   printf("nColors: %d\n", nColors); 
 
   /*------ALLOCATE MEMORY FOR FILES--------*/ 
   vectorSize = fileSize - (14+40+4*nColors); 
//   vectorSize = originalImage.cols*originalImage.rows; 
   printf("vectorSize: %lu\n", vectorSize); 
   edgeImage.data = malloc(vectorSize*sizeof(unsigned char)); 
   if(edgeImage.data == NULL) { 
	printf("Failed to malloc edgeImage.data\n"); 
	exit(0); 
   } 
   printf("%lu bytes malloc'ed for edgeImage.data\n", vectorSize); 
 
   originalImage.data = malloc(vectorSize*sizeof(unsigned char)); 
   if(originalImage.data == NULL) { 
	printf("Failed to malloc originalImage.data\n"); 
	exit(0); 
   } 
   printf("%lu bytes malloc'ed for originalImage.datt\n", vectorSize); 
 
   pResult = (unsigned char *)malloc(vectorSize*sizeof(unsigned char)); 
   if(originalImage.data == NULL) { 
	   printf("Failed to malloc pResult!!\n"); 
	   exit(0); 
   } 
   printf("%lu bytes malloc'ed for pResult;\n", vectorSize); 
 
   /*------COPY HEADER AND COLOR TABLE---------*/ 
   copyImageInfo(bmpInput, bmpOutput); 
   copyColorTable(bmpInput, bmpOutput, nColors); 
   fseek(bmpInput, (14+40+4*nColors), SEEK_SET); 
   fseek(bmpOutput, (14+40+4*nColors), SEEK_SET); 
 
   /* Read input.bmp and store it's raster data into originalImage.data */ 
   for(row=0; row<originalImage.rows; row++) { 
	for(col=0; col<originalImage.cols; col++) { 
	     fread(pChar, sizeof(char), 1, bmpInput); 
	     *(originalImage.data + row*originalImage.cols + col) = *pChar; 
	} 
   } 
 
    Canny(originalImage.data, img, ConfigInput.sigmal, ConfigInput.threshold_h, ConfigInput.threshold_l, pResult); 
	printf("Canny Edge Detection is Finished!\n"); 
    fwrite(pResult,1,vectorSize,bmpOutput); 
 
   printf("See %s for results\n",ConfigInput.OutputFile); 
   fclose(bmpInput); 
   fclose(bmpOutput); 
   free(edgeImage.data);      /* Finished with edgeImage.data */ 
   free(originalImage.data);  /* Finished with originalImage.data */ 
   return 0; 
} 
 
/*----------GET IMAGE INFO SUBPROGRAM--------------*/ 
long getImageInfo(FILE* inputFile, long offset, int numberOfChars) 
{ 
  unsigned char			*ptrC; 
  long				value = 0L; 
  unsigned char			dummy; 
  int				i; 
 
  dummy = '0'; 
  ptrC = &dummy; 
 
  fseek(inputFile, offset, SEEK_SET); 
 
  for(i=1; i<=numberOfChars; i++) 
  { 
    fread(ptrC, sizeof(char), 1, inputFile); 
    /* calculate value based on adding bytes */ 
    value = (long)(value + (*ptrC)*(pow(256, (i-1)))); 
  } 
  return(value); 
 
} /* end of getImageInfo */ 
 
/*-------------COPIES HEADER AND INFO HEADER----------------*/ 
void copyImageInfo(FILE* inputFile, FILE* outputFile) 
{ 
  unsigned char		*ptrC; 
  unsigned char		dummy; 
  int				i; 
 
  dummy = '0'; 
  ptrC = &dummy; 
 
  fseek(inputFile, 0L, SEEK_SET); 
  fseek(outputFile, 0L, SEEK_SET); 
 
  for(i=0; i<=50; i++) 
  { 
    fread(ptrC, sizeof(char), 1, inputFile); 
    fwrite(ptrC, sizeof(char), 1, outputFile); 
  } 
 
} 
 
/*----------------COPIES COLOR TABLE-----------------------------*/ 
void copyColorTable(FILE* inputFile, FILE* outputFile, int nColors) 
{ 
  unsigned char		*ptrC; 
  unsigned char		dummy; 
  int				i; 
 
  dummy = 0x00; 
  ptrC = &dummy; 
 
  fseek(inputFile, 58L, SEEK_SET); 
  fseek(outputFile, 54L, SEEK_SET); 
  for (i=0;i<4;i++) 
  { 
	  fwrite(ptrC,sizeof(char),1,outputFile); 
  } 
  for(i=0; i<(4*(nColors-2)); i++)  /* there are (4*nColors) bytesin color table */ 
  { 
    fread(ptrC, sizeof(char), 1, inputFile);  
    fwrite(ptrC, sizeof(char), 1, outputFile); 
  } 
  dummy = 0xff; 
  for (i=0;i<4;i++) 
  { 
	  fwrite(ptrC,sizeof(char),1,outputFile); 
  } 
 
 
}