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

📄 basica.cpp

📁 图像处理软件,功能比较基础
💻 CPP
字号:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

#include "common.h"
#include "basic.h"
/*****************************   fspace_1d  **************************/
/*  To allocation a 1_dimension dynamic array  */
/*    col -- the number of data                */
/*    length -- the length per datum           */
void *fspace_1d(int col,int length)
{
  void *b;

  b = (void *)calloc(col,length);
  if(!b) return NULL;
  return(b);
}

/*************      fspace_2d    ***************************************/
/*  To allocation a 2_dimension dynamic array  */
/*     row -- the row number                   */
/*     col -- the column number                */
/*     length -- the length per element        */
void **fspace_2d(int row,int col,int lenth)
{
  int i;
  void **b;

  b = (void **)calloc(row,sizeof(void *));
  if(!b) return NULL;
  for(i=0;i<row;i++)
  {
    b[i] = (void *)calloc(col,lenth);
    if (!b[i]) return NULL;
    }
  return(b);
}

/*******************************  ffree_2d   ****************************/
/*  To free a 2_dimension dynamic array  */
void ffree_2d(void **a,int row)
{
  int i;

  for(i=0;i<row;i++) free(a[i]);
  free(a);
}

/*****************   wfread1  ***********************/
/*  To open data file to read from, the file name is a parameter */
/*  NOT USED  */
FILE *wfread1(int *row,int *col,int *type,char *s)
{
  FILE *fp;
  unsigned char head_byte[64];

  if ((fp=fopen(s,"rb"))==NULL) return NULL;

  fread(head_byte,64,1,fp);
  *col = head_byte[4]+head_byte[5]*256;   /* column size   */
  *row = head_byte[6]+head_byte[7]*256;   /* row size      */
  /*  data type. 1:unsigned char,  2:int  3:float  4:double   */
  *type = head_byte[14]+head_byte[15]*256;

  return(fp);
}

/********************  INPUTIMAGEWITHNAME  **********************/
/*  Input a image with name FileName to array Image             */
unsigned char **InputImageWithName(int *Row,int *Col,char *FileName)
{
  int           RowNo,Type;
  unsigned char **Image;
  FILE          *fp;

  fp = wfread1(Row,Col,&Type,FileName);
  if(!fp) return NULL;
  Image = (unsigned char **)fspace_2d(*Row,*Col,sizeof(unsigned char));
  if(!Image) return NULL;
  for(RowNo=0;RowNo<*Row;RowNo++)
    fread(Image[RowNo],sizeof(unsigned char),*Col,fp);
  fclose(fp);

  return(Image);
}

/********************  OUTPUTIMAGEWITHNAME  **********************/
/*  Output a image with name FileName to array Image             */
/*  Notes: Image must be a space allocated pointer               */
void OutputImageWithName(unsigned char **Image,int Row,int Col,char *FileName)
{
  short        RowNo;
  FILE       *fp;

  fp = wfwrite1(Row,Col,1,FileName);
  for(RowNo=0;RowNo<Row;RowNo++)
    fwrite(Image[RowNo],sizeof(unsigned char),Col,fp);
  fclose(fp);
}

/**************************  wfwrite1    *****************************/
/* To open a file to write data in */
FILE *wfwrite1(int row,int col,int type,char *filen)
{
  short i;
  unsigned char head_byte[64];
  FILE *fp;

  if ((fp=fopen(filen,"wb"))==NULL)  return NULL;

  for(i=0;i<64;i++)  head_byte[i] = 0;

  head_byte[0] = 'I';  /* image type. 'I':b/w image, 'C':color image */
  head_byte[1] = 'M';
  head_byte[2] = head_byte[3] = 0;  /* comment space */
  head_byte[4] = col % 256;
  head_byte[5] = col / 256;  /* column size */
  head_byte[6] = row % 256;
  head_byte[7] = row / 256;  /* row size */
  head_byte[8] = head_byte[9] = 0;  /* column start */
  head_byte[10] = head_byte[11] = 0;  /* row start */
  head_byte[12] = head_byte[13] =0;
  head_byte[14] = type % 256;
  head_byte[15] = type / 256;  /* data type. 1:unsigned char, 2:int,   */
                               /*            3:float,         4:double */
  fwrite(head_byte,64,1,fp);

  return(fp);
}

