图像处理 (第一次优化)增加了 二值图像 4位 8位 图像的读取 增加霓虹 浮雕效果

来源:互联网 发布:配电网设计软件 博世 编辑:程序博客网 时间:2024/04/30 03:26




这是代码 全部奉献出来

#include <graphics.h>
#include <time.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include "bitmap.h"
void display(byte *temp,struct BMP_img img,int x,int y);                               //打开图片显示原图
int read_img(FILE *infile, struct BMP_img *img);                                       //把图像信息放到结构体中
void writebmp(byte *path,byte *temp,struct BMP_img img);                               //生成图片 都是24位的
void zhifang(byte *temp,struct BMP_img img,int x,int y);                               //直方图
void displaygray(BMP_img img,int x,int y);              //显示灰度和直方图
void displaytwo(BMP_img img,BYTE k,int x,int y);            //显示二值图像
void Thining(struct BMP_img img,int x,int y);             //图像细化
int GrayEqualize(BMP_img img,int x,int y);                 //均衡
int edgesob(struct BMP_img img,int x,int y);             //边缘检测 sob算子
void edgeroberts(struct BMP_img img,int x,int y);                                      //roberts算子
int edgetidu(struct BMP_img img,int x,int y);                                          //边缘检测梯度算子
void junhenghua(struct BMP_img img,int x,int y);                                       //直方图灰度均衡化
int Dilation(struct BMP_img img,int type,int num,int x,int y);                         //膨胀 num=3,5,7 type 0 水平
int Erosion(struct BMP_img img,int type,int num,int x,int y);                          //腐蚀 num 3,5,7
int WindowTranslation(struct BMP_img img,int type,int num,int down,int up,int x,int y);//窗口变换
int LogTranslation(struct BMP_img img,int k,int x,int y);                              //对数变换
int ThresholdTranslation(struct BMP_img img,byte bytThreshold,int x,int y);            //图像阈值变换
int GrayStretch(struct BMP_img img,int pt1x,int pt1y,int pt2x,int pt2y,int x,int y);   //图像灰度拉伸变换
int LineTranslation(struct BMP_img img,double k,double d,int x,int y);                 //图像线性灰度变换
int fudiao(struct BMP_img img,int x,int y);                                            //浮雕
int nihong(struct BMP_img img,int x,int y);                                            //霓虹
void main()
{   
 int i=0;
 char *path="c:\\416.bmp";//输入要打开图片的路径;
 
 char *pathname=".bmp";
 FILE *f1;
 BMP_img img1;

   // printf("请输入图片路径\n");
  // scanf("%s",path);
 
 if((f1=fopen(path,"rb"))==NULL)
 {
    printf( "\n 不能打开 %s \n", path);
    exit(-1);
 }


 if((read_img(f1, &img1))==0)
 {
  printf("\n 图像读取失败 \n");
  fclose(f1);
  exit(-1);
 }
 
 
     initgraph(800,800);
     display(img1.image,img1,0,0); 
  displaygray(img1,0,1);
     junhenghua(img1,0,2);
     displaytwo(img1,120,0,3);                                    //图像阈值变换
     GrayStretch(img1,10,10,100,100,1,0);                         //图像灰度拉伸变换
  LineTranslation(img1,2,10,1,1);                              //图像线性灰度变换

   
 
     edgeroberts(img1,1,2);  
     edgesob(img1,1,3);////
     edgetidu(img1,2,0);  
  fudiao(img1,2,1);
  nihong(img1,2,2); 
  Thining(img1,2,3);  
  GrayEqualize(img1,3,0);
     Dilation(img1,0,3,3,1);
     WindowTranslation(img1,0,3,50,20,3,2);
     LogTranslation(img1,10,3,3);
     Erosion(img1,1,5,0,0);
   
  fclose(f1);

  while(1);
}

#include <time.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <graphics.h>
#include "math.h"
//#include "liujia2.cpp"
typedef unsigned long       DWORD;
typedef int                 BOOL;
typedef unsigned char       BYTE;
typedef unsigned short      WORD;
typedef float               FLOAT;
typedef unsigned char       byte;
  //char pathout[80]="g:\\temp\\";
