⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imgfile.cpp

📁 基于小波的图像配准
💻 CPP
字号:
/* This test is for write function:
 * xBegin and yBegin is read beginning location;
 * xLen   and yLen   is read size descripter;
 * stenArray         is the image data to be writen     */
#include "imgfile.h"
/*
int main()
{
	FILE* fPtr;
	char *filename="rec.bmp";
	long mode=1;
	long xBegin=0,yBegin=0,xLen=31,yLen=15,i;
	unsigned char b,d;
	unsigned long a;
	unsigned char* stenArray;
//	long begin,width,height,offset,i,j;
	unsigned char* grayValue;
	unsigned char* dataArray;
	long xL=31,yL=15; 
	long* xfLen; long* yfLen;
	
	xfLen=&xL; yfLen=&yL;

//	if((fPtr=fopen("mini.bmp","r+")) == NULL){
//			printf("open error!\n");
//				}
	if((fPtr=imgOpen(filename,xfLen,yfLen,mode)) == NULL){
			printf("open error!\n");
				}
	printf("xfLen is: %ld, yfLen is: %ld\n",xL,yL);
	a=0x0436;
	fseek(fPtr,a,SEEK_SET);
//	c=0xaabbccdd;
	d=0xaa;
	grayValue=&d;
	fwrite(grayValue,1,1,fPtr);
	fseek(fPtr,a,SEEK_SET);
	fread(&b,1,1,fPtr);
	printf("%X\n",b);

	dataArray=(unsigned char*)malloc(xLen*yLen*sizeof(char));
    imgRead(fPtr,xBegin,yBegin,xLen,yLen,dataArray);

	grayValue=(unsigned char*)malloc(xLen*yLen*sizeof(char));
	stenArray=grayValue;
	for(i=0;i<xLen*yLen;i++){
		*grayValue=0x2e;
		printf("%X ",*grayValue);
		grayValue++;
	}
	rewind(fPtr);
	imgWrite(fPtr,xBegin,yBegin,xLen,yLen,stenArray);
	printf("\nHello World!\n");

	rewind(fPtr);

//	dataArray=(char*)malloc(xLen*yLen*sizeof(char));
    imgRead(fPtr,xBegin,yBegin,xLen,yLen,dataArray);
	printf("Hello World!\n");
	fclose(fPtr);
	return 0;
}*/

long  imgWrite(FILE* writePtr, long xBegin,long yBegin, long xLen, long yLen,
		          unsigned char* stenArray)
{
	long begin,width,height,offset,i,j;
	unsigned char* grayValue;
	unsigned char noUseByte=0;

   //read the begin location of image data
	fseek(writePtr,10,SEEK_SET);
	fread(&begin,sizeof(long),1,writePtr);
	//read the width and length of image
	fseek(writePtr,18,SEEK_SET);
    fread(&width,sizeof(long),1,writePtr);
	fread(&height,sizeof(long),1,writePtr);
    //seek to the begining location
	fseek(writePtr,begin,SEEK_SET);
	if((xBegin>=width)||(yBegin>=height)){
		printf("the gaved begin location is out of range!\n");
		return -1;
	}
	if(((xBegin+xLen)>width)||((yBegin+yLen)>height)){
		printf("the size of required data is out of range!\n");
		return -1;
	}
	if((width%4)==0)
		offset=begin+width*yBegin + xBegin;
	else
		offset=begin+(width+(4-width%4)) * yBegin + xBegin;
	fseek(writePtr,offset,SEEK_SET);

	//write the data
	grayValue=stenArray;
	//offset=width-xLen;
	for(i=0;i<yLen;i++){
		/*for(j=0;j<xLen;j++){
			fwrite(grayValue,sizeof(char),1,writePtr);
			grayValue++;
		}*/
		fwrite(grayValue,1,xLen,writePtr);
		grayValue += xLen;
//		fwrite(grayValue,sizeof(char),xLen,writePtr);
		if((width%4)!=0){
			if((xBegin+xLen)==width)
				//fwrite(&noUseByte,sizeof(char),4-width%4,writePtr);
				for(j=0;j<(4-width%4);j++)
					fwrite(&noUseByte,sizeof(char),1,writePtr);
			offset=offset+width+(4-width%4);
		}
		else offset=offset+width;
		fseek(writePtr,offset,SEEK_SET);
//		grayValue += xLen;
	}
	return 0;
}

long  imgRead(FILE* readPtr, long xBegin, long yBegin, long xLen, long yLen,
		          unsigned char* stenArray)
{	
	long begin,width,height,offset,i;
	unsigned char* grayValue;

    /*//read the begin location of image data*/
	fseek(readPtr,10,SEEK_SET);
	fread(&begin,sizeof(long),1,readPtr);
	/*//read the width and length of image*/
	fseek(readPtr,18,SEEK_SET);
        fread(&width,sizeof(long),1,readPtr);
	fread(&height,sizeof(long),1,readPtr);
    /*//seek to the begining location*/
	fseek(readPtr,begin,SEEK_SET);
	if((xBegin>=width)||(yBegin>=height)){
		printf("the gaved begin location is out of range!\n");
		return -1;
	}
	if(((xBegin+xLen)>width)||((yBegin+yLen)>height)){
		printf("the size of required data is out of range!\n");
		return -1;
	}
	if((width%4)==0)
		offset=begin+width*yBegin + xBegin;
	else
		offset=begin+(width+(4-width%4)) * yBegin + xBegin;
	fseek(readPtr,offset,SEEK_SET);

	//read the data
	grayValue=stenArray;

	for(i=0;i<yLen;i++){
		/*for(j=0;j<xLen;j++){
		fread(&tmp,sizeof(char),1,readPtr);
		
		*grayValue=tmp;
		grayValue++;
		}*/
		fread(grayValue,1,xLen,readPtr);
		grayValue += xLen;
		
		/*printf("line %ld\n",i);*/
		if((width%4)==0)offset=offset+width;
		else offset=offset+width+(4-width%4);
		fseek(readPtr,offset,SEEK_SET);
	}
	return 0;
}
	