unsigned char **ReadBMPFile(int *Row,int *Col,char *FileName)
{
	FILE *fp;
	BITMAPFILEHEADER BmpFileHeader;
	BITMAPINFOHEADER BmpInfoHeader;
	short iRow,iCol;
	unsigned char **Image;
	

	short i,j;
	short bytes,linebytes,skip;
	unsigned char d1,d2,d3;
	long off;
	
	 /*attempt to open the file*/
    if ((fp=fopen(FileName,"rb"))==NULL) return NULL;
//    if (fread(&BmpFileHeader,1,sizeof(BITMAPFILEHEADER),fp)!=sizeof(BITMAPFILEHEADER))
	if (fread(&BmpFileHeader.bfType,1,sizeof(unsigned short),fp)!=sizeof(unsigned short)) return NULL;
	if (fread(&BmpFileHeader.bfSize,1,sizeof(unsigned long),fp)!=sizeof(unsigned long)) return NULL;
	if (fread(&BmpFileHeader.bfReserved1,1,sizeof(unsigned short),fp)!=sizeof(unsigned short)) return NULL;
	if (fread(&BmpFileHeader.bfReserved2,1,sizeof(unsigned short),fp)!=sizeof(unsigned short)) return NULL;
	if (fread(&BmpFileHeader.bfOffBits,1,sizeof(unsigned long),fp)!=sizeof(unsigned long)) return NULL;

	if (fread(&BmpInfoHeader,1,sizeof(BITMAPINFOHEADER),fp)!=sizeof(BITMAPINFOHEADER))
		return NULL;

	iCol=(short)BmpInfoHeader.biWidth;
	iRow=(short)BmpInfoHeader.biHeight;

	Image = (unsigned char **)fspace_2d(iRow,iCol,sizeof(unsigned char));
	if(BmpInfoHeader.biBitCount==8) {
		bytes=iCol;
		linebytes=((bytes+3)/4)*4;
		skip=linebytes-bytes;
		for (i=0;i<iRow;i++)
		{   
			off=(long)(linebytes)*(long)(i+1);
			fseek(fp,-off,SEEK_END);
			fread(Image[i],iCol,1,fp);
			}
	}
	else if(BmpInfoHeader.biBitCount==24) {
		 bytes=iCol*3;
		 linebytes=((bytes+3)/4)*4;
		 for (i=0;i<iRow;i++)
		 {    j=0;
			  off=(long)(linebytes)*(long)(i+1);
		      fseek(fp,-off,SEEK_END);
			  while (j<iCol)
			  {      fread(&d1,1,1,fp);
			         fread(&d2,1,1,fp);
					 fread(&d3,1,1,fp);
                     Image[i][j]=(unsigned char)(d1*0.299+d2*0.587+d3*0.114);
					 j++;
			  }
		 }
	}

	fclose(fp);
	*Row = iRow;
	*Col = iCol;
	return Image;
}

void OutputBMPWithName(unsigned char **image, int Row, int Col,char *FileName)
{
	FILE *fp;
	BITMAPFILEHEADER FileHeader;
	BITMAPINFOHEADER InfoHeader;
	RGBQUAD bmiColors[256];

	short i;
	short linebytes,skipstep,skip[3];
	long off;

    for (i=0;i<3;i++)
         skip[i]=0;

    FileHeader.bfType=(short)0x4d42;
    FileHeader.bfReserved1=0;
    FileHeader.bfReserved2=0;

    InfoHeader.biSize=40;
    InfoHeader.biPlanes=1;
    InfoHeader.biCompression=0;
    InfoHeader.biXPelsPerMeter=0;
    InfoHeader.biYPelsPerMeter=0;
    InfoHeader.biClrUsed=0;
    InfoHeader.biClrImportant=0;

    for (i=0;i<256;i++)
    {    bmiColors[i].rgbRed=(unsigned char)i;
	     bmiColors[i].rgbGreen=(unsigned char)i;
		 bmiColors[i].rgbBlue=(unsigned char)i;
		 bmiColors[i].rgbReserved=0;
	}

   /*attempt to open the file*/
    if ((fp=fopen(FileName,"wb"))==NULL) return ;

    linebytes=((Col+3)/4)*4;
    skipstep=linebytes-Col;

	FileHeader.bfSize=Row*Col+54+256*4;
	FileHeader.bfOffBits=54+256*4;

	InfoHeader.biWidth=Col;
	InfoHeader.biHeight=Row;
	InfoHeader.biBitCount=8;
	InfoHeader.biSizeImage=Row*Col;


    //fwrite(&FileHeader,sizeof(BITMAPFILEHEADER),1,fp);
	fwrite(&FileHeader.bfType,1,sizeof(unsigned short),fp);
	fwrite(&FileHeader.bfSize,1,sizeof(unsigned long),fp);
	fwrite(&FileHeader.bfReserved1,1,sizeof(unsigned short),fp);
	fwrite(&FileHeader.bfReserved2,1,sizeof(unsigned short),fp);
	fwrite(&FileHeader.bfOffBits,1,sizeof(unsigned long),fp);

//	fwrite(&FileHeader,14,1,fp);
	fwrite(&InfoHeader,sizeof(BITMAPINFOHEADER),1,fp);
	fwrite(bmiColors,sizeof(RGBQUAD),256,fp);

	for (i=Row-1;i>=0;i--)
	 {    off=(long)(Col)*(long)(i+1);
		  fwrite(image[i],1,Col,fp);
		  fwrite(skip,1,skipstep,fp);
	 }    
	fclose(fp);
	}


/* chang the extented name by increasing 1 */
/* return 1 for success, 0 for error       */
/*                                         */
/* Writen by ZUO Zhengrong                 */
int ChangeFileName(char *filename)
{
  char *pExtName=NULL;
  int nFileLen;	//the length of file name
  int bExtName;	// the flag of the extension name being digital
  int i;
  int start,end,current;

  nFileLen = strlen(filename);
  pExtName = strchr(filename,'.');

  bExtName = 1;
  if(pExtName)  {  //verify if the extension name is digital
    for(i=nFileLen-(&filename[nFileLen-1] - pExtName); i<nFileLen; i++)
	if(filename[i]>'9' || filename[i]<'0') bExtName = 0;
  }
  else bExtName = 0;  //if no extension name
	

  if(bExtName) 
    end = nFileLen-1;
  else 
  {
	/*check if the former name have digital number*/
    if(pExtName==NULL) end = nFileLen-1;  /*end is the pointer to the last digital character*/
    else end = pExtName - filename -1;
  }

  while(filename[end]>'9' || filename[end]<'0') 
  {
    end--;
    if(end==0) return 0;
  }

  start = end;
  while(filename[start]<='9' && filename[start]>='0')  
  {
    start--;
    if(start<0) break;
  }
  start += 1;

  current = end;
  while(1) 
  {
    if(filename[current]=='9') 
    {
       filename[current]='0';
       current--;
       if(current<start) break;
    }
    else 
    {
       filename[current]++;
       break;
    }
  }
  return 1;
}

⌨️ 快捷键说明

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