struct BMP_img
{
    WORD  bfType;
    DWORD size;
 DWORD reser;
 DWORD header_length;
    DWORD infoheader_length;
 DWORD width;
 DWORD height;
 WORD  biplanes;
 WORD  bmp_type;        /* 8bit 24bit; */
 DWORD compres;
 DWORD datasize;
    DWORD bixpm;
 DWORD biypm;
 DWORD clrused;
 DWORD  relclrused;
    BYTE *image;
    byte *header_info;
 
 
 
};
struct RGB
{
 byte bitb;
 byte bitg;
 byte bitr;
 byte  re;
};
void writebmp(const char *name,byte *temp,struct BMP_img img)
{
 
    FILE *infile;
    DWORD lineBytes=(img.width*24+31)/32*4;
 char pathout[80]="g:\\temp\\";
    strcat(pathout,name);
    if((infile=fopen(pathout,"wb"))==NULL)
    {
        printf("\n不能创建%s图\n",pathout);
        exit(-1);
    }
 fwrite(&img.bfType,sizeof(WORD),1,infile);
    img.size=54+img.height*lineBytes;
    fwrite(&img.size,sizeof(DWORD),1,infile);
 
 fwrite(&img.reser,sizeof(DWORD),1,infile);
    img.header_length=54;
 fwrite(&img.header_length,sizeof(DWORD),1,infile);
 img.infoheader_length=40;
 fwrite(&img.infoheader_length,sizeof(DWORD),1,infile);
   
 
 
 fwrite(&img.width, sizeof(DWORD), 1, infile);
 fwrite(&img.height, sizeof(DWORD), 1, infile);
 
 fwrite(&img.biplanes, sizeof(WORD), 1, infile);
 img.bmp_type=24;
    fwrite(&img.bmp_type, sizeof(WORD), 1, infile);
 
 fwrite(&img.compres, sizeof(DWORD), 1, infile);
 
    fwrite(&img.datasize, sizeof(DWORD), 1, infile);
 
 fwrite(&img.bixpm, sizeof(DWORD), 1, infile);
 fwrite(&img.biypm, sizeof(DWORD), 1, infile);
 fwrite(&img.clrused, sizeof(DWORD), 1, infile);
 
 fwrite(&img.relclrused, sizeof(DWORD), 1, infile); 
    fseek(infile,54*sizeof(byte),SEEK_SET);
   
    if(fwrite(temp,img.height*lineBytes*sizeof(BYTE),1,infile)!=1)
    {
        printf("不能写入%s图\n",pathout);
        fclose(infile);
       
    }
 
    fclose(infile);
 
}
int read_img(FILE *infile, BMP_img *img)
{
     // unsigned char n=0;
      DWORD i,j,n,bitcolor;
 
   
 
      byte k,m,q;
     fread(&img->bfType,sizeof(WORD),1,infile);
      //printf("\n打开的图为 %d",img->bfType);
  fread(&img->size,sizeof(DWORD),1,infile);
      printf("\n图片大小为:%d",img->size);
  fread(&img->reser,sizeof(DWORD),1,infile);
      //printf("\n保留位:");
  fread(&img->header_length,sizeof(DWORD),1,infile);
      printf("\n信息长度:%d",img->header_length);
  fread(&img->infoheader_length,sizeof(DWORD),1,infile);
   

    
     fread(&img->width, sizeof(DWORD), 1, infile);
  fread(&img->height, sizeof(DWORD), 1, infile);
      printf( "\n宽度=%d  高度=%d ", img->width, img->height);
  fread(&img->biplanes, sizeof(WORD), 1, infile);
     fread(&img->bmp_type, sizeof(WORD), 1, infile);
      printf("\n位图位数=%d ", img->bmp_type);
  fread(&img->compres, sizeof(DWORD), 1, infile);
   if(img->compres==0) {
   printf("\nbmp图片为非压缩!");
   }
  fread(&img->datasize, sizeof(DWORD), 1, infile);
      printf("\n位图数据大小=%d ",img->datasize);           
  fread(&img->bixpm, sizeof(DWORD), 1, infile);
     fread(&img->biypm, sizeof(DWORD), 1, infile);
     fread(&img->clrused, sizeof(DWORD), 1, infile);
      printf("\n实际使用颜色数=%d ",img->clrused);     
  fread(&img->relclrused, sizeof(DWORD), 1, infile); 

 
  DWORD lineBytes=(img->width*img->bmp_type+31)/32*4;
     printf("\n每行字节数=%d ",lineBytes);    
  DWORD line24=(img->width*24+31)/32*4;
 

 

 byte *temp=(BYTE*)malloc(img->height*lineBytes*sizeof(BYTE));
 
 memset(temp,0x00,img->height*lineBytes*sizeof(BYTE));
 
    if(img->bmp_type<=8)
    { 
  if(img->bmp_type==8)
   bitcolor=256;
  if(img->bmp_type==4)
   bitcolor=16;
  if(img->bmp_type==1)
   bitcolor=2;

 
          RGB *bitmap=new RGB[bitcolor];
          img->image=(unsigned char *)malloc(sizeof(unsigned char)*(line24*img->height));
      memset(img->image,0x00,sizeof(byte)*line24*img->height);
    
        if(img->image==NULL)
        {
            fprintf(stderr, "\n Allocation error for temp in read_bmp() \n");
            return 0;
        } 
    
  fseek(infile,0x36, SEEK_SET);
  fread(bitmap,sizeof(RGB),bitcolor,infile);
 
  fseek(infile, img->header_length, SEEK_SET);

        fread(temp, sizeof(unsigned char),lineBytes*img->height, infile);
      
 

 


        if(temp==NULL)
   printf("\n读取失败\n");
           
      
  
     if(img->bmp_type==8)                                                 //256
     {
      for(i=0;i<img->height;i++)
          for(j=0,n=0;n<line24,j<img->width;n+=3,j++)
       {

   
      img->image[i*line24+n]=bitmap[temp[i*img->width+j]].bitb;
         img->image[i*line24+n+1]=bitmap[temp[i*img->width+j]].bitg;
      img->image[i*line24+n+2]=bitmap[temp[i*img->width+j]].bitr;
       }
   
   
              // putpixel(j,img->height-i,RGB(bitmap[temp[i*img->width+j]].bitr,bitmap[temp[i*img->width+j]].bitg,bitmap[temp[i*img->width+j]].bitb));
    }
          if(img->bmp_type==4)                                               //16
     {
      for(i=0;i<img->height;i++)
          for(j=0,n=0;n,j<img->width;n+=6,j++) 
       {
        img->image[i*line24+n]=bitmap[temp[i*lineBytes+j]&0x0f].bitb;
           img->image[i*line24+n+1]=bitmap[temp[i*lineBytes+j]&0x0f].bitg;
        img->image[i*line24+n+2]=bitmap[temp[i*lineBytes+j]&0x0f].bitr;
        img->image[i*line24+n+3]=bitmap[(temp[i*lineBytes+j]&0xf0)>>4].bitb;
        img->image[i*line24+n+4]=bitmap[(temp[i*lineBytes+j]&0xf0)>>4].bitg;
        img->image[i*line24+n+5]=bitmap[(temp[i*lineBytes+j]&0xf0)>>4].bitr; 
       }
     }
     if(img->bmp_type==1)
     {
      for(i=0;i<img->height;i++)
      {
       n=0;
           for(j=0;j<lineBytes;j++) 
     {
                                
                                 k=temp[i*lineBytes+j];
                           for(m=0;m<8;m++)
         {
          
           if((q=k&0x01)==1)
        { 
        // putpixel(n,img->height-i,RGB(255,255,255));

             img->image[i*line24+n]=0xff;
             img->image[i*line24+n+1]=0xff;
                                  img->image[i*line24+n+2]=0xff;
        }
            if((q=k&0x01)==0)
        {
                              //  putpixel(n,img->height-i,RGB(0,0,0));
         img->image[i*line24+n]=0x00;
         img->image[i*line24+n+1]=0x00;
                                    img->image[i*line24+n+2]=0x00;
        }
           k=(k>>1);
        n=n+3;
         }
       }
      }
     }
 } 
       
// writebmp("原图.bmp",img->image,*img);       
       
/* for(i=0;i<img->height;i++)
  for(n=0,j=0;n<img->width*3,j<img->width;j++,n+=3)
   putpixel(j,img->height-i,RGB( img->image[i*line24+n], img->image[i*line24+n+1], img->image[i*line24+n+2]));*/


   free(temp); 
 //  free (bitmap);
 
     if(img->bmp_type==24)
    {
        img->image=(unsigned char *)malloc(sizeof(unsigned char)*((lineBytes)*img->height));
        if(img->image==NULL)
        {
            fprintf(stderr, "\n Allocation error for temp in read_bmp() \n");
            return 0;
        }

        fseek(infile, img->header_length, SEEK_SET);
        fread(img->image, sizeof(unsigned char), (lineBytes)*img->height, infile);
        /*if(fread(img->image, sizeof(unsigned char), (lineBytes)*img->height, infile)!=(size_t)((lineBytes)*img->height))
        {
            if(feof(infile)) fprintf(stderr, "\n Premature end of file %s \n", infile);
            else             fprintf(stderr, "\n File read error %s \n", infile);
          return 0;
        } */


    }

    return 1;
}

 


void zhifang(byte *temp,struct BMP_img img,int x,int y)//要求temp内为灰度数据;
{
  int lineBytes=(img.width*img.bmp_type+31)/32*4;
  DWORD i,j,n,k;
  byte dat[256];
 
   for(i=0;i<img.height;i++)
   {
    for(j=0,n=0;n<img.width*3,j<img.width;n+=3,j++)
   
    { 
      dat[*(temp+i*lineBytes+n)]++;
   //putpixel(j+x*img.width,y*img.height+img.height-i,RGB(*(temp+i*lineBytes+n+2),*(temp+i*lineBytes+n+1),*(temp+i*lineBytes+n)));
    }
   }
 
  for(k=0;k<256;k++)
  {
   for(i=0;i<dat[k];i++)
      putpixel(k+(x+1)*img.width,y*img.height+img.height-i,RGB(100,100,100));
 
  }
 
}