FILE* imgOpen(char* filename, long* xfLen, long* yfLen,long type)
{
	FILE* openPtr;
	FILE* tmpPtr;
	char* tmpChar="BM";
	long imgSize,tmp;
	long i,c;

	if(type==0){
		if((openPtr=fopen(filename,"rb+")) == NULL){
			printf("open error!\n");
			return NULL;
		}
		else{
			tmpPtr=openPtr;
			fseek(tmpPtr,18,SEEK_SET);//get the width and length of image
			fread(xfLen,sizeof(long),1,tmpPtr);
			fread(yfLen,sizeof(long),1,tmpPtr);
		}
	}
	else if(type==1){
			if((openPtr=fopen(filename,"wb+")) == NULL) return NULL;
			tmpPtr=openPtr;
			fwrite(tmpChar,sizeof(char),2,tmpPtr);//write the "BM"
            //computing the image size
			if(*xfLen%4==0)
				imgSize= *xfLen * *yfLen + 256*4 +54; //+2
			else
			    imgSize= (*xfLen + (4-*xfLen%4))* *yfLen + 256*4 +54; //+2
			fwrite(&imgSize,sizeof(long),1,tmpPtr);//store the image size
			tmp=0;
			fwrite(&tmp,sizeof(long),1,tmpPtr);
			tmp=0x0436;
			//store the begin offset of image data
			fwrite(&tmp,sizeof(long),1,tmpPtr);
			tmp=0x28;
			fwrite(&tmp,sizeof(long),1,tmpPtr);
			//store the width and length of image
			fwrite(xfLen,sizeof(long),1,tmpPtr);
			fwrite(yfLen,sizeof(long),1,tmpPtr);
			//store the bits of each pixel and number of plane
			tmp=0x080001;
			fwrite(&tmp,sizeof(long),1,tmpPtr);
			//whether compress and real size of image data(always 0 here)
			tmp=0;
			fwrite(&tmp,sizeof(long),1,tmpPtr);
			fwrite(&tmp,sizeof(long),1,tmpPtr);
			//store the revolusion,pixels per meter in x and y direction
			tmp=0x0B12;
			fwrite(&tmp,sizeof(long),1,tmpPtr);
			fwrite(&tmp,sizeof(long),1,tmpPtr);
			//store the colors of image and status of color important(always 0 here)
			tmp=0;
			fwrite(&tmp,sizeof(long),1,tmpPtr);
			fwrite(&tmp,sizeof(long),1,tmpPtr);
			//store the RGBquad
			c=0;
			for(i=0;i<256;i++){
				fwrite(&c,sizeof(long),1,tmpPtr);
				c+=0x00010101;
			}	
		}
		 else return NULL;
	return openPtr;
}
void
imgClose(FILE* fPtr)
//	FILE *fPtr;
{
	fclose(fPtr);
}
/*
 *  imgGet
 *  get image data from memory
 
long
/*imgGet(unsigned char *sImage, long xBegin,long yBegin,long xLen,long yLen,
	   long sWidth,long sHeight,unsigned char *stenArray)
//	unsigned char *sImage;
//	long xBegin,yBegin;		
//	long xLen,yLen;			
//	long sWidth,sHeight;		
//	unsigned char *stenArray;
{
        int tx,ty;
	int locBegin;		  
  	locBegin = yBegin*sWidth+xBegin;		
	sImage += locBegin;			        
	for(ty = 0;ty <yLen-1;ty++) {			
		for(tx = 0;tx <xLen;tx++) {				
			*stenArray = *sImage;					
			stenArray++;					
			sImage++;				
		}				
		sImage += sWidth-xLen;		
	}	
	for(tx = 0;tx < xLen;tx++ ) {	
		*stenArray = *sImage;				
		stenArray++;				
		sImage++;			
	}			
	return 0;						
}	
/*
 * imgPut
 /*
long
imgPut(unsigned char *tImage,long xBegin,long yBegin,long xLen,long yLen,
	   long tWidth,long tHeight,unsigned char *stenArray)
//	unsigned char *tImage;
//	long xBegin,yBegin;		
//	long xLen,yLen;			
//	long tWidth,tHeight;		
//	unsigned char *stenArray;
{
        int tx,ty;
	int locBegin;		  
  	locBegin = yBegin*tWidth+xBegin;		
	tImage += locBegin;			        
	for(ty = 0;ty <yLen;ty++) {			
		for(tx = 0;tx <xLen;tx++) {				
			*tImage = *stenArray;					
			stenArray++;					
			tImage++;
		}				
		tImage += tWidth-xLen;		
	}
	return 0;	
}

*/

⌨️ 快捷键说明

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