WorldView-2 Entropy Program

/*

made by Yoshiyuki Yamamoto

ver1.0 2004/5/30

ver1.2 2004/8/16

ver2.0 Aug 19, 2011

 

0.Declaration of HEADER FILES for Including

1.Definition of VARIABLES

2.command line arguments processing

3.MEMORY ALLOCATION(malloc)

4.FILE OPEN+Binary File Input(fopen-fread)

5.Calculating&Writing

6.Final Processing

 

referenced by Programming in C

 

A pointer is a variable which contains the address in memory of another variable.

*/

 

//0.declaration of header files

#include<stdio.h>//fopen,printf,fprintf,fread,fwrite,fclose

#include<stdlib.h>//atoi,malloc

#include<string.h>//?

#include<time.h>//

 

int main(int argc,char*argv[])

{

 

/*1.Definition of Variables*/

 

FILE*fpi1,*fpi2,*fpo;

int i,j,height,width;                     /*int型の最大値,最小値の変数の宣言*/

unsigned short dn1,dn2;         /*DN for Red,NIR Bands*/

unsigned short *file1,*file2;    /*fle1,file2=R,NIR data*/

signed int dn3,*file3;

 

/*2.command line arguments processing*/

 

/*引数の確認*/

 

if(argc!=6)

{

fprintf(stderr,”Usage:%s/n(1)Input_POST_filename/n(2)Input_PRE_filename/n(3)Output_subtracted_filename/n(4)width/n(5)height/n”,argv[0]);

exit(1);

}

 

/*convert a string to integer*/

width=atoi(argv[4]);printf(“W=%d,”,width);

/*入力する画像ファイルの横画素数*/

height=atoi(argv[5]);printf(“H=%d/n”,height);

/*入力する画像ファイルの縦画素数*/

 

/*3.decision the location of the memory.MEMORY ALLOCATION(mallocsizeof-free)*/

file1=(unsigned short*)malloc(width*height*sizeof(short));

if(file1==NULL)

{

printf(“Error on malloc”);

exit(1);

}

 

file2=(unsigned short*)malloc(width*height*sizeof(short));

if(file2==NULL)

{printf(“Error on malloc”);

exit(1);

}

 

file3=(signed int*)malloc(width*height*sizeof(int));

if(file3==NULL)

{printf(“Error on malloc”);

exit(1);

}

 

/*4.FILE OPEN+Binary File Input*/

 

/*FILE OPEN+Binary File Input*/

if((fpi1=fopen(argv[1],”rb”))==NULL)

{

fprintf(std err,”input file open error/n”);

exit(1);

}

bread ((unsigned short*)file1,sizeof(short),width*height,fpi1);

 

/*FILE OPEN+Binary File Input*/

if((fpi2=fopen(argv[2],”rb”))==NULL)

{/*ファイルのオープン*/

fprintf(stderr,”output file open error/n”);

exit(1);

}

fread((unsigned short*)file2,sizeof(short),width*height,fpi2);

 

/*FILE OPEN+Binary File Input*/

if((fop=fopen(argv[3],”wb”))==NULL)

{/*ファイルのオープン*/

fprintf(std err,”output file open error/n”);

exit(1);

}

fread((signed int*)file3,sizeof(int),width*height,fpo);

 

/*5.Calculating&Writing*/

 

for(i=0;i<height;i++)

{

for(j=0;j<width;j++)

{

dn1=file1[i*width+j];

dn2=file2[i*width+j];

dn3=dn1-dn2;//RVI

fwrite(&dn3sizeof(int),1,fpo);/*m単位標高データの書き込み*/

}

}

 

/*6.Final Processing-file close*/

 

free(file1);

free(file2);

free(file3);

 

fclose(fpi1);

fclose(fpi2);

fclose(fpo);

}