void display(byte *temp,struct BMP_img img,int x,int y)
{
    DWORD i,j,n;
    DWORD lineBytes=(img.width*24+31)/32*4;

       for(i=0;i<img.height;i++)
    {
        for(j=0,n=0;n<img.width*3,j<img.width;n+=3,j++)
           
        { 

         putpixel(j+x*img.width,y*img.height+img.height-i,RGB(*(temp+i*lineBytes+n+2),*(temp+i*lineBytes+n+1),*(temp+i*lineBytes+n)));
        }
    }
      //free(temp);
}
void displaygray(BMP_img img,int x,int y)
{   
    DWORD i,j,n;
   
    double gray=0;
    DWORD lineBytes=(img.width*24+31)/32*4;

   byte *temp=(byte *)malloc(sizeof(byte)*img.height*lineBytes);
       if(temp==NULL)
     printf("\nerror\n");
       for(i=0;i<img.height;i++)
   {
        for(j=0,n=0;n<img.width*3,j<img.width;n+=3,j++)

     { 
       gray= 0.299*(float)(img.image[lineBytes*i+n+2])+0.578*(float)(*(img.image+lineBytes*i+n+1))+0.114*(float)(*(img.image+lineBytes*i+n));
       temp[i*lineBytes+n]=(byte)gray;
       temp[i*lineBytes+n+1]=(byte)gray;
       temp[i*lineBytes+n+2]=(byte)gray;
           
     }
   }
   display(temp,img,x,y);
    zhifang(temp,img,x,y);
   writebmp("gray.bmp",temp,img);
   free(temp);

}
void displaytwo(BMP_img img,BYTE k,int x,int y)
{
    DWORD i,j,n;
  
    double gray=0;
  DWORD lineBytes=(img.width*24+31)/32*4;
    byte *temp=(byte *)malloc(sizeof(byte)*img.height*lineBytes);
    byte temp1;
   
    for(i=0;i<img.height;i++)
    {
        for(j=0,n=0;n<img.width*3,j<img.width;n+=3,j++)
           
        { 

         gray= 0.299*(float)(img.image[lineBytes*i+n+2])+0.578*(float)(*(img.image+lineBytes*i+n+1))+0.114*(float)(*(img.image+lineBytes*i+n));
         temp1=(byte)gray;
         temp[i*lineBytes+n+0]=255*(temp1/k);
         temp[i*lineBytes+n+1]=255*(temp1/k);
         temp[i*lineBytes+n+2]=255*(temp1/k);
       
        }
    }   
    display(temp,img,x,y);
    writebmp("bmptwo.bmp",temp,img);
       free(temp);
 }


//图像细化
void Thining(struct BMP_img img,int xx,int yy)
{

 static int erasetable[256]={
  0,0,1,1,0,0,1,1,1,1,0,1,1,1,0,1,1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,1,
  0,0,1,1,0,0,1,1,1,1,0,1,1,1,0,1,1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,1,
  1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1,1,0,0,1,1,0,0,1,1,0,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,1,1,0,0,1,1,1,1,0,1,1,1,0,1,1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,1,
  0,0,1,1,0,0,1,1,1,1,0,1,1,1,0,1,1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,
  1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,
  1,1,0,0,1,1,0,0,1,1,0,1,1,1,0,0,1,1,0,0,1,1,1,0,1,1,0,0,1,0,0,0
 };

   
  DWORD lineBytes=(img.width*24+31)/32*4;

  DWORD dwWidth=0;
     dwWidth=img.width;
  DWORD dwHeight=0;
     dwHeight=img.height;
  BYTE *temp=(BYTE*)malloc(dwHeight*dwWidth*sizeof(BYTE));
                                          //DWORD dwLineBytes=(dwWidth*24+31)/32*4;
  BYTE *temp1=(byte *)malloc(sizeof(byte)*lineBytes*img.height);
  DWORD i,j,n;
 
  double gray;
  BYTE g=0;
  BYTE zs=0,s=0,ys=0,z=0,y=0,zx=0,x=0,yx=0;
  int num=0;

 

   
   
  for(i=0;i<img.height;i++)
  {
      for(j=0,n=0;n<img.width*3,j<img.width;n+=3,j++)
         
      { 
         
         gray= 0.299*(float)(img.image[lineBytes*i+n+2])+0.578*(float)(*(img.image+lineBytes*i+n+1))+0.114*(float)(*(img.image+lineBytes*i+n));
            temp1[i*img.width+j]=(byte)gray;
        }
    }   

 


 
 if(!temp)
 {
  exit(-1);
 }
 
 for(i=0;i<dwHeight;i++)
 {
  for(j=0;j<dwWidth;j++)
  {
   g=temp1[dwWidth*i+j];
   if(g<=120)
   {
    temp1[dwWidth*i+j]=(BYTE)0;
   }
   else
   {
    temp1[dwWidth*i+j]=(BYTE)255;
   }
  }
 }

 memcpy(temp,temp1,dwWidth*dwHeight*sizeof(BYTE));

 for(i=1;i<dwHeight-1;i++)
 {
  for(j=1;j<dwWidth-1;j++)
  {
   g=temp1[dwWidth*i+j];
   if(g==0)
   {
    z=temp1[dwWidth*i+j-1];
    y=temp1[dwWidth*i+j+1];
    if(z==255 || y==255)
    {
     zs=temp1[dwWidth*(i+1)+j-1];
     s=temp1[dwWidth*(i+1)+j];
     ys=temp1[dwWidth*(i+1)+j+1];
     zx=temp1[dwWidth*(i-1)+j-1];
     x=temp1[dwWidth*(i-1)+j];
     yx=temp1[dwWidth*(i-1)+j+1];
     num=zs/255+s/255*2+ys/255*4+z/255*8+y/255*16+zx/255*32+x/255*64+yx/255*128;
     if(erasetable[num]==1)
     {
      temp[dwWidth*i+j]=(BYTE)255;
      temp1[dwWidth*i+j]=(BYTE)255;
     }
    }
   }
  }
 }

 for(i=1;i<dwHeight-1;i++)
 {
  for(j=1;j<dwWidth-1;j++)
  {
   g=temp[dwWidth*i+j];
   if(g==0)
   {
    s=temp1[dwWidth*(i+1)+j];
    x=temp1[dwWidth*(i-1)+j];
    if(s==255 || x==255)
    {
     zs=temp1[dwWidth*(i+1)+j-1];
     z=temp1[dwWidth*i+j-1];
     y=temp1[dwWidth*i+j+1];
     ys=temp1[dwWidth*(i+1)+j+1];
     zx=temp1[dwWidth*(i-1)+j-1];
     yx=temp1[dwWidth*(i-1)+j+1];
     num=zs/255+s/255*2+ys/255*4+z/255*8+y/255*16+zx/255*32+x/255*64+yx/255*128;
     if(erasetable[num]==1)
     {
      temp[dwWidth*i+j]=(BYTE)255;
      temp1[dwWidth*i+j]=(BYTE)255;
     }
    }
   }
  }
 }
 /*for(i=0;i<dwHeight;i++)
     for(j=0;j<dwWidth;j++)
 putpixel(j+img.width,img.height+img.height-i,RGB(*(temp+i*dwWidth+j+2),*(temp+i*dwWidth+j+1),*(temp+i*dwWidth+j)));*/
  for(i=0;i<img.height;i++)
      for(j=0,n=0;j<dwWidth,n<dwWidth*3;j++,n+=3){
          temp1[i*lineBytes+n]=temp[i*dwWidth+j];
          temp1[i*lineBytes+n+1]=temp[i*dwWidth+j];
          temp1[i*lineBytes+n+2]=temp[i*dwWidth+j];}

    //memcpy(temp1,temp,dwHeight*dwWidth*sizeof(BYTE));
    display(temp1,img,xx,yy);
    writebmp("thining.bmp",temp1,img);
       free(temp1);
    free(temp);
   
 
}
 //图像灰度均衡
