C言語でBMP画像の拡大縮小を実現-24ビット

3127 ワード

bmpのスケール
#include
#include
#include

typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned int DWORD;
typedef unsigned long	LDWORD;
typedef long LONG;

#pragma pack(1)/*             ,             1    */
typedef struct tagBITMAPFILEHEADER
{
 WORD bfType; //      。 'BM'
 DWORD bfSize; //       ,      (3-6  ,    )
 WORD bfReserved1;//       ,   0(7-8  )
 WORD bfReserved2;//       ,   0(9-10  ) 
 DWORD bfOffBits;   //RGB      ,         ,      (11-14  ,    )
}BITMAPFILEHEADER;

typedef struct tagBITMAPINFOHEADER
{
 DWORD biSize;	//         (15-18  )
 DWORD biWidth;	//     ,      (19-22  )
 DWORD biHeight;	//     ,      (23-26  )
 WORD biPlanes;	//       ,   1(27-28  )
 WORD biBitCount;	//         ,   1(  )(29-30  ),4(16 ),8(256 )16(   ) 24(   )  

 DWORD biCompression;//      ,   0(   ),(31-34  )
//1(BI_RLE8    ) 2(BI_RLE4    )  

 DWORD biSizeImage;	//     (            4          ),      (35-38  )

 DWORD biXPelsPerMeter;//       ,     (39-42  )
 DWORD biYPelsPerMeter;//       ,     (43-46  )
 DWORD biClrUsed;	//               (47-50  )
 DWORD biClrImportant;	//             (51-54  )
}BITMAPINFOHEADER;

/*
     :Bmp_Smaller
     :      
       :old_bmp_path,new_bmp_path
      :0	ok
	       1	fail
       :zhoubing
     :20190808
 */
int Bmp_Bigger_And_Smaller(const char* old_bmp_path,const char* new_bmp_path)
{

   //          
 BITMAPFILEHEADER head;
 BITMAPINFOHEADER info;

 //      
 memset(&head,0,sizeof(BITMAPFILEHEADER));
 memset(&info,0,sizeof(BITMAPINFOHEADER));

 FILE *fpr1=fopen(old_bmp_path,"rb");
 FILE *fpw2=fopen(new_bmp_path,"wb");


 if(fpr1==NULL||fpw2==NULL)
 {
  printf("      !
"); return -1; } // fread(&head,sizeof(BITMAPFILEHEADER),1,fpr1); fread(&info,sizeof(BITMAPINFOHEADER),1,fpr1); unsigned int old_width=info.biWidth;// unsigned int old_height=info.biHeight;// // unsigned char *src_data=(unsigned char*)malloc(old_width*old_height*3); fseek(fpr1,54,SEEK_SET); fread(src_data,old_width*old_height*3,1,fpr1); printf(" :%d
",old_width); printf(" :%d
",old_height); // unsigned int new_Width,new_Height; printf(" :
"); scanf("%d",&new_Width); printf(" :
"); scanf("%d",&new_Height); head.bfSize=new_Width*new_Height*3+54; info.biWidth=new_Width; info.biHeight=new_Height; // fwrite(&head,sizeof(BITMAPFILEHEADER),1,fpw2); fwrite(&info,sizeof(BITMAPINFOHEADER),1,fpw2); int i=0,j=0; unsigned long dwsrcX,dwsrcY; unsigned char *pucDest; unsigned char *pucSrc; unsigned char *dest_data=(unsigned char*)malloc(new_Width*new_Height*3); for(i=0;i