bmp 缩放代码(BMP位图任意放大 和缩小)

2020-01-06 15:05:11刘景俊

示例:


int main(int argc, char* argv[])
{
  unsigned char *bmp,*bmp2;
bmp=(unsigned char*)malloc(1244214);
bmp2=(unsigned char*)malloc(1244214);
FILE *fp=fopen("D:开机画面.bmp","rb");
fread(bmp,1,304182,fp);

fclose(fp);
  //bmpsetH(bmp,720,bmp2);
  // memcpy(bmp,bmp2,1244214);
BmpSetV(bmp,290,bmp2);
  fp=fopen("D:开机.bmp","wb");
fwrite(bmp2,1,1244214,fp);

fclose(fp);
  free(bmp);
free(bmp2);
return 0;
}

看到这确实不容易,这里ASPKU小编就为大家分享另一个代码


// suofang.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include 

struct header{
  short BM;
  unsigned int filesize;
  unsigned int recv;
  unsigned int offset;
  unsigned int bitmapheadlong;
  unsigned int bitmapwith;
  unsigned int bitmaphight;
  short bitmappageinfo;
  short bitperpixel;
  unsigned int compress;
  unsigned int bitmapsize;
  unsigned int levelresolution;
  unsigned int verticalresolution;
  unsigned int colourmap;
  unsigned int colourmapsize;
}bitmapheader;
#if 0
  typedef struct tagBITMAPFILEHEADER
  {
  WORD bfType; // 位图文件的类型,必须为BM(0-1字节)
  DWORD bfSize; // 位图文件的大小,以字节为单位(2-5字节)
  WORD bfReserved1; // 位图文件保留字,必须为0(6-7字节)
  WORD bfReserved2; // 位图文件保留字,必须为0(8-9字节)
  DWORD bfOffBits; // 位图数据的起始位置,以相对于位图(10-13字节)
  // 文件头的偏移量表示,以字节为单位
  } BITMAPFILEHEADER; 

 typedef struct tagBITMAPINFOHEADER{
  DWORD biSize; // 本结构所占用字节数(14-17字节)
  LONG biWidth; // 位图的宽度,以像素为单位(18-21字节)
  LONG biHeight; // 位图的高度,以像素为单位(22-25字节)
  WORD biPlanes; // 目标设备的级别,必须为1(26-27字节)
  WORD biBitCount;// 每个像素所需的位数,必须是1(双色),(28-29字节)
  // 4(16色),8(256色)或24(真彩色)之一
  DWORD biCompression; // 位图压缩类型,必须是 0(不压缩),(30-33字节)
  // 1(BI_RLE8压缩类型)或2(BI_RLE4压缩类型)之一
  DWORD biSizeImage; // 位图的大小,以字节为单位(34-37字节)
  LONG biXPelsPerMeter; // 位图水平分辨率,每米像素数(38-41字节)
  LONG biYPelsPerMeter; // 位图垂直分辨率,每米像素数(42-45字节)
  DWORD biClrUsed;// 位图实际使用的颜色表中的颜色数(46-49字节)
  DWORD biClrImportant;// 位图显示过程中重要的颜色数(50-53字节)
  } BITMAPINFOHEADER;

  typedef struct tagRGBQUAD {
  BYTE rgbBlue;// 蓝色的亮度(值范围为0-255)
  BYTE rgbGreen; // 绿色的亮度(值范围为0-255)
  BYTE rgbRed; // 红色的亮度(值范围为0-255)
  BYTE rgbReserved;// 保留,必须为0
  } RGBQUAD; 