int GrayEqualize(BMP_img img,int x,int y)
{
 
 DWORD height=img.height;
 DWORD width=img.width;
 WORD bitCount=img.bmp_type;
 DWORD lineBytes=(width*24+31)/32*4;
 BYTE *temp=(byte*)malloc(sizeof(byte)*lineBytes*height);
 memcpy(temp,img.image,sizeof(byte)*lineBytes*height);

 BYTE grayMap[256];
 DWORD grayNum[256];

 BYTE BgrayMap[256];
 DWORD BgrayNum[256];
 BYTE GgrayMap[256];
 DWORD GgrayNum[256];
 BYTE RgrayMap[256];
 DWORD RgrayNum[256];

 DWORD i=0;
 DWORD j=0;
 int n=0;
 int m=0;
 BYTE b=0;
 BYTE g=0;
 BYTE r=0;
 long c=0;
 long bc=0,gc=0,rc=0;

 if(temp==NULL)
 {
  return -1;
 }

 for(n=0;n<256;n++)
 {
  grayNum[n]=0;
  grayMap[n]=0;

  BgrayNum[n]=0;
  BgrayMap[n]=0;
  GgrayNum[n]=0;
  GgrayMap[n]=0;
  RgrayNum[n]=0;
  RgrayMap[n]=0;
 }


  for(i=0;i<height;i++)
  {
   for(j=0;j<width*3;j++)
   {
    b=*(temp+lineBytes*(height-1-i)+j);
    j++;
    g=*(temp+lineBytes*(height-1-i)+j);
    j++;
    r=*(temp+lineBytes*(height-1-i)+j);

    BgrayNum[b]++;
    GgrayNum[g]++;
    RgrayNum[r]++;
   }
  }
  for(n=0;n<256;n++)
  {
   bc=0;gc=0;rc=0;
   for(m=0;m<=n;m++)
   {
    bc+=BgrayNum[m];
    gc+=GgrayNum[m];
    rc+=RgrayNum[m];
   }
   BgrayMap[n]=(BYTE)(bc*255/height/width);
   GgrayMap[n]=(BYTE)(gc*255/height/width);
   RgrayMap[n]=(BYTE)(rc*255/height/width);
  }
  for(i=0;i<height;i++)
  {
   for(j=0,n=0;j<width*3;j++,n++)
   {
    b=*(temp+lineBytes*(height-1-i)+j);
    j++;
    g=*(temp+lineBytes*(height-1-i)+j);
    j++;
    r=*(temp+lineBytes*(height-1-i)+j);

    *(temp+lineBytes*(height-1-i)+n)=BgrayMap[b];
    n++;
    *(temp+lineBytes*(height-1-i)+n)=GgrayMap[g];
    n++;
    *(temp+lineBytes*(height-1-i)+n)=RgrayMap[r];
   }
  }
  /*for(i=0;i<height;i++)
      for(j=0,n=0;n<width*3,j<width;n+=3,j++)
 putpixel(j+img.width,img.height+img.height-i,RGB(*(temp+i*lineBytes+n+2),*(temp+i*lineBytes+n+1),*(temp+i*lineBytes+n)));
 */
  display(temp,img,x,y);
  writebmp("grayequlize.bmp",temp,img);
 
  free(temp);
 
  

 return 0;
}
void edgeroberts(struct BMP_img img,int x,int y)
{
 int i,j,n;
    int height=img.height;
    int width=img.width;
 double gray,result;
  DWORD lineBytes=(img.width*24+31)/32*4;
 BYTE *temp=(byte*)malloc(sizeof(byte)*lineBytes*height);
   // memcpy(temp,img.image,sizeof(byte)*lineBytes*height);
    BYTE *temp1=(byte*)malloc(sizeof(byte)*width*height);
     BYTE *temp2=(byte*)malloc(sizeof(byte)*width*height);
   long p[4];
    for(i=0;i<height;i++)
 {
  for(j=0,n=0;n<width*3,j<width;n+=3,j++)
   
  { 
   
   gray= 0.299*(float)(img.image[lineBytes*i+n+2])+0.578*(float)(*(img.image+lineBytes*i+n+1))+0.114*(float)(*(img.image+lineBytes*i+n));
   
            temp1[i*width+j]=(byte)gray;
        }
    }   


 for(i =0; i<=height-1; i++)
 {
  for(j=0;j<=width-1;j++)//由于使用2×2的模板,为防止越界,所以不处理最下边和最右边的两列像素
  {
    
   if(i==height-1)
                temp2[i*width+j]=0;
            else if(j==width-1)
                 temp2[i*width+j]=0;
           
   else{  
  
   p[0] = (byte)*(temp1+i*width+j);
   p[1] = (byte)*(temp1+i*width+j+1);
   p[2] = (byte)*(temp1+(i+1)*width+j);
   p[3] = (byte)*(temp1+(i+1)*width+j+1);
   
  
   result = sqrt(( p[0] - p[3] )*( p[0] - p[3] )+( p[1] - p[2] )*( p[1] - p[2] ));
   temp2[i*width+j] = (byte)result; 
   }
  }
 }
// for(i=0;i<height;i++)
 // temp1[width*i+width-1]=0;
// for(i=0;i<width;i++)
 // temp1[width*(height-1)+i]=0;

 
 for(i=0;i<height;i++)
        for(j=0,n=0;n<width*3,j<width;n+=3,j++){
            temp[i*lineBytes+n]=temp2[i*width+j];
            temp[i*lineBytes+n+1]=temp2[i*width+j];
            temp[i*lineBytes+n+2]=temp2[i*width+j];}
  
  display(temp,img,x,y);
  writebmp("edgeroberts.bmp",temp,img);
  
  
  
  free(temp2);
   free(temp);   
           free(temp1); 

}
int edgesob(struct BMP_img img,int m,int o)//8邻域
{
   
    int sum1,sum2,sum;double gray;
    DWORD i,j,n;
    DWORD height=img.height;
    DWORD width=img.width;
    DWORD lineBytes=(img.width*24+31)/32*4;
    int p[8];
 BYTE *temp=(byte*)malloc(sizeof(byte)*lineBytes*height);
// memset(temp,0x00,sizeof(byte)*lineBytes*height);
    BYTE *temp1=(byte*)malloc(sizeof(byte)*width*height);
     BYTE *temp2=(byte*)malloc(sizeof(byte)*width*height);

   
 for(i=0;i<img.height;i++)
  {
      for(j=0,n=0;n<img.width*3,j<img.width;n+=3,j++)
         
      { 
         
         gray= 0.299*(float)(img.image[lineBytes*i+n+2])+0.578*(float)(*(img.image+lineBytes*i+n+1))+0.114*(float)(*(img.image+lineBytes*i+n));

            temp1[i*width+j]=(byte)gray;
        }
    }   
      
   
   
 
    for(i=0; i<height-1; i++) 
       {
          for(j=0; j<width-1; j++)
          {
           
       
             if((i==0) || (i==height-1))
                temp2[i*width+j]=0;
            else if((j==0) || (j==width-1))
                temp2[i*width+j]=0;
           
     
             else
      {
               
       p[0]=(byte)*(temp1+(i-1)*width+j-1);
       p[1]=(byte)*(temp1+i*width+j-1);
       p[2]=(byte)*(temp1+(i+1)*width+j-1);
       p[3]=(byte)*(temp1+(i+1)*width+j);
                   p[4]=(byte)*(temp1+(i+1)*width+j+1);
       p[5]=(byte)*(temp1+i*width+j+1);
       p[6]=(byte)*(temp1+(i-1)*width+j+1);
       p[7]=(byte)*(temp1+(i-1)*width+j);
                 
                  sum1=(p[0]+2*p[7]+p[6])-(p[2]+2*p[3]+p[4]);
      sum2=(p[0]+2*p[1]+p[2])-(p[6]+2*p[5]+p[4]);
     //   sum1=(p[0]+p[7]+p[6])-(p[2]+2*p[3]+p[4]);
      //  sum2=(p[0]+p[1]+p[2])-(p[6]+2*p[5]+p[4]);
    /* if(sum1>255)
        sum1=255;
      if(sum1<0) sum1=0;
      if(sum2>255)
       sum2=255;
      if(sum2<0) sum2=0; */
                
               
                   sum = (int)((abs(sum1) + abs(sum2))/2);
       if(sum>255) sum=255;
      
      }
           
            *(temp2+ j+ i*width) =(unsigned char)(sum); 
       
          }
    }
    for(i=0;i<height;i++)
        for(j=0,n=0;n<width*3,j<width;n+=3,j++){
            temp[i*lineBytes+n]=temp2[i*width+j];
            temp[i*lineBytes+n+1]=temp2[i*width+j];
            temp[i*lineBytes+n+2]=temp2[i*width+j];}

  
   
   


 display(temp,img,m,o);
 writebmp("edgesob.bmp",temp,img);
    free(temp);    
    free(temp1); 
 free(temp2);
    return 0;
}
int edgetidu(struct BMP_img img,int m,int o)//8邻域
{
   
    int FX,FY,sum;double gray;
    DWORD i,j,n;
    DWORD height=img.height;
    DWORD width=img.width;
    DWORD lineBytes=(img.width*24+31)/32*4;

 BYTE *temp=(byte*)malloc(sizeof(byte)*lineBytes*height);
// memset(temp,0x00,sizeof(byte)*lineBytes*height);
    BYTE *temp1=(byte*)malloc(sizeof(byte)*width*height);
     BYTE *temp2=(byte*)malloc(sizeof(byte)*width*height);

   
 for(i=0;i<img.height;i++)
  {
      for(j=0,n=0;n<img.width*3,j<img.width;n+=3,j++)
         
      { 
         
         gray= 0.299*(float)(img.image[lineBytes*i+n+2])+0.578*(float)(*(img.image+lineBytes*i+n+1))+0.114*(float)(*(img.image+lineBytes*i+n));

            temp1[i*width+j]=(byte)gray;
        }
    }   
      
   
   
 
    for(i=0; i<height; i++) 
       {
          for(j=0; j<width; j++)
          {
           
       
             if(i==0)
                temp2[i*width+j]=0;
            else if(j==0)
                temp2[i*width+j]=0;
           
     
             else
      {
               
     FX=temp1[i*width+j]-temp1[(i-1)*width+j];
                 FY=temp1[i*width+j]-temp1[i*width+j-1];
               
               
                   sum = (int)sqrt((FX*FX)+(FY*FY));
       if(sum>255) sum=255;
      
      }
           
            *(temp2+ j+ i*width) =(unsigned char)(sum); 
       
          }
    }
    for(i=0;i<height;i++)
        for(j=0,n=0;n<width*3,j<width;n+=3,j++){
            temp[i*lineBytes+n]=temp2[i*width+j];
            temp[i*lineBytes+n+1]=temp2[i*width+j];
            temp[i*lineBytes+n+2]=temp2[i*width+j];}

  
   
   


 display(temp,img,m,o);
 writebmp("tidu.bmp",temp,img);
    free(temp);    
    free(temp1); 
 free(temp2);
    return 0;
}
int nihong(struct BMP_img img,int x,int y)
{
 int i,j;
 double FX,FY,sum;
 int height=img.height;
    int width=img.width;
    DWORD lineBytes=(img.width*24+31)/32*4;
 BYTE *temp1=(byte*)malloc(sizeof(byte)*lineBytes*height);
 memcpy(temp1,img.image,sizeof(byte)*lineBytes*height);
 BYTE *temp=(byte*)malloc(sizeof(byte)*lineBytes*height);
   for(i=0;i<height-1;i++)
    for(j=0;j<(width-1)*3;j++)
    {
     FX=temp1[i*lineBytes+j]-temp1[(i+1)*lineBytes+j];
        FY=temp1[i*lineBytes+j]-temp1[i*lineBytes+j+1];
     sum=2*(int)sqrt((int)((FX*FX)+(FY*FY)));
     if (sum < 0 )
      sum =abs((int)sum);
   if (sum > 255)sum = 255;
     temp[i*lineBytes+j]=(byte)sum;
    }
  


    display(temp,img,x,y);
    writebmp("nihong.bmp",temp,img);
    free(temp);    
  free(temp1);
  return 0;
}
int fudiao(struct BMP_img img,int m,int o)//8邻域
{
   
    int sum;double gray;
    DWORD i,j,n;
    DWORD height=img.height;
    DWORD width=img.width;
    DWORD lineBytes=(img.width*24+31)/32*4;
  
 BYTE *temp1=(byte*)malloc(sizeof(byte)*lineBytes*height);
 if(temp1==NULL)
   printf("\nerror\n");
 memset(temp1,0x00,sizeof(byte)*lineBytes*height);
  

   
 for(i=0;i<img.height;i++)
 {
  for(j=0,n=0;n<img.width*3,j<img.width;n+=3,j++)
   
  { 
   
   gray= 0.299*(float)(img.image[lineBytes*i+n+2])+0.578*(float)(*(img.image+lineBytes*i+n+1))+0.114*(float)(*(img.image+lineBytes*i+n));
   
            temp1[i*width+j]=(byte)gray;
        }
    }   
 
    BYTE *temp2=(byte*)malloc(sizeof(byte)*width*height);
 if(temp2==NULL)
   printf("\nerror\n");
   
 
    for(i=0; i<height; i++) 
 {
  for(j=0; j<width; j++)
  {
           
   
   if(i==0)
                temp2[i*width+j]=0;
          
           
   
   else
   {
               
    sum=temp1[i*width+j]-temp1[(i-1)*width+j]+128;
               
               
   
    if(sum>255) sum=255;
    
   }
           
            *(temp2+ j+ i*width) =(unsigned char)(sum); 
   
  }
    }
 BYTE *temp=(byte*)malloc(sizeof(byte)*lineBytes*height);
 if(temp==NULL)
   printf("\nerror\n");
    for(i=0;i<height;i++)
        for(j=0,n=0;n<width*3,j<width;n+=3,j++){
            temp[i*lineBytes+n]=temp2[i*width+j];
            temp[i*lineBytes+n+1]=temp2[i*width+j];
            temp[i*lineBytes+n+2]=temp2[i*width+j];}
  
  
  
  
  
  
  display(temp,img,m,o);
  writebmp("fudiao.bmp",temp,img);
  free(temp);    
  free(temp1); 
  free(temp2);
  return 0;
}

void junhenghua(struct BMP_img img,int x,int y)
{
 DWORD height=img.height;
 DWORD width=img.width;
 int n[256]={0};
 DWORD m;
    DWORD lineBytes=(img.width*24+31)/32*4;
 double gray=0;
    DWORD i,j,max,min;
 BYTE *temp=(byte*)malloc(sizeof(byte)*lineBytes*height);
 // memset(temp,0x00,sizeof(byte)*lineBytes*height);
    BYTE *temp1=(byte*)malloc(sizeof(byte)*width*height);

 
 double p[256]={0};
    double c[256]={0};
 //fread(temp,sizeof(byte),height*lineBytes,infile);
 
 
 for(i=0;i<img.height;i++)
 {
  for(j=0,m=0;m<img.width*3,j<img.width;m+=3,j++)
   
  { 
   
   gray= 0.299*(float)(img.image[lineBytes*i+m+2])+0.578*(float)(*(img.image+lineBytes*i+m+1))+0.114*(float)(*(img.image+lineBytes*i+m));
   
   temp[i*width+j]=(byte)gray;
   n[(byte)gray]++;
  }
 } 
 
 
 
 for(i=0;i<256;i++){
  p[i] = (double)n[i]/(height*width);
 }
 
 for(i=0;i<256;i++){
  for(j=0;j<=i;j++){
   c[i]+=p[j]; //累计直方图
  }
 }
 max=min=temp[0];
 for(i=0;i<height;i++){
  for(j=0;j<width;j++){
   if(max<temp[i*width+j]){max=temp[i*width+j];}
   else if(min>temp[i*width+j]){min=temp[i*width+j];}
  }
 }
 // printf("%d %d\n",max,min);
 for(i=0;i<height;i++){
  for(j=0;j<width;j++){
   temp1[i*width+j]=(byte)(c[temp[i*width+j]]*(max-min)+min);
  }
 }
 for(i=0;i<height;i++)
  for(j=0,m=0;j<width,m<width*3;j++,m+=3){
   temp[i*lineBytes+m]=temp1[i*width+j];
   temp[i*lineBytes+m+1]=temp1[i*width+j];
   temp[i*lineBytes+m+2]=temp1[i*width+j];}
  
  //fprintf(ft,"%c%c\n%d %d\n%d\n",'P','5',width,height,L);
  zhifang(temp,img,x,y);
  display(temp,img,x,y);
  writebmp("junheng.bmp",temp,img);
  
  free(temp);
  free(temp1);
  
}