#endif
void scale(int srcwith,int srcheight,int destwith,int destheight)
{
  
}
int _tmain(int argc, _TCHAR* argv[4000000])
{
  BITMAPFILEHEADER bmfHdr; 
  BITMAPINFOHEADER bi,bi1;  
  CFile file("d:pic1.bmp",CFile::modeRead);
  file.Read(&bmfHdr, sizeof(BITMAPFILEHEADER)); 
  file.Read(&bi,sizeof(BITMAPINFOHEADER));
  DWORD dwSize = (bi.biWidth*bi.biBitCount+31)/32*4*bi.biHeight; 
  PBYTE pBuf = new BYTE[dwSize]; 
  file.Read(pBuf,dwSize); 
  file.Close(); 

  /////////////////////////////////////////////////////////////////////////
  file.Open("d:tmp1.bmp",CFile::modeCreate|CFile::modeReadWrite); 
  memcpy(&bi1,&bi, sizeof(BITMAPINFOHEADER)); 
  bi1.biWidth = 4000; //400->200 ,718->359
  bi1.biHeight = 4000; //266->133,397->794
  DWORD dwSize1 = (bi1.biWidth * bi1.biBitCount + 31)/32*4*bi1.biHeight; 
  PBYTE pBuf1 = new BYTE[dwSize1]; 
  BYTE *pSrc,*pDest; 

  //ofstream file1("D:tmp.log");

#if 0
///////////////////////////////////最邻近值采样法(速度快)////////////////////////////////////////////////
  float m_xscale,m_yscale;
  m_xscale = (float)bi.biWidth/(float)bi1.biWidth;
  m_yscale = (float)bi.biHeight/(float)bi1.biHeight;
  unsigned long k=0;
  for(int y = 0; y  {
    for(int x=0; x    {
      pSrc = pBuf+(int)(y*m_yscale)*bi.biWidth*3+(int)(x*m_xscale)*3;
      pDest = pBuf1+(int)(y*bi1.biWidth*3+x*3);
      memcpy(pDest,pSrc,3);
      //*(unsigned long*)pDest=k++;
      //*(unsigned long*)pDest=0x000000ff;
    }
  }
////////////////////////////////////缩放结束///////////////////////////////////////////////
#endif

#if 1
/////////////////////////////////////双线性插值法(经过优化)/////////////////////////////////////////////////////
  int sw = bi.biWidth - 1, sh = bi.biHeight - 1, dw = bi1.biWidth - 1, dh = bi1.biHeight - 1;    //源图像宽度,目标图像宽度
  int B, N, x, y;                                          //计算出的目标点对应于源图像中的浮点数横坐标N、纵坐标B,目标整数横坐标x、纵坐标y
  int nPixelSize = bi.biBitCount/8;          //像素大小
  BYTE * pLinePrev, *pLineNext;            //源图像中的行开始坐标和下一行开始坐标
  //BYTE * pDest;
  BYTE * pA, *pB, *pC, *pD;              //源图像中最邻近的四个点
  for ( int i = 0; i <= dh; ++i )      //高度递增
  {
    pDest = ( BYTE * )(pBuf1+bi1.biWidth*i*nPixelSize);
    y = i * sh / dh;
    N = dh - i * sh % dh;
    pLinePrev = ( BYTE * )(pBuf+bi.biWidth*y*nPixelSize);
    y++;
    pLineNext = ( N == dh ) ? pLinePrev : ( BYTE * )(pBuf+bi.biWidth*y*nPixelSize);
    for ( int j = 0; j <= dw; ++j )    //宽度递增
    {
      x = j * sw / dw * nPixelSize;
      B = dw - j * sw % dw;
      pA = pLinePrev + x;
      pB = pA + nPixelSize;
      pC = pLineNext + x;
      pD = pC + nPixelSize;
      if ( B == dw )
      {
        pB = pA;
        pD = pC;
      }
      for ( int k = 0; k < nPixelSize; ++k )
        *pDest++ = ( BYTE )( int )(
          ( B * N * ( *pA++ - *pB - *pC + *pD ) + dw * N * *pB++
          + dh * B * *pC++ + ( dw * dh - dh * B - dw * N ) * *pD++
          + dw * dh / 2 ) / (double)( dw * dh )
        );
    }
  }
#endif

file.Write(&bmfHdr,sizeof(BITMAPFILEHEADER)); 
file.Write(&bi1,sizeof(BITMAPINFOHEADER)); 
file.Write(pBuf1,dwSize1); 
file.Close(); 
//==release p memory==
delete pBuf;
pBuf = NULL;
delete pBuf1;
pBuf1 = NULL;
//==release p memory===
if (pDest!=NULL)
{
pDest = NULL;
}
//if (pSrc!=NULL)
//{
//pSrc = NULL;
//}
//////////////////////////////////////////////////////////////
//MessageBox("Test successful","Info",MB_OKCANCEL);

  return 0;
}