//图像膨胀
int Dilation(struct BMP_img img,int type,int num,int x,int y)
{
 int dwWidth=img.width;
 int dwHeight=img.height;
 byte wBitCount=(byte)img.bmp_type;
 DWORD dwLineBytes=(img.width*24+31)/32*4;
 byte *image=(BYTE*)malloc(dwHeight*dwLineBytes*sizeof(BYTE));
 memcpy(image,img.image,dwHeight*dwLineBytes*sizeof(byte));
  int i=0;
  int j=0;
  int n=0;
 BYTE b=0;
 BYTE g=0;
 BYTE r=0;
 double avg=0;
 BYTE *temp;
 int k=0;

 if(image==NULL)
 {
  return -1;
 }
 if(num!=3 && num!=5 &&num!=7)
 {
  return -1;
 }

 temp=(BYTE*)malloc(dwHeight*dwLineBytes*sizeof(BYTE));
 memcpy(temp,img.image,dwHeight*dwLineBytes*sizeof(byte));

 if(!temp)
 {
  return -1;
 }
 memset(temp,255,dwLineBytes*dwHeight*sizeof(BYTE));
 
  //如果不是二值图像,需要先转换为二值图像
  for(i=0;i<dwHeight;i++)
  {
   for(j=0,n=0;j<dwWidth*3;j++,n++)
   {
    b=*(image+dwLineBytes*i+j);
    j++;
    g=*(image+dwLineBytes*i+j);
    j++;
    r=*(image+dwLineBytes*i+j);

    avg=(b+g+r)/3.0;

    if(avg>=120)
    {
     avg=255;
    }
    else
    {
     avg=0;
    }

    *(image+dwLineBytes*i+n)=(BYTE)avg;
    n++;
    *(image+dwLineBytes*i+n)=(BYTE)avg;
    n++;
    *(image+dwLineBytes*i+n)=(BYTE)avg;
   }
  }

  if(type==0)
  {
   //水平方向
   for(i=0;i<dwHeight;i++)
   {
    for(j=(num-1)/2;j<(dwWidth-(num-1)/2)*3;j++)
    {
     for(k=-(num-1)/2;k<=(num-1)/2;k++)
     {
      b=*(image+dwLineBytes*i+j+k*3);
      g=*(image+dwLineBytes*i+j+1+k*3);
      r=*(image+dwLineBytes*i+j+2+k*3);
      if(b==0 && g==0 && r==0)
      {
       *(temp+dwLineBytes*i+j)=0;
       j++;
       *(temp+dwLineBytes*i+j)=0;
       j++;
       *(temp+dwLineBytes*i+j)=0;
       break;
      }
     }
    }
   }
  }
  else
  {
   //垂直方向
   for(i=(num-1)/2;i<dwHeight-(num-1)/2;i++)
   {
    for(j=0;j<dwWidth*3;j++)
    {
     for(k=-(num-1)/2;k<=(num-1)/2;k++)
     {
      b=*(image+dwLineBytes*(i+k)+j);
      g=*(image+dwLineBytes*(i+k)+j+1);
      r=*(image+dwLineBytes*(i+k)+j+2);
      if(b==0 && g==0 && r==0)
      {
       *(temp+dwLineBytes*i+j)=0;
       j++;
       *(temp+dwLineBytes*i+j)=0;
       j++;
       *(temp+dwLineBytes*i+j)=0;
       break;
      }
     }
    }
   }
  }

 

  display(temp,img,x,y);
  writebmp("Dilation.bmp",temp,img);
  free(image);
 free(temp);
 temp=NULL;
 return 0;
}
//图像腐蚀
int Erosion(BMP_img img,int type,int num,int x,int y)//(BYTE* image,DWORD dwWidth,DWORD dwHeight,WORD wBitCount,int type,int num)
 
{
 int dwWidth=img.width;
    int dwHeight=img.height;
    byte wBitCount=(byte)img.bmp_type;
    DWORD dwLineBytes=(img.width*24+31)/32*4;
    byte *image=(byte *)malloc(sizeof(byte)*dwHeight*dwLineBytes);
 memcpy(image,img.image,sizeof(byte)*dwHeight*dwLineBytes);
    int i=0;
    int j=0;
    int n=0;
    BYTE b=0;
    BYTE g=0;
    BYTE r=0;
    double avg=0;
    BYTE *temp=NULL;
    int k=0;

 if(image==NULL)
 {
  return -1;
 }
 if(num!=3 && num!=5 && num!=7)
 {
  return -1;
 }

 temp=(BYTE*)malloc(dwHeight*dwLineBytes*sizeof(BYTE));
 if(!temp)
 {
  return -1;
 }
 memset(temp,0,dwLineBytes*dwHeight*sizeof(BYTE));


  //如果不是二值图像,需要先转换为二值图像
  for(i=0;i<dwHeight;i++)
  {
   for(j=0,n=0;j<dwWidth*3;j++,n++)
   {
    b=*(image+dwLineBytes*i+j);
    j++;
    g=*(image+dwLineBytes*i+j);
    j++;
    r=*(image+dwLineBytes*i+j);

    avg=(b+g+r)/3.0;

    if(avg>=120)
    {
     avg=255;
    }
    else
    {
     avg=0;
    }

    *(image+dwLineBytes*i+n)=(BYTE)avg;
    n++;
    *(image+dwLineBytes*i+n)=(BYTE)avg;
    n++;
    *(image+dwLineBytes*i+n)=(BYTE)avg;
   }
  }

  if(type==0)
  {
   //水平方向
   for(i=0;i<dwHeight;i++)
   {
    for(j=(num-1)/2;j<(dwWidth-(num-1)/2)*3;j++)
    {
     for(k=-(num-1)/2;k<=(num-1)/2;k++)
     {
      b=*(image+dwLineBytes*i+j+k*3);
      g=*(image+dwLineBytes*i+j+1+k*3);
      r=*(image+dwLineBytes*i+j+2+k*3);
      if(b==255 && g==255 && r==255)
      {
       *(temp+dwLineBytes*i+j)=255;
       j++;
       *(temp+dwLineBytes*i+j)=255;
       j++;
       *(temp+dwLineBytes*i+j)=255;
       break;
      }
     }
    }
   }
  }
  else
  {
   //垂直方向
   for(i=(num-1)/2;i<dwHeight-(num-1)/2;i++)
   {
    for(j=0;j<dwWidth*3;j++)
    {
     for(k=-(num-1)/2;k<=(num-1)/2;k++)
     {
      b=*(image+dwLineBytes*(i+k)+j);
      g=*(image+dwLineBytes*(i+k)+j+1);
      r=*(image+dwLineBytes*(i+k)+j+2);
      if(b==255 && g==255 && r==255)
      {
       *(temp+dwLineBytes*i+j)=255;
       j++;
       *(temp+dwLineBytes*i+j)=255;
       j++;
       *(temp+dwLineBytes*i+j)=255;
       break;
      }
     }
    }
   }
  }

memcpy(image,temp,dwLineBytes*dwHeight*sizeof(BYTE));
/*for(i=0;i<dwHeight;i++)
  for(j=0,n=0;j<dwWidth,n<dwWidth*3;j++,n+=3)
 putpixel(j+img.width,img.height+img.height-i,RGB(*(image+i*dwLineBytes+n+2),*(image+i*dwLineBytes+n+1),*(image+i*dwLineBytes+n)));*/
 display(image,img,x,y);
 writebmp("Erosion.bmp",image,img);
 free(temp);
 free(image);
 return 0;
}


//图像窗口变换
int WindowTranslation(struct BMP_img img,int type,int num,int down,int up,int x,int y)//(BYTE *image,DWORD dwWidth,DWORD dwHeight,WORD wBitCount, int down,int up)
     
{


 int width=img.width;
    int height=img.height;
    byte bitCount=(byte)img.bmp_type;
   DWORD lineBytes=(img.width*24+31)/32*4;
    byte *temp=(byte *)malloc(sizeof(byte)*height*lineBytes);
    memcpy(temp,img.image,sizeof(byte)*height*lineBytes);
    int i=0;
    int j=0;
    int n=0;

 BYTE g=0;
 BYTE b=0;
 BYTE r=0;
 double avg=0;
 
 
 
  for(i=0;i<height;i++)
  {
   for(j=0,n=0;j<width*3;j++,n++)
   {
    b=*(temp+lineBytes*i+j);
    j++;
    g=*(temp+lineBytes*i+j);
    j++;
    r=*(temp+lineBytes*i+j);
   
    avg=(b+g+r)/3.0;
    if(avg<down) avg=0;
    if(avg>up) avg=255;
   
    *(temp+lineBytes*i+n)=(BYTE)avg;
    n++;
    *(temp+lineBytes*i+n)=(BYTE)avg;
    n++;
    *(temp+lineBytes*i+n)=(BYTE)avg;
   }
  }

  display(temp,img,x,y);
  writebmp("windowtranslation.bmp",temp,img);
 
 return 0;
}

//图像对数变换
int LogTranslation(struct BMP_img img,int k,int x,int y)//(BYTE *image,DWORD dwWidth,DWORD dwHeight,WORD wBitCount, int k)
     
{
 int width=img.width;
    int height=img.height;
    byte bitCount=(byte)img.bmp_type;
    DWORD lineBytes=(img.width*24+31)/32*4;
    byte *temp=(byte *)malloc(sizeof(byte)*height*lineBytes);
    memcpy(temp,img.image,sizeof(byte)*height*lineBytes);
    int i=0;
    int j=0;
    int n=0;

 BYTE b=0;
 BYTE g=0;
 BYTE r=0;

 if(temp==NULL)
 {
  return -1;
 }
 

  for(i=0;i<height;i++)
  {
   for(j=0,n=0;j<width*3;j++,n++)
   {
    b=*(temp+lineBytes*i+j);
    j++;
    g=*(temp+lineBytes*i+j);
    j++;
    r=*(temp+lineBytes*i+j);
   
    *(temp+lineBytes*i+n)=(BYTE)(k*log(b+1));
    n++;
    *(temp+lineBytes*i+n)=(BYTE)(k*log(g+1));
    n++;
    *(temp+lineBytes*i+n)=(BYTE)(k*log(r+1));
   }
  }

  display(temp,img,x,y);
  writebmp("LogTranslation.bmp",temp,img);
  free(temp);

 
 
 return 0;
}
//图像线性灰度变换
int LineTranslation(struct BMP_img img,double k,double d,int x,int y)//(BYTE *image,DWORD dwWidth,DWORD dwHeight,WORD wBitCount,double k,double d)
    
{
 int dwWidth=img.width;
    int dwHeight=img.height;
    byte wBitCount=(byte)img.bmp_type;
    int dwLineBytes=(dwWidth*24+31)/32*4;
    byte *image=(byte *)malloc(sizeof(byte)*dwHeight*dwLineBytes);
    memcpy(image,img.image,sizeof(byte)*dwHeight*dwLineBytes);
    int i=0;
    int j=0;
    int n=0;
    BYTE b=0;
    BYTE g=0;
    BYTE r=0;
    double nb=0;
    double ng=0;
    double nr=0;
    double f=0;

 if(image==NULL)
 {
  return -1;
 }
 
 
  for(i=0;i<dwHeight;i++)
  {
   for(j=0,n=0;j<dwWidth*3;j++,n++)
   {
    b=*(image+dwLineBytes*(dwHeight-1-i)+j);
    j++;
    g=*(image+dwLineBytes*(dwHeight-1-i)+j);
    j++;
    r=*(image+dwLineBytes*(dwHeight-1-i)+j);
   
    nb=b*k+d;
    if(nb>255) nb=255;
    if(nb<0) nb=0;
    ng=g*k+d;
    if(ng>255) ng=255;
    if(ng<0) ng=0;
    nr=r*k+d;
    if(nr>255) nr=255;
    if(nr<0) nr=0;
   
    *(image+dwLineBytes*(dwHeight-1-i)+n)=(BYTE)nb;
    n++;
    *(image+dwLineBytes*(dwHeight-1-i)+n)=(BYTE)ng;
    n++;
    *(image+dwLineBytes*(dwHeight-1-i)+n)=(BYTE)nr;
   }
  }
  display(image,img,x,y);
  writebmp("LineTranslation.bmp",image,img);
  free(image);


 
 return 0;
}

//图像灰度拉伸变换
int GrayStretch(struct BMP_img img,int pt1x,int pt1y,int pt2x,int pt2y,int x,int y)//(BYTE* image,DWORD dwWidth,DWORD dwHeight,WORD wBitCount, int pt1x,int pt1y,int pt2x,int pt2y)
  
{
 int dwWidth=img.width;
    int dwHeight=img.height;
    byte wBitCount=(byte)img.bmp_type;
   DWORD dwLineBytes=(img.width*24+31)/32*4;
    byte *image=(byte *)malloc(sizeof(byte)*dwHeight*dwLineBytes);
    memcpy(image,img.image,sizeof(byte)*dwHeight*dwLineBytes);
    int i=0;
    int j=0;
    int n=0;

    BYTE map[256];

 
 
   BYTE b=0;
   BYTE g=0;
   BYTE r=0;
   double avg=0.0;
   int ag=0;

 if(image==NULL)
 {
  return -1;
 }

 for(n=0;n<=pt1x;n++)
 {
  if(pt1x>0)
  {
   map[n]=(BYTE)pt1y*n/pt1x;
  }
  else
  {
   map[n]=0;
  }
 }
 for(;n<=pt2x;n++)
 {
  if(pt2x!=pt1x)
  {
   map[n]=pt1y+(BYTE)((pt2y-pt1y)*(n-pt1x)/(pt2x-pt1x));
  }
  else
  {
   map[n]=pt1y;
  }
 }
 for(;n<256;n++)
 {
  if(pt2x!=255)
  {
   map[n]=pt2y+(BYTE)((255-pt2y)*(n-pt2x)/(255-pt2x));
  }
  else
  {
   map[n]=255;
  }
 }
 
 
  for(i=0;i<dwHeight;i++)
  {
   for(j=0,n=0;j<dwWidth*3;j++,n++)
   {
    b=*(image+dwLineBytes*(dwHeight-1-i)+j);
    j++;
    g=*(image+dwLineBytes*(dwHeight-1-i)+j);
    j++;
    r=*(image+dwLineBytes*(dwHeight-1-i)+j);
   
    avg=(b+g+r)/3.0;
    ag=(int)avg;
   
    *(image+dwLineBytes*(dwHeight-1-i)+n)=(BYTE)map[ag];
    n++;
    *(image+dwLineBytes*(dwHeight-1-i)+n)=(BYTE)map[ag];
    n++;
    *(image+dwLineBytes*(dwHeight-1-i)+n)=(BYTE)map[ag];
   }
  }
  display(image,img,x,y);
  writebmp("GrayStretch.bmp",image,img);
  free(image);

 
 return 0;
}

//图像阈值变换
int ThresholdTranslation(struct BMP_img img,byte bytThreshold,int x,int y)//(BYTE* image,DWORD dwWidth,DWORD dwHeight,WORD wBitCount,BYTE bytThreshold)
      
{
 int dwWidth=img.width;
    int dwHeight=img.height;
    byte wBitCount=(byte)img.bmp_type;
    DWORD dwLineBytes=(img.width*24+31)/32*4;
    byte *image=(byte *)malloc(sizeof(byte)*dwHeight*dwLineBytes);
    memcpy(image,img.image,sizeof(byte)*dwHeight*dwLineBytes);
    int i=0;
    int j=0;
    int n=0;
 BYTE b=0;
 BYTE g=0;
 BYTE r=0;
 double avg=0.0;
 

 
  for(i=0;i<dwHeight;i++)
  {
   for(j=0,n=0;j<dwWidth*3;j++,n++)
   {
    b=*(image+dwLineBytes*(dwHeight-1-i)+j);
    j++;
    g=*(image+dwLineBytes*(dwHeight-1-i)+j);
    j++;
    r=*(image+dwLineBytes*(dwHeight-1-i)+j);

    avg=(b+g+r)/3.0;
    if(avg<bytThreshold)
    {
     avg=0;
    }
    else
    {
     avg=255;
    }

    *(image+dwLineBytes*(dwHeight-1-i)+n)=(BYTE)avg;
    n++;
    *(image+dwLineBytes*(dwHeight-1-i)+n)=(BYTE)avg;
    n++;
    *(image+dwLineBytes*(dwHeight-1-i)+n)=(BYTE)avg;
   }
  }

  display(image,img,x,y);
  writebmp("ThresholdTranslation.bmp",image,img);
  free(image);

 

 return 0;
}